Skip to content

Commit

Permalink
change: components apply after all generators and transformers applied (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
koba1t committed Jun 2, 2023
1 parent 6ce0bf3 commit 0add0f9
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 11 deletions.
12 changes: 8 additions & 4 deletions api/internal/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
122 changes: 116 additions & 6 deletions api/krusty/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ resources:
- deploy.yaml
configMapGenerator:
- name: my-configmap
literals:
literals:
- testValue=purple
- otherValue=green
`)
Expand All @@ -64,7 +64,7 @@ resources:
configMapGenerator:
- name: my-configmap
behavior: merge
literals:
literals:
- testValue=blue
- compValue=red
`)
Expand Down Expand Up @@ -154,7 +154,7 @@ spec:
configMapGenerator:
- name: my-configmap
behavior: merge
literals:
literals:
- otherValue=orange
`),
writeK("prod", `
Expand Down Expand Up @@ -319,7 +319,7 @@ resources:
configMapGenerator:
- name: my-configmap
behavior: merge
literals:
literals:
- compValue=red
- testValue=blue
`),
Expand Down Expand Up @@ -350,7 +350,7 @@ kind: Component
configMapGenerator:
- name: my-configmap
behavior: merge
literals:
literals:
- otherValue=orange
`),
},
Expand Down Expand Up @@ -562,7 +562,7 @@ kind: Component
configMapGenerator:
- name: my-configmap
behavior: merge
literals:
literals:
- otherValue=orange
`),
},
Expand Down Expand Up @@ -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)
})
}
}
2 changes: 1 addition & 1 deletion go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
go.starlark.net v0.0.0-20190528202925-30ae18b8564f/go.mod h1:c1/X6cHgvdXj6pUlmWKMkuqRnW4K8x2vwt6JAaaircg=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
Expand Down Expand Up @@ -69,7 +70,6 @@ sigs.k8s.io/kustomize/api v0.8.9/go.mod h1:OTaWCS8krICmepGNRxSdhOuywXAl7AieML4y2
sigs.k8s.io/kustomize/api v0.11.4/go.mod h1:k+8RsqYbgpkIrJ4p9jcdPqe8DprLxFUUO0yNOq8C+xI=
sigs.k8s.io/kustomize/cmd/config v0.10.7/go.mod h1:ZTbW6xLlf2ohaOzyWbaRj+fxLbSRjsExCfXGijY4Xt4=
sigs.k8s.io/kustomize/cmd/config v0.11.0/go.mod h1:vsC4WPJub39YAH1aMeuU5dzCwe2zY6EbEKDo0PddiIc=
sigs.k8s.io/kustomize/cmd/config v0.11.2/go.mod h1:PCpHxyu10daTnbMfn3xhH1vppn7L8jsS3qpRKXb7Lkc=
sigs.k8s.io/kustomize/kyaml v0.7.1/go.mod h1:ne3F9JPhW2wrVaLslxBsEe6MQJQ9YK5rUutrdhBWXwI=
sigs.k8s.io/kustomize/kyaml v0.10.18/go.mod h1:h94DSoDbmnN4BTc6VTX7tGNGXZy29rbPo+R4jGMvA8U=
sigs.k8s.io/kustomize/kyaml v0.13.6/go.mod h1:yHP031rn1QX1lr/Xd934Ri/xdVNG8BE2ECa78Ht/kEg=
Expand Down

0 comments on commit 0add0f9

Please sign in to comment.