Skip to content

Commit a7cbd9b

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 a7cbd9b

File tree

8 files changed

+602
-2
lines changed

8 files changed

+602
-2
lines changed

api/bases/operator.openstack.org_openstacks.yaml

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,126 @@ spec:
5757
description: ControllerManager - tunings for the controller
5858
manager container
5959
properties:
60+
env:
61+
description: Env - Environment variables for the container
62+
items:
63+
description: EnvVar represents an environment variable
64+
present in a Container.
65+
properties:
66+
name:
67+
description: Name of the environment variable. Must
68+
be a C_IDENTIFIER.
69+
type: string
70+
value:
71+
description: |-
72+
Variable references $(VAR_NAME) are expanded
73+
using the previously defined environment variables in the container and
74+
any service environment variables. If a variable cannot be resolved,
75+
the reference in the input string will be unchanged. Double $$ are reduced
76+
to a single $, which allows for escaping the $(VAR_NAME) syntax: i.e.
77+
"$$(VAR_NAME)" will produce the string literal "$(VAR_NAME)".
78+
Escaped references will never be expanded, regardless of whether the variable
79+
exists or not.
80+
Defaults to "".
81+
type: string
82+
valueFrom:
83+
description: Source for the environment variable's
84+
value. Cannot be used if value is not empty.
85+
properties:
86+
configMapKeyRef:
87+
description: Selects a key of a ConfigMap.
88+
properties:
89+
key:
90+
description: The key to select.
91+
type: string
92+
name:
93+
default: ""
94+
description: |-
95+
Name of the referent.
96+
This field is effectively required, but due to backwards compatibility is
97+
allowed to be empty. Instances of this type with an empty value here are
98+
almost certainly wrong.
99+
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
100+
type: string
101+
optional:
102+
description: Specify whether the ConfigMap
103+
or its key must be defined
104+
type: boolean
105+
required:
106+
- key
107+
type: object
108+
x-kubernetes-map-type: atomic
109+
fieldRef:
110+
description: |-
111+
Selects a field of the pod: supports metadata.name, metadata.namespace, `metadata.labels['<KEY>']`, `metadata.annotations['<KEY>']`,
112+
spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP, status.podIPs.
113+
properties:
114+
apiVersion:
115+
description: Version of the schema the FieldPath
116+
is written in terms of, defaults to "v1".
117+
type: string
118+
fieldPath:
119+
description: Path of the field to select in
120+
the specified API version.
121+
type: string
122+
required:
123+
- fieldPath
124+
type: object
125+
x-kubernetes-map-type: atomic
126+
resourceFieldRef:
127+
description: |-
128+
Selects a resource of the container: only resources limits and requests
129+
(limits.cpu, limits.memory, limits.ephemeral-storage, requests.cpu, requests.memory and requests.ephemeral-storage) are currently supported.
130+
properties:
131+
containerName:
132+
description: 'Container name: required for
133+
volumes, optional for env vars'
134+
type: string
135+
divisor:
136+
anyOf:
137+
- type: integer
138+
- type: string
139+
description: Specifies the output format of
140+
the exposed resources, defaults to "1"
141+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
142+
x-kubernetes-int-or-string: true
143+
resource:
144+
description: 'Required: resource to select'
145+
type: string
146+
required:
147+
- resource
148+
type: object
149+
x-kubernetes-map-type: atomic
150+
secretKeyRef:
151+
description: Selects a key of a secret in the
152+
pod's namespace
153+
properties:
154+
key:
155+
description: The key of the secret to select
156+
from. Must be a valid secret key.
157+
type: string
158+
name:
159+
default: ""
160+
description: |-
161+
Name of the referent.
162+
This field is effectively required, but due to backwards compatibility is
163+
allowed to be empty. Instances of this type with an empty value here are
164+
almost certainly wrong.
165+
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
166+
type: string
167+
optional:
168+
description: Specify whether the Secret or
169+
its key must be defined
170+
type: boolean
171+
required:
172+
- key
173+
type: object
174+
x-kubernetes-map-type: atomic
175+
type: object
176+
required:
177+
- name
178+
type: object
179+
type: array
60180
resources:
61181
description: |-
62182
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: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,126 @@ spec:
5757
description: ControllerManager - tunings for the controller
5858
manager container
5959
properties:
60+
env:
61+
description: Env - Environment variables for the container
62+
items:
63+
description: EnvVar represents an environment variable
64+
present in a Container.
65+
properties:
66+
name:
67+
description: Name of the environment variable. Must
68+
be a C_IDENTIFIER.
69+
type: string
70+
value:
71+
description: |-
72+
Variable references $(VAR_NAME) are expanded
73+
using the previously defined environment variables in the container and
74+
any service environment variables. If a variable cannot be resolved,
75+
the reference in the input string will be unchanged. Double $$ are reduced
76+
to a single $, which allows for escaping the $(VAR_NAME) syntax: i.e.
77+
"$$(VAR_NAME)" will produce the string literal "$(VAR_NAME)".
78+
Escaped references will never be expanded, regardless of whether the variable
79+
exists or not.
80+
Defaults to "".
81+
type: string
82+
valueFrom:
83+
description: Source for the environment variable's
84+
value. Cannot be used if value is not empty.
85+
properties:
86+
configMapKeyRef:
87+
description: Selects a key of a ConfigMap.
88+
properties:
89+
key:
90+
description: The key to select.
91+
type: string
92+
name:
93+
default: ""
94+
description: |-
95+
Name of the referent.
96+
This field is effectively required, but due to backwards compatibility is
97+
allowed to be empty. Instances of this type with an empty value here are
98+
almost certainly wrong.
99+
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
100+
type: string
101+
optional:
102+
description: Specify whether the ConfigMap
103+
or its key must be defined
104+
type: boolean
105+
required:
106+
- key
107+
type: object
108+
x-kubernetes-map-type: atomic
109+
fieldRef:
110+
description: |-
111+
Selects a field of the pod: supports metadata.name, metadata.namespace, `metadata.labels['<KEY>']`, `metadata.annotations['<KEY>']`,
112+
spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP, status.podIPs.
113+
properties:
114+
apiVersion:
115+
description: Version of the schema the FieldPath
116+
is written in terms of, defaults to "v1".
117+
type: string
118+
fieldPath:
119+
description: Path of the field to select in
120+
the specified API version.
121+
type: string
122+
required:
123+
- fieldPath
124+
type: object
125+
x-kubernetes-map-type: atomic
126+
resourceFieldRef:
127+
description: |-
128+
Selects a resource of the container: only resources limits and requests
129+
(limits.cpu, limits.memory, limits.ephemeral-storage, requests.cpu, requests.memory and requests.ephemeral-storage) are currently supported.
130+
properties:
131+
containerName:
132+
description: 'Container name: required for
133+
volumes, optional for env vars'
134+
type: string
135+
divisor:
136+
anyOf:
137+
- type: integer
138+
- type: string
139+
description: Specifies the output format of
140+
the exposed resources, defaults to "1"
141+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
142+
x-kubernetes-int-or-string: true
143+
resource:
144+
description: 'Required: resource to select'
145+
type: string
146+
required:
147+
- resource
148+
type: object
149+
x-kubernetes-map-type: atomic
150+
secretKeyRef:
151+
description: Selects a key of a secret in the
152+
pod's namespace
153+
properties:
154+
key:
155+
description: The key of the secret to select
156+
from. Must be a valid secret key.
157+
type: string
158+
name:
159+
default: ""
160+
description: |-
161+
Name of the referent.
162+
This field is effectively required, but due to backwards compatibility is
163+
allowed to be empty. Instances of this type with an empty value here are
164+
almost certainly wrong.
165+
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
166+
type: string
167+
optional:
168+
description: Specify whether the Secret or
169+
its key must be defined
170+
type: boolean
171+
required:
172+
- key
173+
type: object
174+
x-kubernetes-map-type: atomic
175+
type: object
176+
required:
177+
- name
178+
type: object
179+
type: array
60180
resources:
61181
description: |-
62182
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:

0 commit comments

Comments
 (0)