| gio | 07eb108 | 2024-10-25 14:35:56 +0400 | [diff] [blame] | 1 | {{- /* |
| 2 | Copyright Broadcom, Inc. All Rights Reserved. |
| 3 | SPDX-License-Identifier: APACHE-2.0 |
| 4 | */}} |
| 5 | |
| 6 | {{- if .Values.networkPolicy.enabled }} |
| 7 | kind: NetworkPolicy |
| 8 | apiVersion: {{ include "common.capabilities.networkPolicy.apiVersion" . }} |
| 9 | metadata: |
| 10 | name: {{ include "mongodb.fullname" . }} |
| 11 | namespace: {{ include "common.names.namespace" . | quote }} |
| 12 | {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.labels .Values.commonLabels ) "context" . ) }} |
| 13 | labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }} |
| 14 | app.kubernetes.io/component: mongodb |
| 15 | {{- if or .Values.annotations .Values.commonAnnotation }} |
| 16 | {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.annotations .Values.commonAnnotations ) "context" . ) }} |
| 17 | annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }} |
| 18 | {{- end }} |
| 19 | spec: |
| 20 | {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }} |
| 21 | podSelector: |
| 22 | matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }} |
| 23 | app.kubernetes.io/component: mongodb |
| 24 | policyTypes: |
| 25 | - Ingress |
| 26 | - Egress |
| 27 | {{- if .Values.networkPolicy.allowExternalEgress }} |
| 28 | egress: |
| 29 | - {} |
| 30 | {{- else }} |
| 31 | egress: |
| 32 | # Allow dns resolution |
| 33 | - ports: |
| 34 | - port: 53 |
| 35 | protocol: UDP |
| 36 | - port: 53 |
| 37 | protocol: TCP |
| 38 | # Allow connection to other cluster pods |
| 39 | - ports: |
| 40 | - port: {{ .Values.containerPorts.mongodb }} |
| 41 | to: |
| 42 | - podSelector: |
| 43 | matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 14 }} |
| 44 | {{- if (and .Values.externalAccess.enabled .Values.externalAccess.autoDiscovery.enabled) }} |
| 45 | {{- $kubernetesEndpoints := lookup "v1" "Endpoints" (include "common.names.namespace" .) "kubernetes" }} |
| 46 | {{- range $kubernetesEndpoints.subsets }} |
| 47 | # Allow connection to API server, required by auto-discovery containers |
| 48 | - ports: |
| 49 | {{- range .ports }} |
| 50 | - port: {{ .port }} |
| 51 | {{- end }} |
| 52 | to: |
| 53 | {{- range .addresses }} |
| 54 | - ipBlock: |
| 55 | cidr: {{ printf "%s/32" .ip }} |
| 56 | {{- end }} |
| 57 | {{- end }} |
| 58 | {{- end }} |
| 59 | {{- if .Values.networkPolicy.extraEgress }} |
| 60 | {{- include "common.tplvalues.render" ( dict "value" .Values.rts.networkPolicy.extraEgress "context" $ ) | nindent 4 }} |
| 61 | {{- end }} |
| 62 | {{- end }} |
| 63 | ingress: |
| 64 | - ports: |
| 65 | - port: {{ .Values.containerPorts.mongodb }} |
| 66 | {{- if .Values.metrics.enabled }} |
| 67 | - port: {{ .Values.metrics.containerPort }} |
| 68 | {{- end }} |
| 69 | {{- if not .Values.networkPolicy.allowExternal }} |
| 70 | from: |
| 71 | - podSelector: |
| 72 | matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 14 }} |
| 73 | app.kubernetes.io/component: mongodb |
| 74 | {{- if .Values.networkPolicy.addExternalClientAccess }} |
| 75 | - podSelector: |
| 76 | matchLabels: |
| 77 | {{ template "common.names.fullname" . }}-client: "true" |
| 78 | {{- end }} |
| 79 | {{- if .Values.networkPolicy.ingressPodMatchLabels }} |
| 80 | - podSelector: |
| 81 | matchLabels: {{- include "common.tplvalues.render" (dict "value" .Values.networkPolicy.ingressPodMatchLabels "context" $ ) | nindent 14 }} |
| 82 | {{- end }} |
| 83 | {{- $ingressNSMatchLabels := coalesce .Values.networkPolicy.ingressNSMatchLabels .Values.networkPolicy.namespaceSelector }} |
| 84 | {{- if $ingressNSMatchLabels }} |
| 85 | - namespaceSelector: |
| 86 | matchLabels: {{- include "common.tplvalues.render" (dict "value" $ingressNSMatchLabels "context" $ ) | nindent 14 }} |
| 87 | {{- $ingressNSPodMatchLabels := coalesce .Values.networkPolicy.ingressNSPodMatchLabels .Values.networkPolicy.podSelector }} |
| 88 | {{- if $ingressNSPodMatchLabels }} |
| 89 | podSelector: |
| 90 | matchLabels: {{- include "common.tplvalues.render" (dict "value" $ingressNSPodMatchLabels "context" $ ) | nindent 14 }} |
| 91 | {{- end }} |
| 92 | {{- end }} |
| 93 | {{- end }} |
| 94 | {{- $extraIngress := coalesce .Values.networkPolicy.extraIngress .Values.networkPolicy.customRules }} |
| 95 | {{- if $extraIngress }} |
| 96 | {{- include "common.tplvalues.render" ( dict "value" $extraIngress "context" $ ) | nindent 4 }} |
| 97 | {{- end }} |
| 98 | {{- end }} |