# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
{{- if .Values.features.mysqlMonitor.enabled }}

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysqld-config
  labels:
    app: mysqld-config
data:
  mysqld-config-file: |
    [mysqld]
    init_connect='SET NAMES utf8'
    slow_query_log=ON
    event_scheduler=ON
    long_query_time=1
    slow_query_log_file=/data/slow.log
    [client]
    default-character-set=utf8
    [mysql]
    default-character-set=utf8

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mock-sql-mysql
  labels:
    app: mysql-load
data:
  mock-sql: |
    CREATE DATABASE IF NOT EXISTS test;
    USE test;
    CREATE TABLE IF NOT EXISTS `t1`(
       `te1` VARCHAR(100) NOT NULL
    )ENGINE=InnoDB DEFAULT CHARSET=utf8;
    SET GLOBAL event_scheduler = 1;

    CREATE EVENT `event_1`
    ON SCHEDULE EVERY 1 SECOND
    DO INSERT INTO t1 values('test');

    CREATE EVENT `event_2`
    ON SCHEDULE EVERY 1 SECOND
    DO UPDATE t1 SET `te1` = 1;

    CREATE EVENT `event_3`
    ON SCHEDULE EVERY 1 SECOND
    DO DELETE FROM t1;

    CREATE EVENT `event_4`
    ON SCHEDULE EVERY 1 SECOND
    DO COMMIT;

    CREATE EVENT `event_5`
    ON SCHEDULE EVERY 1 SECOND
    DO SELECT SLEEP(3);

---
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  selector:
    app: mysql
  ports:
    - protocol: TCP
      port: 3306
      name: mysqld
    - protocol: TCP
      port: 9104
      name: mysqld-exporter

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
      annotations:
        sidecar.istio.io/inject: "false"
    spec:
      containers:
        - name: fluent-bit
          image: fluent/fluent-bit:1.9
          env:
            - name: SW_SERVICE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
          volumeMounts:
            - name: slowsql-mysql-fluent-bit
              mountPath: /fluent-bit/etc/
            - name: shared-data
              mountPath: /data/
        - name: mysqld-exporter
          image: prom/mysqld-exporter:v0.14.0
          env:
            - name: DATA_SOURCE_NAME
              value: root:password@(mysql:3306)/
          resources:
            limits:
              cpu: 100m
              memory: "128Mi"
          ports:
            - containerPort: 9104
              name: metrics
        - name: mysql
          image: mysql:8.0.30
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: password
          ports:
            - containerPort: 3306
          volumeMounts:
            - name: mysqld-config
              mountPath: /etc/mysql/conf.d/
            - name: shared-data
              mountPath: /data/
      volumes:
        - name: shared-data
          emptyDir: {}
        - name: slowsql-mysql-fluent-bit
          configMap:
            name: slowsql-mysql-fluent-bit
            items:
              - key: fluent-bit-conf
                path: fluent-bit.conf
              - key: fluent-bit-parser-conf
                path: fluent-bit-parser.conf
              - key: fluent-bit-script-lua
                path: fluent-bit-script.lua
        - name: mysqld-config
          configMap:
            name: mysqld-config
            items:
              - key: mysqld-config-file
                path: config-file.cnf

---
apiVersion: batch/v1
kind: Job
metadata:
  name: mysql-load-deployment # @feature: mysql; set up job to trigger mysql commands, you don't need this in production env.
spec:
  template:
    metadata:
      name: mysql-load-deployment
      annotations:
        sidecar.istio.io/inject: "false"
    spec:
      restartPolicy: Never
      initContainers:
        - name: wait-for-mysql
          image: busybox
          command:  ["sh", "-c", "until nc -z mysql 3306 > /dev/null; do echo Waiting for mysql.; sleep 2; done;"]
      containers:
        - name: mysql-load
          image: mysql:8.0.30
          command:
            - bash
            - -c
            - mysql -hmysql -uroot -ppassword < /conf/mock.sql
          volumeMounts:
            - name: mock-sql-vol-mysql
              mountPath: /conf
      volumes:
        - name: mock-sql-vol-mysql
          configMap:
            name: mock-sql-mysql
            items:
              - key: mock-sql
                path: mock.sql
{{- end }}
