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

{{- if and .Values.queryScheduler.enabled .Values.queryScheduler.networkPolicy.enabled }}
kind: NetworkPolicy
apiVersion: {{ include "common.capabilities.networkPolicy.apiVersion" . }}
metadata:
  name: {{ template "grafana-mimir.query-scheduler.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-mimir
    app.kubernetes.io/component: query-scheduler
  {{- 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.queryScheduler.podLabels .Values.commonLabels ) "context" . ) }}
  podSelector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: grafana-mimir
      app.kubernetes.io/component: query-scheduler
  policyTypes:
    - Ingress
    - Egress
  {{- if .Values.queryScheduler.networkPolicy.allowExternalEgress }}
  egress:
    - {}
  {{- else }}
  egress:
    # Allow dns resolution
    - ports:
        - port: 53
          protocol: UDP
        - port: 53
          protocol: TCP
    # Allow outbound connections to mimir cluster
    - ports:
        - port: {{ .Values.mimir.containerPorts.http }}
        - port: {{ .Values.mimir.containerPorts.gossipRing }}
        - port: {{ .Values.mimir.containerPorts.grpc }}
      to:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 14 }}
              app.kubernetes.io/part-of: grafana-mimir
    # 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 Index
    - ports:
      {{- if .Values.memcachedindex.enabled }}
        - port: {{ .Values.memcachedindex.containerPorts.memcached }}
      to:
        - podSelector:
            matchLabels:
              app.kubernetes.io/name: memcachedindex
              app.kubernetes.io/instance: {{ .Release.Name }}              
      {{- else }}
        - port: {{ .Values.externalMemcachedIndex.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.memcachedmetadata.enabled }}
        - port: {{ .Values.memcachedmetadata.containerPorts.memcached }}
      to:
        - podSelector:
            matchLabels:
              app.kubernetes.io/name: memcachedmetadata
              app.kubernetes.io/instance: {{ .Release.Name }}              
      {{- else }}
        - port: {{ .Values.externalMemcachedMetadata.port }}
      {{- end }}
    {{- if .Values.minio.enabled }}
    # Allow outbound connections to S3
    - ports:
        - port: {{ .Values.minio.containerPorts.api }}
      to:
        - podSelector:
            matchLabels:
              app.kubernetes.io/name: minio
              app.kubernetes.io/instance: {{ .Release.Name }}              
    {{- end }}
    {{- if .Values.queryScheduler.networkPolicy.extraEgress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.queryScheduler.networkPolicy.extraEgress "context" $ ) | nindent 4 }}
    {{- end }}
  {{- end }}
  ingress:
    - ports:
        - port: {{ .Values.mimir.containerPorts.http }}
        - port: {{ .Values.mimir.containerPorts.gossipRing }}
        - port: {{ .Values.mimir.containerPorts.grpc }}
      {{- if not .Values.queryScheduler.networkPolicy.allowExternal }}
      from:
        - podSelector:
            matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 14 }}
              app.kubernetes.io/part-of: grafana-mimir
        - podSelector:
            matchLabels:
              {{ template "grafana-mimir.query-scheduler.fullname" . }}-query-scheduler: "true"
        {{- if .Values.queryScheduler.networkPolicy.ingressNSMatchLabels }}
        - namespaceSelector:
            matchLabels:
              {{- range $key, $value := .Values.queryScheduler.networkPolicy.ingressNSMatchLabels }}
              {{ $key | quote }}: {{ $value | quote }}
              {{- end }}
          {{- if .Values.queryScheduler.networkPolicy.ingressNSPodMatchLabels }}
          podSelector:
            matchLabels:
              {{- range $key, $value := .Values.queryScheduler.networkPolicy.ingressNSPodMatchLabels }}
              {{ $key | quote }}: {{ $value | quote }}
              {{- end }}
          {{- end }}
        {{- end }}
      {{- end }}
    {{- if .Values.queryScheduler.networkPolicy.extraIngress }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.queryScheduler.networkPolicy.extraIngress "context" $ ) | nindent 4 }}
    {{- end }}
{{- end }}