# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: yunikorn-scheduler
  labels:
    app: yunikorn
    chart: {{ include "yunikorn.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  {{- if .Values.hostNetwork }}
  strategy:
    type: Recreate
  {{- end }}
  replicas: 1
  selector:
    matchLabels:
      app: yunikorn
      component: yunikorn-scheduler
      release: {{ .Release.Name }}
  template:
    metadata:
      name: yunikorn-scheduler
      labels:
        app: yunikorn
        component: yunikorn-scheduler
        release: {{ .Release.Name }}
        {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      annotations:
        {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- with .Values.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if .Values.hostNetwork }}
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      {{- end }}
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
      {{- range .Values.imagePullSecrets }}
        - name: {{ . }}
      {{- end }}
      {{- end }}
      serviceAccountName: {{ .Values.serviceAccount }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.priorityClassName }}
      priorityClassName: {{ . }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: yunikorn-scheduler-k8s
      {{- if .Values.enableSchedulerPlugin }}
          image: "{{ .Values.pluginImage.repository }}:{{ .Values.pluginImage.tag }}"
          imagePullPolicy: {{ .Values.pluginImage.pullPolicy }}
      {{- else }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
      {{- end }}
          ports:
            - name: http1
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          env:
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: GOMEMLIMIT
              value: "{{ .Values.goMemoryLimit }}"
            - name: GOGC
              value: "{{ .Values.goGC }}"
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          {{- with .Values.schedulerSecurityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
        {{- if .Values.enableWebService }}
        - name: yunikorn-scheduler-web
          image: "{{ .Values.web.image.repository }}:{{ .Values.web.image.tag }}"
          imagePullPolicy: {{ .Values.web.image.pullPolicy }}
          ports:
            - name: http2
              containerPort: {{ .Values.service.portWeb }}
              protocol: TCP
          env:
            - name: GOMEMLIMIT
              value: "{{ .Values.web.goMemoryLimit }}"
            - name: GOGC
              value: "{{ .Values.web.goGC }}"
          resources:
            {{- toYaml .Values.web.resources | nindent 12 }}
          {{- with .Values.webSecurityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
        {{- end }}
