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
This commit is contained in:
Kostiantyn Kalynovskyi 2020-03-25 19:47:29 -05:00
parent 423d75400e
commit 7a2d01789e
4 changed files with 92 additions and 3 deletions

View File

@ -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()))

View File

@ -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())
}

View File

@ -0,0 +1,2 @@
resources:
- unordered-resources.yaml

View File

@ -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
...