Skip to content

Commit 44fd95e

Browse files
committed
Add NetworkPolicy for operator pod
Replace the kubebuilder-scaffolded metrics NetworkPolicy with a policy for the ceph-csi-operator pod. The policy denies all ingress (no exposed ports needed -- metrics are disabled, health probes bypass NP) and allows open egress (required for API server access where the ClusterIP varies per cluster and CNI ipBlock behavior is inconsistent). The policy is disabled by default. Set NETWORK_POLICY=true to include it in the generated installer YAMLs: NETWORK_POLICY=true make build-installer Assisted-by: Claude <noreply@anthropic.com> Signed-off-by: Rakshith R <rar@redhat.com>
1 parent 818aaba commit 44fd95e

5 files changed

Lines changed: 34 additions & 32 deletions

File tree

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ WATCH_NAMESPACE ?= ""
1212

1313
IMG ?= $(IMAGE_REGISTRY)/$(REGISTRY_NAMESPACE)/$(IMAGE_NAME):$(IMAGE_TAG)
1414

15+
# NETWORK_POLICY controls whether NetworkPolicy manifests are included
16+
# in the generated installer YAMLs. Set to true to enable.
17+
NETWORK_POLICY ?= false
18+
1519
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
1620
ENVTEST_K8S_VERSION = 1.29.0
1721

@@ -74,6 +78,15 @@ resources:
7478
endef
7579
export BUILD_CSI_RBAC_OVERLAY
7680

81+
# Helpers to toggle the network-policy kustomize resource.
82+
# Called from build targets when NETWORK_POLICY=true.
83+
define enable-network-policy
84+
$(if $(filter true,$(NETWORK_POLICY)),cd config/default && sed -i 's|#- ../network-policy|- ../network-policy|' kustomization.yaml)
85+
endef
86+
define disable-network-policy
87+
$(if $(filter true,$(NETWORK_POLICY)),cd config/default && sed -i 's|^- ../network-policy|#- ../network-policy|' kustomization.yaml)
88+
endef
89+
7790
.PHONY: all
7891
all: build
7992

@@ -192,19 +205,23 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
192205

193206
.PHONY: build-installer
194207
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
208+
$(call enable-network-policy)
195209
mkdir -p build deploy/all-in-one
196210
cd build && echo "$$BUILD_INSTALLER_OVERLAY" > kustomization.yaml
197211
cd build && $(KUSTOMIZE) edit add resource ../config/default/
198212
$(KUSTOMIZE) build build > deploy/all-in-one/install.yaml
199213
rm -rf build
214+
$(call disable-network-policy)
200215

201216
.PHONY: build-openshift-installer
202217
build-openshift-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs, deployment, and OpenShift SCC.
218+
$(call enable-network-policy)
203219
mkdir -p build deploy/all-in-one
204220
cd build && echo "$$BUILD_INSTALLER_OVERLAY" > kustomization.yaml
205221
cd build && $(KUSTOMIZE) edit add resource ../config/openshift/
206222
$(KUSTOMIZE) build build > deploy/all-in-one/install-openshift.yaml
207223
rm -rf build
224+
$(call disable-network-policy)
208225

209226
.PHONY: build-helm-installer
210227
build-helm-installer: manifests generate kustomize helmify ## Generate helm charts for the operator.

config/default/kustomization.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ resources:
1818
#- ../prometheus
1919
# [METRICS] Expose the controller manager metrics service.
2020
# - metrics_service.yaml
21-
# [NETWORK POLICY] Protect the /metrics endpoint and Webhook Server with NetworkPolicy.
22-
# Only Pod(s) running a namespace labeled with 'metrics: enabled' will be able to gather the metrics.
23-
# Only CR(s) which requires webhooks and are applied on namespaces labeled with 'webhooks: enabled' will
24-
# be able to communicate with the Webhook Server.
21+
# [NETWORK POLICY] Protect the operator pod with NetworkPolicy.
22+
# Denies all ingress and allows open egress for API server access.
2523
#- ../network-policy
2624

2725
#patches:

config/network-policy/allow-metrics-traffic.yaml

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
resources:
2-
- allow-metrics-traffic.yaml
2+
- operator-network-policy.yaml
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: NetworkPolicy
3+
metadata:
4+
name: ceph-csi-controller-manager
5+
spec:
6+
podSelector:
7+
matchLabels:
8+
control-plane: ceph-csi-op-controller-manager
9+
# Ingress is not specified — deny all inbound traffic.
10+
egress:
11+
- {}
12+
policyTypes:
13+
- Ingress
14+
- Egress

0 commit comments

Comments
 (0)