Skip to content

Commit

Permalink
fix: env vars with fieldRef
Browse files Browse the repository at this point in the history
add proper support for fieldRef and resourceFieldRef

fixes #74
  • Loading branch information
vbehar authored and arttor committed Feb 17, 2023
1 parent 5b663ea commit 8eb9f6d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
14 changes: 14 additions & 0 deletions examples/operator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ spec:
value: {{ .Values.controllerManager.manager.var2 }}
- name: VAR3_MY_ENV
value: {{ .Values.controllerManager.manager.var3MyEnv }}
- name: VAR4
valueFrom:
configMapKeyRef:
key: VAR4
name: my-operator-configmap-vars
- name: VAR5
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: VAR6
valueFrom:
resourceFieldRef:
divisor: "0"
resource: limits.cpu
- name: KUBERNETES_CLUSTER_DOMAIN
value: {{ .Values.kubernetesClusterDomain }}
image: {{ .Values.controllerManager.manager.image.repository }}:{{ .Values.controllerManager.manager.image.tag
Expand Down
25 changes: 15 additions & 10 deletions pkg/processor/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,18 +283,23 @@ func processPodContainer(name string, appMeta helmify.AppMetadata, c corev1.Cont
func processEnv(name string, appMeta helmify.AppMetadata, c corev1.Container, values *helmify.Values) (corev1.Container, error) {
containerName := strcase.ToLowerCamel(c.Name)
for i := 0; i < len(c.Env); i++ {
if c.Env[i].ValueFrom != nil && c.Env[i].ValueFrom.SecretKeyRef != nil {
c.Env[i].ValueFrom.SecretKeyRef.Name = appMeta.TemplatedName(c.Env[i].ValueFrom.SecretKeyRef.Name)
} else if c.Env[i].ValueFrom != nil && c.Env[i].ValueFrom.ConfigMapKeyRef != nil {
c.Env[i].ValueFrom.ConfigMapKeyRef.Name = appMeta.TemplatedName(c.Env[i].ValueFrom.ConfigMapKeyRef.Name)
} else {

err := unstructured.SetNestedField(*values, c.Env[i].Value, name, containerName, "env", strcase.ToLowerCamel(strings.ToLower(c.Env[i].Name)))
if err != nil {
return c, errors.Wrap(err, "unable to set deployment value field")
if c.Env[i].ValueFrom != nil {
switch {
case c.Env[i].ValueFrom.SecretKeyRef != nil:
c.Env[i].ValueFrom.SecretKeyRef.Name = appMeta.TemplatedName(c.Env[i].ValueFrom.SecretKeyRef.Name)
case c.Env[i].ValueFrom.ConfigMapKeyRef != nil:
c.Env[i].ValueFrom.ConfigMapKeyRef.Name = appMeta.TemplatedName(c.Env[i].ValueFrom.ConfigMapKeyRef.Name)
case c.Env[i].ValueFrom.FieldRef != nil, c.Env[i].ValueFrom.ResourceFieldRef != nil:
// nothing to change here, keep the original value
}
c.Env[i].Value = fmt.Sprintf(envValue, name, containerName, strcase.ToLowerCamel(strings.ToLower(c.Env[i].Name)))
continue
}

err := unstructured.SetNestedField(*values, c.Env[i].Value, name, containerName, "env", strcase.ToLowerCamel(strings.ToLower(c.Env[i].Name)))
if err != nil {
return c, errors.Wrap(err, "unable to set deployment value field")
}
c.Env[i].Value = fmt.Sprintf(envValue, name, containerName, strcase.ToLowerCamel(strings.ToLower(c.Env[i].Name)))
}
return c, nil
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/processor/deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ spec:
secretKeyRef:
name: my-operator-secret-vars
key: VAR1
- name: VAR2
valueFrom:
configMapKeyRef:
name: my-operator-configmap-vars
key: VAR2
- name: VAR3
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: VAR4
valueFrom:
resourceFieldRef:
resource: limits.cpu
- name: VAR5
value: "123"
image: controller:latest
livenessProbe:
httpGet:
Expand Down
13 changes: 13 additions & 0 deletions test_data/k8s-operator-kustomize.output
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,19 @@ spec:
value: "ciao"
- name: VAR3_MY_ENV
value: "ciao"
- name: VAR4
valueFrom:
configMapKeyRef:
name: my-operator-configmap-vars
key: VAR4
- name: VAR5
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: VAR6
valueFrom:
resourceFieldRef:
resource: limits.cpu
image: controller:latest
livenessProbe:
httpGet:
Expand Down

0 comments on commit 8eb9f6d

Please sign in to comment.