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

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