apiVersion: v1
kind: Pod
metadata:
  name: sqlflow-server
  labels:
    app: sqlflow-server
spec:
  containers:
  - name: server
    image: sqlflow/sqlflow:server
    imagePullPolicy: IfNotPresent
    env:
    - name: SQLFLOW_WORKFLOW_LOGVIEW_ENDPOINT
      value: "http://localhost:9001"
    - name: SQLFLOW_WORKFLOW_STEP_IMAGE
      value: sqlflow/sqlflow:step
    command:
      - sqlflowserver
      - --argo-mode
  - name: notebook
    image: sqlflow/sqlflow:jupyter
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 8888
      name: notebook
    command:
    - sh
    - -c
    - 'export SQLFLOW_DATASOURCE=mysql://root:root@tcp\(${MY_POD_IP}:3306\)/?maxAllowedPacket=0 && jupyter notebook --ip=0.0.0.0 --port=8888 --allow-root --NotebookApp.token=""'
    env:
    # Tell the Jupyter Notebook magic command the SQLFlow gRPC server address.
    - name: SQLFLOW_SERVER
      value: "localhost:50051"
    # When Kubernetes starts a container, it fills in environment variables 
    # before it runs the command.  Here Kubernetes fills in MY_POD_IP with the
    # IP address of the pod, which contains the MySQL server container.  The
    # above command use MY_POD_IP to construct SQLFLOW_DATASOURCE, which, will
    # be retrieved by the Jupyter Notebook magic command and be sent to the
    # SQLFlow gRPC server in each request.  The SQLFlow server compiles the value
    # into each step container, so these steps knows the MySQL server IP.
    - name: MY_POD_IP
      valueFrom:
        fieldRef:
          fieldPath: status.podIP
  - name: mysql
    image: sqlflow/sqlflow:mysql
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 3306
      protocol: TCP
    env:
    - name: MYSQL_HOST
      value: "0.0.0.0"
    - name: MYSQL_PORT
      value: "3306"
    readinessProbe:
      exec:
        # The entrypoing script of sqlflow/sqlflow:mysql populates datasets into the
        # MySQL server and then touch a file /work/mysql-initd to indicate the
        # completion of population.  Only if the file is created, the cat command
        # exits with 0, and indicates the readiness of the MySQL server.
        command:
        - cat
        - /work/mysql-inited
      initialDelaySeconds: 5
      periodSeconds: 5
