---
# Source: airflow/templates/statefulsets-workers.yaml
## Workers are not in deployment, but in StatefulSet, to allow each worker expose a mini-server
## that only serve logs, that will be used by the web server.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: airflow-worker
  labels:
    app: airflow
    component: worker
    chart: airflow-5.2.0
    release: airflow
    heritage: Tiller
spec:
  serviceName: "airflow-worker"
  updateStrategy:
    ## Kill the workers as soon as possible, the scheduler will restart the failed job later
    type: RollingUpdate
  ## Use experimental burst mode for faster StatefulSet scaling
  ##   https://github.com/kubernetes/kubernetes/commit/c2c5051adf096ffd48bf1dcf5b11cb47e464ecdd
  podManagementPolicy: Parallel
  replicas: 1
  selector:
    matchLabels:
      app: airflow
      component: worker
      release: airflow
  template:
    metadata:
      annotations:
        checksum/config-env: 4ee020624b4c4e6c7e5b7a1e9161839eaeb06c462968cd4c3e1066cdef5eaeca
        checksum/config-git-clone: f1ccca4494c87cfd3b6a582326f8b995ffb3c65bf4ed4341c188de9e98dd9ae5
        checksum/config-scripts: c286309579b6482ec7922f5bf3d1c509aecd617e8ea9c9a305af610658a21dc0
      labels:
        app: airflow
        component: worker
        release: airflow
    spec:
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      serviceAccountName: airflow
      containers:
        - name: airflow-worker
          imagePullPolicy: IfNotPresent
          image: "puckel/docker-airflow:1.10.4"
          envFrom:
            - configMapRef:
                name: "airflow-env"
          env:          
            - name: POSTGRES_USER
              value: "postgres"
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: airflow-postgresql
                  key: postgres-password
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: airflow-redis
                  key: redis-password
          volumeMounts:
            - name: scripts
              mountPath: /usr/local/scripts
          args:
            - "bash"
            - "-c"
            - >
              echo 'waiting 60s...' &&
              sleep 60 &&
              mkdir -p /usr/local/airflow/.local/bin &&
              export PATH=/usr/local/airflow/.local/bin:$PATH &&
              echo 'executing worker...' &&
              airflow worker
          ports:
            - name: wlog
              containerPort: 8793
              protocol: TCP
          resources:
            {}
            
      volumes:
        - name: scripts
          configMap:
            name: airflow-scripts
            defaultMode: 0755
        - name: dags-data
          emptyDir: {}
