Skip to content

Commit 8587b23

Browse files
committed
feat(common/networkpolicy): add templates for selectors
1 parent c04e9b9 commit 8587b23

4 files changed

Lines changed: 84 additions & 40 deletions

File tree

charts/common/templates/_ciliumnetworkpolicy.tpl

Lines changed: 0 additions & 27 deletions
This file was deleted.

charts/common/templates/_cni.tpl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{{- define "common.networkPolicy.type" -}}
2+
{{- if eq .Values.global.networkPolicy.type "auto" -}}
3+
{{- if .Capabilities.APIVersions.Has "cilium.io/v2/CiliumNetworkPolicy" -}}
4+
cilium
5+
{{- else -}}
6+
native
7+
{{- end -}}
8+
{{- else -}}
9+
{{- .Values.global.networkPolicy.type -}}
10+
{{- end -}}
11+
{{- end -}}
Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
{{- define "common.networkpolicy.identities.kube-apiserver" -}}
1+
{{- define "common.networkPolicy.identity.kube-apiserver" -}}
22
{{- $identities := list -}}
3-
{{- $inClusterControlPlaneEntity := (dict "fromEntities" (list "kube-apiserver")) -}}
4-
{{- $konnectivityDaemonSetNamespace := "kube-system" -}}
5-
{{- $konnectivityDameonSetName := "konnectivity-agent" -}}
6-
{{- $konnectivityProxiedControlPlaneMatchLabels := dict
7-
"io.kubernetes.pod.namespace" $konnectivityDaemonSetNamespace
8-
"k8s-app" $konnectivityDameonSetName
9-
-}}
10-
{{- $konnectivityProxiedControlPlaneEndpoint := (dict "fromEndpoints" (list (dict "matchLabels" $konnectivityProxiedControlPlaneMatchLabels))) -}}
11-
{{- $matchLabels := dict -}}
12-
{{- if lookup "apps/v1" "DaemonSet" $konnectivityDaemonSetNamespace $konnectivityDameonSetName -}}
13-
{{- $identities = append $identities $konnectivityProxiedControlPlaneEndpoint -}}
3+
{{- $kubeSystemNamespace := "kube-system" -}}
4+
{{- $konnectivityName := "konnectivity-agent" -}}
5+
{{- if lookup "apps/v1" "DaemonSet" $kubeSystemNamespace $konnectivityName -}}
6+
{{- $identities = append $identities (dict "endpoint" (dict
7+
"namespace" $kubeSystemNamespace
8+
"pod" (dict
9+
"k8s-app" $konnectivityName
10+
)
11+
"serviceAccount" $konnectivityName
12+
))
13+
-}}
1414
{{- else -}}
15-
{{- $identities = append $identities $inClusterControlPlaneEntity -}}
15+
{{- $identities = append $identities (dict "endpoint" (dict
16+
"namespace" $kubeSystemNamespace
17+
"pod" (dict
18+
"tier" "control-plane"
19+
"component" "kube-apiserver"
20+
)
21+
))
22+
-}}
23+
{{- $identities = append $identities (dict "entity" "kube-apiserver") -}}
1624
{{- end -}}
1725
{{- toYaml $identities -}}
1826
{{- end -}}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{{- define "common.networkPolicy.rule.from.kube-apiserver" -}}
2+
{{- $useCilium := eq .cilium true -}}
3+
{{- $identities := include "common.networkPolicy.identity.kube-apiserver" (dict) | fromYamlArray -}}
4+
{{- $rules := list -}}
5+
{{- $ports := list -}}
6+
{{/* Process ports from input, handling both a single as well as a list of protocols */}}
7+
{{- if hasKey . "ports" -}}
8+
{{- range $port, $protocols := (.ports | default (list)) -}}
9+
{{- $_protocols := $protocols -}}
10+
{{- if kindIs "string" $protocols -}}
11+
{{- $_protocols = list $protocols -}}
12+
{{- end -}}
13+
{{- range $protocol := $_protocols -}}
14+
{{- $ports = append $ports (dict "port" $port "protocol" $protocol) -}}
15+
{{- end }}
16+
{{- end -}}
17+
{{- end -}}
18+
{{/* Create rules for each identity based on network policy type */}}
19+
{{- range $identity := $identities -}}
20+
{{- $rule := dict -}}
21+
{{- $endpoint := $identity.endpoint -}}
22+
{{/* For cilium use entity or endpoint based rules */}}
23+
{{- if $useCilium -}}
24+
{{- if hasKey $identity "entity" -}}
25+
{{- $rule = dict "fromEntities" (list $identity.entity) -}}
26+
{{- else -}}
27+
{{- $matchLabels := dict "io.kubernetes.pod.namespace" $endpoint.namespace -}}
28+
{{- if hasKey $endpoint "serviceAccount" -}}
29+
{{- $matchLabels = set $matchLabels "io.cilium.k8s.policy.serviceaccount" $endpoint.serviceAccount -}}
30+
{{- else -}}
31+
{{- $matchLabels = mustMerge $matchLabels $endpoint.pod -}}
32+
{{- end -}}
33+
{{- $rule = dict "fromEndpoints" (list (dict "matchLabels" $matchLabels)) -}}
34+
{{- end -}}
35+
{{- if $ports }}
36+
{{- $rule = set $rule "toPorts" (list (dict "ports" $ports)) -}}
37+
{{- end -}}
38+
{{/* For native k8s use namespace- and podSelector */}}
39+
{{- else -}}
40+
{{- $from := dict "namespaceSelector" (dict "matchLabels" (dict "kubernetes.io/metadata.name" $endpoint.namespace)) -}}
41+
{{- $from = set $from "podSelector" (dict "matchLabels" $endpoint.pod) -}}
42+
{{- $rule = set $rule "from" (list $from) -}}
43+
{{- if $ports -}}
44+
{{- $rule = set $rule "ports" $ports -}}
45+
{{- end -}}
46+
{{- end -}}
47+
48+
{{- $rules = append $rules $rule -}}
49+
{{- end -}}
50+
51+
{{- toYaml $rules -}}
52+
{{- end -}}

0 commit comments

Comments
 (0)