Skip to content

Commit a7dc068

Browse files
authored
automation:Simulate network restriction for network-policy test coverage (#2348)
Verify CNAO pods can operate under network restrictions affecting the project namespace by simulating network restriction using network-policies. The affected CI jobs are the following check patch jobs: - workflow - monitoring - ipam-ext - kubemacpool - kube-secondary-dns Signed-off-by: Or Mergi <[email protected]>
1 parent e19a891 commit a7dc068

6 files changed

+112
-0
lines changed

automation/check-patch.e2e-kube-secondary-dns-functests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ main() {
2929

3030
trap teardown EXIT
3131

32+
echo "Simulate network restrictions on CNAO namespace"
33+
./hack/install-network-policy.sh
34+
3235
./hack/deploy-kubevirt.sh
3336
cd ${TMP_COMPONENT_PATH}
3437
make create-nodeport

automation/check-patch.e2e-kubemacpool-functests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ main() {
4444

4545
trap teardown EXIT
4646

47+
echo "Simulate network restrictions on CNAO namespace"
48+
./hack/install-network-policy.sh
49+
4750
echo "Deploy KubeVirt latest stable release"
4851
./hack/deploy-kubevirt.sh
4952

automation/check-patch.e2e-kubevirt-ipam-controller-functests.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ main() {
6868
./cluster/cert-manager-install.sh
6969
deploy_cnao
7070
deploy_cnao_cr
71+
echo "Simulate network restrictions on CNAO namespace"
72+
./hack/install-network-policy.sh
7173
./hack/deploy-kubevirt.sh
7274
./cluster/kubectl.sh -n kubevirt patch kubevirt kubevirt --type=json --patch '[{"op":"add","path":"/spec/configuration/developerConfiguration","value":{"featureGates":[]}},{"op":"add","path":"/spec/configuration/developerConfiguration/featureGates/-","value":"NetworkBindingPlugins"},{"op":"add","path":"/spec/configuration/developerConfiguration/featureGates/-","value":"DynamicPodInterfaceNaming"}]'
7375
./cluster/kubectl.sh -n kubevirt patch kubevirt kubevirt --type=json --patch '[{"op":"add","path":"/spec/configuration/network","value":{"binding":{"l2bridge":{"domainAttachmentType":"managedTap","migration":{}}}}}]'

automation/check-patch.e2e-monitoring-k8s.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ main() {
2828
make cluster-operator-push
2929
make cluster-operator-install
3030

31+
echo "Simulate network restriction on CNAO namespace"
32+
./hack/install-network-policy.sh
33+
3134
make E2E_TEST_EXTRA_ARGS="-ginkgo.noColor --ginkgo.junit-report=$ARTIFACTS/junit.functest.xml" test/e2e/monitoring
3235
}
3336

automation/check-patch.e2e-workflow-k8s.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ main() {
2424
./hack/deploy-kubevirt.sh
2525
make cluster-operator-push
2626
make cluster-operator-install
27+
28+
echo "Simulate network restriction on CNAO namespace"
29+
./hack/install-network-policy.sh
30+
2731
make E2E_TEST_EXTRA_ARGS="-ginkgo.noColor --ginkgo.junit-report=$ARTIFACTS/junit.functest.xml" test/e2e/workflow
2832
}
2933

hack/install-network-policy.sh

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/bin/bash -ex
2+
#
3+
# Copyright 2025 Red Hat, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
# This script install NetworkPolicy that affects CNAO namespace.
19+
# The network-policy blocks egress/ingress traffic in CNAO namespace with the following exceptions:
20+
# 1. Allow egress to cluster API and DNS for pods who labeled with
21+
# "hco.kubevirt.io/allow-access-cluster-services"
22+
# 2. Allow ingress to metrics endpoint to pods who labeled with
23+
# "hco.kubevirt.io/allow-prometheus-access"
24+
25+
readonly ns="$(./cluster/kubectl.sh get pod -l name=cluster-network-addons-operator -A -o=custom-columns=NS:.metadata.namespace --no-headers | head -1)"
26+
[[ -z "${ns}" ]] && echo "FATAL: CNAO pods not found. Make sure its installed" && exit 1
27+
28+
cat <<EOF | ./cluster/kubectl.sh -n "${ns}" apply -f -
29+
apiVersion: networking.k8s.io/v1
30+
kind: NetworkPolicy
31+
metadata:
32+
name: default-deny-all
33+
spec:
34+
podSelector: {}
35+
policyTypes:
36+
- Ingress
37+
- Egress
38+
ingress: []
39+
egress: []
40+
---
41+
apiVersion: networking.k8s.io/v1
42+
kind: NetworkPolicy
43+
metadata:
44+
name: allow-egress-to-cluster-dns
45+
spec:
46+
podSelector:
47+
matchExpressions:
48+
- key: hco.kubevirt.io/allow-access-cluster-services
49+
operator: Exists
50+
policyTypes:
51+
- Egress
52+
egress:
53+
- to:
54+
- namespaceSelector:
55+
matchLabels:
56+
kubernetes.io/metadata.name: kube-system
57+
podSelector:
58+
matchLabels:
59+
k8s-app: kube-dns
60+
ports:
61+
- protocol: TCP
62+
port: dns-tcp
63+
- protocol: UDP
64+
port: dns
65+
---
66+
apiVersion: networking.k8s.io/v1
67+
kind: NetworkPolicy
68+
metadata:
69+
name: allow-egress-to-cluster-api
70+
spec:
71+
podSelector:
72+
matchExpressions:
73+
- key: hco.kubevirt.io/allow-access-cluster-services
74+
operator: Exists
75+
policyTypes:
76+
- Egress
77+
egress:
78+
- ports:
79+
- protocol: TCP
80+
port: 6443
81+
---
82+
apiVersion: networking.k8s.io/v1
83+
kind: NetworkPolicy
84+
metadata:
85+
name: allow-ingress-to-metrics-endpoint
86+
spec:
87+
podSelector:
88+
matchExpressions:
89+
- key: hco.kubevirt.io/allow-prometheus-access
90+
operator: Exists
91+
policyTypes:
92+
- Ingress
93+
ingress:
94+
- ports:
95+
- protocol: TCP
96+
port: metrics
97+
EOF

0 commit comments

Comments
 (0)