apiVersion: batch/v1
kind: Job
metadata:
  name: init-database
  namespace: mlflow
spec:
  template:
    spec:
      containers:
      - name: init-database
        image: docker.io/mysql/mysql-server:8.0.3
        imagePullPolicy: IfNotPresent
        command:
        - /bin/bash
        - -c
        - |
          curl -C - -sSL -o mc https://dl.min.io/client/mc/release/linux-amd64/mc && \
          chmod +x mc && \
          ./mc alias set minio $(MLFLOW_S3_ENDPOINT_URL) $(AWS_ACCESS_KEY_ID) $(AWS_SECRET_ACCESS_KEY) --api S3v4 && \
          ./mc --insecure mb minio/mlflow && \
          mysql -h $(MYSQL_DB_HOST) -u $(MYSQL_DB_USER) -e 'CREATE DATABASE IF NOT EXISTS $(MYSQL_DB_NAME);' && \
          curl -X POST http://localhost:15020/quitquitquit
          # job won't complete unless all containers finish.
          # POST /quitquitquit triggers a shutdown on istio sidecar
        env:
        - name: MLFLOW_S3_ENDPOINT_URL
          value: "http://minio.platiagro:9000"
        - name: AWS_ACCESS_KEY_ID
          valueFrom:
            secretKeyRef:
              name: minio-secrets
              key: MINIO_ACCESS_KEY
        - name: AWS_SECRET_ACCESS_KEY
          valueFrom:
            secretKeyRef:
              name: minio-secrets
              key: MINIO_SECRET_KEY
        - name: MYSQL_DB_HOST
          value: "mysql.platiagro"
        - name: MYSQL_DB_NAME
          value: "mlflow"
        - name: MYSQL_DB_USER
          valueFrom:
            secretKeyRef:
              name: mysql-secrets
              key: MYSQL_DB_USER
        - name: MYSQL_PWD
          valueFrom:
            secretKeyRef:
              name: mysql-secrets
              key: MYSQL_DB_PASSWORD
      restartPolicy: OnFailure
