Skip to content

Commit fceebdb

Browse files
Add networkpolices to protect metrics endpoint and allow communication with webhooks
1 parent 4c4103e commit fceebdb

File tree

38 files changed

+747
-20
lines changed

38 files changed

+747
-20
lines changed

.github/workflows/test-sample-go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
run: |
2525
KUSTOMIZATION_FILE_PATH="testdata/project-v4/config/default/kustomization.yaml"
2626
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
27-
sed -i '46s/^#//' $KUSTOMIZATION_FILE_PATH
27+
sed -i '51s/^#//' $KUSTOMIZATION_FILE_PATH
2828
2929
- name: Test
3030
run: |

docs/book/src/cronjob-tutorial/testdata/project/config/default/kustomization.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ resources:
2727
- ../prometheus
2828
# [METRICS] Expose the controller manager metrics service.
2929
- metrics_service.yaml
30+
# [NETWORK POLICY] Protect the /metrics endpoint and Webhook Server with NetworkPolicy.
31+
# Only Pod(s) running a namespace labeled with 'metrics: enabled' will be able to gather the metrics.
32+
# Only CR(s) which requires webhooks and are applied on namespaces labeled with 'webhooks: enabled' will
33+
# be able to communicate with the Webhook Server.
34+
#- ../network-policy
3035

3136
# Uncomment the patches line if you enable Metrics, and/or are using webhooks and cert-manager
3237
patches:
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This NetworkPolicy allows ingress traffic
2+
# with Pods running on namespaces labeled with 'metrics: enabled'. Only Pods on those
3+
# namespaces are able to gathering data from the metrics endpoint.
4+
apiVersion: networking.k8s.io/v1
5+
kind: NetworkPolicy
6+
metadata:
7+
labels:
8+
app.kubernetes.io/name: project
9+
app.kubernetes.io/managed-by: kustomize
10+
name: allow-metrics-traffic
11+
namespace: system
12+
spec:
13+
podSelector:
14+
matchLabels:
15+
control-plane: controller-manager
16+
policyTypes:
17+
- Ingress
18+
ingress:
19+
# This allows ingress traffic from any namespace with the label metrics: enabled
20+
- from:
21+
- namespaceSelector:
22+
matchLabels:
23+
metrics: enabled # Only from namespaces with this label
24+
ports:
25+
- port: 8443
26+
protocol: TCP
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This NetworkPolicy allows ingress traffic to your webhook server running
2+
# as part of the controller-manager from specific namespaces and pods. CR(s) which uses webhooks
3+
# will only work when applied in namespaces labeled with 'webhook: enabled'
4+
apiVersion: networking.k8s.io/v1
5+
kind: NetworkPolicy
6+
metadata:
7+
labels:
8+
app.kubernetes.io/name: project
9+
app.kubernetes.io/managed-by: kustomize
10+
name: allow-webhook-traffic
11+
namespace: system
12+
spec:
13+
podSelector:
14+
matchLabels:
15+
control-plane: controller-manager
16+
policyTypes:
17+
- Ingress
18+
ingress:
19+
# This allows ingress traffic from any namespace with the label webhook: enabled
20+
- from:
21+
- namespaceSelector:
22+
matchLabels:
23+
webhook: enabled # Only from namespaces with this label
24+
ports:
25+
- port: 443
26+
protocol: TCP
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
resources:
2+
- allow-webhook-traffic.yaml
3+
- allow-metrics-traffic.yaml

docs/book/src/getting-started/testdata/project/config/default/kustomization.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ resources:
2727
#- ../prometheus
2828
# [METRICS] Expose the controller manager metrics service.
2929
- metrics_service.yaml
30+
# [NETWORK POLICY] Protect the /metrics endpoint and Webhook Server with NetworkPolicy.
31+
# Only Pod(s) running a namespace labeled with 'metrics: enabled' will be able to gather the metrics.
32+
# Only CR(s) which requires webhooks and are applied on namespaces labeled with 'webhooks: enabled' will
33+
# be able to communicate with the Webhook Server.
34+
#- ../network-policy
3035

3136
# Uncomment the patches line if you enable Metrics, and/or are using webhooks and cert-manager
3237
patches:
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This NetworkPolicy allows ingress traffic
2+
# with Pods running on namespaces labeled with 'metrics: enabled'. Only Pods on those
3+
# namespaces are able to gathering data from the metrics endpoint.
4+
apiVersion: networking.k8s.io/v1
5+
kind: NetworkPolicy
6+
metadata:
7+
labels:
8+
app.kubernetes.io/name: project
9+
app.kubernetes.io/managed-by: kustomize
10+
name: allow-metrics-traffic
11+
namespace: system
12+
spec:
13+
podSelector:
14+
matchLabels:
15+
control-plane: controller-manager
16+
policyTypes:
17+
- Ingress
18+
ingress:
19+
# This allows ingress traffic from any namespace with the label metrics: enabled
20+
- from:
21+
- namespaceSelector:
22+
matchLabels:
23+
metrics: enabled # Only from namespaces with this label
24+
ports:
25+
- port: 8443
26+
protocol: TCP
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
resources:
2+
- allow-metrics-traffic.yaml

docs/book/src/reference/metrics.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,19 @@ enhance the controller-runtime and address these considerations.
188188
</aside>
189189

190190

191-
### By using Network Policy
191+
### By using Network Policy (You can optionally enable)
192192

193193
NetworkPolicy acts as a basic firewall for pods within a Kubernetes cluster, controlling traffic
194-
flow at the IP address or port level. However, it doesn't handle authentication (authn), authorization (authz),
195-
or encryption directly like [kube-rbac-proxy](https://github.com/brancz/kube-rbac-proxy) solution.
194+
flow at the IP address or port level. However, it doesn't handle `authn/authz`.
195+
196+
Uncomment the following line in the `config/default/kustomization.yaml`:
197+
198+
```
199+
# [NETWORK POLICY] Protect the /metrics endpoint and Webhook Server with NetworkPolicy.
200+
# Only Pod(s) running a namespace labeled with 'metrics: enabled' will be able to gather the metrics.
201+
# Only CR(s) which uses webhooks and applied on namespaces labeled 'webhooks: enabled' will be able to work properly.
202+
#- ../network-policy
203+
```
196204

197205
### By exposing the metrics endpoint using HTTPS and CertManager
198206

pkg/plugins/common/kustomize/v2/scaffolds/init.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"sigs.k8s.io/kubebuilder/v4/pkg/plugins"
2525
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/kdefault"
2626
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/manager"
27+
network_policy "sigs.k8s.io/kubebuilder/v4/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/network-policy"
2728
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/prometheus"
2829
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/rbac"
2930
)
@@ -79,6 +80,8 @@ func (s *initScaffolder) Scaffold() error {
7980
&kdefault.ManagerMetricsPatch{},
8081
&manager.Config{Image: imageName},
8182
&kdefault.Kustomization{},
83+
&network_policy.Kustomization{},
84+
&network_policy.NetworkPolicyAllowMetrics{},
8285
&prometheus.Kustomization{},
8386
&prometheus.Monitor{},
8487
}

0 commit comments

Comments
 (0)