<p>I have a StatefulSet like this:</p>
<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: myns
  name: myapp
spec:
  replicas: 3
  template:
    spec:
      containers:
      - name: mycontainer
        image: ...
        ...
        env:
        - name: MY_ENV1
          value: &quot;1&quot;
</code></pre>
<p>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:</p>
<pre><code>namespace: myns

resources:
...

patches:
- patch: |-
    - op: add
      path: &quot;/spec/template/spec/containers/0/env/-&quot;
      value:
        - name: MY_ENV2
          value: &quot;2&quot;
  target:
    kind: StatefulSet
    namespace: myns
    name: myapp
</code></pre>
<p>The problem is that it doesn't work. If I run <code>kustomize build</code> I don't see this additional variable (I see other variations).</p>
<p><strong>Can anyone help me to understand how to implement it?</strong></p>

Salvatore D'angelo
<p>The problem with the issue was that I forgot to add <code>version: v1</code> in the target section of the patch. Then you should also remove the <code>-</code> in front of the tag name in the value section. The result should be something like this:</p>
<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: myns
  name: myapp
spec:
  replicas: 3
  template:
    spec:
      containers:
      - name: mycontainer
        image: ...
        ...
        env:
        - name: MY_ENV1
          value: &quot;1&quot;
</code></pre>
<p>here the <code>kustomization.yaml</code>file:</p>
<pre><code>namespace: myns

resources:
...

patches:
- patch: |-
    - op: add
      path: &quot;/spec/template/spec/containers/0/env/-&quot;
      value:
        name: MY_ENV2
        value: &quot;2&quot;
  target:
    kind: StatefulSet
    namespace: myns
    name: myapp
</code></pre>
<p>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.</p>
