如何在 kustomization.yaml 中为 CRD 中指定的容器镜像设置镜像名称/标签

时间:2021-07-07 14:47:43

标签: kubernetes yaml kustomize

如何使用图像字段通过 kustomization.yaml 为 CRD 中指定的容器图像设置图像名称/标签?

当容器映像在 DeploymentStatefulSet 中指定但不转换 CRD 资源时,images 字段运行良好:

apiVersion: foo.example.com/v1alpha1
kind: Application
spec:
  image: xxx

致:

apiVersion: foo.example.com/v1alpha1
kind: Application
spec:
  image: new-image:tag

2 个答案:

答案 0 :(得分:3)

使用 yq 可以轻松解决您的任务。该命令取决于您使用的 yq 实现:

mikefarah/yq - version 4

IMAGE="new-image:tag" yq e '.spec.image = strenv(IMAGE)'

kislyuk/yq

yq -y --arg IMAGE "new-image:tag" '.spec.image |= $IMAGE'

答案 1 :(得分:1)

这是一个使用 ImageTagTransformer 的工作示例。

给出以下 yaml 文件:

# transformer_1.yaml
---
apiVersion: builtin
kind: ImageTagTransformer
metadata:
  name: not-important-here
imageTag:
  name: xxx
  newName: new-image
  newTag: tag
fieldSpecs:
- path: spec/image
  kind: Application

# application.yaml
---
apiVersion: foo.example.com/v1alpha1
kind: Application
spec:
  image: xxx

# kustomization.yaml
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- application.yaml
transformers:
- transformer_1.yaml

运行这个命令:

$ kustomize build ./ | kpt cfg grep "kind=Application"
apiVersion: foo.example.com/v1alpha1
kind: Application
spec:
  image: new-image:tag

但是,我仍然需要想办法让它与 skaffold 工作流一起工作。

更新

来自 skaffold 社区的 feedback 表示它不支持重写清单,除了 allowlist 中的清单。所以吸取的教训是,skaffold 还不支持重写任何 CRD。