kind: ConfigMap
metadata:
  name: conf-d
  namespace: mysql
apiVersion: v1
data:
  datadir.cnf: |
    [mysqld]
    datadir=/data/db
  galera.cnf: |
    #
    # * Galera-related settings
    #
    # https://mariadb.com/kb/en/mariadb/galera-cluster-system-variables/
    #
    [galera]
    # Mandatory settings
    wsrep_on=ON
    wsrep_provider="/usr/lib/galera/libgalera_smm.so"
    #init-new-cluster#wsrep_new_cluster=TRUE
    #init-recover#wsrep_recover=TRUE
    wsrep_cluster_address="gcomm://mariadb-0.mariadb,mariadb-1.mariadb,mariadb-2.mariadb"
    binlog_format=ROW
    default_storage_engine=InnoDB
    innodb_autoinc_lock_mode=2
    wsrep-sst-method=rsync

    #
    # Allow server to accept connections on all interfaces.
    #
    bind-address=0.0.0.0
    #
    # Optional setting
    #wsrep_slave_threads=1
    #innodb_flush_log_at_trx_commit=0
  utf8.cnf: |
    [client-server]

    # This will be passed to all mysql clients
    [client]
    default-character-set = utf8mb4

    # The MySQL server
    [mysqld]
    character-set-server = utf8mb4
    collation-server = utf8mb4_unicode_ci

    [mysql]
    default-character-set = utf8mb4

    [mysqldump]

    # This group is only read by MariaDB servers, not by MySQL.
    # If you use the same .cnf file for MySQL and MariaDB,
    # you can put MariaDB-only options here
    [mariadb]

    [mariadb-10.1]

    [mariadb-10.2]
  init.sh: |
    #!/bin/bash
    set -x
    
    # copy configs to local directory
    cp -R /mnt/mysql/conf.d /etc/mysql

    HOST_ID=${HOSTNAME##*-}

    STATEFULSET_SERVICE=$(dnsdomainname -d)
    POD_FQDN=$(dnsdomainname -A)

    echo "This is pod $HOST_ID ($POD_FQDN) for statefulset $STATEFULSET_SERVICE"

    [ -z "$DATADIR" ] && exit "Missing DATADIR variable" && exit 1

    SUGGEST_EXEC_COMMAND="kubectl --namespace=$POD_NAMESPACE exec -c init-config $POD_NAME --"

    function wsrepNewCluster {
      sed -i 's|^#init-new-cluster#||' /etc/mysql/conf.d/galera.cnf
    }

    function wsrepRecover {
      sed -i 's|^#init-recover#||' /etc/mysql/conf.d/galera.cnf
    }

    [[ $STATEFULSET_SERVICE = mariadb.* ]] || echo "WARNING: unexpected service name $STATEFULSET_SERVICE, Peer detection below may fail falsely."

    if [ $HOST_ID -eq 0 ]; then
      echo "This is the 1st statefulset pod. Checking if the statefulset is down ..."
      getent hosts mariadb
      [ $? -eq 2 ] && {
        # https://github.com/docker-library/mariadb/commit/f76084f0f9dc13f29cce48c727440eb79b4e92fa#diff-b0fa4b30392406b32de6b8ffe36e290dR80
        if [ ! -d "$DATADIR/mysql" ]; then
          echo "No database in $DATADIR; configuring $POD_NAME for initial start"
          wsrepNewCluster
        else
          set +x
          echo "----- ACTION REQUIRED -----"
          echo "No peers found, but data exists. To start in wsrep_new_cluster mode, run:"
          echo "  $SUGGEST_EXEC_COMMAND touch /tmp/confirm-new-cluster"
          echo "Or to start in recovery mode, to see replication state, run:"
          echo "  $SUGGEST_EXEC_COMMAND touch /tmp/confirm-recover"
          echo "Or to try a regular start (for example after recovery + manual intervention), run:"
          echo "  $SUGGEST_EXEC_COMMAND touch /tmp/confirm-resume"
          echo "Waiting for response ..."
          while [ ! -f /tmp/confirm-resume ]; do
            sleep 1
            if [ "$AUTO_NEW_CLUSTER" = "true" ]; then
              echo "The AUTO_NEW_CLUSTER env was set to $AUTO_NEW_CLUSTER, will proceed without confirmation"
              wsrepNewCluster
              touch /tmp/confirm-resume
            elif [ -f /tmp/confirm-new-cluster ]; then
              echo "Confirmation received. Resuming new cluster start ..."
              wsrepNewCluster
              touch /tmp/confirm-resume
            elif [ -f /tmp/confirm-recover ]; then
              echo "Confirmation received. Resuming in recovery mode."
              echo "Note: to start the other pods you need to edit OrderedReady and add a command: --wsrep-recover"
              wsrepRecover
              touch /tmp/confirm-resume
            fi
          done
          rm /tmp/confirm-*
          set -x
        fi
      }
    fi

    # https://github.com/docker-library/mariadb/blob/master/10.2/docker-entrypoint.sh#L62
    mysqld --verbose --help --log-bin-index="$(mktemp -u)" | tee /tmp/mariadb-start-config | grep -e ^version -e ^datadir -e ^wsrep -e ^binlog -e ^character-set -e ^collation
