---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/name: mysql
    app.kubernetes.io/part-of: nextcloud
    app.kubernetes.io/component: database
  name: mysql
  namespace: nextcloud
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: mysql
      app.kubernetes.io/part-of: nextcloud
      app.kubernetes.io/component: database
  revisionHistoryLimit: 3
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kubectl.kubernetes.io/default-container: mysql
      labels:
        app.kubernetes.io/name: mysql
        app.kubernetes.io/part-of: nextcloud
        app.kubernetes.io/component: database
    spec:
      containers:
      - env:
        - name: MYSQL_DATABASE
          value: "cloud"
        envFrom:
        - secretRef:
            name: mysql-creds
        image: mariadb:10.4.12
        imagePullPolicy: IfNotPresent
        name: mysql
        args:
        - '--transaction-isolation=READ-COMMITTED'
        - '--max-allowed-packet=33554432'
        ports:
        - containerPort: 3306
          name: mysql
        livenessProbe:
          exec:
            command:
            - sh
            - -c
            - "mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}"
          initialDelaySeconds: 30
          timeoutSeconds: 5
        readinessProbe:
          exec:
            command:
            - sh
            - -c
            - "mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}"
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: data
        - mountPath: /docker-entrypoint-initdb.d
          name: init
        resources:
          requests:
            cpu: 45m
            memory: 410Mi
      - env:
        - name: CRON_SCHEDULE
          value: "5 3 * * *"
        - name: MYSQL_HOST
          value: "localhost"
        - name: MYSQL_USER
          value: "root"
        - name: MYSQL_HOST
          value: "127.0.0.1"
        - name: MYSQL_PORT
          value: "3306"
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              key: MYSQL_ROOT_PASSWORD
              name: mysql-creds
        image: woolfg/mysql-backup-sidecar:v0.3.4-mariadb-10.4
        name: backup
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: data
        - mountPath: /backup
          name: backup
      - env:
        - name: DATA_SOURCE_NAME
          value: "$(EXPORTER_USER):$(EXPORTER_PASS)@(127.0.0.1:3306)/"
        envFrom:
        - secretRef:
            name: mysql-init
        image: quay.io/prometheus/mysqld-exporter:latest
        imagePullPolicy: IfNotPresent
        name: exporter
        args:
        - "--collect.info_schema.innodb_metrics"
        - "--collect.info_schema.userstats"
        - "--collect.perf_schema.eventsstatements"
        - "--collect.perf_schema.indexiowaits"
        - "--collect.perf_schema.tableiowaits"
        ports:
        - containerPort: 9104
          name: metrics
        resources:
          requests:
            cpu: 2m
            memory: 12Mi
          limits:
            memory: 20Mi
      restartPolicy: Always
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: mysql-data
      - name: backup
        persistentVolumeClaim:
          claimName: mysql-backup
      - name: init
        secret:
          secretName: mysql-init
      nodeSelector:
        kubernetes.io/hostname: "metal01"  # TODO: migrate to galera or distributed postgres
        # storage.infra/raid: "true"
      tolerations: []
