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

{{- if and .Values.controlPlane.enabled .Values.controlPlane.networkPolicy.enabled }}
kind: NetworkPolicy
apiVersion: {{ include "common.capabilities.networkPolicy.apiVersion" . }}
metadata:
  name: {{ template "apisix.control-plane.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: apisix
    app.kubernetes.io/component: control-plane
  {{- 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.controlPlane.podLabels .Values.commonLabels ) "context" . ) }}
  podSelector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: apisix
      app.kubernetes.io/component: control-plane
  policyTypes:
    - Ingress
    - Egress
  {{- if .Values.controlPlane.networkPolicy.allowExternalEgress }}
  egress:
    - {}
  {{- else }}
  egress:
    - ports:
        # Allow dns resolution
        - port: 53
          protocol: UDP
        - port: 53
          protocol: TCP
        # Allow access to kube-apiserver
        {{- range $port := .Values.controlPlane.networkPolicy.kubeAPIServerPorts }}
        - port: {{ $port }}
        {{- end }}
    # Allow outbound connections to etcd
    - ports:
        - port: {{ include "apisix.etcd.port" . }}
      to: 
        {{- if .Values.etcd.enabled }}
        - podSelector:
            matchLabels:
              app.kubernetes.io/name: etcd
              app.kubernetes.io/instance: {{ .Release.Name }}
        {{- end }}
    {{- if .Values.controlPlane.networkPolicy.extraEgress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.controlPlane.networkPolicy.extraEgress "context" $ ) | nindent 4 }}
    {{- end }}
  {{- end }}
  ingress:
    - ports:
        - port: {{ .Values.controlPlane.containerPorts.adminAPI }}
        - port: {{ .Values.controlPlane.containerPorts.configServer }}
        - port: {{ .Values.controlPlane.containerPorts.control }}
        - port: {{ .Values.controlPlane.containerPorts.metrics }}
      {{- if not .Values.controlPlane.networkPolicy.allowExternal }}
      from:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 14 }}
              app.kubernetes.io/part-of: apisix
        - podSelector:
            matchLabels:
              {{ template "apisix.control-plane.fullname" . }}-client: "true"
        {{- if .Values.controlPlane.networkPolicy.ingressNSMatchLabels }}
        - namespaceSelector:
            matchLabels:
              {{- range $key, $value := .Values.controlPlane.networkPolicy.ingressNSMatchLabels }}
              {{ $key | quote }}: {{ $value | quote }}
              {{- end }}
          {{- if .Values.controlPlane.networkPolicy.ingressNSPodMatchLabels }}
          podSelector:
            matchLabels:
              {{- range $key, $value := .Values.controlPlane.networkPolicy.ingressNSPodMatchLabels }}
              {{ $key | quote }}: {{ $value | quote }}
              {{- end }}
          {{- end }}
        {{- end }}
      {{- end }}
    {{- if .Values.controlPlane.networkPolicy.extraIngress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.controlPlane.networkPolicy.extraIngress "context" $ ) | nindent 4 }}
    {{- end }}
{{- end }}
