|
| 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