kustomize:如何从命令行传递 `newTag`

kustomize: how to pass `newTag` from command line

我正在使用 https://kustomize.io/ 下面是我的 kustomization.yaml 文件,

我有多个 docker 个图像,并且在部署期间都具有相同的 tag。我可以手动更改所有 tag 值,我可以 运行 它 kubectl apply -k . 通过命令提示符。

问题是,我不想手动更改此文件,我想在 kubectl apply -k . 命令中将 tag 值作为命令行参数发送。有办法吗?谢谢。

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
images:
- name: foo/bar
  newTag: "36"
- name: zoo/too
  newTag: "36"

我认为使用 "file" 方法是正确的方法,即使在处理测试场景时也是最佳解决方案。

通过 "正确的方式" 我的意思是这就是你应该如何使用 kustomize - 将你的环境特定数据保存在单独的目录中.

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

  1. kustomize build . 之前,您可以使用以下方法更改这些值:
kustomize edit set image foo/bar=12.5

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

  1. 使用envsubst方法:
  • deployment.yamlkustomization.yamlbase 目录中:
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

带有测试覆盖的目录树:

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

  • 使用 overlay/test 目录中的变量新建 kustomization2.yaml
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

envsubst之后目录中的文件:


.
├── base
│   ├── deployment.yaml
│   └── kustomization.yaml
└── overlays
    └── test
        ├── kustomization2.yaml
        └── kustomization.yaml
  1. 您始终可以将 kustomize build . 的结果通过管道传输到 kubectl 以动态更改图像:
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

:

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