<p>I have a project that needs to <strong>inject or update environment variables</strong> in the pod container, using <code>kubebuilder</code> and <code>controller-runtime</code>,</p>
<p>My plan is as follows:</p>
<pre><code>func Reconcile(){
    // get added environment variables
    // get matched pods
    // update container env
}
</code></pre>
<p>After I tried to update, the following error occurred</p>
<pre><code> &quot;namespace&quot;: &quot;default&quot;, &quot;error&quot;: &quot;Pod \&quot;busybox\&quot; is invalid: spec: Forbidden: pod updates may not change fields other than `spec.containers[*].image`, `spec.initContainers[*].image`, `spec.activeDeadlineSeconds` or `spec.tolerations` (only additions to existing tolerations)\n  core.PodSpec{\n  \tVolumes:        []core.Volume{{Name: \&quot;default-token-bcr86\&quot;, VolumeSource: core.VolumeSource{Secret: &amp;core.SecretVolumeSource{SecretName: \&quot;default-token-bcr86\&quot;, DefaultMode: &amp;420}}}},\n  \tInitContainers: nil,\n  \tContainers: []core.Container{\n  \t\t{\n  \t\t\t... // 5 identical fields\n  \t\t\tPorts:        nil,\n  \t\t\tEnvFrom:      nil,\n- \t\t\tEnv:          []core.EnvVar{
</code></pre>
<pre><code># pod.yml of busybox
apiVersion: v1
kind: Pod
metadata:
  name: busybox
  namespace: default
  labels:
    match: &quot;test&quot;
spec:
  containers:
  - image: busybox
    command:
      - sleep
      - &quot;3600&quot;
    imagePullPolicy: IfNotPresent
    name: busybox
  restartPolicy: Always

</code></pre>
<p>but it seems that env cannot be updated in the source code</p>
<p><code>//  k8s.io/api/core/v1/types.go</code></p>
<pre><code>// A single application container that you want to run within a pod.
type Container struct {
    .....
    // List of sources to populate environment variables in the container.
    // The keys defined within a source must be a C_IDENTIFIER. All invalid keys
    // will be reported as an event when the container is starting. When a key exists in multiple
    // sources, the value associated with the last source will take precedence.
    // Values defined by an Env with a duplicate key will take precedence.
    // Cannot be updated.
    // +optional
    EnvFrom []EnvFromSource `json:&quot;envFrom,omitempty&quot; protobuf:&quot;bytes,19,rep,name=envFrom&quot;`
    // List of environment variables to set in the container.
    // Cannot be updated.
    // +optional
    // +patchMergeKey=name
    // +patchStrategy=merge
    Env []EnvVar `json:&quot;env,omitempty&quot; patchStrategy:&quot;merge&quot; patchMergeKey:&quot;name&quot; protobuf:&quot;bytes,7,rep,name=env&quot;`
    .....
}
</code></pre>
<p>I would appreciate it if you could tell me how to solve it.</p>

<p>You can't do this on a running pod without killing the pod and restarting it with a new config (containing your updated env vars) because these are set at pod startup.</p>
<p>Some options:</p>
<ol>
<li>Kill the pod and recreate it with the new env vars.</li>
<li>Update the env vars in the deployment instead of the pod which will rollout the change.</li>
<li>Consider using some other means of storing these key-value pairs (ex: a volume, or a Custom Resource).</li>
</ol>
<p>Can you describe your use case a bit more?</p>
