Skip to content

Commit 2368780

Browse files
committed
fix(tests/charts): unblock helm test hooks under chart NetworkPolicy
kind v0.31+/kindest/node v1.35+ enforces NetworkPolicy via kindnetd. The chart NetworkPolicy's podSelector matched the helm test hook pod (which inherited the app selectorLabels), denying its egress and causing wget to time out reaching the app Service. - Give the hook pod distinct labels (app.kubernetes.io/name: <chart>-test-connection) so the chart NetworkPolicy no longer selects it. - Emit a dedicated NetworkPolicy for the hook pod allowing DNS + egress to the app pod on the service port. This also satisfies Checkov CKV2_K8S_6 (comments are stripped by Helm and cannot be used to skip).
1 parent 9c3f1f3 commit 2368780

2 files changed

Lines changed: 82 additions & 2 deletions

File tree

tests/charts/application/templates/tests/test-connection.yaml

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ spec:
1414
template:
1515
metadata:
1616
labels:
17-
{{- include "test-application.labels" . | nindent 8 }}
17+
# Distinct labels so the chart's NetworkPolicy podSelector does not
18+
# match this hook pod (a dedicated NetworkPolicy is defined below).
19+
app.kubernetes.io/name: {{ include "test-application.name" . }}-test-connection
20+
app.kubernetes.io/instance: {{ .Release.Name }}
21+
app.kubernetes.io/component: test
22+
app.kubernetes.io/managed-by: {{ .Release.Service }}
23+
helm.sh/chart: {{ include "test-application.chart" . }}
1824
spec:
1925
automountServiceAccountToken: false
2026
securityContext:
@@ -54,4 +60,38 @@ spec:
5460
- NET_RAW
5561
- ALL
5662
restartPolicy: Never
63+
{{- if .Values.networkPolicy.enabled }}
64+
---
65+
apiVersion: networking.k8s.io/v1
66+
kind: NetworkPolicy
67+
metadata:
68+
name: "{{ include "test-application.fullname" . }}-test-connection"
69+
namespace: {{ .Values.namespace | default "app-system" }}
70+
labels:
71+
{{- include "test-application.labels" . | nindent 4 }}
72+
annotations:
73+
"helm.sh/hook": test
74+
spec:
75+
podSelector:
76+
matchLabels:
77+
app.kubernetes.io/name: {{ include "test-application.name" . }}-test-connection
78+
app.kubernetes.io/instance: {{ .Release.Name }}
79+
app.kubernetes.io/component: test
80+
policyTypes:
81+
- Egress
82+
egress:
83+
- to: []
84+
ports:
85+
- protocol: UDP
86+
port: 53
87+
- protocol: TCP
88+
port: 53
89+
- to:
90+
- podSelector:
91+
matchLabels:
92+
{{- include "test-application.selectorLabels" . | nindent 14 }}
93+
ports:
94+
- protocol: TCP
95+
port: {{ .Values.service.port }}
96+
{{- end }}
5797
# jscpd:ignore-end

tests/charts/umbrella-application/charts/app/templates/tests/test-connection.yaml

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ spec:
1313
template:
1414
metadata:
1515
labels:
16-
{{- include "app.labels" . | nindent 8 }}
16+
# Distinct labels so the chart's NetworkPolicy podSelector does not
17+
# match this hook pod (a dedicated NetworkPolicy is defined below).
18+
app.kubernetes.io/name: {{ include "app.name" . }}-test-connection
19+
app.kubernetes.io/instance: {{ .Release.Name }}
20+
app.kubernetes.io/component: test
21+
app.kubernetes.io/managed-by: {{ .Release.Service }}
22+
helm.sh/chart: {{ include "app.chart" . }}
1723
spec:
1824
automountServiceAccountToken: false
1925
securityContext:
@@ -53,3 +59,37 @@ spec:
5359
- NET_RAW
5460
- ALL
5561
restartPolicy: Never
62+
{{- if .Values.networkPolicy.enabled }}
63+
---
64+
apiVersion: networking.k8s.io/v1
65+
kind: NetworkPolicy
66+
metadata:
67+
name: "{{ include "app.fullname" . }}-test-connection"
68+
namespace: {{ .Values.namespace | default "app-system" }}
69+
labels:
70+
{{- include "app.labels" . | nindent 4 }}
71+
annotations:
72+
"helm.sh/hook": test
73+
spec:
74+
podSelector:
75+
matchLabels:
76+
app.kubernetes.io/name: {{ include "app.name" . }}-test-connection
77+
app.kubernetes.io/instance: {{ .Release.Name }}
78+
app.kubernetes.io/component: test
79+
policyTypes:
80+
- Egress
81+
egress:
82+
- to: []
83+
ports:
84+
- protocol: UDP
85+
port: 53
86+
- protocol: TCP
87+
port: 53
88+
- to:
89+
- podSelector:
90+
matchLabels:
91+
{{- include "app.selectorLabels" . | nindent 14 }}
92+
ports:
93+
- protocol: TCP
94+
port: {{ .Values.service.port }}
95+
{{- end }}

0 commit comments

Comments
 (0)