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

{{- if and (eq .Values.architecture "replication") .Values.replica.networkPolicy.enabled }}
kind: NetworkPolicy
apiVersion: {{ include "common.capabilities.networkPolicy.apiVersion" . }}
metadata:
  name: {{ template "keydb.replica.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: keydb
    app.kubernetes.io/component: replica
  {{- 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 .Values.replica.podLabels .Values.commonLabels ) "context" . ) }}
  podSelector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: keydb
      app.kubernetes.io/component: replica
  policyTypes:
    - Ingress
    - Egress
  egress:
    {{- if .Values.replica.networkPolicy.allowExternalEgress }}
    - {}
    {{- else }}
    # Allow dns resolution
    - ports:
        - port: 53
          protocol: UDP
        - port: 53
          protocol: TCP
    # Allow outbound connections to master pods
    - ports:
        - port: {{ .Values.master.containerPorts.redis }}
      to:
        - podSelector:
            {{- $masterPodLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.master.podLabels .Values.commonLabels ) "context" . ) }}
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $masterPodLabels "context" $ ) | nindent 6 }}
              app.kubernetes.io/part-of: keydb
              app.kubernetes.io/component: master
    {{- if .Values.replica.networkPolicy.extraEgress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.replica.networkPolicy.extraEgress "context" $ ) | nindent 4 }}
    {{- end }}
    {{- end }}
  ingress:
    - ports:
        - port: {{ .Values.replica.containerPorts.keydb }}
        {{- if .Values.metrics.enabled }}
        - port: {{ .Values.metrics.containerPorts.http }}
        {{- if ne .Values.metrics.containerPorts.http .Values.metrics.service.port }}
        - port: {{ .Values.metrics.service.port }}
        {{- end }}
        {{- end }}
        {{- range .Values.replica.extraContainerPorts }}
        - port: {{ .containerPort }}
        {{- end }}
      {{- if not .Values.replica.networkPolicy.allowExternal }}
      from:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 14 }}
        {{- if .Values.replica.networkPolicy.addExternalClientAccess }}
        - podSelector:
            matchLabels:
              {{ template "common.names.fullname" . }}-client: "true"
        {{- end }}
        {{- if .Values.replica.networkPolicy.ingressPodMatchLabels }}
        - podSelector:
            matchLabels: {{- include "common.tplvalues.render" (dict "value" .Values.replica.networkPolicy.ingressPodMatchLabels "context" $ ) | nindent 14 }}
        {{- end }}
        {{- if .Values.replica.networkPolicy.ingressNSMatchLabels }}
        - namespaceSelector:
            matchLabels: {{- include "common.tplvalues.render" (dict "value" .Values.replica.networkPolicy.ingressNSMatchLabels "context" $ ) | nindent 14 }}
          {{- if .Values.replica.networkPolicy.ingressNSPodMatchLabels }}
          podSelector:
            matchLabels:
              matchLabels: {{- include "common.tplvalues.render" (dict "value" .Values.replica.networkPolicy.ingressNSPodMatchLabels "context" $ ) | nindent 14 }}
          {{- end }}
        {{- end }}
      {{- end }}
    {{- if .Values.replica.networkPolicy.extraIngress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.replica.networkPolicy.extraIngress "context" $ ) | nindent 4 }}
    {{- end }}
{{- end }}
