5

I have a StatefulSet like this:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: myns
  name: myapp
spec:
  replicas: 3
  template:
    spec:
      containers:
      - name: mycontainer
        image: ...
        ...
        env:
        - name: MY_ENV1
          value: "1"

Now I want to add via Kustomize a second environment variable because it is used only in the dev environment. I did something like this:

namespace: myns

resources:
...

patches:
- patch: |-
    - op: add
      path: "/spec/template/spec/containers/0/env/-"
      value:
        - name: MY_ENV2
          value: "2"
  target:
    kind: StatefulSet
    namespace: myns
    name: myapp

The problem is that it doesn't work. If I run kustomize build I don't see this additional variable (I see other variations).

Can anyone help me to understand how to implement it?

Salvatore D'angelo
  • 1,019
  • 3
  • 14
  • 39
  • Which kustomize version are you using (`kustomize version` command)? – Mikolaj S. Nov 04 '21 at 17:22
  • Here the version v4.4.0 – Salvatore D'angelo Nov 05 '21 at 15:44
  • Hi @Salvatore D'angelo, any progress? – Mikolaj S. Nov 09 '21 at 18:33
  • I tried to replicate your issue and it's working fine on v4.4.0 - I have second environment variable. Could you please share your current folder structure, filenames and full yaml files (can be with different images name), so I can fully reproduce your issue? – Mikolaj S. Nov 10 '21 at 10:24
  • My problem was that I forgot to add: version: v1 in the target section. Moreover, you need also to remove the "-" in front of the tag name in the value section of the patch. Now the "add" works fine, the env variable is appended. Now I have a similar issue where instead of add I need replace to replace the value of an env variable already existing in the base YAML. – Salvatore D'angelo Nov 11 '21 at 09:29

2 Answers2

12

The problem with the issue was that I forgot to add version: v1 in the target section of the patch. Then you should also remove the - in front of the tag name in the value section. The result should be something like this:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: myns
  name: myapp
spec:
  replicas: 3
  template:
    spec:
      containers:
      - name: mycontainer
        image: ...
        ...
        env:
        - name: MY_ENV1
          value: "1"

here the kustomization.yamlfile:

namespace: myns

resources:
...

patches:
- patch: |-
    - op: add
      path: "/spec/template/spec/containers/0/env/-"
      value:
        name: MY_ENV2
        value: "2"
  target:
    kind: StatefulSet
    namespace: myns
    name: myapp

This worked fine for my original question. I have now a similar problem where this time I need to replace an existing environment variable in the base deployment.

Salvatore D'angelo
  • 1,019
  • 3
  • 14
  • 39
3

I would consider to use envFrom in your StatefulSet to load the variables from a ConfigMap, then you can use a configMapGenerator with N number of literals.

See Configure all key-value pairs in a ConfigMap as container environment variables for example of envFrom:

Use envFrom to define all of the ConfigMap's data as container environment variables. The key from the ConfigMap becomes the environment variable name in the Pod.

configMapGenerator is a good way to generate this ConfigMap. It can be done many different ways, but here is an example with literals - from the same documentation page, but adapted to your example:

cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: example-configmap-2
  literals:
  - MY_ENV1=Foo
  - MY_ENV2=Bar
EOF
Jonas
  • 121,568
  • 97
  • 310
  • 388
  • I already do this for other variables. In my case, I cannot do this because the value of my variable is $(POD_NAME).otherstring where POD_NAME is another variable that retrieves the pod name from the StatefulSet YAML. For this reason, I cannot externalize it in a ConfigMap. It depends on the StatefulSet deployment. – Salvatore D'angelo Nov 03 '21 at 17:27
  • I have found some examples here but they didn't work: https://stackoverflow.com/questions/55377644/is-there-a-way-to-make-kustomize-merge-headings-in-a-yaml-file-instead-of-comple – Salvatore D'angelo Nov 03 '21 at 17:34