Skip to content

Commit 4c1795a

Browse files
committed
feat(chart): add additionalEnvironmentVariables to controller deployment
Add additionalEnvironmentVariables to the Helm chart values so users can inject arbitrary environment variables into the controller container without modifying the chart. Each entry is a standard Kubernetes EnvVar object. Use this in e2e tests to pass KUBEWARDEN_DISABLE_CLIENT_CACHE=true via --set at install time, replacing the fragile post-install Deployment patch that also had a bug (searched for container name 'manager' instead of 'controller', so the env var was never actually injected). Signed-off-by: José Guilherme Vanz <jguilhermevanz@suse.com> Assisted-by: Github Copilot
1 parent c58e103 commit 4c1795a

5 files changed

Lines changed: 56 additions & 58 deletions

File tree

charts/kubewarden-controller/templates/deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ spec:
108108
{{- end }}
109109
{{- end }}
110110
{{- end }}
111+
{{- with .Values.additionalEnvironmentVariables }}
112+
{{- toYaml . | nindent 10 }}
113+
{{- end }}
111114

112115
image: '{{ template "system_default_registry" . }}{{ .Values.image.repository }}:{{ .Values.image.tag }}'
113116
imagePullPolicy: {{ .Values.image.pullPolicy }}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
suite: additionalEnvironmentVariables configuration
2+
templates:
3+
- deployment.yaml
4+
tests:
5+
- it: "should not add extra env vars when additionalEnvironmentVariables is empty (default)"
6+
asserts:
7+
- isNullOrEmpty:
8+
path: spec.template.spec.containers[0].env
9+
10+
- it: "should inject a single env var with a plain value"
11+
set:
12+
additionalEnvironmentVariables:
13+
- name: MY_VAR
14+
value: "my-value"
15+
asserts:
16+
- contains:
17+
path: spec.template.spec.containers[0].env
18+
any: true
19+
content:
20+
name: MY_VAR
21+
value: "my-value"
22+
23+
- it: "should inject multiple env vars"
24+
set:
25+
additionalEnvironmentVariables:
26+
- name: FIRST_VAR
27+
value: "first"
28+
- name: SECOND_VAR
29+
value: "second"
30+
asserts:
31+
- contains:
32+
path: spec.template.spec.containers[0].env
33+
any: true
34+
content:
35+
name: FIRST_VAR
36+
value: "first"
37+
- contains:
38+
path: spec.template.spec.containers[0].env
39+
any: true
40+
content:
41+
name: SECOND_VAR
42+
value: "second"

charts/kubewarden-controller/values.schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"additionalAnnotations": {
66
"type": "object"
77
},
8+
"additionalEnvironmentVariables": {
9+
"type": "array"
10+
},
811
"additionalLabels": {
912
"type": "object"
1013
},

charts/kubewarden-controller/values.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ preDeleteJob:
230230
# kubewarden-controller deployment settings:
231231
podAnnotations: {}
232232
nodeSelector: {}
233+
# additionalEnvironmentVariables is a list of additional environment variables
234+
# to inject into the controller container.
235+
# Each entry is a standard Kubernetes EnvVar object (name, value, valueFrom, …).
236+
# Example:
237+
# additionalEnvironmentVariables:
238+
# - name: MY_VAR
239+
# value: "my-value"
240+
additionalEnvironmentVariables: []
233241
# Resource limits & requests
234242
# Ref: https://kubernetes.io/docs/user-guide/compute-resources/
235243
resources:

e2e/main_test.go

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ import (
66
"fmt"
77
"os"
88
"testing"
9-
"time"
109

11-
appsv1 "k8s.io/api/apps/v1"
12-
corev1 "k8s.io/api/core/v1"
13-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14-
"sigs.k8s.io/e2e-framework/klient/wait"
15-
"sigs.k8s.io/e2e-framework/klient/wait/conditions"
1610
"sigs.k8s.io/e2e-framework/pkg/env"
1711
"sigs.k8s.io/e2e-framework/pkg/envconf"
1812
"sigs.k8s.io/e2e-framework/pkg/envfuncs"
@@ -77,21 +71,6 @@ func TestMain(m *testing.M) {
7771
return ctx, fmt.Errorf("failed to install kubewarden-controller helm chart: %w", err)
7872
}
7973

80-
// Inject KUBEWARDEN_DISABLE_CLIENT_CACHE into the controller
81-
// container so that all client reads bypass the informer cache and
82-
// hit the API server directly. This ensures RBAC permissions (including
83-
// the "get" verb) are fully evaluated during e2e tests without
84-
// exposing this testing knob in the Helm chart.
85-
if err = injectDisableClientCacheEnvVar(ctx, cfg); err != nil {
86-
return ctx, fmt.Errorf("failed to inject disable client cache env var: %w", err)
87-
}
88-
89-
// Wait explicitly for kubewarden-controller deployment to be ready
90-
err = waitForKubewardenControllerDeployment(ctx, cfg)
91-
if err != nil {
92-
return ctx, fmt.Errorf("failed to wait for kubewarden-controller deployment: %w", err)
93-
}
94-
9574
return ctx, nil
9675
},
9776
)
@@ -103,40 +82,3 @@ func TestMain(m *testing.M) {
10382

10483
os.Exit(testenv.Run(m))
10584
}
106-
107-
// waitForKubewardenControllerDeployment waits for the kubewarden-controller deployment to be ready.
108-
func waitForKubewardenControllerDeployment(_ context.Context, cfg *envconf.Config) error {
109-
// Wait for the kubewarden-controller deployment to be available
110-
return wait.For(conditions.New(cfg.Client().Resources()).DeploymentConditionMatch(
111-
&appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "kubewarden-controller", Namespace: namespace}},
112-
appsv1.DeploymentAvailable,
113-
corev1.ConditionTrue,
114-
), wait.WithTimeout(5*time.Minute), wait.WithInterval(1*time.Second))
115-
}
116-
117-
// injectDisableClientCacheEnvVar patches the kubewarden-controller Deployment
118-
// to set KUBEWARDEN_DISABLE_CLIENT_CACHE=true on the controller container.
119-
// This causes the controller to bypass the informer cache for all client reads,
120-
// ensuring RBAC permissions are fully evaluated by the API server during e2e
121-
// tests. The env var is not exposed in the Helm chart values intentionally.
122-
func injectDisableClientCacheEnvVar(ctx context.Context, cfg *envconf.Config) error {
123-
deployment := &appsv1.Deployment{}
124-
if err := cfg.Client().Resources().Get(ctx, "kubewarden-controller", namespace, deployment); err != nil {
125-
return fmt.Errorf("failed to get kubewarden-controller deployment: %w", err)
126-
}
127-
128-
for i, container := range deployment.Spec.Template.Spec.Containers {
129-
if container.Name != "manager" {
130-
continue
131-
}
132-
deployment.Spec.Template.Spec.Containers[i].Env = append(
133-
deployment.Spec.Template.Spec.Containers[i].Env,
134-
corev1.EnvVar{Name: "KUBEWARDEN_DISABLE_CLIENT_CACHE", Value: "true"},
135-
)
136-
}
137-
138-
if err := cfg.Client().Resources().Update(ctx, deployment); err != nil {
139-
return fmt.Errorf("failed to update kubewarden-controller deployment: %w", err)
140-
}
141-
return nil
142-
}

0 commit comments

Comments
 (0)