0

I have a Kubernetes Statefulset and im using envFrom to add environment variables from ConfigMaps and Secrets, by defining configMapRefs and secretRefs in an 'extra-values.yaml' file and including that file in my helm install command.

The Statefulset.yaml snippet:

apiVersion: apps/v1
kind: StatefulSet
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    spec:
      containers:
        - name: {{ .Chart.Name | lower}}
          envFrom:
          {{- if .Values.envFrom }}
          {{- toYaml .Values.envFrom | nindent 10}}
          {{- end }}

The values.yaml file has a single envFrom: line with no children, and the extra-values.yaml file contains the configMapRefs and secretRefs:

envFrom:
- configMapRef:
    name: my-configmap-name
- configMapRef:
    name: another-configmap-name
- secretRef:
    name: my-secret-name
- secretRef:
    name: second-secret-name

The Helm install command:

helm install myapp /some-folder/myapps-chart-folder -f extra-values.yaml

What I want to do is install myapp without the extra-values.yaml file, and then use the kubectl patch command to add the configMapRefs and secretRefs to the statefulset and its pods.

I can manually do a kubectl edit statefulset to make these changes, which will terminate and restart the pod(s) with the correct environment variables.

But I cannot for the life of me figure out the correct syntax and parameters for the kubectl patch command, despite hours of research, trial, and error, and repeated headbanging. Help!

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
Steve
  • 347
  • 3
  • 11
  • 1
    You said as lot of what _didn't_ work, but you haven't included in your question what you _did_ try and what outcome it produced for you. Regardless, you're looking for `kubectl -n WHATEVER patch -p '{"spec":{"template":{"spec":{"containers":[{"name":"WHATEVER", "envFrom":[{"configMapRef":{"name": "my-configmap-name"}}, ...]}]}}}}' statefulset/WHATEVER` right? – mdaniel Sep 28 '21 at 02:56
  • Why not use `helm upgrade -f extra-values.yaml` to add in the values (and then the current state of the system can be recorded in source control)? Why this particular workflow of installing a partly-configured system, then completing the configuration? – David Maze Sep 28 '21 at 10:48
  • @DavidMaze, I did not think to use `helm upgrade` but that also works, and the syntax is easier than `kubectl patch`, thanks! To answer your second question, the Kubernetes app is install on-premise at the customer site, and then configured based on the customer's environment. – Steve Sep 29 '21 at 14:33

1 Answers1

1

Thanks to mdaniel for the answer, which contains the clue to what I was missing. Basically, I completely overlooked the fact that the containers element is an array (because my statefulset only specified one container, duh). In all of the kubectl patch command variations that I tried, I did not treat containers as an array, and never specified the container name, so kubectl patch never really had the correct information to act on.

So as suggested, the command that worked was something like this:

kubectl patch statefulset my-statefulset -p '{"spec": {"template": {"spec": {"containers": [{"name":"the-container-name", "envFrom": [{"configMapRef":{"name":"my-configmap-name"}}, {"configMapRef":{"name":"another-configmap-name"}}] }] }}}}'
Steve
  • 347
  • 3
  • 11
  • this way seems incorrect as it replaces array, while in most cases if you want to patch you would like to add additional item – Alex K Jul 28 '22 at 00:33