how to set image name/tag for container images specified in CRDs in kustomization.yaml

7/7/2021

how to set image name/tag for container images specified in CRDs through the kustomization.yaml using the images field?

The images field works well when the container images are specified in either Deployment or StatefulSet, but not transform a CRD resource from:

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

To:

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

2 Answers

7/7/2021

Your task can be solved easily using yq. The command depends on the yq implementation you are using:

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'

-- jpseng
Source: StackOverflow

7/7/2021

This is a working example of using the ImageTagTransformer.

Given below yaml files:

<!-- language: 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

Run this command:

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

However, I still need to figure out ways to get it work with skaffold workflow.

Update

the feedback from skaffold community said it doesn't support rewriting manifests except those are in an allowlist. So the lesson learn is, skaffold doesn't support rewriting any CRDs yet.

-- shawnzhu
Source: StackOverflow