apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "ksmysql.fullname" . }}
  labels:
    app: {{ template "ksmysql.name" . }}
    tier: {{ template "ksmysql.name" . }}
    chart: {{ template "ksmysql.name" . }}
    release: {{ .Release.Name | quote }}
    {{- include "ksmysql.labels" . | nindent 4 }}
spec:
  serviceName: "ksmysql"
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "ksmysql.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        release: {{ .Release.Name | quote }}
        {{- include "ksmysql.selectorLabels" . | nindent 8 }}
    spec:
      containers:
        - image: knowstreaming/knowstreaming-mysql:0.7.0
          name: {{ .Chart.Name }}
          env:
            - name: MYSQL_DATABASE
              value: {{ .Values.mysql.dbname }}
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  key: rootPassword
                  name: {{ include "ksmysql.fullname" . }}
            - name: MYSQL_ROOT_HOST
              valueFrom:
                secretKeyRef:
                  key: rootHost
                  name: {{ include "ksmysql.fullname" . }}
            - name: TZ
              value: Asia/Shanghai
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:
            - name: data
              mountPath: /data
            - name: configmap
              mountPath: /etc/my.cnf
              subPath: my.cnf
      volumes:
        - name: configmap
          configMap:
            name: {{ include "ksmysql.fullname" . }}
  {{- if .Values.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          - {{ .Values.persistence.accessMode | quote }}
        resources:
          requests:
            storage: {{ .Values.persistence.size | quote }}
      {{- if .Values.persistence.storageClass }}
        {{- if (eq "-" .Values.persistence.storageClass) }}
        storageClassName: ""
        {{- else }}
        storageClassName: "{{ .Values.persistence.storageClass }}"
        {{- end }}
      {{- end }}
  {{- end }}
