{{/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}

{{- if and .Values.cluster.enabled .Values.cluster.liaison }}
apiVersion: apps/v1
kind: Deployment
metadata:
  labels: {{ include "banyandb.labels" . | nindent 4 }}
    app.kubernetes.io/component: liaison
  name: {{ template "banyandb.fullname" . }}-liaison
spec:
  replicas: {{ .Values.cluster.liaison.replicas }}
  selector:
    matchLabels: {{ include "banyandb.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: liaison
  strategy: {{ toYaml .Values.cluster.liaison.updateStrategy | nindent 4 }}
  template:
    metadata:
      labels: {{ include "banyandb.labels" . | nindent 8 }}
        app.kubernetes.io/component: liaison
      {{- if .Values.cluster.liaison.podAnnotations }}
      annotations:
{{ toYaml .Values.cluster.liaison.podAnnotations | indent 8 }}
      {{- end }}
    spec:
      serviceAccountName: {{ template "banyandb.serviceAccountName" . }}
      {{- with .Values.cluster.liaison.securityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      priorityClassName: {{ .Values.cluster.liaison.priorityClassName }}
      containers:
        - name: liaison
          {{- if eq .Values.cluster.ui.type "Embedded"  }}
          image: {{ .Values.image.repository }}:{{ required "banyandb.image.tag is required" .Values.image.tag }}
          {{- else }}
          image: {{ .Values.image.repository }}:{{ required "banyandb.image.tag is required" .Values.image.tag }}-slim
          {{- end }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            {{- range $env := .Values.cluster.liaison.env }}
            - name: {{ $env.name }}
              value: {{ $env.value }}
            {{- end }}
            {{- if .Values.cluster.liaison.tls }}
            {{- if .Values.cluster.liaison.tls.grpcSecretName }}
            - name: BYDB_TLS
              value: "true"
            - name: BYDB_CERT_FILE
              value: "/etc/tls/{{ .Values.cluster.liaison.tls.grpcSecretName }}/tls.crt"
            - name: BYDB_KEY_FILE
              value: "/etc/tls/{{ .Values.cluster.liaison.tls.grpcSecretName }}/tls.key"
            - name: BYDB_HTTP_GRPC_CERT_FILE
              value: "/etc/tls/{{ .Values.cluster.liaison.tls.grpcSecretName }}/tls.crt"
            {{- end }}
            {{- if .Values.cluster.liaison.tls.httpSecretName }}
            - name: BYDB_HTTP_TLS
              value: "true"
            - name: BYDB_HTTP_CERT_FILE
              value: "/etc/tls/{{ .Values.cluster.liaison.tls.httpSecretName }}/tls.crt"
            - name: BYDB_HTTP_KEY_FILE
              value: "/etc/tls/{{ .Values.cluster.liaison.tls.httpSecretName }}/tls.key"
            {{- end }}
            {{- end }}
            {{- if and .Values.etcd.auth.rbac.create (not .Values.etcd.auth.rbac.allowNoneAuthentication) }}
            - name: BYDB_ETCD_USERNAME
              value: "root"
            - name: BYDB_ETCD_PASSWORD
              value: {{ .Values.etcd.auth.rbac.rootPassword }}
            {{- end }}
            {{- if .Values.etcd.auth.client.secureTransport }}
            - name: BYDB_ETCD_TLS_CA_FILE
              value: "/etc/tls/{{ .Values.cluster.liaison.tls.etcdSecretName }}/ca.crt"
            {{- end }}
            {{- if .Values.etcd.auth.client.enableAuthentication }}
            - name: BYDB_ETCD_TLS_CERT_FILE
              value: "/etc/tls/{{ .Values.cluster.liaison.tls.etcdSecretName }}/tls.crt"
            - name: BYDB_ETCD_TLS_KEY_FILE
              value: "/etc/tls/{{ .Values.cluster.liaison.tls.etcdSecretName }}/tls.key"
            {{- end }}
            {{- if and (not .Values.etcd.enabled) .Values.cluster.etcdEndpoints }}
            - name: BYDB_ETCD_ENDPOINTS
              value: "{{- .Values.cluster.etcdEndpoints | join "," -}}"
            {{- else }}
            {{- include "banyandb.etcdEndpoints" . | nindent 12 }}
            {{- end }}
          args:
            - liaison
          ports:
            - containerPort: 17912
              name: grpc
            - containerPort: 17913
              name: http
            - containerPort: 6060
              name: pprof
            - containerPort: 2121
              name: observebility
          readinessProbe:
            httpGet:
              path: /api/healthz
              port: 17913
              {{- if .Values.cluster.liaison.tls }}
              scheme: HTTPS
              {{- else }}
              scheme: HTTP
              {{- end }}
            initialDelaySeconds: {{ .Values.cluster.liaison.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.cluster.liaison.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.cluster.liaison.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.cluster.liaison.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.cluster.liaison.readinessProbe.failureThreshold }}
          livenessProbe:
            httpGet:
              path: /api/healthz
              port: 17913
              {{- if .Values.cluster.liaison.tls }}
              scheme: HTTPS
              {{- else }}
              scheme: HTTP
              {{- end }}
            initialDelaySeconds: {{ .Values.cluster.liaison.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.cluster.liaison.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.cluster.liaison.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.cluster.liaison.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.cluster.liaison.livenessProbe.failureThreshold }}
          startupProbe:
            httpGet:
              path: /api/healthz
              port: 17913
              {{- if .Values.cluster.liaison.tls }}
              scheme: HTTPS
              {{- else }}
              scheme: HTTP
              {{- end }}
            initialDelaySeconds: {{ .Values.cluster.liaison.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.cluster.liaison.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.cluster.liaison.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.cluster.liaison.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.cluster.liaison.startupProbe.failureThreshold }}
          {{- if.Values.cluster.liaison.resources }}
          resources:
            {{- if.Values.cluster.liaison.resources.requests }}
            requests:
              {{- range $request := .Values.cluster.liaison.resources.requests }}
              {{ $request.key }}: {{ $request.value }}
              {{- end }}
            {{- end }}
            {{- if.Values.cluster.liaison.resources.limits }}
            limits:
              {{- range $limit := .Values.cluster.liaison.resources.limits }}
              {{ $limit.key }}: {{ $limit.value }}
              {{- end }}
            {{- end }}
          {{- end }}

          {{- if .Values.cluster.liaison.tls }}
          volumeMounts:
            {{- if .Values.cluster.liaison.tls.grpcSecretName }}
            - mountPath: /etc/tls/{{ .Values.cluster.liaison.tls.grpcSecretName }}
              name: {{ .Values.cluster.liaison.tls.grpcSecretName }}-volume
            {{- end }}
            {{- if .Values.cluster.liaison.tls.etcdSecretName }}
            - mountPath: /etc/tls/{{ .Values.cluster.liaison.tls.etcdSecretName }}
              name: {{ .Values.cluster.liaison.tls.etcdSecretName }}-volume
            {{- end }}
            {{- if and .Values.cluster.liaison.tls.httpSecretName (ne .Values.cluster.liaison.tls.httpSecretName .Values.cluster.liaison.tls.grpcSecretName) }}
            - mountPath: /etc/tls/{{ .Values.cluster.liaison.tls.httpSecretName }}
              name: {{ .Values.cluster.liaison.tls.httpSecretName }}-volume
            {{- end }}
          {{- end }}

      {{- if .Values.cluster.liaison.tls }}
      volumes:
        {{- if .Values.cluster.liaison.tls.grpcSecretName }}
        - name: {{ .Values.cluster.liaison.tls.grpcSecretName }}-volume
          secret:
            secretName: {{ .Values.cluster.liaison.tls.grpcSecretName }}
        {{- end }}
        {{- if .Values.cluster.liaison.tls.etcdSecretName }}
        - name: {{ .Values.cluster.liaison.tls.etcdSecretName }}-volume
          secret:
            secretName: {{ .Values.cluster.liaison.tls.etcdSecretName }}
        {{- end }}
        {{- if and .Values.cluster.liaison.tls.httpSecretName (ne .Values.cluster.liaison.tls.httpSecretName .Values.cluster.liaison.tls.grpcSecretName) }}
        - name: {{ .Values.cluster.liaison.tls.httpSecretName }}-volume
          secret:
            secretName: {{ .Values.cluster.liaison.tls.httpSecretName }}
        {{- end }}
      {{- end }}

      {{- if .Values.cluster.liaison.tolerations }}
      tolerations:
        {{- range $toleration := .Values.cluster.liaison.tolerations }}
        - key: {{ $toleration.key }}
          operator: {{ $toleration.operator }}
          value: {{ $toleration.value }}
          effect: {{ $toleration.effect }}
        {{- end }}
      {{- end }}

      {{- if .Values.cluster.liaison.nodeSelector }}
      nodeSelector:
        {{- range $selector := .Values.cluster.liaison.nodeSelector }}
        {{ $selector.key }}: {{ $selector.value }}
        {{- end }}
      {{- end }}

      {{- if .Values.cluster.liaison.affinity }}
      affinity: {{ toYaml .Values.cluster.liaison.affinity | nindent 8 }}
      {{- else if or .Values.cluster.liaison.podAffinityPreset .Values.cluster.liaison.podAntiAffinityPreset }}
      affinity:
        {{- if and .Values.cluster.liaison.podAffinityPreset (not (empty .Values.cluster.liaison.podAffinityPreset)) }}
        podAffinity:
          {{- if eq .Values.cluster.liaison.podAffinityPreset "soft" }}
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchLabels: {{ include "banyandb.selectorLabels" . | nindent 20 }}
                    app.kubernetes.io/component: liaison
                topologyKey: kubernetes.io/hostname
          {{- else if eq .Values.cluster.liaison.podAffinityPreset "hard" }}
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                labelSelector:
                  matchLabels: {{ include "banyandb.selectorLabels" . | nindent 20 }}
                    app.kubernetes.io/component: liaison
              topologyKey: kubernetes.io/hostname
          {{- end }}
        {{- end }}
        {{- if and .Values.cluster.liaison.podAntiAffinityPreset (not (empty .Values.cluster.liaison.podAntiAffinityPreset)) }}
        podAntiAffinity:
          {{- if eq .Values.cluster.liaison.podAntiAffinityPreset "soft" }}
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchLabels: {{ include "banyandb.selectorLabels" . | nindent 20 }}
                    app.kubernetes.io/component: liaison
                topologyKey: kubernetes.io/hostname
          {{- else if eq .Values.cluster.liaison.podAntiAffinityPreset "hard" }}
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                labelSelector:
                  matchLabels: {{ include "banyandb.selectorLabels" . | nindent 20 }}
                    app.kubernetes.io/component: liaison
              topologyKey: kubernetes.io/hostname
          {{- end }}
        {{- end }}
      {{- end }}
{{- end }}
