{{- if .Values.custom.scheduler_enable }}
{{ $scheduler_affinity := or .Values.custom.scheduler_affinity .Values.custom.default_affinity }}
{{ $scheduler_tolerations := or .Values.custom.scheduler_tolerations .Values.custom.default_tolerations }}
{{ $scheduler_sc := or .Values.custom.scheduler_sc .Values.custom.default_sc }}
{{ $scheduler_ns := or .Values.custom.scheduler_ns .Values.custom.default_ns }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-scheduler-configmap
  namespace: {{ .Release.Namespace }}
  {{- if .Values.custom.common_labels }}
  labels:
    {{- toYaml .Values.custom.common_labels | nindent 4 }}
  {{- end }}
data:
  {{- if .Values.custom.scheduler_config_override }}
  volcano-scheduler.conf: |
    {{- .Values.custom.scheduler_config_override | nindent 4 }}
  {{- else }}
  {{- (.Files.Glob .Values.basic.scheduler_config_file).AsConfig | nindent 2}}
  {{- end }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ .Release.Name }}-scheduler
  namespace: {{ .Release.Namespace }}
  {{- if .Values.custom.common_labels }}
  labels:
    {{- toYaml .Values.custom.common_labels | nindent 4 }}
  {{- end }}
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: {{ .Release.Name }}-scheduler
  {{- if .Values.custom.common_labels }}
  labels:
    {{- toYaml .Values.custom.common_labels | nindent 4 }}
  {{- end }}
rules:
  - apiGroups: ["apiextensions.k8s.io"]
    resources: ["customresourcedefinitions"]
    verbs: ["create", "get", "list", "watch", "delete"]
  - apiGroups: ["batch.volcano.sh"]
    resources: ["jobs"]
    verbs: ["get", "list", "watch", "update", "delete"]
  - apiGroups: ["batch.volcano.sh"]
    resources: ["jobs/status"]
    verbs: ["update", "patch"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["create", "list", "watch", "update", "patch"]
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "list", "watch", "patch", "delete"]
  - apiGroups: [""]
    resources: ["pods/status"]
    verbs: ["update"]
  - apiGroups: [""]
    resources: ["pods/binding"]
    verbs: ["create"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["list", "watch", "update"]
  - apiGroups: [""]
    resources: ["persistentvolumes"]
    verbs: ["list", "watch", "update"]
  - apiGroups: [""]
    resources: ["namespaces", "services", "replicationcontrollers"]
    verbs: ["list", "watch", "get"]
  - apiGroups: [""]
    resources: ["resourcequotas"]
    verbs: ["list", "watch"]
  - apiGroups: [""]
    resources: ["nodes"]
    verbs: ["get","list", "watch","update","patch"]
  - apiGroups: [ "storage.k8s.io" ]
    resources: ["storageclasses", "csinodes", "csidrivers", "csistoragecapacities"]
    verbs: [ "list", "watch" ]
  - apiGroups: ["policy"]
    resources: ["poddisruptionbudgets"]
    verbs: ["list", "watch"]
  - apiGroups: ["scheduling.k8s.io"]
    resources: ["priorityclasses"]
    verbs: ["get", "list", "watch"]
  - apiGroups: ["scheduling.incubator.k8s.io", "scheduling.volcano.sh"]
    resources: ["queues"]
    verbs: ["get", "list", "watch", "create", "delete"]
  - apiGroups: ["scheduling.incubator.k8s.io", "scheduling.volcano.sh"]
    resources: ["queues/status"]
    verbs: ["update"]
  - apiGroups: ["scheduling.incubator.k8s.io", "scheduling.volcano.sh"]
    resources: ["podgroups"]
    verbs: ["list", "watch", "update"]
  - apiGroups: ["nodeinfo.volcano.sh"]
    resources: ["numatopologies"]
    verbs: ["get", "list", "watch", "delete"]
  - apiGroups: [""]
    resources: ["configmaps"]
    verbs: ["get", "create", "delete", "update"]
  - apiGroups: ["apps"]
    resources: ["daemonsets", "replicasets", "statefulsets"]
    verbs: ["list", "watch", "get"]
  - apiGroups: ["coordination.k8s.io"]
    resources: ["leases"]
    verbs: ["get", "create", "update", "watch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: {{ .Release.Name }}-scheduler-role
  {{- if .Values.custom.common_labels }}
  labels:
    {{- toYaml .Values.custom.common_labels | nindent 4 }}
  {{- end }}
subjects:
  - kind: ServiceAccount
    name: {{ .Release.Name }}-scheduler
    namespace: {{ .Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: {{ .Release.Name }}-scheduler
  apiGroup: rbac.authorization.k8s.io

---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: {{ .Release.Name }}-scheduler
  namespace: {{ .Release.Namespace }}
  labels:
    app: volcano-scheduler
    {{- if or (.Values.custom.scheduler_labels) (.Values.custom.common_labels) }}
    {{- mustMerge (.Values.custom.scheduler_labels | default (dict)) (.Values.custom.common_labels | default (dict)) | toYaml | nindent 4 }}
    {{- end }}
spec:
  replicas: {{ .Values.custom.scheduler_replicas }}
  selector:
    matchLabels:
      app: volcano-scheduler
  template:
    metadata:
      labels:
        app: volcano-scheduler
        {{- if or (.Values.custom.scheduler_podLabels) (.Values.custom.common_labels) }}
        {{- mustMerge (.Values.custom.scheduler_podLabels | default (dict)) (.Values.custom.common_labels | default (dict)) | toYaml | nindent 8 }}
        {{- end }}
    spec:
      {{- if $scheduler_tolerations }}
      tolerations: {{- toYaml $scheduler_tolerations | nindent 8 }}
      {{- end }}
      {{- if $scheduler_ns }}
      nodeSelector: {{- toYaml $scheduler_ns | nindent 8 }}
      {{- end }}
      {{- if $scheduler_affinity }}
      affinity:
        {{- toYaml $scheduler_affinity | nindent 8 }}
      {{- end }}
      {{- if $scheduler_sc }}
      securityContext:
        {{- toYaml $scheduler_sc | nindent 8 }}
      {{- end }}
      serviceAccount: {{ .Release.Name }}-scheduler
      priorityClassName: system-cluster-critical
      {{- if .Values.basic.image_pull_secret }}
      imagePullSecrets:
          - name: {{ .Values.basic.image_pull_secret }}
      {{- end }}
      containers:
        - name: {{ .Release.Name }}-scheduler
          image: {{ .Values.basic.image_registry }}/{{.Values.basic.scheduler_image_name}}:{{.Values.basic.image_tag_version}}
          {{- if .Values.custom.scheduler_resources }}
          resources:
          {{- toYaml .Values.custom.scheduler_resources | nindent 12 }}
          {{- end }}
          args:
            - --logtostderr
            - --scheduler-conf=/volcano.scheduler/{{base .Values.basic.scheduler_config_file}}
            - --enable-healthz=true
            - --enable-metrics=true
            - --leader-elect={{ .Values.custom.leader_elect_enable }}
            {{- if .Values.custom.leader_elect_enable }}
            - --leader-elect-resource-namespace={{ .Release.Namespace }}
            {{- end }}
            - -v={{.Values.custom.scheduler_log_level}}
            - 2>&1
          env:
            - name: DEBUG_SOCKET_DIR
              value: /tmp/klog-socks
          imagePullPolicy: {{ .Values.basic.image_pull_policy }}
          volumeMounts:
            - name: scheduler-config
              mountPath: /volcano.scheduler
            - name: klog-sock
              mountPath: /tmp/klog-socks
          {{- if .Values.custom.scheduler_default_csc }}
          securityContext:
            {{- toYaml .Values.custom.scheduler_default_csc | nindent 12 }}
          {{- end }}
      volumes:
        - name: scheduler-config
          configMap:
            name: {{ .Release.Name }}-scheduler-configmap
        - name: klog-sock
          hostPath:
            path: /tmp/klog-socks
---
apiVersion: v1
kind: Service
metadata:
  annotations:
    prometheus.io/path: /metrics
    prometheus.io/port: "8080"
    prometheus.io/scrape: "true"
  name: {{ .Release.Name }}-scheduler-service
  namespace: {{ .Release.Namespace }}
  labels:
    app: volcano-scheduler
    {{- if .Values.custom.common_labels }}
    {{- toYaml .Values.custom.common_labels | nindent 4 }}
    {{- end }}
spec:
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 8080
    name: "metrics"
  selector:
    app: volcano-scheduler
  type: ClusterIP
{{- end }}
