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

# Check Zookeeper settings
# if enabled zookeeper's persistence, should set correct storageClass for it
{{- if .Values.governance.zookeeper.persistence.enabled }}
{{- required "Detected .Values.governance.zookeeper.persistence.enabled is true, please set a correct .Values.governance.zookeeper.persistence.storageClass !" .Values.governance.zookeeper.persistence.storageClass -}}
{{- end }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: {{ template "common.names.fullname" . }}
  name: {{ template "common.names.fullname" . }}
spec:
  replicas: {{ .Values.compute.replicas | default 1 }}
  selector:
    matchLabels:
      app: {{ template "common.names.fullname" . }}
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      annotations:
        {{- if .Values.compute.agent.enabled }}
        prometheus.io/path: /metrics
        prometheus.io/port: "9090"
        prometheus.io/scrape: "true"
        {{- end }}
      labels:
        app: {{ template "common.names.fullname" . }}
    spec:
      {{- if .Values.compute.mysqlConnector.version }}
      initContainers:
        - name: download-mysql-jar
          image: busybox:1.36
          command: ["sh","-c","wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/{{.Values.compute.mysqlConnector.version}}/mysql-connector-java-{{.Values.compute.mysqlConnector.version}}.jar;
          wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/{{.Values.compute.mysqlConnector.version}}/mysql-connector-java-{{.Values.compute.mysqlConnector.version}}.jar.md5;
          if [ $(md5sum /mysql-connector-java-{{.Values.compute.mysqlConnector.version}}.jar | cut -d ' ' -f1) = $(cat /mysql-connector-java-{{.Values.compute.mysqlConnector.version}}.jar.md5) ];
          then echo success;
          else echo failed;exit 1;fi;mv /mysql-connector-java-{{.Values.compute.mysqlConnector.version}}.jar /opt/shardingsphere-proxy/ext-lib"]
          volumeMounts:
            - name: mysql-connect-jar
              mountPath: /opt/shardingsphere-proxy/ext-lib
      {{- end }}
        {{- if .Values.compute.agent.enabled }}
        - name: agent-metrics-jar
          image: busybox:1.36
          command: ["sh","-c","wget https://archive.apache.org/dist/shardingsphere/{{ .Values.compute.image.tag | default .Chart.AppVersion }}/apache-shardingsphere-{{ .Values.compute.image.tag | default .Chart.AppVersion }}-shardingsphere-agent-bin.tar.gz;
          mkdir /opt/shardingsphere-proxy/agent;
          tar -zxvf apache-shardingsphere-{{ .Values.compute.image.tag | default .Chart.AppVersion }}-shardingsphere-agent-bin.tar.gz -C /opt/shardingsphere-proxy/agent --strip-component 1"]
          volumeMounts:
            - name: metrics-jar
              mountPath: /opt/shardingsphere-proxy/agent
        {{- if eq .Values.compute.image.tag "5.3.2" }}
        - name: replace-start-script 
          image: "{{ .Values.compute.image.repository }}:{{ .Values.compute.image.tag | default .Chart.AppVersion }}"
          command: ["sh","-c","sed -i 's#exec $JAVA ${JAVA_OPTS} ${JAVA_MEM_OPTS} -classpath ${CLASS_PATH} ${MAIN_CLASS}#exec $JAVA ${JAVA_OPTS} ${JAVA_MEM_OPTS} -classpath ${CLASS_PATH} ${AGENT_PARAM} ${MAIN_CLASS}#g' /opt/shardingsphere-proxy/bin/start.sh; cp /opt/shardingsphere-proxy/bin/start.sh /opt/shardingsphere-proxy/tmpbin/start.sh;"]
          volumeMounts:
            - name: replace-start-script 
              mountPath: /opt/shardingsphere-proxy/tmpbin
        {{- end }}
        {{- end }}
      containers:
        - image: "{{ .Values.compute.image.repository }}:{{ .Values.compute.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.compute.image.pullPolicy }}
          name: shardingsphere-proxy
          startupProbe:
            tcpSocket:
              port: {{ .Values.compute.startPort }}
            failureThreshold: 6
            periodSeconds: 10
          livenessProbe:
            periodSeconds: 5
            tcpSocket:
              port: {{ .Values.compute.startPort }}
          readinessProbe:
            periodSeconds: 5
            tcpSocket:
              port: {{ .Values.compute.startPort }}
          env:
            - name: PORT
              value: "{{ .Values.compute.startPort }}"
            {{- if  .Values.compute.agent.enabled}}
            - name: "JAVA_TOOL_OPTIONS"
              value: "-javaagent:/opt/shardingsphere-proxy/agent/shardingsphere-agent-{{ .Values.compute.image.tag | default .Chart.AppVersion }}.jar"
            {{- end }}
          ports:
            - name: start-port
              containerPort: {{ .Values.compute.startPort }}
          {{- if .Values.compute.resources }}
          resources: {{- toYaml .Values.compute.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- if .Values.compute.agent.enabled}}
            - name: metrics-jar
              mountPath: /opt/shardingsphere-proxy/agent
            - name: agent-config
              mountPath: /opt/shardingsphere-proxy/agent/conf
            {{- if eq .Values.compute.image.tag "5.3.2" }}
            - name: replace-start-script 
              mountPath: /opt/shardingsphere-proxy/bin
            {{- end  }}
            {{- end  }}
            - name: config
              mountPath: /opt/shardingsphere-proxy/conf
            {{- if .Values.compute.mysqlConnector.version }}
            - name: mysql-connect-jar
              mountPath: /opt/shardingsphere-proxy/ext-lib
            {{- end }}
      imagePullSecrets:
      {{- toYaml .Values.compute.imagePullSecrets | nindent 8 }}
      volumes:
       {{- if .Values.compute.agent.enabled}}
       {{- if eq .Values.compute.image.tag "5.3.2" }}
        - name: replace-start-script 
          emptyDir: { }     
       {{- end  }}
       {{- end  }}
        - name: config
          configMap:
            name: {{ printf "%s-configuration" (include "common.names.fullname" .) }}
      {{- if .Values.compute.mysqlConnector.version}}
        - name: mysql-connect-jar
          emptyDir: { }
      {{- end}}
      {{- if .Values.compute.agent.enabled}}
        - name: metrics-jar
          emptyDir: { }
        - name: agent-config
          configMap:
            name: {{ printf "%s-agent-configuration" (include "common.names.fullname" .) }}
            items:
              - key: agent.yaml
                path: agent.yaml
       {{- end}}
