<p>When I want to exctract the current value of some container env variabe I could use jsonpath with syntax like:</p>

<pre><code>kubectl get pods -l component='somelabel' -n somenamespace -o \
jsonpath='{.items[*].spec.containers[*].env[?(@.name=="SOME_ENV_VARIABLE")].value}')
</code></pre>

<p>That will return me the value of env varialbe with the name <code>SOME_ENV_VARIABLE</code>. Pod section with container env variables in json will look like this:</p>

<pre><code>            "spec": {
                "containers": [
                    {
                        "env": [
                            {
                                "name": "SOME_ENV_VARIABLE",
                                "value": "some_value"
                            },
                            {
                                "name": "ANOTHER_ENV_VARIABLE",
                                "value": "another_value"
                            }
                        ],
</code></pre>

<p>When I want to patch some value in my deployment I'm using commands with syntax like:</p>

<pre><code>kubectl -n kube-system patch svc kubernetes-dashboard --type='json' -p="[{'op': 'replace', 'path': '/spec/ports/0/nodePort', 'value': $PORT}]"
</code></pre>

<p>But how can I patch a variable with <code>'op': 'replace'</code> in cases where I need to use expression like <code>env[?(@.name=="SOME_ENV_VARIABLE")]</code>? Which syntax I should use?</p>

lexadler
<p>Rather than <a href="https://kubernetes.io/docs/reference/kubectl/cheatsheet/#patching-resources" rel="noreferrer">kubectl patch</a> command, you can make use of <a href="https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#-em-env-em-" rel="noreferrer">kubectl set env</a> to update environment variable of k8s deployment.</p>

<pre><code>envvalue=$(kubectl get pods -l component='somelabel' -n somenamespace -o jsonpath='{.items[*].spec.containers[*].env[?(@.name=="SOME_ENV_VARIABLE")].value}')
kubectl set env deployment/my-app-deploy op=$envvalue
</code></pre>

<p>Hope this helps.</p>
