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

{{- if and .Values.tracking.enabled .Values.tracking.networkPolicy.enabled }}
kind: NetworkPolicy
apiVersion: {{ include "common.capabilities.networkPolicy.apiVersion" . }}
metadata:
  name: {{ template "mlflow.v0.tracking.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: mlflow
    app.kubernetes.io/component: tracking
  {{- 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.tracking.podLabels .Values.commonLabels ) "context" . ) }}
  podSelector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: mlflow
      app.kubernetes.io/component: tracking
  policyTypes:
    - Ingress
    - Egress
  {{- if .Values.tracking.networkPolicy.allowExternalEgress }}
  egress:
    - {}
  {{- else }}
  egress:
    # Allow dns resolution
    - ports:
        - port: 53
          protocol: UDP
        - port: 53
          protocol: TCP
        # Allow connection to these sources for downloading models
        - port: 80
        - port: 443
        - port: 22
    # Allow outbound connections to other pods
    - ports:
        - port: {{ .Values.tracking.containerPorts.http }}
        - port: {{ include "mlflow.v0.tracking.port" . }}
      to:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 14 }}
              app.kubernetes.io/part-of: mlflow
              app.kubernetes.io/component: tracking
    # Allow outbound connections to the database
    - ports:
        - port: {{ include "mlflow.v0.database.port" . }}
      {{- if .Values.postgresql.enabled }}
      to:
        - podSelector:
            matchLabels:
              app.kubernetes.io/name: postgresql
              app.kubernetes.io/instance: {{ .Release.Name }}
      {{- end }}
    # Allow outbound connections to the S3 backend
    - ports:
        - port: {{ include "mlflow.v0.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 }}
    {{- if .Values.tracking.networkPolicy.extraEgress }}
    {{- include "common.tplvalues.render" (dict "value" .Values.tracking.networkPolicy.extraEgress "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
  ingress:
    - ports:
        - port: {{ .Values.tracking.containerPorts.http }}
        - port: {{ include "mlflow.v0.tracking.port" . }}
      from:
      {{- if not .Values.tracking.networkPolicy.allowExternal }}
        - podSelector:
            matchLabels:
              {{ template "common.names.fullname" . }}-client: "true"
      {{- end }}
      {{- if .Values.tracking.networkPolicy.ingressNSMatchLabels }}
        - namespaceSelector:
            matchLabels:
              {{- range $key, $value := .Values.tracking.networkPolicy.ingressNSMatchLabels }}
              {{ $key | quote }}: {{ $value | quote }}
              {{- end }}
          {{- if .Values.tracking.networkPolicy.ingressNSPodMatchLabels }}
          podSelector:
            matchLabels:
              {{- range $key, $value := .Values.tracking.networkPolicy.ingressNSPodMatchLabels }}
              {{ $key | quote }}: {{ $value | quote }}
              {{- end }}
          {{- end }}
        {{- end }}
    {{- if .Values.tracking.networkPolicy.extraIngress }}
    {{- include "common.tplvalues.render" (dict "value" .Values.tracking.networkPolicy.extraIngress "context" $) | nindent 4 }}
    {{- end }}
{{- end }}
