# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "k8s-spot-rescheduler.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "k8s-spot-rescheduler.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "k8s-spot-rescheduler.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create the name of the service account to use
*/}}
{{- define "k8s-spot-rescheduler.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
    {{ default (include "k8s-spot-rescheduler.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}
 # clusterrole.yaml
{{- if .Values.rbac.create }}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ template "k8s-spot-rescheduler.fullname" . }}
  labels:
    app: {{ template "k8s-spot-rescheduler.name" . }}
    chart: {{ template "k8s-spot-rescheduler.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
rules:
  # For leader election
  - apiGroups:
      - ""
    resources:
      - endpoints
    verbs:
      - get
      - update
      - create
  - apiGroups:
      - ""
    resources:
      - events
    verbs:
      - create
      - patch

  # For listing and watching items
  - apiGroups:
      - ""
    resources:
      - nodes
      - pods
      - replicasets
      - replicationcontrollers
      - services
      - statefulsets
      - poddisruptionbudgets
      - persistentvolumes
      - persistentvolumeclaims
    verbs:
      - list
      - get
      - watch
  - apiGroups:
    - apps
    resources:
      - statefulsets
    verbs:
      - list
      - get
      - watch
  - apiGroups:
    - extensions
    resources:
      - replicasets
    verbs:
      - list
      - get
      - watch
  - apiGroups:
    - policy
    resources:
      - poddisruptionbudgets
    verbs:
      - list
      - get
      - watch

  # For rescheduling pods
  - apiGroups:
    - ""
    resources:
      - nodes
    verbs:
      - update
  - apiGroups:
    - ""
    resources:
      - pods/eviction
    verbs:
      - create

  - apiGroups:
      - storage.k8s.io
    resources:
      - storageclasses
    verbs:
      - list
      - get
      - watch
{{- end }}
 # clusterrolebinding.yaml
{{- if .Values.rbac.create }}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ template "k8s-spot-rescheduler.fullname" . }}
  labels:
    app: {{ template "k8s-spot-rescheduler.name" . }}
    chart: {{ template "k8s-spot-rescheduler.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ template "k8s-spot-rescheduler.fullname" . }}
subjects:
- kind: ServiceAccount
  name: {{ template "k8s-spot-rescheduler.serviceAccountName" . }}
  namespace: {{ .Release.Namespace }}
{{- end }}
 # deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "k8s-spot-rescheduler.fullname" . }}
  labels:
    app: {{ template "k8s-spot-rescheduler.name" . }}
    chart: {{ template "k8s-spot-rescheduler.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "k8s-spot-rescheduler.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "k8s-spot-rescheduler.name" . }}
        release: {{ .Release.Name }}
{{- with .Values.podAnnotations }}
      annotations:
{{ toYaml . | indent 8 }}
{{- end }}
    spec:
      serviceAccountName: {{ template "k8s-spot-rescheduler.serviceAccountName" . }}
      {{- if .Values.priorityClassName }}
      priorityClassName: "{{ .Values.priorityClassName }}"
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - k8s-spot-rescheduler
            - --namespace={{ .Release.Namespace }}
          {{- range $key, $value := .Values.cmdOptions }}
            - --{{ $key }}{{ if $value }}={{ $value }}{{ end }}
          {{- end }}
          ports:
          - name: http
            containerPort: 9235
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- if .Values.affinity }}
      affinity:
{{ toYaml .Values.affinity | indent 8 }}
    {{- end }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
 # pdb.yaml
{{- if .Values.podDisruptionBudget -}}
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  name: {{ template "k8s-spot-rescheduler.fullname" . }}
  labels:
    app: {{ template "k8s-spot-rescheduler.name" . }}
    chart: {{ template "k8s-spot-rescheduler.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  selector:
    matchLabels:
      app: {{ template "k8s-spot-rescheduler.name" . }}
      release: {{ .Release.Name }}
{{ toYaml .Values.podDisruptionBudget | indent 2 }}
{{- end -}}
 # serviceaccount.yaml
{{- if .Values.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "k8s-spot-rescheduler.serviceAccountName" . }}
  labels:
    app: {{ template "k8s-spot-rescheduler.name" . }}
    chart: {{ template "k8s-spot-rescheduler.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
{{- end }}

# Default values for k8s-spot-rescheduler.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1

podAnnotations: {}

podDisruptionBudget: {}
  # maxUnavailable: 1

image:
  repository: quay.io/pusher/k8s-spot-rescheduler
  tag: v0.3.0
  pullPolicy: IfNotPresent

# The full list of available options https://github.com/pusher/k8s-spot-rescheduler#flags
cmdOptions:
  v: 2
  running-in-cluster: true
  kube-api-content-type: application/vnd.kubernetes.protobuf
  housekeeping-interval: 10s
  node-drain-delay: 10m
  pod-eviction-timeout: 2m
  max-graceful-termination: 2m
  listen-address: 0.0.0.0:9235
  on-demand-node-label: node-role.kubernetes.io/worker
  spot-node-label: node-role.kubernetes.io/spot-worker
  delete-non-replicated-pods: false

resources: {}
  # We usually recommend not to specify default resources and to leave this as a conscious
  # choice for the user. This also increases chances charts run on environments with little
  # resources, such as Minikube. If you do want to specify resources, uncomment the following
  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
  # limits:
  #  cpu: 100m
  #  memory: 128Mi
  # requests:
  #  cpu: 100m
  #  memory: 128Mi

# Affinity for pod assignment
# Ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity
affinity: {}

tolerations: []

# Node labels for pod assignment
# Ref: https://kubernetes.io/docs/user-guide/node-selection/
nodeSelector: {}

rbac:
  # Specifies whether RBAC resources should be created
  create: true

serviceAccount:
  # Specifies whether a ServiceAccount should be created
  create: true
  # The name of the ServiceAccount to use.
  # If not set and create is true, a name is generated using the fullname template
  name:

# Leverage a PriorityClass to ensure your pods survive resource shortages
# ref: https://kubernetes.io/docs/concepts/configuration/pod-priority-preemption/
# PriorityClass: system-cluster-critical
priorityClassName: ""
