I need to add a metadata label to every object in a kubernetes manifest; extraLabel: TEST-1234567890
. The label value varies per test/acc/prod environment. I use the following patch configuration to apply the label to each kubernetes object, per environment:
test/kustomize.yml:
resources:
- ../base
patches:
- path: patch-test.yml
test/patch-test.yml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: some-deployment
labels:
extraLabel: TEST-1234567890
spec:
template:
metadata:
labels:
extraLabel: TEST-1234567890
---
apiVersion: v1
kind: Service
metadata:
name: some-service
labels:
extraLabel: TEST-1234567890
---
...etc
This is a bit cumbersome because each kubernetes object has to be manually added to the patch manifest for each environment, and the label value is duplicated across multiple patch files. I am looking for a way to have kustomize automatically apply the label (and value, varying per environment) to each kubernetes object.
I have tried creating a patch manifest containing a commonLabels
section, however the use of commonLabels resulted in the label also being applied to selector.matchLabels
and labelSelector.matchLabels
, which is an undesired outcome. Attempting to patch the commonLabels section using:
- target:
kind: Deployment
patch: |-
- op: remove
path: /spec/selector/matchLabels/extraLabel
... etc
Did not work. Would anyone have a resolution to this matter?
For anyone interested, I managed to add labels to every object by using the labels[].pairs
section.
test/kustomize.yml:
resources:
- ../base
labels:
- pairs:
extraLabel: TEST-1234567890
someOtherLabel: abc
includeTemplates: true
includeTemplates
is set to true to add the labels to template sections as well (such as spec.template.metadata.labels
)