mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
feat: support labels key in transformer configuration (#5556)
* feat: support labels key in transformer configuration Allow the usage of a separate transformer configuration for the labels key, similar to what is currently available for commonLabels and commonAnnotations. This aims to provide the same functionality that commonLabels currently provide for labels, since commonLabels is deprecated and slated for removal in a future release. * chore(transformerconfig): add nolint hint Add a nolint hint to the new method so the returns can stay consistent with one another. * fix: changes from code review * Rename methods `AddCommonLabelFieldSpec` and `AddLabelFieldSpec` to `AddCommonLabelsFieldSpec` and `AddLabelsFieldSpec`. * Add extra test to verify scenarios applying labels to Custom Resource Definitions.
This commit is contained in:
@@ -335,3 +335,400 @@ spec:
|
||||
location: Arizona
|
||||
`)
|
||||
}
|
||||
|
||||
func TestLabelTransformerConfig(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
kustomization string
|
||||
transformerConfig string
|
||||
expectedResult string
|
||||
}{
|
||||
{
|
||||
name: "includeSelectors=false, includeTemplates=false, include template via transformerConfig",
|
||||
kustomization: `configurations:
|
||||
- config/configurations.yaml
|
||||
|
||||
labels:
|
||||
- includeSelectors: false
|
||||
includeTemplates: false
|
||||
pairs:
|
||||
location: planet-earth
|
||||
environment: dev
|
||||
|
||||
resources:
|
||||
- resources/deployment.yaml
|
||||
`,
|
||||
transformerConfig: `labels:
|
||||
- path: spec/template/metadata/labels
|
||||
create: true
|
||||
kind: Deployment
|
||||
`,
|
||||
expectedResult: `apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
app: sample-deploy
|
||||
environment: dev
|
||||
location: planet-earth
|
||||
name: sample-deploy
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: sample-deploy
|
||||
strategy: {}
|
||||
template:
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
app: sample-deploy
|
||||
environment: dev
|
||||
location: planet-earth
|
||||
spec:
|
||||
containers:
|
||||
- image: hello-world:latest
|
||||
name: hello-world
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "includeSelectors=true, includeTemplates=false, include template via transformerConfig",
|
||||
kustomization: `configurations:
|
||||
- config/configurations.yaml
|
||||
|
||||
labels:
|
||||
- includeSelectors: true
|
||||
includeTemplates: false
|
||||
pairs:
|
||||
location: planet-earth
|
||||
environment: dev
|
||||
|
||||
resources:
|
||||
- resources/deployment.yaml
|
||||
`,
|
||||
transformerConfig: `labels:
|
||||
- path: spec/template/metadata/labels
|
||||
create: true
|
||||
kind: Deployment
|
||||
`,
|
||||
expectedResult: `apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
app: sample-deploy
|
||||
environment: dev
|
||||
location: planet-earth
|
||||
name: sample-deploy
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: sample-deploy
|
||||
environment: dev
|
||||
location: planet-earth
|
||||
strategy: {}
|
||||
template:
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
app: sample-deploy
|
||||
environment: dev
|
||||
location: planet-earth
|
||||
spec:
|
||||
containers:
|
||||
- image: hello-world:latest
|
||||
name: hello-world
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "includeSelectors=false, includeTemplates=true, no transformerConfig",
|
||||
kustomization: `labels:
|
||||
- includeSelectors: false
|
||||
includeTemplates: true
|
||||
pairs:
|
||||
location: planet-earth
|
||||
environment: dev
|
||||
|
||||
resources:
|
||||
- resources/deployment.yaml
|
||||
`,
|
||||
expectedResult: `apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
app: sample-deploy
|
||||
environment: dev
|
||||
location: planet-earth
|
||||
name: sample-deploy
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: sample-deploy
|
||||
strategy: {}
|
||||
template:
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
app: sample-deploy
|
||||
environment: dev
|
||||
location: planet-earth
|
||||
spec:
|
||||
containers:
|
||||
- image: hello-world:latest
|
||||
name: hello-world
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "includeSelectors=false, includeTemplates=false, no transformerConfig",
|
||||
kustomization: `labels:
|
||||
- includeSelectors: false
|
||||
includeTemplates: false
|
||||
pairs:
|
||||
location: planet-earth
|
||||
environment: dev
|
||||
|
||||
resources:
|
||||
- resources/deployment.yaml
|
||||
`,
|
||||
expectedResult: `apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
app: sample-deploy
|
||||
environment: dev
|
||||
location: planet-earth
|
||||
name: sample-deploy
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: sample-deploy
|
||||
strategy: {}
|
||||
template:
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
app: sample-deploy
|
||||
spec:
|
||||
containers:
|
||||
- image: hello-world:latest
|
||||
name: hello-world
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
th := kusttest_test.MakeHarness(t)
|
||||
th.WriteK(".", tc.kustomization)
|
||||
th.WriteF("resources/deployment.yaml",
|
||||
`apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
app: sample-deploy
|
||||
name: sample-deploy
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: sample-deploy
|
||||
|
||||
strategy: {}
|
||||
template:
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
app: sample-deploy
|
||||
spec:
|
||||
containers:
|
||||
- image: hello-world:latest
|
||||
name: hello-world
|
||||
`)
|
||||
if tc.transformerConfig != "" {
|
||||
th.WriteF("config/configurations.yaml", tc.transformerConfig)
|
||||
}
|
||||
|
||||
output := th.Run(".", th.MakeDefaultOptions())
|
||||
|
||||
th.AssertActualEqualsExpected(output, tc.expectedResult)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLabelTransformerConfigWithCustomResources(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
kustomization string
|
||||
transformerConfig string
|
||||
expectedResult string
|
||||
}{
|
||||
{
|
||||
name: "include template via transformerConfig",
|
||||
kustomization: `configurations:
|
||||
- config/configurations.yaml
|
||||
|
||||
labels:
|
||||
- includeSelectors: false
|
||||
includeTemplates: false
|
||||
pairs:
|
||||
location: planet-earth
|
||||
environment: dev
|
||||
|
||||
resources:
|
||||
- resources/custom-resource.yaml
|
||||
`,
|
||||
transformerConfig: `labels:
|
||||
- path: spec/template/metadata/labels
|
||||
create: true
|
||||
kind: SampleResource
|
||||
`,
|
||||
expectedResult: `apiVersion: custom.example.org/v1
|
||||
kind: SampleResource
|
||||
metadata:
|
||||
labels:
|
||||
environment: dev
|
||||
location: planet-earth
|
||||
name: sample-resource
|
||||
namespace: sample-namespace
|
||||
spec:
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
environment: dev
|
||||
location: planet-earth
|
||||
spec:
|
||||
containers:
|
||||
- env:
|
||||
- name: VARIABLE
|
||||
value: value
|
||||
image: index.docker.io/library/hello-world
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "include selector via transformerConfig",
|
||||
kustomization: `configurations:
|
||||
- config/configurations.yaml
|
||||
|
||||
labels:
|
||||
- includeSelectors: false
|
||||
includeTemplates: false
|
||||
pairs:
|
||||
location: planet-earth
|
||||
environment: dev
|
||||
|
||||
resources:
|
||||
- resources/custom-resource.yaml
|
||||
`,
|
||||
transformerConfig: `labels:
|
||||
- path: spec/selectors/labels
|
||||
create: true
|
||||
kind: SampleResource
|
||||
`,
|
||||
expectedResult: `apiVersion: custom.example.org/v1
|
||||
kind: SampleResource
|
||||
metadata:
|
||||
labels:
|
||||
environment: dev
|
||||
location: planet-earth
|
||||
name: sample-resource
|
||||
namespace: sample-namespace
|
||||
spec:
|
||||
selectors:
|
||||
labels:
|
||||
environment: dev
|
||||
location: planet-earth
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- env:
|
||||
- name: VARIABLE
|
||||
value: value
|
||||
image: index.docker.io/library/hello-world
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "include selectors and labels via transformerConfig",
|
||||
kustomization: `configurations:
|
||||
- config/configurations.yaml
|
||||
|
||||
labels:
|
||||
- includeSelectors: false
|
||||
includeTemplates: false
|
||||
pairs:
|
||||
location: planet-earth
|
||||
environment: dev
|
||||
|
||||
resources:
|
||||
- resources/custom-resource.yaml
|
||||
`,
|
||||
transformerConfig: `
|
||||
labels:
|
||||
- path: spec/selectors/labels
|
||||
create: true
|
||||
kind: SampleResource
|
||||
- path: spec/template/metadata/labels
|
||||
create: true
|
||||
kind: SampleResource
|
||||
`,
|
||||
expectedResult: `apiVersion: custom.example.org/v1
|
||||
kind: SampleResource
|
||||
metadata:
|
||||
labels:
|
||||
environment: dev
|
||||
location: planet-earth
|
||||
name: sample-resource
|
||||
namespace: sample-namespace
|
||||
spec:
|
||||
selectors:
|
||||
labels:
|
||||
environment: dev
|
||||
location: planet-earth
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
environment: dev
|
||||
location: planet-earth
|
||||
spec:
|
||||
containers:
|
||||
- env:
|
||||
- name: VARIABLE
|
||||
value: value
|
||||
image: index.docker.io/library/hello-world
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
th := kusttest_test.MakeHarness(t)
|
||||
th.WriteK(".", tc.kustomization)
|
||||
th.WriteF("resources/custom-resource.yaml",
|
||||
`apiVersion: custom.example.org/v1
|
||||
kind: SampleResource
|
||||
metadata:
|
||||
name: sample-resource
|
||||
namespace: sample-namespace
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- image: index.docker.io/library/hello-world
|
||||
env:
|
||||
- name: VARIABLE
|
||||
value: value
|
||||
`)
|
||||
|
||||
th.WriteF("config/configurations.yaml", tc.transformerConfig)
|
||||
|
||||
output := th.Run(".", th.MakeDefaultOptions())
|
||||
|
||||
th.AssertActualEqualsExpected(output, tc.expectedResult)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user