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

{{- if and .Values.envoy.useDaemonSet .Values.envoy.networkPolicy.enabled }}
kind: NetworkPolicy
apiVersion: {{ include "common.capabilities.networkPolicy.apiVersion" . }}
metadata:
  name: {{ template "cilium.envoy.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cilium
    app.kubernetes.io/component: envoy
  {{- 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.envoy.podLabels .Values.commonLabels ) "context" . ) }}
  podSelector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: cilium
      app.kubernetes.io/component: envoy
  policyTypes:
    {{- if or .Values.envoy.metrics.enabled .Values.envoy.extraContainerPorts .Values.envoy.networkPolicy.extraIngress }}
    - Ingress
    {{- end }}
    - Egress
  egress:
    {{- if .Values.envoy.networkPolicy.allowExternalEgress }}
    - {}
    {{- else }}
    - ports:
        # Allow dns resolution
        - port: 53
          protocol: UDP
        - port: 53
          protocol: TCP
    {{- if .Values.envoy.networkPolicy.extraEgress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.envoy.networkPolicy.extraEgress "context" $ ) | nindent 4 }}
    {{- end }}
    {{- end }}
  {{- if or .Values.envoy.metrics.enabled .Values.envoy.extraContainerPorts .Values.envoy.networkPolicy.extraIngress }}
  ingress:
    {{- if or .Values.envoy.metrics.enabled .Values.envoy.extraContainerPorts }}
    - ports:
        {{- if .Values.envoy.metrics.enabled }}
        - port: {{ .Values.envoy.containerPorts.metrics }}
        {{- end }}
        {{- range .Values.envoy.extraContainerPorts }}
        - port: {{ .containerPort }}
        {{- end }}
      {{- if not .Values.envoy.networkPolicy.allowExternal }}
      from:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 14 }}
        {{- if .Values.envoy.networkPolicy.addExternalClientAccess }}
        - podSelector:
            matchLabels:
              {{ template "common.names.fullname" . }}-client: "true"
        {{- end }}
        {{- if .Values.envoy.networkPolicy.ingressPodMatchLabels }}
        - podSelector:
            matchLabels: {{- include "common.tplvalues.render" (dict "value" .Values.envoy.networkPolicy.ingressPodMatchLabels "context" $ ) | nindent 14 }}
        {{- end }}
        {{- if .Values.envoy.networkPolicy.ingressNSMatchLabels }}
        - namespaceSelector:
            matchLabels: {{- include "common.tplvalues.render" (dict "value" .Values.envoy.networkPolicy.ingressNSMatchLabels "context" $ ) | nindent 14 }}
          {{- if .Values.envoy.networkPolicy.ingressNSPodMatchLabels }}
          podSelector:
            matchLabels:
              matchLabels: {{- include "common.tplvalues.render" (dict "value" .Values.envoy.networkPolicy.ingressNSPodMatchLabels "context" $ ) | nindent 14 }}
          {{- end }}
        {{- end }}
      {{- end }}
    {{- end }}
    {{- if .Values.envoy.networkPolicy.extraIngress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.envoy.networkPolicy.extraIngress "context" $ ) | nindent 4 }}
    {{- end }}
  {{- end }}
{{- end }}
