Skip to content

Commit 929f330

Browse files
lmicciniclaude
andcommitted
Add support for configuring environment variables for operators
This change allows users to specify custom environment variables for operator containers through the operatorOverrides field in the OpenStack CR. Changes: - Add Env field to ContainerSpec in the API types - Implement mergeEnvVars() to merge custom environment variables with defaults - Update SetOverrides() to apply environment variable overrides - Enhance operator deployment templates to support both Value and ValueFrom for environment variables (supporting secrets, configmaps, and field refs) Usage example: apiVersion: operator.openstack.org/v1beta1 kind: OpenStack metadata: name: openstack spec: operatorOverrides: - name: infra controllerManager: env: - name: SOME_ENV_VAR value: "some-value" Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6fd66d8 commit 929f330

8 files changed

Lines changed: 494 additions & 2 deletions

File tree

api/bases/operator.openstack.org_openstacks.yaml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,72 @@ spec:
5757
description: ControllerManager - tunings for the controller
5858
manager container
5959
properties:
60+
env:
61+
items:
62+
properties:
63+
name:
64+
type: string
65+
value:
66+
type: string
67+
valueFrom:
68+
properties:
69+
configMapKeyRef:
70+
properties:
71+
key:
72+
type: string
73+
name:
74+
default: ""
75+
type: string
76+
optional:
77+
type: boolean
78+
required:
79+
- key
80+
type: object
81+
x-kubernetes-map-type: atomic
82+
fieldRef:
83+
properties:
84+
apiVersion:
85+
type: string
86+
fieldPath:
87+
type: string
88+
required:
89+
- fieldPath
90+
type: object
91+
x-kubernetes-map-type: atomic
92+
resourceFieldRef:
93+
properties:
94+
containerName:
95+
type: string
96+
divisor:
97+
anyOf:
98+
- type: integer
99+
- type: string
100+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
101+
x-kubernetes-int-or-string: true
102+
resource:
103+
type: string
104+
required:
105+
- resource
106+
type: object
107+
x-kubernetes-map-type: atomic
108+
secretKeyRef:
109+
properties:
110+
key:
111+
type: string
112+
name:
113+
default: ""
114+
type: string
115+
optional:
116+
type: boolean
117+
required:
118+
- key
119+
type: object
120+
x-kubernetes-map-type: atomic
121+
type: object
122+
required:
123+
- name
124+
type: object
125+
type: array
60126
resources:
61127
description: |-
62128
Resources - Compute Resources for the service operator controller manager

api/operator/v1beta1/openstack_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ type ContainerSpec struct {
214214
// Resources - Compute Resources for the service operator controller manager
215215
// https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
216216
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
217+
218+
// +kubebuilder:validation:Optional
219+
// Env - Environment variables for the container
220+
Env []corev1.EnvVar `json:"env,omitempty"`
217221
}
218222

219223
// OpenStackStatus defines the observed state of OpenStack

api/operator/v1beta1/zz_generated.deepcopy.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindata/operator/managers.yaml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,27 @@ spec:
4141
- /manager
4242
env:
4343
{{- range .Deployment.Manager.Env }}
44-
- name: {{ .Name }}
44+
- name: '{{ .Name }}'
45+
{{- if .Value }}
4546
value: '{{ .Value }}'
47+
{{- end }}
48+
{{- if .ValueFrom }}
49+
valueFrom:
50+
{{- if .ValueFrom.FieldRef }}
51+
fieldRef:
52+
fieldPath: '{{ .ValueFrom.FieldRef.FieldPath }}'
53+
{{- end }}
54+
{{- if .ValueFrom.ConfigMapKeyRef }}
55+
configMapKeyRef:
56+
name: '{{ .ValueFrom.ConfigMapKeyRef.Name }}'
57+
key: '{{ .ValueFrom.ConfigMapKeyRef.Key }}'
58+
{{- end }}
59+
{{- if .ValueFrom.SecretKeyRef }}
60+
secretKeyRef:
61+
name: '{{ .ValueFrom.SecretKeyRef.Name }}'
62+
key: '{{ .ValueFrom.SecretKeyRef.Key }}'
63+
{{- end }}
64+
{{- end }}
4665
{{- end }}
4766
image: {{ .Deployment.Manager.Image }}
4867
livenessProbe:

config/crd/bases/operator.openstack.org_openstacks.yaml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,72 @@ spec:
5757
description: ControllerManager - tunings for the controller
5858
manager container
5959
properties:
60+
env:
61+
items:
62+
properties:
63+
name:
64+
type: string
65+
value:
66+
type: string
67+
valueFrom:
68+
properties:
69+
configMapKeyRef:
70+
properties:
71+
key:
72+
type: string
73+
name:
74+
default: ""
75+
type: string
76+
optional:
77+
type: boolean
78+
required:
79+
- key
80+
type: object
81+
x-kubernetes-map-type: atomic
82+
fieldRef:
83+
properties:
84+
apiVersion:
85+
type: string
86+
fieldPath:
87+
type: string
88+
required:
89+
- fieldPath
90+
type: object
91+
x-kubernetes-map-type: atomic
92+
resourceFieldRef:
93+
properties:
94+
containerName:
95+
type: string
96+
divisor:
97+
anyOf:
98+
- type: integer
99+
- type: string
100+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
101+
x-kubernetes-int-or-string: true
102+
resource:
103+
type: string
104+
required:
105+
- resource
106+
type: object
107+
x-kubernetes-map-type: atomic
108+
secretKeyRef:
109+
properties:
110+
key:
111+
type: string
112+
name:
113+
default: ""
114+
type: string
115+
optional:
116+
type: boolean
117+
required:
118+
- key
119+
type: object
120+
x-kubernetes-map-type: atomic
121+
type: object
122+
required:
123+
- name
124+
type: object
125+
type: array
60126
resources:
61127
description: |-
62128
Resources - Compute Resources for the service operator controller manager

config/operator/managers.yaml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,27 @@ spec:
4141
- /manager
4242
env:
4343
{{- range .Deployment.Manager.Env }}
44-
- name: {{ .Name }}
44+
- name: '{{ .Name }}'
45+
{{- if .Value }}
4546
value: '{{ .Value }}'
47+
{{- end }}
48+
{{- if .ValueFrom }}
49+
valueFrom:
50+
{{- if .ValueFrom.FieldRef }}
51+
fieldRef:
52+
fieldPath: '{{ .ValueFrom.FieldRef.FieldPath }}'
53+
{{- end }}
54+
{{- if .ValueFrom.ConfigMapKeyRef }}
55+
configMapKeyRef:
56+
name: '{{ .ValueFrom.ConfigMapKeyRef.Name }}'
57+
key: '{{ .ValueFrom.ConfigMapKeyRef.Key }}'
58+
{{- end }}
59+
{{- if .ValueFrom.SecretKeyRef }}
60+
secretKeyRef:
61+
name: '{{ .ValueFrom.SecretKeyRef.Name }}'
62+
key: '{{ .ValueFrom.SecretKeyRef.Key }}'
63+
{{- end }}
64+
{{- end }}
4665
{{- end }}
4766
image: {{ .Deployment.Manager.Image }}
4867
livenessProbe:

internal/operator/override.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,42 @@ func SetOverrides(opOvr operatorv1beta1.OperatorSpec, op *Operator) {
111111
op.Deployment.Manager.Resources.Requests.Memory = opOvr.ControllerManager.Resources.Requests.Memory().String()
112112
}
113113
}
114+
if len(opOvr.ControllerManager.Env) > 0 {
115+
op.Deployment.Manager.Env = mergeEnvVars(op.Deployment.Manager.Env, opOvr.ControllerManager.Env)
116+
}
114117
if len(opOvr.Tolerations) > 0 {
115118
op.Deployment.Tolerations = mergeTolerations(op.Deployment.Tolerations, opOvr.Tolerations)
116119
}
117120
}
118121

122+
// mergeEnvVars merges custom environment variables with default environment variables.
123+
// If a custom env var has the same name as a default one, it overrides the default.
124+
// Otherwise, the custom env var is added to the list.
125+
func mergeEnvVars(defaults, custom []corev1.EnvVar) []corev1.EnvVar {
126+
if len(custom) == 0 {
127+
return defaults
128+
}
129+
130+
// Start with a copy of defaults
131+
merged := make([]corev1.EnvVar, len(defaults))
132+
copy(merged, defaults)
133+
134+
// For each custom env var, check if it should override a default one
135+
for _, customEnv := range custom {
136+
f := func(c corev1.EnvVar) bool {
137+
return c.Name == customEnv.Name
138+
}
139+
idx := slices.IndexFunc(merged, f)
140+
if idx >= 0 {
141+
merged[idx] = customEnv
142+
} else {
143+
merged = append(merged, customEnv)
144+
}
145+
}
146+
147+
return merged
148+
}
149+
119150
// mergeTolerations merges custom tolerations with default tolerations.
120151
// If a custom toleration has the same key as a default one, it overrides the default.
121152
// Otherwise, the custom toleration is added to the list.

0 commit comments

Comments
 (0)