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

{{- if .Values.masterCoordinator.networkPolicy.enabled }}
kind: NetworkPolicy
apiVersion: {{ include "common.capabilities.networkPolicy.apiVersion" . }}
metadata:
  name: {{ template "dremio.master-coordinator.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: dremio
    app.kubernetes.io/component: master-coordinator
    {{- /* We need an extra label that applies to both master and non-master coordinators. Taken from upstream dremio chart */}}
    app: dremio-coordinator
  {{- 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.masterCoordinator.podLabels .Values.commonLabels ) "context" . ) }}
  podSelector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: dremio
      app.kubernetes.io/component: master-coordinator
      app: dremio-coordinator
  policyTypes:
    - Ingress
    - Egress
  {{- if .Values.masterCoordinator.networkPolicy.allowExternalEgress }}
  egress:
    - {}
  {{- else }}
  egress:
    # Allow dns resolution
    - ports:
        - port: 53
          protocol: UDP
        - port: 53
          protocol: TCP
    # Allow outbound connections to other cluster pods
    - ports:
        - port: {{ .Values.dremio.containerPorts.web }}
        - port: {{ .Values.dremio.containerPorts.client }}
        - port: {{ .Values.dremio.containerPorts.flight }}
        - port: {{ .Values.dremio.containerPorts.fabric }}
        - port: {{ .Values.dremio.containerPorts.conduit }}
      to:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 14 }}
              part-of: dremio
    # Allow outbound connections to S3
    - ports:
        - port: {{ include "dremio.s3.port" . }}
      {{- if .Values.minio.enabled }}
        - port: {{ .Values.minio.containerPorts.api }}
      to:
        - podSelector:
            matchLabels:
              app.kubernetes.io/name: minio
              app.kubernetes.io/instance: {{ .Release.Name }}
        {{- end }}
    # Allow outbound connections to zookeeper
    - ports:
        - port: {{ include "dremio.zookeeper.port" . }}
      to:
        {{- if .Values.zookeeper.enabled }}
        - podSelector:
            matchLabels:
              app.kubernetes.io/name: zookeeper
              app.kubernetes.io/instance: {{ .Release.Name }}
        {{- end }}
    {{- if .Values.masterCoordinator.networkPolicy.extraEgress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.masterCoordinator.networkPolicy.extraEgress "context" $ ) | nindent 4 }}
    {{- end }}
  {{- end }}
  ingress:
    - ports:
        - port: {{ .Values.dremio.containerPorts.web }}
        {{- if (le (int .Values.coordinator.replicaCount) 0) }}
        - port: {{ .Values.dremio.containerPorts.client }}
        - port: {{ .Values.dremio.containerPorts.flight }}
        {{- end }}
        - port: {{ .Values.dremio.containerPorts.fabric }}
        - port: {{ .Values.dremio.containerPorts.conduit }}
        {{- if .Values.metrics.enabled }}
        - port: {{ .Values.dremio.containerPorts.jmx }}
        - port: {{ .Values.metrics.containerPorts.metrics }}
        {{- end }}
        {{- range .Values.masterCoordinator.extraContainerPorts }}
        - port: {{ . }}
        {{- end }}
      {{- if not .Values.masterCoordinator.networkPolicy.allowExternal }}
      from:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 14 }}
              app.kubernetes.io/part-of: dremio
        {{- if .Values.masterCoordinator.networkPolicy.addExternalClientAccess }}
        - podSelector:
            matchLabels:
              {{ template "dremio.master-coordinator.fullname" . }}-client: "true"
        {{- end }}
        {{- if .Values.masterCoordinator.networkPolicy.ingressPodMatchLabels }}
        - podSelector:
            matchLabels: {{- include "common.tplvalues.render" (dict "value" .Values.masterCoordinator.networkPolicy.ingressPodMatchLabels "context" $ ) | nindent 14 }}
        {{- end }}
        {{- if .Values.masterCoordinator.networkPolicy.ingressNSMatchLabels }}
        - namespaceSelector:
            matchLabels:
              {{- range $key, $value := .Values.masterCoordinator.networkPolicy.ingressNSMatchLabels }}
              {{ $key | quote }}: {{ $value | quote }}
              {{- end }}
          {{- if .Values.masterCoordinator.networkPolicy.ingressNSPodMatchLabels }}
          podSelector:
            matchLabels:
              {{- range $key, $value := .Values.masterCoordinator.networkPolicy.ingressNSPodMatchLabels }}
              {{ $key | quote }}: {{ $value | quote }}
              {{- end }}
          {{- end }}
        {{- end }}
      {{- end }}
    {{- if .Values.masterCoordinator.networkPolicy.extraIngress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.masterCoordinator.networkPolicy.extraIngress "context" $ ) | nindent 4 }}
    {{- end }}
{{- end }}
