# 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.

# This yaml have dorisCluster CRD all fields. The yaml only for reference how to config, not for deploy on kubernetes.

apiVersion: doris.selectdb.com/v1
kind: DorisCluster
metadata:
  name: {{ template "doriscluster.name" . }}
  namespace: {{ template "doriscluster.namespace" . }}
  labels:
    app.kubernetes.io/name: doriscluster
    app.kubernetes.io/instance: {{ template "doriscluster.name" . }}
    app.kubernetes.io/part-of: {{ template "kube-control.name" . }}
  {{- if .Values.dorisCluster.annotations }}
  annotations:
    {{- toYaml .Values.dorisCluster.annotations | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.dorisCluster.adminUser }}
  adminUser:
    {{- toYaml .Values.dorisCluster.adminUser | nindent 4 }}
  {{- else if .Values.dorisCluster.authSecret }}
  authSecret: {{ template "doriscluster.secret.name" . }}
  {{- end }}
  feSpec:
    replicas: {{ .Values.feSpec.replicas }}
    {{- if .Values.feSpec.electionNumber }}
    electionNumber: {{ .Values.feSpec.electionNumber }}
    {{- end }}
    {{- if .Values.feSpec.labels }}
    podLabels:
    {{- toYaml .Values.feSpec.labels | nindent 6 }}
    {{- end }}
    image: {{ .Values.feSpec.image.repository }}:{{ .Values.feSpec.image.tag }}
    {{- if .Values.feSpec.image.imagePullSecrets }}
    imagePullSecrets:
    {{- toYaml .Values.feSpec.image.imagePullSecrets | nindent 4 }}
    {{- end }}

    {{- if .Values.feSpec.service }}
    service:
      type: {{ .Values.feSpec.service.type }}
      {{- if and (eq "LoadBalancer" .Values.feSpec.service.type) .Values.feSpec.service.loadbalancerIP }}
      loadBalancerIP: {{ .Values.feSpec.service.loadbalancerIP }}
      {{- end }}
      {{- if .Values.feSpec.service.servicePorts }}
      servicePorts:
      {{- toYaml .Values.feSpec.service.servicePorts | nindent 6 }}
      {{- end }}
      {{- if .Values.feSpec.service.annotations }}
      annotations:
      {{- toYaml .Values.feSpec.service.annotations | nindent 8 }}
      {{- end }}
    {{- end }}

    {{- if .Values.feSpec.configMap }}
    configMapInfo:
    {{- $originalMap := .Values.feSpec.configMap }}
    {{- $defaultMap := omit $originalMap "mountConfigMaps" }}
    {{- if $defaultMap }}
      configMapName: {{ template "doriscluster.default.feConfigMap.name" . }}
      resolveKey: {{ template "doriscluster.default.feConfig.resolveKey" . }}
    {{- end }}
    {{- if .Values.feSpec.configMap.mountConfigMaps }}
      configMaps:
        {{- template "doriscluster.feConfig.configMaps" . }}
    {{- end }}
    {{- end }}

    {{- if .Values.feSpec.resource }}
    {{- toYaml .Values.feSpec.resource | nindent 4 }}
    {{- else }}
    {{- include "doriscluster.default.resource" .}}
    {{- end }}


    {{- if .Values.feSpec.nodeSelector }}
    nodeSelector:
    {{- toYaml .Values.feSpec.nodeSelector | nindent 6 }}
    {{- end }}
    {{- if .Values.feSpec.env }}
    envVars:
    {{- toYaml .Values.feSpec.env | nindent 4 }}
    {{- end }}
    {{- if .Values.feSpec.affinity }}
    affinity:
    {{- toYaml .Values.feSpec.affinity | nindent 6 }}
    {{- end }}
    {{- if .Values.feSpec.tolerations }}
    tolerations:
    {{- toYaml .Values.feSpec.tolerations | nindent 4 }}
    {{- end }}
    {{- if .Values.feSpec.hostAliases }}
    hostAliases:
    {{- toYaml .Values.feSpec.hostAliases | nindent 4 }}
    {{- end }}
    {{- if .Values.feSpec.persistentVolumeClaim }}
    persistentVolumes:
    {{- template "doriscluster.fe.pvc" . }}
    {{- end }}
    {{- if .Values.feSpec.systemInitialization }}
    systemInitialization:
    {{- if .Values.feSpec.systemInitialization.initImage }}
      initImage: {{ .Values.feSpec.systemInitialization.initImage }}
    {{- end }}
    {{- if .Values.feSpec.systemInitialization.command }}
      command: {{ .Values.feSpec.systemInitialization.command }}
    {{- end }}
    {{- end }}
  beSpec:
    replicas: {{ .Values.beSpec.replicas }}
    {{- if .Values.beSpec.labels }}
    podLabels:
    {{- toYaml .Values.beSpec.labels | nindent 6 }}
    {{- end }}
    image: {{ .Values.beSpec.image.repository }}:{{ .Values.beSpec.image.tag }}
    {{- if .Values.beSpec.image.imagePullSecrets }}
    imagePullSecrets:
    {{- toYaml .Values.beSpec.image.imagePullSecrets | nindent 4 }}
    {{- end }}

    {{- if .Values.beSpec.service }}
    service:
      type: {{ .Values.beSpec.service.type }}
      {{- if and (eq "LoadBalancer" .Values.beSpec.service.type) .Values.beSpec.service.loadbalancerIP }}
      loadBalancerIP: {{ .Values.beSpec.service.loadbalancerIP }}
      {{- end }}
      {{- if .Values.beSpec.service.servicePorts }}
      servicePorts:
        {{- toYaml .Values.beSpec.service.servicePorts | nindent 6 }}
      {{- end }}
      {{- if .Values.beSpec.service.annotations }}
      annotations:
      {{- toYaml .Values.beSpec.service.annotations | nindent 8 }}
      {{- end }}
    {{- end }}

    {{- if .Values.beSpec.configMap }}
    configMapInfo:
    {{- $originalMap := .Values.beSpec.configMap }}
    {{- $defaultMap := omit $originalMap "mountConfigMaps" }}
    {{- if $defaultMap }}
      configMapName: {{ template "doriscluster.default.beConfigMap.name" . }}
      resolveKey: {{ template "doriscluster.default.beConfig.resolveKey" . }}
    {{- end }}
    {{- if .Values.beSpec.configMap.mountConfigMaps }}
      configMaps:
        {{- template "doriscluster.beConfig.configMaps" . }}
    {{- end }}
    {{- end }}

    {{- if .Values.beSpec.resource }}
    {{- toYaml .Values.beSpec.resource | nindent 4 }}
    {{- else }}
    {{- include "doriscluster.default.resource" .}}
    {{- end }}

    {{- if .Values.beSpec.nodeSelector }}
    nodeSelector:
    {{- toYaml .Values.beSpec.nodeSelector | nindent 6 }}
    {{- end }}
    {{- if .Values.beSpec.env }}
    envVars:
    {{- toYaml .Values.beSpec.env | nindent 4 }}
    {{- end }}
    {{- if .Values.beSpec.affinity }}
    affinity:
    {{- toYaml .Values.beSpec.affinity | nindent 6 }}
    {{- end }}
    {{- if .Values.beSpec.tolerations }}
    tolerations:
    {{- toYaml .Values.beSpec.tolerations | nindent 4 }}
    {{- end }}
    {{- if .Values.beSpec.hostAliases }}
    hostAliases:
    {{- toYaml .Values.beSpec.hostAliases | nindent 4 }}
    {{- end }}
    {{- if .Values.beSpec.persistentVolumeClaim }}
    persistentVolumes:
    {{- template "doriscluster.be.pvc" . }}
    {{- end }}

    {{- if .Values.beSpec.systemInitialization }}
    systemInitialization:
    {{- if .Values.beSpec.systemInitialization.initImage }}
      initImage: {{ .Values.beSpec.systemInitialization.initImage }}
    {{- end }}
    {{- if .Values.beSpec.systemInitialization.command }}
      command: {{ .Values.beSpec.systemInitialization.command }}
    {{- end }}
    {{- end }}
  {{- if .Values.dorisCluster.enabledCn }}
  cnSpec:
    replicas: {{ .Values.cnSpec.replicas }}

    {{- if .Values.cnSpec.labels }}
    podLabels:
    {{- toYaml .Values.cnSpec.labels | nindent 6 }}
    {{- end }}
    image: {{ .Values.cnSpec.image.repository }}:{{ .Values.cnSpec.image.tag }}
    {{- if .Values.cnSpec.image.imagePullSecrets }}
    imagePullSecrets:
    {{- toYaml .Values.cnSpec.image.imagePullSecrets | nindent 4 }}
    {{- end }}

    {{- if .Values.cnSpec.service }}
    service:
      type: {{ .Values.cnSpec.service.type }}
      {{- if and (eq "LoadBalancer" .Values.cnSpec.service.type) .Values.cnSpec.service.loadbalancerIP }}
      loadBalancerIP: {{ .Values.cnSpec.service.loadbalancerIP }}
      {{- end }}
      {{- if .Values.cnSpec.service.servicePorts }}
      servicePorts:
        {{- toYaml .Values.cnSpec.service.servicePorts | nindent 6 }}
      {{- end }}
      {{- if .Values.cnSpec.service.annotations }}
      annotations:
      {{- toYaml .Values.cnSpec.service.annotations | nindent 8 }}
      {{- end }}
    {{- end }}

    {{- if .Values.cnSpec.configMap }}
    configMapInfo:
    {{- $originalMap := .Values.cnSpec.configMap }}
    {{- $defaultMap := omit $originalMap "mountConfigMaps" }}
    {{- if $defaultMap }}
      configMapName: {{ template "doriscluster.default.cnConfigMap.name" . }}
      resolveKey: {{ template "doriscluster.default.cnConfig.resolveKey" . }}
    {{- end }}
    {{- if .Values.cnSpec.configMap.mountConfigMaps }}
      configMaps:
        {{- template "doriscluster.cnConfig.configMaps" . }}
    {{- end }}
    {{- end }}

    {{- if .Values.cnSpec.resource }}
    {{- toYaml .Values.cnSpec.resource | nindent 4 }}
    {{- else }}
    {{- include "doriscluster.default.resource" .}}
    {{- end }}

    {{- if .Values.cnSpec.nodeSelector }}
    nodeSelector:
    {{- toYaml .Values.cnSpec.nodeSelector | nindent 6 }}
    {{- end }}
    {{- if .Values.cnSpec.env }}
    envVars:
    {{- toYaml .Values.cnSpec.env | nindent 4 }}
    {{- end }}
    {{- if .Values.cnSpec.affinity }}
    affinity:
    {{- toYaml .Values.cnSpec.affinity | nindent 6 }}
    {{- end }}
    {{- if .Values.cnSpec.tolerations }}
    tolerations:
    {{- toYaml .Values.cnSpec.tolerations | nindent 4 }}
    {{- end }}
    {{- if .Values.cnSpec.hostAliases }}
    hostAliases:
    {{- toYaml .Values.cnSpec.hostAliases | nindent 4 }}
    {{- end }}

    {{- if .Values.cnSpec.persistentVolumeClaim }}
    persistentVolumes:
    {{- template "doriscluster.cn.pvc" . }}
    {{- end }}
    {{- if .Values.cnSpec.systemInitialization }}
    systemInitialization:
    {{- if .Values.cnSpec.systemInitialization.initImage }}
      initImage: {{ .Values.cnSpec.systemInitialization.initImage }}
    {{- end }}
    {{- if .Values.cnSpec.systemInitialization.command }}
      command: {{ .Values.cnSpec.systemInitialization.command }}
    {{- end }}
    {{- end }}
    {{- if .Values.cnSpec.autoScalingPolicy.enable }}
    autoScalingPolicy:
      version: {{default ( include "doriscluster.default.autoScalerVersion" . ) .Values.cnSpec.autoScalingPolicy.version }}
      minReplicas: {{ .Values.cnSpec.autoScalingPolicy.minReplicas}}
      maxReplicas: {{ .Values.cnSpec.autoScalingPolicy.maxReplicas}}
      hpaPolicy:
      {{- toYaml .Values.cnSpec.autoScalingPolicy.hpaPolicy | nindent 8 }}
    {{- end }}
  {{- end }}

  {{- if .Values.dorisCluster.enabledBroker }}
  brokerSpec:
    replicas: {{ .Values.brokerSpec.replicas }}

    {{- if .Values.brokerSpec.labels }}
    podLabels:
    {{- toYaml .Values.brokerSpec.labels | nindent 6 }}
    {{- end }}
    image: {{ .Values.brokerSpec.image.repository }}:{{ .Values.brokerSpec.image.tag }}
    {{- if .Values.brokerSpec.image.imagePullSecrets }}
    imagePullSecrets:
    {{- toYaml .Values.brokerSpec.image.imagePullSecrets | nindent 4 }}
    {{- end }}

    {{- if .Values.brokerSpec.configMap }}
    configMapInfo:
    {{- $originalMap := .Values.brokerSpec.configMap }}
    {{- $defaultMap := omit $originalMap "mountConfigMaps" }}
    {{- if $defaultMap }}
      configMapName: {{ template "doriscluster.default.brokerConfigMap.name" . }}
      resolveKey: {{ template "doriscluster.default.brokerConfig.resolveKey" . }}
    {{- end }}
    {{- if .Values.brokerSpec.configMap.mountConfigMaps }}
      configMaps:
        {{- template "doriscluster.brokerConfig.configMaps" . }}
    {{- end }}
    {{- end }}

    {{- if .Values.brokerSpec.resource }}
    {{- toYaml .Values.brokerSpec.resource | nindent 4 }}
    {{- else }}
    {{- include "doriscluster.default.resource" .}}
    {{- end }}

    {{- if .Values.brokerSpec.nodeSelector }}
    nodeSelector:
    {{- toYaml .Values.brokerSpec.nodeSelector | nindent 6 }}
    {{- end }}
    {{- if .Values.brokerSpec.env }}
    envVars:
    {{- toYaml .Values.brokerSpec.env | nindent 4 }}
    {{- end }}
    {{- if .Values.brokerSpec.affinity }}
    affinity:
    {{- toYaml .Values.brokerSpec.affinity | nindent 6 }}
    {{- end }}
    {{- if .Values.brokerSpec.tolerations }}
    tolerations:
    {{- toYaml .Values.brokerSpec.tolerations | nindent 4 }}
    {{- end }}
    {{- if .Values.brokerSpec.hostAliases }}
    hostAliases:
    {{- toYaml .Values.brokerSpec.hostAliases | nindent 4 }}
    {{- end }}
    {{- if .Values.brokerSpec.persistentVolumeClaim }}
    persistentVolumes:
    {{- template "doriscluster.broker.pvc" . }}
    {{- end }}
    {{- if .Values.brokerSpec.systemInitialization }}
    systemInitialization:
    {{- if .Values.brokerSpec.systemInitialization.initImage }}
      initImage: {{ .Values.brokerSpec.systemInitialization.initImage }}
    {{- end }}
    {{- if .Values.brokerSpec.systemInitialization.command }}
      command: {{ .Values.brokerSpec.systemInitialization.command }}
    {{- end }}
    {{- end }}
  {{- end }}
