#
# 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.
#
---
# database statefulset
{{- if eq .Values.option.database "mysql" }}
{{- if not .Values.mysql.useExternal }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "devlake.fullname" . }}-mysql
  labels:
    {{- include "devlake.labels" . | nindent 4 }}
spec:
  replicas: 1
  serviceName: {{ include "devlake.fullname" . }}-mysql
  selector:
    matchLabels:
      {{- include "devlake.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "devlake.selectorLabels" . | nindent 8 }}
        devlakeComponent: mysql
        {{- with .Values.mysql.extraLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- with .Values.mysql.securityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: mysql
          image: "{{ .Values.mysql.image.repository }}:{{ .Values.mysql.image.tag }}"
          imagePullPolicy: {{ .Values.mysql.image.pullPolicy }}
          args:
            - "mysqld"
            - "--character-set-server=utf8mb4"
            - "--collation-server=utf8mb4_bin"
            - "--skip-log-bin"
          ports:
            - name: mysql
              containerPort: 3306
              protocol: TCP
          livenessProbe:
            exec:
              command:
                - "sh"
                - "-c"
                - "mysqladmin ping -u root -p$MYSQL_ROOT_PASSWORD"
            initialDelaySeconds: 60
            timeoutSeconds: 30
          readinessProbe:
            exec:
              command:
                - "sh"
                - "-c"
                - "mysqladmin ping -u root -p$MYSQL_ROOT_PASSWORD"
            initialDelaySeconds: 5
            timeoutSeconds: 10
          {{- with .Values.mysql.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          envFrom:
            - secretRef:
                name: {{ include "devlake.mysql.secret" . }}
          {{- with .Values.envs }}
          env:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          volumeMounts:
            - mountPath: /var/lib/mysql
              name: {{ include "devlake.fullname" . }}-mysql-data
          {{- with .Values.mysql.containerSecurityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      {{- with .Values.mysql.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.mysql.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.mysql.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
  volumeClaimTemplates:
    - metadata:
        name: {{ include "devlake.fullname" . }}-mysql-data
      spec:
        accessModes: ["ReadWriteOnce"]
        {{- with .Values.mysql.storage.class }}
        storageClassName: "{{ . }}"
        {{- end }}
        resources:
          requests:
            storage: "{{ .Values.mysql.storage.size }}"
{{- end }}
{{- end }}


