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

{{- if and .Values.tableManager.enabled .Values.tableManager.networkPolicy.enabled }}
kind: NetworkPolicy
apiVersion: {{ include "common.capabilities.networkPolicy.apiVersion" . }}
metadata:
  name: {{ template "grafana-loki.table-manager.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: grafana-loki
    app.kubernetes.io/component: table-manager
  {{- 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.tableManager.podLabels .Values.commonLabels ) "context" . ) }}
  podSelector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: grafana-loki
      app.kubernetes.io/component: table-manager
  policyTypes:
    - Ingress
    - Egress
  {{- if .Values.tableManager.networkPolicy.allowExternalEgress }}
  egress:
    - {}
  {{- else }}
  egress:
    # Allow dns resolution
    - ports:
        - port: 53
          protocol: UDP
        - port: 53
          protocol: TCP
    # Allow outbound connections to loki cluster
    - ports:
        - port: {{ .Values.loki.containerPorts.http }}
        - port: {{ .Values.loki.containerPorts.gossipRing }}
        - port: {{ .Values.loki.containerPorts.grpc }}
      to:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 14 }}
              app.kubernetes.io/part-of: grafana-loki
    # Allow outbound connections to gateway
    - ports:
        - port: {{ .Values.gateway.containerPorts.http }}
      to:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 14 }}
              app.kubernetes.io/part-of: grafana-loki
              app.kubernetes.io/component: gateway
    # Allow outbound connections to Memcached Chunks
    - ports:
      {{- if .Values.memcachedchunks.enabled }}
        - port: {{ .Values.memcachedchunks.containerPorts.memcached }}
      to:
        - podSelector:
            matchLabels:
              app.kubernetes.io/name: memcachedchunks
              app.kubernetes.io/instance: {{ .Release.Name }}              
      {{- else }}
        - port: {{ .Values.externalMemcachedChunks.port }}
      {{- end }}
    # Allow outbound connections to Memcached Metadata
    - ports:
      {{- if .Values.memcachedindexqueries.enabled }}
        - port: {{ .Values.memcachedindexqueries.containerPorts.memcached }}
      to:
        - podSelector:
            matchLabels:
              app.kubernetes.io/name: memcachedindexqueries
              app.kubernetes.io/instance: {{ .Release.Name }}              
      {{- else }}
        - port: {{ .Values.externalMemcachedIndexQueries.port }}
      {{- end }}
    # Allow outbound connections to Memcached Frontend
    - ports:
      {{- if .Values.memcachedfrontend.enabled }}
        - port: {{ .Values.memcachedfrontend.containerPorts.memcached }}
      to:
        - podSelector:
            matchLabels:
              app.kubernetes.io/name: memcachedfrontend
              app.kubernetes.io/instance: {{ .Release.Name }}              
      {{- else }}
        - port: {{ .Values.externalMemcachedFrontend.port }}
      {{- end }}
    # Allow outbound connections to Memcached Metadata
    - ports:
      {{- if .Values.memcachedindexwrites.enabled }}
        - port: {{ .Values.memcachedindexwrites.containerPorts.memcached }}
      to:
        - podSelector:
            matchLabels:
              app.kubernetes.io/name: memcachedindexwrites
              app.kubernetes.io/instance: {{ .Release.Name }}              
      {{- else }}
        - port: {{ .Values.externalMemcachedIndexWrites.port }}
      {{- end }}
    {{- if .Values.tableManager.networkPolicy.extraEgress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.tableManager.networkPolicy.extraEgress "context" $ ) | nindent 4 }}
    {{- end }}
  {{- end }}
  ingress:
    - ports:
        - port: {{ .Values.loki.containerPorts.http }}
        - port: {{ .Values.loki.containerPorts.gossipRing }}
        - port: {{ .Values.loki.containerPorts.grpc }}
      {{- if not .Values.tableManager.networkPolicy.allowExternal }}
      from:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 14 }}
              app.kubernetes.io/part-of: grafana-loki
        {{- if .Values.tableManager.networkPolicy.addExternalClientAccess }}
        - podSelector:
            matchLabels:
              {{ template "grafana-loki.table-manager.fullname" . }}-table-manager: "true"
        {{- end }}
        {{- if .Values.tableManager.networkPolicy.ingressPodMatchLabels }}
        - podSelector:
            matchLabels: {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.networkPolicy.ingressPodMatchLabels "context" $ ) | nindent 14 }}
        {{- end }}
        {{- if .Values.tableManager.networkPolicy.ingressNSMatchLabels }}
        - namespaceSelector:
            matchLabels: {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.networkPolicy.ingressNSMatchLabels "context" $ ) | nindent 14 }}
          {{- if .Values.tableManager.networkPolicy.ingressNSPodMatchLabels }}
          podSelector:
            matchLabels: {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.networkPolicy.ingressNSPodMatchLabels "context" $ ) | nindent 14 }}
          {{- end }}
        {{- end }}
      {{- end }}
    {{- if .Values.tableManager.networkPolicy.extraIngress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.tableManager.networkPolicy.extraIngress "context" $ ) | nindent 4 }}
    {{- end }}
{{- end }}