{{- if and .Values.newapi.enabled .Values.networkPolicy.enabled }}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: {{ include "bp-newapi.fullname" . }}
  labels:
    {{- include "bp-newapi.labels" . | nindent 4 }}
spec:
  podSelector:
    matchLabels:
      {{- include "bp-newapi.selectorLabels" . | nindent 6 }}
  policyTypes:
    - Ingress
    - Egress
  ingress:
    - from:
        - namespaceSelector:
            matchLabels:
              {{- toYaml .Values.networkPolicy.ingress.fromNamespaceLabels | nindent 14 }}
      ports:
        - port: {{ .Values.newapi.port }}
          protocol: TCP
  egress:
    # In-cluster service egress.
    {{- range .Values.networkPolicy.egress }}
    - to:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: {{ .namespaceLabel }}
      ports:
        - port: {{ .port }}
          protocol: TCP
    {{- end }}
    # DNS resolution.
    - to:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: kube-system
      ports:
        - port: 53
          protocol: UDP
        - port: 53
          protocol: TCP
    # External egress (commercial-provider channels) — the cluster
    # egress gateway enforces destination policy. NewAPI itself only
    # needs HTTPS to reach what the gateway permits.
    - to:
        - ipBlock:
            cidr: 0.0.0.0/0
            except:
              - 10.0.0.0/8
              - 172.16.0.0/12
              - 192.168.0.0/16
      ports:
        - port: 443
          protocol: TCP
{{- end }}
