Score:0

How to edit patch items in a Kustomization file for doing gitops with Helm (and avoiding patches piling up)

us flag

I'm looking for a better way to update docker images defined ina HelmRelease using GitOps, as my current method is generating noise.

After introducing Helm to a cluster I'm managing with GitOps, I'm finding some difficulties on how to properly declare new docker image builds to be used in the cluster.

In a deployment I can use a simple Kustomization resource to replace image elements, e.g.:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: my-namespace

resources:
- namespace.yaml
- my-deployment.yaml

images:
- name: my/image
  newName: my/image
  newTag: updated-tag

and with every new build I simply modify the file with

kustomize edit set image my/image=my/image:updated-tag

Now with Helm I cannot use the same trick, as I need to update the tag spec.values.image in a HelmRelease, and Kustomize does not seem to have a shorthand for that. So the option is to create a patch:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: my-namespace

resources:
- namespace.yaml
- my-helm-release.yaml

patches:
- patch: '[{"op": "replace", "path": "/spec/values/image", "value": "my/image:updated-tag"}]'
  target:
    kind: HelmRelease
    name: my-helm-release
    namespace: my-namespace

by using a similar command:

kustomize edit add patch \
    --kind HelmRelease \
    --name my-helm-release \
    --namespace my-namespace --patch "[{\"op\": \"replace\", \"path\": \"/spec/values/image\", \"value\": \"my/image:updated-tag\"}]"

(don't mind much the quoted quotes, bear with me)

The problem comes when running multiple times this command. While the kustomize edit set image overwrites the previous value, in this later case a new patch is appended to the list with the more-updated-tag.

patches:
- patch: '[{"op": "replace", "path": "/spec/values/image", "value": "my/image:updated-tag"}]'
  target:
    kind: HelmRelease
    name: my-helm-release
    namespace: my-namespace
- patch: '[{"op": "replace", "path": "/spec/values/image", "value": "my/image:more-updated-tag"}]'
  target:
    kind: HelmRelease
    name: my-helm-release
    namespace: my-namespace

How can I avoid this repetition and adding more and more noise to my files?

Thanks!

Score:1
bo flag

found a cleaner way to do it with yq.

yq -ei '.spec.values.image.tag = "YOURTAG"' yourfile.yaml
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.