# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "cluster-overprovisioner.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 "cluster-overprovisioner.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 "cluster-overprovisioner.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Return the appropriate apiVersion for PriorityClass.
*/}}
{{- define "PriorityClass.apiVersion" -}}
{{- if semverCompare ">=1.8-0, <1.11-0" .Capabilities.KubeVersion.GitVersion -}}
{{- print "v1alpha1" -}}
{{- else if semverCompare ">=1.11-0" .Capabilities.KubeVersion.GitVersion -}}
{{- print "v1beta1" -}}
{{- end -}}
{{- end -}}
 # deployments.yaml
{{- $relName := .Release.Name -}}
{{- $relService := .Release.Service -}}
{{- $fullname :=  include "cluster-overprovisioner.fullname" . -}}
{{- $name :=  include "cluster-overprovisioner.name" . -}}
{{- $chart :=  include "cluster-overprovisioner.chart" . -}}
{{- $chartName := .Chart.Name }}
{{- $podSecurityContext := .Values.podSecurityContext }}
{{- $priorityClassName := .Values.priorityClassOverprovision.name }}
{{- $repository := .Values.image.repository }}
{{- $imageTag := .Values.image.tag }}
{{- $pullPolicy := .Values.image.pullPolicy }}
{{- $imagePullSecrets := .Values.image.pullSecrets }}

{{ range .Values.deployments }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: "{{ $fullname }}-{{ .name }}"
  labels:
    app.kubernetes.io/name: {{ $name }}
    cluster-overprovisioner-name: {{ .name }}
    helm.sh/chart: {{ $chart }}
    app.kubernetes.io/instance: {{ $relName }}
    app.kubernetes.io/managed-by: {{ $relService }}
spec:
  strategy:
    type: Recreate
  replicas: {{ .replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ $name }}
      cluster-overprovisioner-name: {{ .name }}
      app.kubernetes.io/instance: {{ $relName }}
  template:
    metadata:
    {{- with .annotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      labels:
        app.kubernetes.io/name: {{ $name }}
        cluster-overprovisioner-name: {{ .name }}
        app.kubernetes.io/instance: {{ $relName }}
    {{- with .labels }}
        {{- toYaml . | nindent 8 }}
    {{- end }}
    spec:
      priorityClassName: {{ $priorityClassName }}
      securityContext:
        {{- toYaml $podSecurityContext | nindent 8 }}
      containers:
        - name: {{ $chartName }}
          image: "{{ $repository }}:{{ $imageTag }}"
          imagePullPolicy: {{ $pullPolicy }}
          resources:
            {{- toYaml .resources | nindent 12 }}
    {{- if $imagePullSecrets }}
      imagePullSecrets:
      {{- range $imagePullSecrets }}
        - name: {{ . }}
      {{- end }}
    {{- end }}
    {{- with .nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
---
{{- end }}
 # priorityclass-default.yaml
{{- if .Values.priorityClassDefault.enabled }}
apiVersion: scheduling.k8s.io/{{ template "PriorityClass.apiVersion" . }}
kind: PriorityClass
metadata:
  name: {{ .Values.priorityClassDefault.name }}
  labels:
    app.kubernetes.io/name: {{ include "cluster-overprovisioner.name" . }}
    helm.sh/chart: {{ include "cluster-overprovisioner.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
value: {{ .Values.priorityClassDefault.value }}
globalDefault: true
description: "Default priority class for all pods"
{{- end }}
 # priorityclass-overprovision.yaml
apiVersion: scheduling.k8s.io/{{ template "PriorityClass.apiVersion" . }}
kind: PriorityClass
metadata:
  name: {{ .Values.priorityClassOverprovision.name }}
  labels:
    app.kubernetes.io/name: {{ include "cluster-overprovisioner.name" . }}
    helm.sh/chart: {{ include "cluster-overprovisioner.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
value: {{ .Values.priorityClassOverprovision.value }}
globalDefault: false
description: "Priority class used for overprovision pods"

podSecurityContext: {}
  # fsGroup: 2000

priorityClassOverprovision:
  name: overprovisioning
  value: -1

priorityClassDefault:
  enabled: true
  name: default
  value: 0

image:
  repository: k8s.gcr.io/pause
  tag: 3.1
  pullPolicy: IfNotPresent

  ## Optionally specify an array of imagePullSecrets.
  ## Secrets must be manually created in the namespace.
  ## ref: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/
  ##
  # pullSecrets:
  #   - myRegistrKeySecretName

nameOverride: ""

fullnameOverride: ""

deployments: []
  # - name: default
  #   annotations: {}
  #   replicaCount: 1
  #   nodeSelector: {}
  #   resources: {}
  #   tolerations: []
  #   affinity: {}
  #   labels: {}
