{{- if .Values.proxy.networkPolicy.enabled -}}
apiVersion: {{ include "common.capabilities.networkPolicy.apiVersion" . }}
kind: NetworkPolicy
metadata:
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: proxy
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  name: {{ include "jupyterhub.proxy.name" . }}
  namespace: {{ .Release.Namespace | quote }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  podSelector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: proxy
  policyTypes:
    - Ingress
    - Egress
  ingress:
    {{- if .Values.proxy.networkPolicy.extraIngress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.proxy.networkPolicy.extraIngress "context" $ ) | nindent 4 }}
    {{- end }}
    # Pods with label "hub.jupyter.org/network-access-proxy-api" --> Proxy API
    - ports:
        - port: {{ .Values.proxy.containerPort.api }}
      from:
        - podSelector:
            matchLabels:
              hub.jupyter.org/network-access-proxy-api: "true"
        {{- if .Values.proxy.networkPolicy.allowInterspaceAccess }}
          namespaceSelector:
            matchLabels: {}
        - namespaceSelector:
            matchLabels:
              hub.jupyter.org/network-access-proxy-api: "true"
        {{- end }}
  egress:
    # Proxy --> Hub
    - ports:
        - port: {{ coalesce .Values.hub.containerPorts.http .Values.hub.containerPort }}
      to:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" . | nindent 14 }}
              app.kubernetes.io/component: hub
    # Proxy --> Single User
    - ports:
        - port: {{ .Values.singleuser.containerPort }}
      to:
        - podSelector:
            matchLabels:
               {{- include "common.labels.matchLabels" . | nindent 14 }}
              app.kubernetes.io/component: singleuser
    ## DNS access
    - ports:
        - protocol: UDP
          port: 53
        - protocol: TCP
          port: 53
    {{- if .Values.proxy.networkPolicy.extraEgress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.proxy.networkPolicy.extraEgress "context" $ ) | nindent 4 }}
    {{- end }}
{{- end }}
