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

{{- if .Values.hub.networkPolicy.enabled }}
apiVersion: {{ include "common.capabilities.networkPolicy.apiVersion" . }}
kind: NetworkPolicy
metadata:
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: hub
  name: {{ include "jupyterhub.hub.name" . }}
  namespace: {{ .Release.Namespace | quote }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- $hubPodLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.hub.podLabels .Values.commonLabels ) "context" . ) }}
  podSelector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $hubPodLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: hub
  policyTypes:
    - Ingress
    - Egress
  {{- if .Values.hub.networkPolicy.allowExternalEgress }}
  egress:
    - {}
  {{- else }}
  egress:
    # Hub --> Proxy API
    - ports:
        - port: {{ .Values.proxy.containerPort.api }}
      to:
        {{- $proxyPodLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.proxy.podLabels .Values.commonLabels ) "context" . ) }}
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $proxyPodLabels "context" $ ) | nindent 14 }}
              app.kubernetes.io/component: proxy
    # Hub --> Single User
    - ports:
        - port: {{ .Values.singleuser.containerPorts.http }}
      to:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 14 }}
              app.kubernetes.io/component: singleuser
    ## DNS access
    - ports:
        - protocol: UDP
          port: 53
        - protocol: TCP
          port: 53
    ## PostgreSQL access
    - ports:
        - protocol: UDP
          port: {{ .Values.postgresql.service.ports.postgresql }}
        - protocol: TCP
          port: {{ .Values.postgresql.service.ports.postgresql }}
    {{- if .Values.hub.networkPolicy.extraEgress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.hub.networkPolicy.extraEgress "context" $ ) | nindent 4 }}
    {{- end }}
  {{- end }}
  ingress:
    # Pods with label "hub.jupyter.org/network-access-hub" --> Hub
    - ports:
        - port: {{ .Values.hub.containerPorts.http }}
      {{- if not .Values.hub.networkPolicy.allowExternal }}
      from:
        - podSelector:
            matchLabels:
              hub.jupyter.org/network-access-hub: "true"
        {{- if .Values.hub.networkPolicy.allowInterspaceAccess }}
          namespaceSelector:
            matchLabels: {}
        - namespaceSelector:
            matchLabels:
              hub.jupyter.org/network-access-hub: "true"
        {{- end }}
        {{- if .Values.proxy.networkPolicy.ingressNSMatchLabels }}
        - namespaceSelector:
            matchLabels:
              {{- range $key, $value := .Values.proxy.networkPolicy.ingressNSMatchLabels }}
              {{ $key | quote }}: {{ $value | quote }}
              {{- end }}
          {{- if .Values.proxy.networkPolicy.ingressNSPodMatchLabels }}
          podSelector:
            matchLabels:
              {{- range $key, $value := .Values.proxy.networkPolicy.ingressNSPodMatchLabels }}
              {{ $key | quote }}: {{ $value | quote }}
              {{- end }}
          {{- end }}
        {{- end }}
      {{- end }}
    {{- if .Values.hub.metrics.serviceMonitor.enabled }}
    # Prometheus pods with label "app.kubernetes.io/component: prometheus" --> Hub
    - from:
      - namespaceSelector: {}
        podSelector:
          matchLabels:
            app.kubernetes.io/component: prometheus
    {{- end }}
    {{- if .Values.hub.networkPolicy.extraIngress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.hub.networkPolicy.extraIngress "context" $ ) | nindent 4 }}
    {{- end }}

{{- end }}
