{{- if .Values.networkPolicy.enabled }}
kind: NetworkPolicy
apiVersion: {{ template "networkPolicy.apiVersion" . }}
metadata:
  name: {{ template "common.names.fullname" . }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
  namespace: {{ .Release.Namespace | quote }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  podSelector:
    matchLabels:
      app.kubernetes.io/name: {{ template "common.names.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  policyTypes:
    - Ingress
    - Egress
  egress:
    # Allow dns resolution
    - ports:
      - port: 53
        protocol: UDP
    # Allow outbound connections to other cluster pods
    - ports:
        - port: {{ .Values.redis.containerPorts.redis }}
        - port: {{ .Values.redis.containerPorts.bus }}
      to:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" . | nindent 14 }}
  ingress:
    # Allow inbound connections
    - ports:
        - port: {{ .Values.redis.containerPorts.redis }}
        - port: {{ .Values.redis.containerPorts.bus }}
      from:
        {{- if not .Values.networkPolicy.allowExternal }}
        - podSelector:
            matchLabels:
              {{ template "common.names.fullname" . }}-client: "true"
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" . | nindent 14 }}
        {{- end }}
        {{- if .Values.networkPolicy.ingressNSMatchLabels }}
        - namespaceSelector:
            matchLabels:
              {{- range $key, $value := .Values.networkPolicy.ingressNSMatchLabels }}
              {{ $key | quote }}: {{ $value | quote }}
              {{- end }}
        {{- end }}
        {{- if .Values.networkPolicy.ingressNSPodMatchLabels }}
        - podSelector:
            matchLabels:
              {{- range $key, $value := .Values.networkPolicy.ingressNSPodMatchLabels }}
              {{ $key | quote }}: {{ $value | quote }}
              {{- end }}
        {{- end }}
    {{- if .Values.metrics.enabled }}
    # Allow prometheus scrapes for metrics
    - ports:
        - port: 9121
    {{- end }}
{{- end }}
