apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql-master
  labels:
    app: mysql
    component: master
spec:
  selector:
    matchLabels:
      app: mysql
      component: master
  serviceName: mysql-master
  replicas: 1
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: mysql
        component: master
    spec:      
      securityContext:
        fsGroup: 1001
        runAsUser: 1001
      serviceAccountName: default
      containers:
        - name: mysql
          image: docker.io/bitnami/mysql:8.0.20-debian-10-r59
          imagePullPolicy: "IfNotPresent"
          env:
            - name: BITNAMI_DEBUG
              value: "false"
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql
                  key: mysql-root-password
            - name: MYSQL_USER
              value: "mysqluser"
            - name: MYSQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql
                  key: mysql-password
            - name: MYSQL_DATABASE
              value: "dataflow"
          ports:
            - name: mysql
              containerPort: 3306
          livenessProbe:
            exec:
              command:
                - sh
                - -c
                - |
                  password_aux="${MYSQL_ROOT_PASSWORD:-}"
                  if [ -f "${MYSQL_ROOT_PASSWORD_FILE:-}" ]; then
                      password_aux=$(cat $MYSQL_ROOT_PASSWORD_FILE)
                  fi
                  mysqladmin status -uroot -p$password_aux
            initialDelaySeconds: 120
            periodSeconds: 10
            timeoutSeconds: 1
            successThreshold: 1
            failureThreshold: 3
          readinessProbe:
            exec:
              command:
                - sh
                - -c
                - |
                  password_aux="${MYSQL_ROOT_PASSWORD:-}"
                  if [ -f "${MYSQL_ROOT_PASSWORD_FILE:-}" ]; then
                      password_aux=$(cat $MYSQL_ROOT_PASSWORD_FILE)
                  fi
                  mysqladmin status -uroot -p$password_aux
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 1
            successThreshold: 1
            failureThreshold: 3
          resources:
            limits: {}
            requests: {}
          volumeMounts:
            - name: data
              mountPath: /bitnami/mysql
            - name: config
              mountPath: /opt/bitnami/mysql/conf/my.cnf
              subPath: my.cnf
      volumes:
        - name: config
          configMap:
            name: mysql-master
  volumeClaimTemplates:
    - metadata:
        name: data
        labels:
          app: mysql
          component: master
      spec:
        accessModes:
          - "ReadWriteOnce"
        resources:
          requests:
            storage: "8Gi"
