1

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
Jonas
  • 121,568
  • 97
  • 310
  • 388
shawnzhu
  • 7,233
  • 4
  • 35
  • 51
  • A similar question has been asked here with the answer by using ImageTagTransformer: https://github.com/kubernetes-sigs/kustomize/issues/3542 – shawnzhu Jul 07 '21 at 20:21

2 Answers2

4

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
  • 1,618
  • 6
  • 18
  • Thank you! I believe it works by using `yq`. it would be great to include such solution as a kustomize plugin so that it can be done with YAML – shawnzhu Jul 07 '21 at 20:21
2

This is a working example of using the ImageTagTransformer.

Given below yaml files:

# 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:

$ 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
  • 7,233
  • 4
  • 35
  • 51