{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if .Values.pluginBarmanCloud.networkPolicy.enabled }}
kind: NetworkPolicy
apiVersion: {{ include "common.capabilities.networkPolicy.apiVersion" . }}
metadata:
  name: {{ template "cloudnative-pg.plugin-barman-cloud.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- /* Updating app.kubernetes.io/name because to avoid "duplicate deployment" error: https://github.com/cloudnative-pg/cloudnative-pg/blob/9c2769815ff78cbfc28e2a2818f3a04add33477c/pkg/certs/operator_deployment.go#L120 */}}
  {{- $appNameLabels := dict "app.kubernetes.io/name" "plugin-barman-cloud" }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list $appNameLabels .Values.commonLabels) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: plugin-barman-cloud
    app.kubernetes.io/part-of: cloudnative-pg
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list $appNameLabels .Values.pluginBarmanCloud.podLabels .Values.commonLabels ) "context" . ) }}
  podSelector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: cloudnative-pg
      app.kubernetes.io/component: plugin-barman-cloud
  policyTypes:
    - Ingress
    - Egress
  {{- if .Values.pluginBarmanCloud.networkPolicy.allowExternalEgress }}
  egress:
    - {}
  {{- else }}
  egress:
    - ports:
        # Allow dns resolution
        - port: 53
          protocol: UDP
        - port: 53
          protocol: TCP
        # Allow access to kube-apiserver
        {{- range $port := .Values.pluginBarmanCloud.networkPolicy.kubeAPIServerPorts }}
        - port: {{ $port }}
        {{- end }}
    # PostgreSQL instances have the label cnpg.io/podRole: "instance". The barman plugin will inject a sidecar
    - to:
        - podSelector:
            matchLabels:
              cnpg.io/podRole: "instance"
    {{- if .Values.pluginBarmanCloud.networkPolicy.extraEgress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.pluginBarmanCloud.networkPolicy.extraEgress "context" $ ) | nindent 4 }}
    {{- end }}
  {{- end }}
  ingress:
    - ports:
        - port: {{ .Values.pluginBarmanCloud.containerPorts.grpc }}
        {{- if .Values.pluginBarmanCloud.metrics.enabled }}
        - port: {{ .Values.pluginBarmanCloud.containerPorts.metrics }}
        {{- end }}
      {{- if not .Values.pluginBarmanCloud.networkPolicy.allowExternal }}
      from:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 14 }}
              app.kubernetes.io/part-of: cloudnative-pg
        - podSelector:
            matchLabels:
              {{ template "cloudnative-pg.plugin-barman-cloud.fullname" . }}-client: "true"
        {{- if .Values.pluginBarmanCloud.networkPolicy.ingressNSMatchLabels }}
        - namespaceSelector:
            matchLabels:
              {{- range $key, $value := .Values.pluginBarmanCloud.networkPolicy.ingressNSMatchLabels }}
              {{ $key | quote }}: {{ $value | quote }}
              {{- end }}
          {{- if .Values.pluginBarmanCloud.networkPolicy.ingressNSPodMatchLabels }}
          podSelector:
            matchLabels:
              {{- range $key, $value := .Values.pluginBarmanCloud.networkPolicy.ingressNSPodMatchLabels }}
              {{ $key | quote }}: {{ $value | quote }}
              {{- end }}
          {{- end }}
        {{- end }}
      {{- end }}
    {{- if .Values.pluginBarmanCloud.networkPolicy.extraIngress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.pluginBarmanCloud.networkPolicy.extraIngress "context" $ ) | nindent 4 }}
    {{- end }}
{{- end }}
