change: components apply after all generators and transformers applied (#5170)

* change: components apply after all generator and transformer applied

* fix name for a test case

* add comment about when the components will be executed

* components are applied before transformer
This commit is contained in:
yugo kobayashi
2023-06-03 02:26:54 +09:00
committed by GitHub
parent 6ce0bf390c
commit 0add0f95e2
3 changed files with 125 additions and 11 deletions

View File

@@ -202,10 +202,6 @@ func (kt *KustTarget) accumulateTarget(ra *accumulator.ResAccumulator) (
if err != nil {
return nil, errors.WrapPrefixf(err, "accumulating resources")
}
ra, err = kt.accumulateComponents(ra, kt.kustomization.Components)
if err != nil {
return nil, errors.WrapPrefixf(err, "accumulating components")
}
tConfig, err := builtinconfig.MakeTransformerConfig(
kt.ldr, kt.kustomization.Configurations)
if err != nil {
@@ -230,6 +226,14 @@ func (kt *KustTarget) accumulateTarget(ra *accumulator.ResAccumulator) (
if err != nil {
return nil, err
}
// components are expected to execute after reading resources and adding generators ,before applying transformers and validation.
// https://github.com/kubernetes-sigs/kustomize/pull/5170#discussion_r1212101287
ra, err = kt.accumulateComponents(ra, kt.kustomization.Components)
if err != nil {
return nil, errors.WrapPrefixf(err, "accumulating components")
}
err = kt.runTransformers(ra)
if err != nil {
return nil, err

View File

@@ -39,7 +39,7 @@ resources:
- deploy.yaml
configMapGenerator:
- name: my-configmap
literals:
literals:
- testValue=purple
- otherValue=green
`)
@@ -64,7 +64,7 @@ resources:
configMapGenerator:
- name: my-configmap
behavior: merge
literals:
literals:
- testValue=blue
- compValue=red
`)
@@ -154,7 +154,7 @@ spec:
configMapGenerator:
- name: my-configmap
behavior: merge
literals:
literals:
- otherValue=orange
`),
writeK("prod", `
@@ -319,7 +319,7 @@ resources:
configMapGenerator:
- name: my-configmap
behavior: merge
literals:
literals:
- compValue=red
- testValue=blue
`),
@@ -350,7 +350,7 @@ kind: Component
configMapGenerator:
- name: my-configmap
behavior: merge
literals:
literals:
- otherValue=orange
`),
},
@@ -562,7 +562,7 @@ kind: Component
configMapGenerator:
- name: my-configmap
behavior: merge
literals:
literals:
- otherValue=orange
`),
},
@@ -657,3 +657,113 @@ components:
})
}
}
func TestOrderOfAccumulatedComponent(t *testing.T) {
tests := map[string]struct {
input []FileGen
expectedOutput string
}{
"components_using_a_generated_resource_by_configMapGenerator": {
input: []FileGen{
writeK("", `
resources:
- resource.yaml
components:
- components
configMapGenerator:
- name: generated-resource
literals:
- key=value
options:
disableNameSuffixHash: true`),
writeF("resource.yaml", `
apiVersion: v1
kind: ConfigMap
metadata:
name: config-from-resources
data:
foo: bar`),
writeC("/components", `
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
nameSuffix: -suffix
`),
},
expectedOutput: `
apiVersion: v1
data:
foo: bar
kind: ConfigMap
metadata:
name: config-from-resources-suffix
---
apiVersion: v1
data:
key: value
kind: ConfigMap
metadata:
name: generated-resource-suffix
`},
"components_are_applied_before_replacements_transformer_applied": {
input: []FileGen{
writeK("", `
resources:
- resource.yaml
components:
- components
replacements:
- source:
kind: Pod
name: myapp-pod
fieldPath: metadata.namespace
targets:
- select:
kind: Pod
name: myapp-pod
fieldPaths:
- spec.containers.[name=myapp-container].env.[name=CURRENT_POD_NAMESPACE].value`),
writeF("resource.yaml", `
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
namespace: dev-assa
spec:
containers:
- image: busybox
name: myapp-container
env:
- name: CURRENT_POD_NAMESPACE
value: "$(PLACEHOLDER)"`),
writeC("/components", `
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
namespace: kustomize-namespace`),
},
expectedOutput: `
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
namespace: kustomize-namespace
spec:
containers:
- env:
- name: CURRENT_POD_NAMESPACE
value: kustomize-namespace
image: busybox
name: myapp-container
`},
}
for testName, test := range tests {
t.Run(testName, func(t *testing.T) {
th := kusttest_test.MakeHarness(t)
for _, f := range test.input {
f(th)
}
m := th.Run(".", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, test.expectedOutput)
})
}
}