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:
parent
423d75400e
commit
7a2d01789e
@ -14,6 +14,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/v3/pkg/resource"
|
"sigs.k8s.io/kustomize/v3/pkg/resource"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/target"
|
"sigs.k8s.io/kustomize/v3/pkg/target"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/types"
|
"sigs.k8s.io/kustomize/v3/pkg/types"
|
||||||
|
"sigs.k8s.io/kustomize/v3/plugin/builtin"
|
||||||
|
|
||||||
docplugins "opendev.org/airship/airshipctl/pkg/document/plugins"
|
docplugins "opendev.org/airship/airshipctl/pkg/document/plugins"
|
||||||
"opendev.org/airship/airshipctl/pkg/log"
|
"opendev.org/airship/airshipctl/pkg/log"
|
||||||
@ -31,7 +32,6 @@ type KustomizeBuildOptions struct {
|
|||||||
KustomizationPath string
|
KustomizationPath string
|
||||||
OutputPath string
|
OutputPath string
|
||||||
LoadRestrictor loader.LoadRestrictorFunc
|
LoadRestrictor loader.LoadRestrictorFunc
|
||||||
OutOrder int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// BundleFactory contains the objects within a bundle
|
// BundleFactory contains the objects within a bundle
|
||||||
@ -70,7 +70,6 @@ func NewBundle(fSys FileSystem, kustomizePath string, outputPath string) (Bundle
|
|||||||
KustomizationPath: kustomizePath,
|
KustomizationPath: kustomizePath,
|
||||||
OutputPath: outputPath,
|
OutputPath: outputPath,
|
||||||
LoadRestrictor: loader.RestrictionRootOnly,
|
LoadRestrictor: loader.RestrictionRootOnly,
|
||||||
OutOrder: 0,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// init an empty bundle factory
|
// init an empty bundle factory
|
||||||
@ -116,7 +115,10 @@ func NewBundle(fSys FileSystem, kustomizePath string, outputPath string) (Bundle
|
|||||||
return bundle, err
|
return bundle, err
|
||||||
}
|
}
|
||||||
err = bundle.SetKustomizeResourceMap(m)
|
err = bundle.SetKustomizeResourceMap(m)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = bundle.OrderLegacy()
|
||||||
return bundle, err
|
return bundle, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,6 +158,11 @@ func (b *BundleFactory) GetFileSystem() FileSystem {
|
|||||||
return b.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
|
// GetAllDocuments returns all documents in this bundle
|
||||||
func (b *BundleFactory) GetAllDocuments() ([]Document, error) {
|
func (b *BundleFactory) GetAllDocuments() ([]Document, error) {
|
||||||
docSet := make([]Document, len(b.ResMap.Resources()))
|
docSet := make([]Document, len(b.ResMap.Resources()))
|
||||||
|
@ -138,3 +138,29 @@ func TestBundleDocumentFiltering(t *testing.T) {
|
|||||||
assert.Contains(b.String(), "workflow-controller")
|
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())
|
||||||
|
}
|
||||||
|
2
pkg/document/testdata/order/kustomization.yaml
vendored
Normal file
2
pkg/document/testdata/order/kustomization.yaml
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
resources:
|
||||||
|
- unordered-resources.yaml
|
54
pkg/document/testdata/order/unordered-resources.yaml
vendored
Normal file
54
pkg/document/testdata/order/unordered-resources.yaml
vendored
Normal 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
|
||||||
|
...
|
Loading…
Reference in New Issue
Block a user