apiVersion: v1
kind: ConfigMap
metadata:
  name: proxysql-configmap
  namespace: proxysql
data: 
  proxysql.cnf: |
    datadir="/var/lib/proxysql"

    admin_variables=
    {
        admin_credentials="admin:{{ADMIN_PASSWORD}};cluster:{{CLUSTER_PASSWORD}}"
        mysql_ifaces="0.0.0.0:6032"
        refresh_interval=2000
        cluster_username="cluster"
        cluster_password="{{CLUSTER_PASSWORD}}"
    }

    mysql_variables=
    {
        threads=4
        max_connections=2048
        default_query_delay=0
        default_query_timeout=36000000
        have_compress=true
        poll_timeout=2000
        interfaces="0.0.0.0:6033;/tmp/proxysql.sock"
        default_schema="information_schema"
        stacksize=1048576
        server_version="8.0.23"
        connect_timeout_server=3000
        monitor_username="proxysql"
        monitor_password="{{MONITOR_PASSWORD}}"
        monitor_history=600000
        monitor_connect_interval=60000
        monitor_ping_interval=10000
        monitor_read_only_interval=1500
        monitor_read_only_timeout=500
        ping_interval_server_msec=120000
        ping_timeout_server=500
        commands_stats=true
        sessions_sort=true
        connect_retries_on_failure=10
    }

    mysql_galera_hostgroups =
    (
        {

            writer_hostgroup=10
            backup_writer_hostgroup=20
            reader_hostgroup=30
            offline_hostgroup=9999
            max_writers=1
            writer_is_also_reader=2
            max_transactions_behind=30
            active=1
        }
    )
    mysql_query_rules =
    (
        {
            rule_id=100
            active=1
            match_pattern="^SELECT .* FOR UPDATE"
            destination_hostgroup=10
            apply=1
        },
        {
            rule_id=200
            active=1
            match_pattern="^SELECT .*"
            destination_hostgroup=30
            apply=1
        },
        {
            rule_id=300
            active=1
            match_pattern=".*"
            destination_hostgroup=10
            apply=1
        }
    )
    mysql_servers =
    (
        { address="multinode-db1" , port=3306 , hostgroup=10, max_connections=100 },
        { address="multinode-db2" , port=3306 , hostgroup=20, max_connections=100 },
        { address="multinode-db3" , port=3306 , hostgroup=30, max_connections=100 }
    )

    mysql_users =
    (
      {
          username="nextcloud_antagning"
          password="{{ANTAGNING_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_bth"
          password="{{BTH_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_chalmers"
          password="{{CHALMERS_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_du"
          password="{{DU_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_esh"
          password="{{ESH_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_fhs"
          password="{{FHS_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_gu"
          password="{{GU_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_hb"
          password="{{HB_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_hhs"
          password="{{HHS_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_hig"
          password="{{HIG_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_his"
          password="{{HIS_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_hj"
          password="{{HJ_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_hv"
          password="{{HV_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_irf"
          password="{{IRF_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_kb"
          password="{{KB_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_ki"
          password="{{KI_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_kkh"
          password="{{KKH_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_kmh"
          password="{{KMH_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_konstfack"
          password="{{KONSTFACK_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_kth"
          password="{{KTH_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_kva"
          password="{{KVA_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_liu"
          password="{{LIU_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_lnu"
          password="{{LNU_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_ltu"
          password="{{LTU_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_lu"
          password="{{LU_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_mau"
          password="{{MAU_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_miun"
          password="{{MIUN_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_nordunet"
          password="{{NORDUNET_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_nrm"
          password="{{NRM_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_oru"
          password="{{ORU_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_rkh"
          password="{{RKH_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_shh"
          password="{{SHH_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_sics"
          password="{{SICS_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_slu"
          password="{{SLU_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_smhi"
          password="{{SMHI_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_sp"
          password="{{SP_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_swamid"
          password="{{SWAMID_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_ths"
          password="{{THS_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_uhr"
          password="{{UHR_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_umu"
          password="{{UMU_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_uniarts"
          password="{{UNIARTS_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_uu"
          password="{{UU_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_vinnova"
          password="{{VINNOVA_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
      {
          username="nextcloud_vr"
          password="{{VR_PASSWORD}}"
          default_hostgroup=10
          transaction_persistent=1
          active=1
      },
    )

    proxysql_servers =
    (
        { hostname = "proxysql-0.proxysqlcluster", port = 6032, weight = 1 },
        { hostname = "proxysql-1.proxysqlcluster", port = 6032, weight = 1 },
        { hostname = "proxysql-2.proxysqlcluster", port = 6032, weight = 1 }
    )
  healthcheck.sh: |
    #!/bin/bash
    num_servers=$(mysql -NB -u admin -h 127.0.0.1 -P 6032 -e "select count(*) from main.mysql_servers where hostgroup_id = 10")
    if [[ ${num_servers} -lt 1 ]]; then
      exit 1
    fi
    exit 0
