1

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

I have multiple docker images and during deployment all having same tag. I can manually change all the tag value and I can run it kubectl apply -k . through command prompt.

Question is, I don't want to change this file manually, I want to send tag value as a command line argument within kubectl apply -k . command. Is there way to do that? Thanks.

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
images:
- name: foo/bar
  newTag: "36"
- name: zoo/too
  newTag: "36"
user584018
  • 10,186
  • 15
  • 74
  • 160
  • 1
    I don't have time to test it right now, but maybe you can use ENV vars with some tricks. Check https://github.com/kubernetes-sigs/kustomize/issues/2301 – whites11 Jul 09 '21 at 15:46
  • Thanks @whites11 . I didn't get it fully. Whenever you have time, please suggest how to do in my case. Appreciate – user584018 Jul 09 '21 at 15:58

1 Answers1

1

It my opinion using "file" approach is the correct way and the best solution even while working with your test scenario.

By the "correct way" I mean this is how you should work with kustomize - keeping your environment specific data into separate directory.

kustomize supports the best practice of storing one’s entire configuration in a version control system.

  1. Before kustomize build . you can change those values using:
kustomize edit set image foo/bar=12.5

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
...
images:
- name: foo/bar
  newName: "12.5"

  1. Using envsubst approach:
  • deployment.yaml and kustomization.yaml in base directory:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml


apiVersion: apps/v1
kind: Deployment
metadata:
  name: the-deployment
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 1  
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: test
        image: foo/bar:1.2

directory tree with test overlay:

├── base
│   ├── deployment.yaml
│   └── kustomization.yaml
└── overlays
    └── test
        └── kustomization2.yaml

  • Create new kustomization2.yaml with variables in overlay/test directory:
cd overlays/test
cat kustomization2.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
images:
- name: foo/bar
  newTag: "${IMAGE_TAG}"
export IMAGE_TAG="2.2.11" ; envsubst < kustomization2.yaml > kustomization.yaml ; kustomize build .


output:

apiVersion: apps/v1
kind: Deployment
metadata:
...
    spec:
      containers:
      - image: foo/bar:2.2.11
        name: test

Files in directory after envsubst :


.
├── base
│   ├── deployment.yaml
│   └── kustomization.yaml
└── overlays
    └── test
        ├── kustomization2.yaml
        └── kustomization.yaml
  1. You can always pipe the result from kustomize build . into kubectl to change the image on the fly :
kustomize build . | kubectl set image -f -  test=nginx3:4 --local -o yaml 

Output:
apiVersion: apps/v1
kind: Deployment
metadata:
...
    spec:
      containers:
      - image: nginx3:4
        name: test

Note:

build in solution

Build-time side effects from CLI args or env variables

Changing kustomize build configuration output as a result of additional >arguments or flags to build, or by consulting shell environment variable >values in build code, would frustrate that goal.

kustomize insteads offers kustomization file edit commands. Like any shell >command, they can accept environment variable arguments.

For example, to set the tag used on an image to match an environment >variable, run kustomize edit set image nginx:$MY_NGINX_VERSION as part of some encapsulating work flow executed before kustomize build

Mark
  • 3,644
  • 6
  • 23