kustomize: how to pass `newTag` as Label

Viewed 705

I am using https://kustomize.io/ have below is my kustomization.yaml file,

I would like to pass newTag image version to labels on deployment.yaml when i use ArgoCD to apply this file. Does anyone have any idea without using shell script to sed the newtag to deployment.yaml file.

deployment.yaml

apiVersion: "apps/v1"
kind: "Deployment"
metadata:
  name: "hellowolrd"
spec:
  template:
    metadata:
      labels:
        app: aggregate
        appversion: ${newtag} <<<<<

kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: hellowolrd
  newName: hellowolrd
  newTag: 12345
2 Answers

You can't do this directly with newTag value. However you can use a PatchTransformer built-in plugin to change appversion value.

Add this

resources:
  - deployment.yaml
patches:
  - patch: |-
      - op: replace
        path: /spec/template/metadata/labels/appversion
        value: v2
    target:
      kind: Deployment

to your kustomization.yaml, and run kustomize build.
The result will look like this

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hellowolrd
spec:
  template:
    metadata:
      labels:
        app: aggregate
        appversion: v2

Yo ucan read more here.

Related