apiVersion: batch/v1
kind: Job
metadata:
  name: buildkite-agent
spec:
  template:
    metadata:
      labels:
        application: buildkite-agent
    spec:
      restartPolicy: OnFailure
      terminationGracePeriodSeconds: 1800
      containers:
      - name: buildkite-agent
        image: buildkite/agent:latest
        args: ["start", "--disconnect-after-job", "--disconnect-after-job-timeout", "300"]
        imagePullPolicy: Always
        securityContext:
          privileged: true 
        env:
          - name: TERM
            value: dumb
          - name: BUILDKITE_AGENT_TOKEN
            valueFrom: {secretKeyRef: {name: buildkite-agent, key: token}}
          - name: BUILDKITE_API_TOKEN
            valueFrom: {secretKeyRef: {name: buildkite-api, key: token}}
          - name: BUILDKITE_BUILD_PATH
            value: /scratch
          - name: BUILDKITE_AGENT_DEBUG
            value: "true"
          - name: BUILDKITE_GIT_CLEAN_FLAGS
            value: "-fdqx"
        volumeMounts:
        - name: ssh-keys
          mountPath: /root/.ssh/id_rsa
          subPath: id_rsa
        - name: ssh-keys
          mountPath: /root/.ssh/id_rsa.pub
          subPath: id_rsa.pub
        - name: docker-binary
          mountPath: /usr/bin/docker
        - name: docker-socket  
          mountPath: /docker/docker.sock
        resources:
          requests:
            cpu: "500m"
            memory: "2000M"
          limits:
            cpu: "2000m"
            memory: "5000M"
      volumes:
        - name: ssh-keys
          secret:
            secretName: buildkite-agent-ssh
            defaultMode: 0400
        - name: docker-binary
          hostPath: {path: /usr/bin/docker}
        - name: docker-socket
          hostPath: {path: /var/run/docker.sock}
