3

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
エック
  • 65
  • 2
  • 7

2 Answers2

3

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.

  • This is a good one, but I would like to change only `kustomization.yaml` file. Is it possible to pass `newTag` to `value: v2` like `value: images[helloworld].newTag`? – エック Feb 11 '22 at 09:13
  • 1
    You have to add the code in first block to *kustomization.yaml* file, no need to create/change anything else. Unfortunately, you can't self-reference a value inside a file, and since `patch` block is a literal string, anything in `value` will be passed to generated yaml as-is –  Feb 11 '22 at 09:37
0

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

Joe Bowbeer
  • 3,574
  • 3
  • 36
  • 47