apiVersion: apps/v1
kind: Deployment
metadata:
  name: initdbd-deploy
  labels:
    app: initdbd
    version: v1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: initdbd  #选择 pod label
      version: v1
  template:
    metadata:
      labels:
        app: initdbd #标注 pod label
        version: v1
    spec:
      containers:
      - name: initdbd
        image: mysql:8.0
        imagePullPolicy: Always
        env:
        - name: MYSQL_SVC
          value: $(MYSQL_SVC)
        command:
        - bash
        - "-c"
        - |
          while true;do
            echo "${MYSQL_SVC}"

            if [[ $(mysql -h "${MYSQL_SVC}" -uroot -e "use test_db;show tables;") ]]; then
              sleep 100s
              continue
            fi

            if [ -d /initdbd ]; then
              cd /initdbd
              mysql -h "${MYSQL_SVC}" -uroot < ping_table.sql
              mysql -h "${MYSQL_SVC}" -uroot < user_table.sql
              mysql -h "${MYSQL_SVC}" -uroot -e "use test_db;show tables;"
            fi

            sleep 1s
          done
        volumeMounts:
        - name: initdbd-vol
          mountPath: /initdbd
      volumes:
      - name: initdbd-vol
        hostPath:
          path: /var/lib/mysqlx/initdbd-debug #宿主机路径
          type: Directory
