{{- 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
        {{- if .Values.meteringSidecar.enabled }}
        # Metering sidecar (#798) exposes its observed-proxy port
        # to the same caller set as NewAPI itself; the Service
        # routes /v1/* traffic through it.
        - port: {{ .Values.meteringSidecar.port }}
          protocol: TCP
        {{- end }}
  egress:
    {{- if .Values.meteringSidecar.enabled }}
    # Metering sidecar emits to NATS JetStream (#798 + #795
    # [Q-mine-3]). The bp-nats-jetstream Service lives in
    # nats-system and listens on the standard nats-io port.
    - to:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: nats-system
      ports:
        - port: 4222
          protocol: TCP
    {{- end }}
    {{- if .Values.cnpg.enabled }}
    # bp-cnpg Postgres — the CNPG Cluster CR is rendered into
    # `.Release.Namespace` by templates/cnpg-cluster.yaml. The
    # operator pods live in cnpg-system; the actual Postgres
    # workload Pods (the ones bp-newapi must reach on 5432)
    # live in the same namespace as this NetworkPolicy. Select
    # by the operator-emitted `cnpg.io/cluster=<clusterName>`
    # Pod label so the rule survives the operator namespace
    # being different from the data-plane namespace (issue #1901).
    - to:
        - podSelector:
            matchLabels:
              cnpg.io/cluster: {{ printf "%s-newapi-pg" (include "bp-newapi.fullname" .) }}
      ports:
        - port: 5432
          protocol: TCP
    {{- end }}
    # In-cluster service egress (peer blueprints in their own
    # namespaces, e.g. bp-valkey, bp-keycloak, bp-vllm).
    {{- 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 }}
