Skip to content

Commit c67a1fb

Browse files
committed
manifest-generator: add network-policies generation support
A new environment variable CDI_DEPLOY_NP has been added with the options of true and false (defaults to false) to control the deployment of CDI's network policies. The network policies deployed when CDI_DEPLOY_NP is set to true are the ones generated by the createNetworkPolicies factory function as well as static ones for denying all traffic in the namespace and allow traffic to kube-apiserver and DNS for pods labeled with `np.kubevirt.io/allow-access-cluster-services`. Signed-off-by: Adi Aloni <[email protected]>
1 parent 60d6e5d commit c67a1fb

File tree

7 files changed

+103
-26
lines changed

7 files changed

+103
-26
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ help: ## Print this message and exit
6464
@echo " WHAT Path to the package to test. Default is './pkg/... ./cmd/...' for unit tests and './test/...' for functional tests."
6565
@echo " RELREF Required by release-description. Must be a commit or tag. Should be newer than $$PREREF."
6666
@echo " PREREF Required by release-description. Must also be a commit or tag. Should be older than $$RELREF."
67+
@echo " CDI_DEPLOY_NP Deploy CDI's utility network policies. Default is 'false'."
6768

6869
all: manifests bazel-build-images ## Clean up previous build artifacts, compile all CDI packages and build containers
6970

@@ -170,10 +171,10 @@ cluster-clean-test-infra:
170171
CDI_CLEAN="test-infra" ./cluster-sync/clean.sh
171172

172173
cluster-sync-cdi: cluster-clean-cdi
173-
./cluster-sync/sync.sh CDI_AVAILABLE_TIMEOUT=${CDI_AVAILABLE_TIMEOUT} DOCKER_PREFIX=${DOCKER_PREFIX} DOCKER_TAG=${DOCKER_TAG} PULL_POLICY=${PULL_POLICY} CDI_NAMESPACE=${CDI_NAMESPACE}
174+
./cluster-sync/sync.sh CDI_AVAILABLE_TIMEOUT=${CDI_AVAILABLE_TIMEOUT} DOCKER_PREFIX=${DOCKER_PREFIX} DOCKER_TAG=${DOCKER_TAG} PULL_POLICY=${PULL_POLICY} CDI_NAMESPACE=${CDI_NAMESPACE} CDI_DEPLOY_NP=${CDI_DEPLOY_NP}
174175

175176
cluster-sync-test-infra: cluster-clean-test-infra
176-
CDI_SYNC="test-infra" ./cluster-sync/sync.sh CDI_AVAILABLE_TIMEOUT=${CDI_AVAILABLE_TIMEOUT} DOCKER_PREFIX=${DOCKER_PREFIX} DOCKER_TAG=${DOCKER_TAG} PULL_POLICY=${PULL_POLICY} CDI_NAMESPACE=${CDI_NAMESPACE}
177+
CDI_SYNC="test-infra" ./cluster-sync/sync.sh CDI_AVAILABLE_TIMEOUT=${CDI_AVAILABLE_TIMEOUT} DOCKER_PREFIX=${DOCKER_PREFIX} DOCKER_TAG=${DOCKER_TAG} PULL_POLICY=${PULL_POLICY} CDI_NAMESPACE=${CDI_NAMESPACE} CDI_DEPLOY_NP=${CDI_DEPLOY_NP}
177178

178179
cluster-sync: cluster-sync-cdi cluster-sync-test-infra ## Build the controller/importer/cloner, and push it into a running cluster. The cluster must be up before running a cluster sync. Also generates a manifest and applies it to the running cluster after pushing the images to it.
179180

cluster-sync/clean.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ for n in ${NAMESPACES[@]}; do
8080
_kubectl -n ${n} delete rolebinding -l ${label}
8181
_kubectl -n ${n} delete roles -l ${label}
8282
_kubectl -n ${n} delete serviceaccounts -l ${label}
83+
_kubectl -n ${n} delete networkpolicies -l ${label}
8384
done
8485
done
8586

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: NetworkPolicy
3+
metadata:
4+
name: allow-all-cdi-test
5+
labels:
6+
cdi.kubevirt.io: ""
7+
spec:
8+
podSelector:
9+
matchLabels:
10+
cdi.kubevirt.io/testing: ""
11+
policyTypes:
12+
- Ingress
13+
- Egress
14+
ingress:
15+
- {}
16+
egress:
17+
- {}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: NetworkPolicy
3+
metadata:
4+
name: default-deny
5+
labels:
6+
cdi.kubevirt.io: ""
7+
spec:
8+
podSelector: { }
9+
policyTypes:
10+
- Ingress
11+
- Egress
12+
---
13+
apiVersion: networking.k8s.io/v1
14+
kind: NetworkPolicy
15+
metadata:
16+
name: cdi-allow-egress-to-cluster-services
17+
labels:
18+
cdi.kubevirt.io: ""
19+
spec:
20+
podSelector:
21+
matchExpressions:
22+
- key: np.kubevirt.io/allow-access-cluster-services
23+
operator: In
24+
values:
25+
- "true"
26+
policyTypes:
27+
- Egress
28+
egress:
29+
- ports:
30+
- protocol: TCP
31+
port: 6443
32+
- to:
33+
- namespaceSelector:
34+
matchLabels:
35+
kubernetes.io/metadata.name: kube-system
36+
podSelector:
37+
matchLabels:
38+
k8s-app: kube-dns
39+
ports:
40+
- protocol: TCP
41+
port: dns-tcp
42+
- protocol: UDP
43+
port: dns

cluster-sync/sync.sh

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ source ./cluster-up/hack/common.sh
1111
source ./cluster-up/cluster/${KUBEVIRT_PROVIDER}/provider.sh
1212

1313
if [ "${KUBEVIRT_PROVIDER}" = "external" ]; then
14-
CDI_SYNC_PROVIDER="external"
14+
CDI_SYNC_PROVIDER="external"
1515
else
16-
CDI_SYNC_PROVIDER="kubevirtci"
16+
CDI_SYNC_PROVIDER="kubevirtci"
1717
fi
1818

1919
source ./cluster-sync/${CDI_SYNC_PROVIDER}/provider.sh
@@ -68,8 +68,8 @@ function kill_running_operator {
6868
out=$(_kubectl get pods -n $CDI_NAMESPACE)
6969
out=($out)
7070
length=${#out[@]}
71-
for ((idx=0; idx<${#out[@]}; idx=idx+5)); do
72-
if [[ ${out[idx]} == cdi-operator-* ]] && [[ ${out[idx+2]} == "Running" ]]; then
71+
for ((idx = 0; idx < ${#out[@]}; idx = idx + 5)); do
72+
if [[ ${out[idx]} == cdi-operator-* ]] && [[ ${out[idx + 2]} == "Running" ]]; then
7373
_kubectl delete pod ${out[idx]} -n $CDI_NAMESPACE --grace-period=0 --force
7474
return
7575
fi
@@ -132,25 +132,25 @@ NEW_CDI_VER_PODS="./_out/tests/new_cdi_ver_pods"
132132
# Note it will fail update to the same version
133133
function wait_cdi_pods_updated {
134134
echo "Waiting $CDI_PODS_UPDATE_TIMEOUT seconds for all CDI pods to update"
135-
if [ -f $NEW_CDI_VER_PODS ] ; then
135+
if [ -f $NEW_CDI_VER_PODS ]; then
136136
mv $NEW_CDI_VER_PODS $OLD_CDI_VER_PODS
137137
fi
138138
wait_time=0
139139
ret=0
140140
while [[ $ret -eq 0 ]] && [[ $wait_time -lt ${CDI_PODS_UPDATE_TIMEOUT} ]]; do
141141
wait_time=$((wait_time + 5))
142-
_kubectl get pods -n $CDI_NAMESPACE -l '!cdi.kubevirt.io/testing' -o=jsonpath='{range .items[*]}{.metadata.name}{"\n"}{.metadata.uid}{"\n"}{.spec.containers[*].image}{"\n"}{end}' > $NEW_CDI_VER_PODS
143-
if [ -f $OLD_CDI_VER_PODS ] ; then
142+
_kubectl get pods -n $CDI_NAMESPACE -l '!cdi.kubevirt.io/testing' -o=jsonpath='{range .items[*]}{.metadata.name}{"\n"}{.metadata.uid}{"\n"}{.spec.containers[*].image}{"\n"}{end}' >$NEW_CDI_VER_PODS
143+
if [ -f $OLD_CDI_VER_PODS ]; then
144144
grep -qFxf $OLD_CDI_VER_PODS $NEW_CDI_VER_PODS || ret=$?
145-
if [ $ret -eq 0 ] ; then
145+
if [ $ret -eq 0 ]; then
146146
sleep 5
147147
fi
148148
else
149149
ret=1
150150
fi
151151
done
152152
echo "Waited $wait_time seconds"
153-
if [ $ret -eq 0 ] ; then
153+
if [ $ret -eq 0 ]; then
154154
echo "Not all pods updated"
155155
exit 1
156156
fi
@@ -168,7 +168,7 @@ function dump_upgrade_info {
168168
# Setup some datavolumes in older version for testing upgrades
169169
# Done unconditionally to make it easier to write tests.
170170
function setup_for_upgrade_testing {
171-
if _kubectl get namespace cdi-testing-old-version-artifacts ; then
171+
if _kubectl get namespace cdi-testing-old-version-artifacts; then
172172
echo "Old version testing environment already setup"
173173
return
174174
fi
@@ -199,6 +199,9 @@ function setup_for_upgrade_testing {
199199
# Start functional test HTTP server.
200200
# We skip the functional test additions for external provider for now, as they're specific
201201
if [ "${CDI_SYNC}" == "test-infra" ]; then
202+
if [ "${CDI_DEPLOY_NP}" == true ]; then
203+
_kubectl apply -n ${CDI_NAMESPACE} -f "./cluster-sync/networkpolicies/cdi-testing-np.yaml"
204+
fi
202205
configure_storage
203206
_kubectl apply -f "./_out/manifests/cdi-testing-sa.yaml"
204207
_kubectl apply -f "./_out/manifests/bad-webserver.yaml"
@@ -227,10 +230,11 @@ if [ "${CDI_SYNC}" == "test-infra" ]; then
227230
# vCenter (VDDK) test service:
228231
_kubectl apply -f "./_out/manifests/vcenter.yaml"
229232
fi
230-
233+
231234
if _kubectl get crd securitycontextconstraints.security.openshift.io >/dev/null 2>&1; then
232235
_kubectl apply -f "./_out/manifests/cdi-testing-scc.yaml"
233236
fi
237+
234238
exit 0
235239
fi
236240

@@ -249,7 +253,7 @@ wait_cdi_crd_installed $CDI_INSTALL_TIMEOUT
249253

250254
# If we are upgrading, verify our current value.
251255
if [[ ! -z "$UPGRADE_FROM" ]]; then
252-
UPGRADE_FROM_LIST=( $UPGRADE_FROM )
256+
UPGRADE_FROM_LIST=($UPGRADE_FROM)
253257
for VERSION in ${UPGRADE_FROM_LIST[@]}; do
254258
echo $VERSION
255259
if [ "$VERSION" != "${UPGRADE_FROM_LIST[0]}" ]; then
@@ -265,10 +269,10 @@ if [[ ! -z "$UPGRADE_FROM" ]]; then
265269
retry_counter=0
266270
kill_count=0
267271
while [[ $retry_counter -lt $CDI_UPGRADE_RETRY_COUNT ]] && [ "$operator_version" != "$VERSION" ]; do
268-
cdi_cr_phase=`_kubectl get CDI -o=jsonpath='{.items[*].status.phase}{"\n"}'`
269-
observed_version=`_kubectl get CDI -o=jsonpath='{.items[*].status.observedVersion}{"\n"}'`
270-
target_version=`_kubectl get CDI -o=jsonpath='{.items[*].status.targetVersion}{"\n"}'`
271-
operator_version=`_kubectl get CDI -o=jsonpath='{.items[*].status.operatorVersion}{"\n"}'`
272+
cdi_cr_phase=$(_kubectl get CDI -o=jsonpath='{.items[*].status.phase}{"\n"}')
273+
observed_version=$(_kubectl get CDI -o=jsonpath='{.items[*].status.observedVersion}{"\n"}')
274+
target_version=$(_kubectl get CDI -o=jsonpath='{.items[*].status.targetVersion}{"\n"}')
275+
operator_version=$(_kubectl get CDI -o=jsonpath='{.items[*].status.operatorVersion}{"\n"}')
272276
echo "Phase: $cdi_cr_phase, observedVersion: $observed_version, operatorVersion: $operator_version, targetVersion: $target_version"
273277
retry_counter=$((retry_counter + 1))
274278
if [[ $kill_count -lt 1 ]]; then
@@ -292,20 +296,20 @@ if [[ ! -z "$UPGRADE_FROM" ]]; then
292296
retry_counter=0
293297
_kubectl apply -f "./_out/manifests/release/cdi-operator.yaml"
294298
while [[ $retry_counter -lt $CDI_UPGRADE_RETRY_COUNT ]] && [ "$observed_version" != "latest" ]; do
295-
cdi_cr_phase=`_kubectl get CDI -o=jsonpath='{.items[*].status.phase}{"\n"}'`
296-
observed_version=`_kubectl get CDI -o=jsonpath='{.items[*].status.observedVersion}{"\n"}'`
297-
target_version=`_kubectl get CDI -o=jsonpath='{.items[*].status.targetVersion}{"\n"}'`
298-
operator_version=`_kubectl get CDI -o=jsonpath='{.items[*].status.operatorVersion}{"\n"}'`
299+
cdi_cr_phase=$(_kubectl get CDI -o=jsonpath='{.items[*].status.phase}{"\n"}')
300+
observed_version=$(_kubectl get CDI -o=jsonpath='{.items[*].status.observedVersion}{"\n"}')
301+
target_version=$(_kubectl get CDI -o=jsonpath='{.items[*].status.targetVersion}{"\n"}')
302+
operator_version=$(_kubectl get CDI -o=jsonpath='{.items[*].status.operatorVersion}{"\n"}')
299303
echo "Phase: $cdi_cr_phase, observedVersion: $observed_version, operatorVersion: $operator_version, targetVersion: $target_version"
300304
retry_counter=$((retry_counter + 1))
301305
_kubectl get pods -n $CDI_NAMESPACE
302306
sleep 5
303307
done
304308
if [ $retry_counter -eq $CDI_UPGRADE_RETRY_COUNT ]; then
305-
echo "Unable to deploy to latest version"
306-
cdi_obj=$(_kubectl get CDI -o yaml)
307-
echo $cdi_obj
308-
exit 1
309+
echo "Unable to deploy to latest version"
310+
cdi_obj=$(_kubectl get CDI -o yaml)
311+
echo $cdi_obj
312+
exit 1
309313
fi
310314
wait_cdi_available
311315
wait_cdi_pods_updated
@@ -323,6 +327,13 @@ if [ "${KUBEVIRT_PROVIDER}" != "external" ]; then
323327
configure_prometheus
324328
fi
325329

330+
# Network policies
331+
if [ "${CDI_DEPLOY_NP}" == true ]; then
332+
_kubectl apply -n ${CDI_NAMESPACE} -f "./cluster-sync/networkpolicies/cluster-services-np.yaml"
333+
_kubectl apply -n ${CDI_NAMESPACE} -f "./_out/manifests/release/network-policies.yaml"
334+
fi
335+
336+
326337
# Grab all the CDI crds so we can check if they are structural schemas
327338
cdi_crds=$(_kubectl get crd -l cdi.kubevirt.io -o jsonpath={.items[*].metadata.name})
328339
crds=($cdi_crds)

hack/build/build-manifests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ mkdir -p "${MANIFEST_GENERATED_DIR}/"
3535
#generate operator related manifests used to deploy cdi with operator-framework
3636
generateResourceManifest $generator $MANIFEST_GENERATED_DIR "operator" "everything" "operator-everything.yaml.in"
3737

38+
#generate networkpolicy manifests
39+
generateResourceManifest $generator $MANIFEST_GENERATED_DIR "namespaced" "networkpolicies" "network-policies.yaml.in"
40+
3841
#process templated manifests and populate them with generated manifests
3942
tempDir=${MANIFEST_TEMPLATE_DIR}
4043
processDirTemplates ${tempDir} ${OUT_DIR}/manifests ${OUT_DIR}/manifests/templates ${generator} ${MANIFEST_GENERATED_DIR}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{index .GeneratedManifests "network-policies.yaml.in"}}

0 commit comments

Comments
 (0)