Skip to content

Commit 0515f90

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 - Configure OPERATOR_SCOPE_NAMESPACE for rabbitmq-cluster-operator: apiVersion: operator.openstack.org/v1beta1 kind: OpenStack metadata: name: openstack spec: operatorOverrides: - name: rabbitmq-cluster controllerManager: env: - name: OPERATOR_SCOPE_NAMESPACE value: "namespace1,namespace2,namespace3" Co-Authored-By: Claude <noreply@anthropic.com>
1 parent b102924 commit 0515f90

File tree

11 files changed

+524
-1
lines changed

11 files changed

+524
-1
lines changed

api/bases/operator.openstack.org_openstacks.yaml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,72 @@ spec:
3939
properties:
4040
controllerManager:
4141
properties:
42+
env:
43+
items:
44+
properties:
45+
name:
46+
type: string
47+
value:
48+
type: string
49+
valueFrom:
50+
properties:
51+
configMapKeyRef:
52+
properties:
53+
key:
54+
type: string
55+
name:
56+
default: ""
57+
type: string
58+
optional:
59+
type: boolean
60+
required:
61+
- key
62+
type: object
63+
x-kubernetes-map-type: atomic
64+
fieldRef:
65+
properties:
66+
apiVersion:
67+
type: string
68+
fieldPath:
69+
type: string
70+
required:
71+
- fieldPath
72+
type: object
73+
x-kubernetes-map-type: atomic
74+
resourceFieldRef:
75+
properties:
76+
containerName:
77+
type: string
78+
divisor:
79+
anyOf:
80+
- type: integer
81+
- type: string
82+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
83+
x-kubernetes-int-or-string: true
84+
resource:
85+
type: string
86+
required:
87+
- resource
88+
type: object
89+
x-kubernetes-map-type: atomic
90+
secretKeyRef:
91+
properties:
92+
key:
93+
type: string
94+
name:
95+
default: ""
96+
type: string
97+
optional:
98+
type: boolean
99+
required:
100+
- key
101+
type: object
102+
x-kubernetes-map-type: atomic
103+
type: object
104+
required:
105+
- name
106+
type: object
107+
type: array
42108
resources:
43109
properties:
44110
claims:

api/go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ github.com/openstack-k8s-operators/lib-common/modules/openstack v0.6.1-0.2025110
139139
github.com/openstack-k8s-operators/lib-common/modules/storage v0.6.1-0.20251122131503-b76943960b6c h1:dVIaDL5BeIdJjERGaN/XlcvZVplfkzh0uUfiVUHj/6Q=
140140
github.com/openstack-k8s-operators/lib-common/modules/storage v0.6.1-0.20251122131503-b76943960b6c/go.mod h1:fy1lvz3uuzzh01DKKdgroXvmJgMpJBsvl2r9eTtAll0=
141141
github.com/openstack-k8s-operators/lib-common/modules/test v0.6.1-0.20251122131503-b76943960b6c h1:YdTv3RXKfFg2QHXtLJSnKaPruslyp1Fd+ArcsxLcy6k=
142-
github.com/openstack-k8s-operators/lib-common/modules/test v0.6.1-0.20251122131503-b76943960b6c/go.mod h1:lgYyrXEYA2BPsq4Kg6dqa+QsHgOjMPyOsEYrvyYW3jk=
143142
github.com/openstack-k8s-operators/manila-operator/api v0.6.1-0.20251128162048-2454ee694c60 h1:MzY54K+s30dSTw6pLB+hu1hVo1NB2lo9XbbTifk0Lxc=
144143
github.com/openstack-k8s-operators/manila-operator/api v0.6.1-0.20251128162048-2454ee694c60/go.mod h1:GkUShNT3xXH7SWfgZfWeDTcKm2D4JL0Tv/qpBvZtV4w=
145144
github.com/openstack-k8s-operators/mariadb-operator/api v0.6.1-0.20251125174936-3989cc50de31 h1:pcxorrSN8YlCad3RgJWfMOdhlYtiT6ztYs+oTIYsxxc=

api/operator/v1beta1/openstack_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ type ContainerSpec struct {
238238
// Resources - Compute Resources for the service operator controller manager
239239
// https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
240240
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
241+
242+
// +kubebuilder:validation:Optional
243+
// Env - Environment variables for the container
244+
Env []corev1.EnvVar `json:"env,omitempty"`
241245
}
242246

243247
// 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/rabbit.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,26 @@ spec:
2929
fieldPath: metadata.namespace
3030
{{- range .RabbitmqOperator.Deployment.Manager.Env }}
3131
- name: '{{ .Name }}'
32+
{{- if .Value }}
3233
value: '{{ .Value }}'
34+
{{- end }}
35+
{{- if .ValueFrom }}
36+
valueFrom:
37+
{{- if .ValueFrom.FieldRef }}
38+
fieldRef:
39+
fieldPath: '{{ .ValueFrom.FieldRef.FieldPath }}'
40+
{{- end }}
41+
{{- if .ValueFrom.ConfigMapKeyRef }}
42+
configMapKeyRef:
43+
name: '{{ .ValueFrom.ConfigMapKeyRef.Name }}'
44+
key: '{{ .ValueFrom.ConfigMapKeyRef.Key }}'
45+
{{- end }}
46+
{{- if .ValueFrom.SecretKeyRef }}
47+
secretKeyRef:
48+
name: '{{ .ValueFrom.SecretKeyRef.Name }}'
49+
key: '{{ .ValueFrom.SecretKeyRef.Key }}'
50+
{{- end }}
51+
{{- end }}
3352
{{- end }}
3453
image: {{ .RabbitmqOperator.Deployment.Manager.Image }}
3554
name: operator

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,72 @@ spec:
3939
properties:
4040
controllerManager:
4141
properties:
42+
env:
43+
items:
44+
properties:
45+
name:
46+
type: string
47+
value:
48+
type: string
49+
valueFrom:
50+
properties:
51+
configMapKeyRef:
52+
properties:
53+
key:
54+
type: string
55+
name:
56+
default: ""
57+
type: string
58+
optional:
59+
type: boolean
60+
required:
61+
- key
62+
type: object
63+
x-kubernetes-map-type: atomic
64+
fieldRef:
65+
properties:
66+
apiVersion:
67+
type: string
68+
fieldPath:
69+
type: string
70+
required:
71+
- fieldPath
72+
type: object
73+
x-kubernetes-map-type: atomic
74+
resourceFieldRef:
75+
properties:
76+
containerName:
77+
type: string
78+
divisor:
79+
anyOf:
80+
- type: integer
81+
- type: string
82+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
83+
x-kubernetes-int-or-string: true
84+
resource:
85+
type: string
86+
required:
87+
- resource
88+
type: object
89+
x-kubernetes-map-type: atomic
90+
secretKeyRef:
91+
properties:
92+
key:
93+
type: string
94+
name:
95+
default: ""
96+
type: string
97+
optional:
98+
type: boolean
99+
required:
100+
- key
101+
type: object
102+
x-kubernetes-map-type: atomic
103+
type: object
104+
required:
105+
- name
106+
type: object
107+
type: array
42108
resources:
43109
properties:
44110
claims:

config/manifests/bases/openstack-operator.clusterserviceversion.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,13 @@ spec:
436436
- description: TLS - overrides tls parameters for public endpoint
437437
displayName: TLS
438438
path: telemetry.aodhApiOverride.tls
439+
- description: CloudKittyAPIOverride, provides the ability to override the generated
440+
manifest of several child resources.
441+
displayName: Cloud Kitty APIOverride
442+
path: telemetry.cloudKittyApiOverride
443+
- description: TLS - overrides tls parameters for public endpoint
444+
displayName: TLS
445+
path: telemetry.cloudKittyApiOverride.tls
439446
- description: Enabled - Whether OpenStack Telemetry services should be deployed
440447
and managed
441448
displayName: Enabled

config/operator/deployment/kustomization.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,28 @@ images:
1414
- name: controller
1515
newName: quay.io/openstack-k8s-operators/openstack-operator
1616
newTag: latest
17+
patches:
18+
- patch: '[{"op": "replace", "path": "/spec/template/spec/containers/0/env/0", "value":
19+
{"name": "OPENSTACK_RELEASE_VERSION", "value": "0.1.11-1764742415"}}]'
20+
target:
21+
kind: Deployment
22+
name: openstack-operator-controller-operator
23+
namespace: system
24+
- patch: '[{"op": "replace", "path": "/spec/template/spec/containers/0/env/1", "value":
25+
{"name": "OPERATOR_IMAGE_URL", "value": "quay.io/lmiccini/openstack-operator:v0.1.11"}}]'
26+
target:
27+
kind: Deployment
28+
name: openstack-operator-controller-operator
29+
namespace: system
30+
- patch: '[{"op": "replace", "path": "/spec/template/spec/containers/0/env/0", "value":
31+
{"name": "OPENSTACK_RELEASE_VERSION", "value": "0.5.0-1764743976"}}]'
32+
target:
33+
kind: Deployment
34+
name: openstack-operator-controller-operator
35+
namespace: system
36+
- patch: '[{"op": "replace", "path": "/spec/template/spec/containers/0/env/1", "value":
37+
{"name": "OPERATOR_IMAGE_URL", "value": "quay.io/openstack-k8s-operators/openstack-operator:latest"}}]'
38+
target:
39+
kind: Deployment
40+
name: openstack-operator-controller-operator
41+
namespace: system

config/operator/rabbit.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,26 @@ spec:
2929
fieldPath: metadata.namespace
3030
{{- range .RabbitmqOperator.Deployment.Manager.Env }}
3131
- name: '{{ .Name }}'
32+
{{- if .Value }}
3233
value: '{{ .Value }}'
34+
{{- end }}
35+
{{- if .ValueFrom }}
36+
valueFrom:
37+
{{- if .ValueFrom.FieldRef }}
38+
fieldRef:
39+
fieldPath: '{{ .ValueFrom.FieldRef.FieldPath }}'
40+
{{- end }}
41+
{{- if .ValueFrom.ConfigMapKeyRef }}
42+
configMapKeyRef:
43+
name: '{{ .ValueFrom.ConfigMapKeyRef.Name }}'
44+
key: '{{ .ValueFrom.ConfigMapKeyRef.Key }}'
45+
{{- end }}
46+
{{- if .ValueFrom.SecretKeyRef }}
47+
secretKeyRef:
48+
name: '{{ .ValueFrom.SecretKeyRef.Name }}'
49+
key: '{{ .ValueFrom.SecretKeyRef.Key }}'
50+
{{- end }}
51+
{{- end }}
3352
{{- end }}
3453
image: {{ .RabbitmqOperator.Deployment.Manager.Image }}
3554
name: operator

internal/operator/override.go

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

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

0 commit comments

Comments
 (0)