'kustomize: how to pass `newTag` as Label

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


Solution 1:[1]

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.

Solution 2:[2]

I think you can create a Component to do this by combining the label replacement (v2) with a ReplacementTransformer that copies the new label into the image tag.

https://github.com/kubernetes-sigs/kustomize/issues/4174#issuecomment-917194980

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 p10l
Solution 2 Joe Bowbeer