kustomize
kustomize copied to clipboard
includeTemplates doesn't work
Describe the bug
Labels aren't added to the pod metadata and the Deployment spec. includeTemplates has no effect.
Files that can reproduce the issue
kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
metadata:
name: base-kustomization
commonLabels:
app: test-server
labels:
- pairs:
app.kubernetes.io/name: test-server
app.kubernetes.io/instance: test-server
app.kubernetes.io/version: ${SERVER_VERSION}
app.kubernetes.io/component: test
app.kubernetes.io/part-of: test
app.kubernetes.io/managed-by: test
app.kubernetes.io/created-by: test
includeTemplates: true
resources:
- deployment.yaml
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-server
spec:
replicas: 1
template:
metadata:
name: test-server
spec:
restartPolicy: Always
containers:
- name: test-server
image: test-server
ports:
- containerPort: 8080
Expected output
The Pod should have all defined labels from (commonLabels and labels).
Actual output
The Pod has only the labels from commonLabels.
Kustomize version
{Version:kustomize/v4.5.5 GitCommit:daa3e5e2c2d3a4b8c94021a7384bfb06734bcd26 BuildDate:2022-05-20T20:25:40Z GoOs:linux GoArch:amd64}
Platform
Ubuntu 20.04 LTS inside WSL
/triage accepted
As a workaround, the bug only seems to occur if the spec.template.metadata.labels does not exist in the first place. Even labels: {} will generate the expected behaviour (this is likely a clue as to what the fix needs to be).
@aibarbetta as the author of this feature, would you be able to look into this bug?
/assign
The culprit is found here: https://github.com/kubernetes-sigs/kustomize/blob/5948f6aa63d57fdfadb77ad896b8b40bf9c2ffed/api/internal/target/kusttarget_configplugin.go#L289-L292
The CreateIfNotPresent should be true. Though, I don't know if this would make side effect elsewhere. But, if we go by the logic found within commonLabels, it shouldn't be a problem: https://github.com/kubernetes-sigs/kustomize/blob/d17ef91d94a9050c6b0d2cdc952827dd3771ec6b/api/konfig/builtinpluginconsts/commonlabels.go#L30-L32
As a bonus, the flag shouldn't be hardcoded to only spec/template/metadata/labels, but should include everything that is create: true from the commonLabels field specs: api/konfig/builtinpluginconsts/commonlabels.go