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

{{- if and .Values.vulture.enabled .Values.vulture.networkPolicy.enabled }}
kind: NetworkPolicy
apiVersion: {{ include "common.capabilities.networkPolicy.apiVersion" . }}
metadata:
  name: {{ template "grafana-tempo.vulture.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: vulture
  {{- 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.vulture.podLabels .Values.commonLabels ) "context" . ) }}
  podSelector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: vulture
  policyTypes:
    - Ingress
    - Egress
  {{- if .Values.vulture.networkPolicy.allowExternalEgress }}
  egress:
    - {}
  {{- else }}
  egress:
    # Allow dns resolution
    - ports:
        - port: 53
          protocol: UDP
        - port: 53
          protocol: TCP
    # Allow outbound connections to tempo cluster
    - ports:
        - port: {{ .Values.tempo.containerPorts.web }}
        - port: {{ .Values.tempo.containerPorts.gossipRing }}
        - port: {{ .Values.tempo.containerPorts.grpc }}
        {{/* The following ports are hardcoded in grafana-tempo */}}
        {{- if .Values.tempo.traces.jaeger.thriftCompact }}
        - port: 6831
          protocol: UDP
        {{- end }}
        {{- if .Values.tempo.traces.jaeger.thriftBinary }}
        - port: 6832
          protocol: UDP
        {{- end }}
        {{- if .Values.tempo.traces.jaeger.thriftHttp }}
        - port: 14268
        {{- end }}
        {{- if .Values.tempo.traces.jaeger.grpc }}
        - port: 14250
        {{- end }}
        {{- if .Values.tempo.traces.zipkin }}
        - port: 9411
        {{- end }}
        {{- if .Values.tempo.traces.otlp.http }}
        - port: 4318
        {{- end }}
        {{- if .Values.tempo.traces.otlp.grpc }}
        - port: 4317
        {{- end }}
        {{- if .Values.tempo.traces.opencensus }}
        - port: 55678
        {{- end }}
      to:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 14 }}
    {{- if .Values.vulture.enabled }}
    # Allow outbound connections to vulture
    - ports:
        - port: {{ .Values.vulture.containerPorts.http }}
      to:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 14 }}
              app.kubernetes.io/component: vulture
    {{- end }}
    # Allow outbound connections to Memcached
    - ports:
      {{- if .Values.memcached.enabled }}
        - port: {{ .Values.memcached.containerPorts.memcached }}
      to:
        - podSelector:
            matchLabels:
              app.kubernetes.io/name: memcached
              app.kubernetes.io/instance: {{ .Release.Name }}              
      {{- else }}
        - port: {{ .Values.externalMemcached.port }}
      {{- end }}
    {{- if .Values.vulture.networkPolicy.extraEgress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.vulture.networkPolicy.extraEgress "context" $ ) | nindent 4 }}
    {{- end }}
  {{- end }}
  ingress:
    - ports:
        - port: {{ .Values.vulture.containerPorts.http }}
      {{- if not .Values.vulture.networkPolicy.allowExternal }}
      from:
        {{- if .Values.vulture.networkPolicy.addExternalClientAccess }}
        - podSelector:
            matchLabels:
              {{ template "grafana-tempo.vulture.fullname" . }}-vulture: "true"
        {{- end }}
        {{- if .Values.vulture.networkPolicy.ingressPodMatchLabels }}
        - podSelector:
            matchLabels: {{- include "common.tplvalues.render" (dict "value" .Values.vulture.networkPolicy.ingressPodMatchLabels "context" $ ) | nindent 14 }}
        {{- end }}
        {{- if .Values.vulture.networkPolicy.ingressNSMatchLabels }}
        - namespaceSelector:
            matchLabels: {{- include "common.tplvalues.render" (dict "value" .Values.vulture.networkPolicy.ingressNSMatchLabels "context" $ ) | nindent 14 }}
          {{- if .Values.vulture.networkPolicy.ingressNSPodMatchLabels }}
          podSelector:
            matchLabels: {{- include "common.tplvalues.render" (dict "value" .Values.vulture.networkPolicy.ingressNSPodMatchLabels "context" $ ) | nindent 14 }}
          {{- end }}
        {{- end }}
      {{- end }}
    {{- if .Values.vulture.networkPolicy.extraIngress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.vulture.networkPolicy.extraIngress "context" $ ) | nindent 4 }}
    {{- end }}
{{- end }}
