'Kustomize using Strategic Merge patch on helmreleases

We keep in our Flux repo our HelmReleases. We use Kustomize to edit some of the keys in the HelmReleases. I tried using Strategic Merge patch in order to append a value into a list but instead the list was overwritten (which is the default it seems..)

Is there a way to use Strategic Merge Patch on HelmReleases in a way that will allow me to append values to a list (patch - merge) ?

My base.yaml is :

apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
  name: MyReleaseX
spec:
  releaseName: serviceXRelease
  chart:
    spec:
      chart: serviceXchart
      sourceRef:
        kind: HelmRepository
        name: my-repo
      valuesFiles:
        - values.yaml
  values:
     env:
        - name: ACCOUNT
          value: "x5" 

My kustomization file under overlay dir:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - ../base/serviceX
patchesStrategicMerge:
  - serviceX.yaml

I want to add in my overlay another env variable (I don't want to overwrite it the existing env).

When I tried the following in my overlay/ServiceX.yaml the list was overwritten and I had only one value:

apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
  name: serviceXRelease
spec:

  values:
     env:
       - name: NEW_ENV
         value: "val"

Is my only option using json patches instead of Strategic Merge patch like suggested here (just use merge instead of replace) ?



Solution 1:[1]

Personally, I prefer the solution @The Fool suggested. However, in my case that solution didn't work, might be related to Kustomize's version or the apiVersion I used (v4.4.1).

The following is the solution I used (json patches) :

My base/servicex.yaml is kept the same as i posted.

The kustomization file

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - ../base/serviceX # 

patches:
  - path: patch-env.yaml
    target:
      group: helm.toolkit.fluxcd.io
      version: v2beta1
      kind: HelmRelease
      name: MyReleaseX

The patch file :

- op: add
  path: "/spec/values/env/0"
  value:
    name: NEW_ENV
    value: VAL

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1