# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

kind: Job
apiVersion: batch/v1
metadata:
  name: gpu-node-pool-autoscaler
  namespace: kube-system
  labels:
    k8s-app: gpu-node-pool-autoscaler
spec:
  activeDeadlineSeconds: 3600
  ttlSecondsAfterFinished: 60
  template:
    spec:
      serviceAccountName: pod-broker-node-init
      restartPolicy: OnFailure
      nodeSelector:
        cloud.google.com/gke-nodepool: "gpu-cos"
      tolerations:
        - key: "app.broker/tier"
          effect: "NoSchedule"
          operator: "Exists"
        - key: "app.broker/node-init"
          effect: "NoSchedule"
          operator: "Exists"
        - key: "nvidia.com/gpu"
          effect: "NoSchedule"
          operator: "Exists"
        - key: "cloud.google.com/gke-accelerator-init"
          effect: "NoSchedule"
          operator: "Exists"
      containers:
        ###
        # self-deleting job.
        ###
        - image: google/cloud-sdk
          name: delete-job
          command: ["/bin/sh"]
          args:
            - -exc
            - |
              sleep 10
              kubectl delete job -n ${NAMESPACE} ${JOB_NAME}
          env:
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: JOB_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.labels['job-name']
