From 7a2d01789e4fa918af356a53e316dc419489aad6 Mon Sep 17 00:00:00 2001 From: Kostiantyn Kalynovskyi Date: Wed, 25 Mar 2020 19:47:29 -0500 Subject: [PATCH] Add ordering of the documents By default kustomize cli orders k8s resources, in a way, that they can easily by applied to cluster, namespaces and crds first. In this patch set we implementing same behavior Relates-To: #131 Closes: #131 Change-Id: I4fc75366627ed361ac1da48e89a35949bcb79801 --- pkg/document/bundle.go | 13 +++-- pkg/document/bundle_test.go | 26 +++++++++ .../testdata/order/kustomization.yaml | 2 + .../testdata/order/unordered-resources.yaml | 54 +++++++++++++++++++ 4 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 pkg/document/testdata/order/kustomization.yaml create mode 100644 pkg/document/testdata/order/unordered-resources.yaml diff --git a/pkg/document/bundle.go b/pkg/document/bundle.go index 5687dba37..80326ad2b 100644 --- a/pkg/document/bundle.go +++ b/pkg/document/bundle.go @@ -14,6 +14,7 @@ import ( "sigs.k8s.io/kustomize/v3/pkg/resource" "sigs.k8s.io/kustomize/v3/pkg/target" "sigs.k8s.io/kustomize/v3/pkg/types" + "sigs.k8s.io/kustomize/v3/plugin/builtin" docplugins "opendev.org/airship/airshipctl/pkg/document/plugins" "opendev.org/airship/airshipctl/pkg/log" @@ -31,7 +32,6 @@ type KustomizeBuildOptions struct { KustomizationPath string OutputPath string LoadRestrictor loader.LoadRestrictorFunc - OutOrder int } // BundleFactory contains the objects within a bundle @@ -70,7 +70,6 @@ func NewBundle(fSys FileSystem, kustomizePath string, outputPath string) (Bundle KustomizationPath: kustomizePath, OutputPath: outputPath, LoadRestrictor: loader.RestrictionRootOnly, - OutOrder: 0, } // init an empty bundle factory @@ -116,7 +115,10 @@ func NewBundle(fSys FileSystem, kustomizePath string, outputPath string) (Bundle return bundle, err } err = bundle.SetKustomizeResourceMap(m) - + if err != nil { + return nil, err + } + err = bundle.OrderLegacy() return bundle, err } @@ -156,6 +158,11 @@ func (b *BundleFactory) GetFileSystem() FileSystem { return b.FileSystem } +// OrderLegacy uses kustomize Transformer plugin to correct order of resources +func (b *BundleFactory) OrderLegacy() error { + return builtin.NewLegacyOrderTransformerPlugin().Transform(b.GetKustomizeResourceMap()) +} + // GetAllDocuments returns all documents in this bundle func (b *BundleFactory) GetAllDocuments() ([]Document, error) { docSet := make([]Document, len(b.ResMap.Resources())) diff --git a/pkg/document/bundle_test.go b/pkg/document/bundle_test.go index 0f7a4be9e..c952f160c 100644 --- a/pkg/document/bundle_test.go +++ b/pkg/document/bundle_test.go @@ -138,3 +138,29 @@ func TestBundleDocumentFiltering(t *testing.T) { assert.Contains(b.String(), "workflow-controller") }) } + +func TestBundleOrder(t *testing.T) { + bundle := testutil.NewTestBundle(t, "testdata/order") + + docs, err := bundle.GetAllDocuments() + require.NoError(t, err) + require.Len(t, docs, 3) + + // first must be namespace argo-namespace + doc := docs[0] + require.NotNil(t, doc) + assert.Equal(t, "Namespace", doc.GetKind()) + assert.Equal(t, "argo-namespace", doc.GetName()) + + // second must be CRD named workflows.argoproj.io + doc = docs[1] + require.NotNil(t, doc) + assert.Equal(t, "CustomResourceDefinition", doc.GetKind()) + assert.Equal(t, "workflows.argoproj.io", doc.GetName()) + + // second must be CR workflow-controller + doc = docs[2] + require.NotNil(t, doc) + assert.Equal(t, "Deployment", doc.GetKind()) + assert.Equal(t, "workflow-controller", doc.GetName()) +} diff --git a/pkg/document/testdata/order/kustomization.yaml b/pkg/document/testdata/order/kustomization.yaml new file mode 100644 index 000000000..5cf2aee8b --- /dev/null +++ b/pkg/document/testdata/order/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - unordered-resources.yaml diff --git a/pkg/document/testdata/order/unordered-resources.yaml b/pkg/document/testdata/order/unordered-resources.yaml new file mode 100644 index 000000000..d0fe1ef15 --- /dev/null +++ b/pkg/document/testdata/order/unordered-resources.yaml @@ -0,0 +1,54 @@ + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + annotations: + airshipit.org/clustertype: target + labels: + app: workflow-controller + arbitrary-label: some-label + name: workflow-controller + namespace: argo-namespace +spec: + selector: + matchLabels: + app: workflow-controller + template: + metadata: + labels: + app: workflow-controller + spec: + containers: + - args: + - --configmap + - workflow-controller-configmap + - --executor-image + - argoproj/argoexec:v2.3.0 + command: + - workflow-controller + image: argoproj/workflow-controller:v2.3.0 + name: workflow-controller + serviceAccountName: argo +--- +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + annotations: + airshipit.org/clustertype: target + name: workflows.argoproj.io +spec: + group: argoproj.io + names: + kind: Workflow + plural: workflows + shortNames: + - wf + scope: Namespaced + version: v1alpha1 +--- +apiVersion: v1 +kind: Namespace +metadata: + name: argo-namespace +... \ No newline at end of file