{{- if .Values.networkPolicy.enabled }}
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: {{ include "rabbitmq.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
spec:
  podSelector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
  ingress:
    # Allow inbound connections
    - ports:
        - port: 4369  # EPMD
        - port: {{ .Values.service.port }}
        - port: {{ .Values.service.tlsPort }}
        - port: {{ .Values.service.distPort }}
        - port: {{ .Values.service.managerPort }}
      {{- if not .Values.networkPolicy.allowExternal }}
      from:
        - podSelector:
            matchLabels:
              {{ template "rabbitmq.fullname" . }}-client: "true"
        - podSelector:
            matchLabels:
              app: {{ template "rabbitmq.name" . }}
              release: {{ .Release.Name }}
        {{- if .Values.networkPolicy.additionalRules }}
        {{- include "common.tplvalues.render" (dict "value" .Values.networkPolicy.additionalRules "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
    # Allow prometheus scrapes
    - ports:
        - port: {{ .Values.service.metricsPort }}
{{- end }}
