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

{{- if .Values.msgTopologyOperator.networkPolicy.enabled }}
kind: NetworkPolicy
apiVersion: {{ include "common.capabilities.networkPolicy.apiVersion" . }}
metadata:
  name: {{ template "rmqco.msgTopologyOperator.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: messaging-topology-operator
    app.kubernetes.io/part-of: rabbitmq
  {{- 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.msgTopologyOperator.podLabels .Values.commonLabels ) "context" . ) }}
  podSelector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: messaging-topology-operator
      app.kubernetes.io/part-of: rabbitmq
  policyTypes:
    - Ingress
    - Egress
  {{- if .Values.msgTopologyOperator.networkPolicy.allowExternalEgress }}
  egress:
    - {}
  {{- else }}
  egress:
    - ports:
        # Allow dns resolution
        - port: 53
          protocol: UDP
        - port: 53
          protocol: TCP
        # Allow rabbitmq api
        - port: 15672
          protocol: TCP
        # Allow access to kube-apiserver
        {{- range $port := .Values.msgTopologyOperator.networkPolicy.kubeAPIServerPorts }}
        - port: {{ $port }}
        {{- end }}
    # RabbitMQCluster instances have the label app.kubernetes.io/component: rabbitmq
    - to: 
        - podSelector:
            matchLabels:
              app.kubernetes.io/component: rabbitmq
        {{- if not .Values.msgTopologyOperator.watchAllNamespaces }}
        {{- $watchNamespaces := default (list (include "common.names.namespace" .)) .Values.msgTopologyOperator.watchNamespaces }}
          namespaceSelector:
            matchExpressions:
              - key: namespace
                operator: In
                values: 
                {{- range $namespace := $watchNamespaces }}
                  - {{ $namespace }}
                {{- end }}
        {{- end }}
    {{- if .Values.msgTopologyOperator.networkPolicy.extraEgress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.msgTopologyOperator.networkPolicy.extraEgress "context" $ ) | nindent 4 }}
    {{- end }}
  {{- end }}
  ingress:
    - ports:
        {{/* Webhook port is hardcoded in the operator code */}}
        - port: 9443
        {{- if .Values.msgTopologyOperator.metrics.service.enabled }}
        - port: {{ .Values.msgTopologyOperator.containerPorts.metrics }}
        {{- end }}
      {{- if not .Values.msgTopologyOperator.networkPolicy.allowExternal }}
      from:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 14 }}
              app.kubernetes.io/part-of: rabbitmq
        - podSelector:
            matchLabels:
              {{ template "rmqco.msgTopologyOperator.fullname" . }}-client: "true"
        {{- if .Values.msgTopologyOperator.networkPolicy.ingressNSMatchLabels }}
        - namespaceSelector:
            matchLabels:
              {{- range $key, $value := .Values.msgTopologyOperator.networkPolicy.ingressNSMatchLabels }}
              {{ $key | quote }}: {{ $value | quote }}
              {{- end }}
          {{- if .Values.msgTopologyOperator.networkPolicy.ingressNSPodMatchLabels }}
          podSelector:
            matchLabels:
              {{- range $key, $value := .Values.msgTopologyOperator.networkPolicy.ingressNSPodMatchLabels }}
              {{ $key | quote }}: {{ $value | quote }}
              {{- end }}
          {{- end }}
        {{- end }}
      {{- end }}
    {{- if .Values.msgTopologyOperator.networkPolicy.extraIngress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.msgTopologyOperator.networkPolicy.extraIngress "context" $ ) | nindent 4 }}
    {{- end }}
{{- end }}
