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

{{/*
Create a default fully qualified controller name
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "cosbench.controller.fullname" -}}
{{- printf "%s-cosbench-controller" .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified driver name
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "cosbench.driver.fullname" -}}
{{- printf "%s-cosbench-driver" .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- end -}}

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

{{/*
Create the name of the service account to use for the controller component
*/}}
{{- define "cosbench.serviceAccountName.controller" -}}
{{- if .Values.serviceAccounts.controller.create -}}
    {{ default (include "cosbench.controller.fullname" .) .Values.serviceAccounts.controller.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccounts.controller.name }}
{{- end -}}
{{- end -}}

{{/*
Create the name of the service account to use for the driver component
*/}}
{{- define "cosbench.serviceAccountName.driver" -}}
{{- if .Values.serviceAccounts.driver.create -}}
    {{ default (include "cosbench.driver.fullname" .) .Values.serviceAccounts.driver.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccounts.driver.name }}
{{- end -}}
{{- end -}}

{{/*
Create a comma seperated list of drivers
*/}}
{{- define "cosbench.driversList" -}}
{{- $count := (int .Values.driver.replicaCount) -}}
{{- $release := .Release.Name -}}
{{- range $v := until $count }}{{ $release }}-cosbench-driver-{{ $v }}.{{ $release }}-cosbench-driver{{ if ne $v (sub $count 1) }},{{- end -}}{{- end -}}
{{- end -}}
 # controller-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "cosbench.controller.fullname" . }}
  labels:
    app: {{ template "cosbench.name" . }}
    chart: {{ template "cosbench.chart" . }}
    component: controller
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ template "cosbench.name" . }}
      release: {{ .Release.Name }}
      component: controller
  template:
    metadata:
      labels:
        app: {{ template "cosbench.name" . }}
        release: {{ .Release.Name }}
        component: controller
    spec:
      serviceAccountName: {{ template "cosbench.serviceAccountName.controller" . }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.controller.image.repository }}:{{ .Values.controller.image.tag }}"
          imagePullPolicy: {{ .Values.controller.image.pullPolicy }}
          ports:
            - name: controller
              containerPort: 19088
              protocol: TCP
          env:
            - name: LOG_LEVEL
              value: "{{ .Values.controller.logLevel }}"
            - name: DRIVERS
              value: "{{ template "cosbench.driversList" . }}"
          args: ['java','-Dcosbench.tomcat.config=conf/controller-tomcat-server.xml','-server','-cp','main/*','org.eclipse.equinox.launcher.Main','-configuration','conf/.controller','-console','19089']
 # controller-ingress.yaml
{{- if .Values.controller.ingress.enabled -}}
{{- $fullName := include "cosbench.controller.fullname" . -}}
{{- $servicePort := .Values.controller.service.port -}}
{{- $ingressPath := .Values.controller.ingress.path -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ $fullName }}
  labels:
    app: {{ template "cosbench.name" . }}
    chart: {{ template "cosbench.chart" . }}
    component: controller
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
{{- with .Values.controller.ingress.annotations }}
  annotations:
{{ toYaml . | indent 4 }}
{{- end }}
spec:
{{- if .Values.controller.ingress.tls }}
  tls:
  {{- range .Values.controller.ingress.tls }}
    - hosts:
      {{- range .hosts }}
        - {{ . }}
      {{- end }}
      secretName: {{ .secretName }}
  {{- end }}
{{- end }}
  rules:
  {{- range .Values.controller.ingress.hosts }}
    - host: {{ . }}
      http:
        paths:
          - path: {{ $ingressPath }}
            backend:
              serviceName: {{ $fullName }}
              servicePort: controller
  {{- end }}
{{- end }}
 # controller-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "cosbench.controller.fullname" . }}
  labels:
    app: {{ template "cosbench.name" . }}
    chart: {{ template "cosbench.chart" . }}
    component: controller
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
spec:
  type: {{ .Values.controller.type }}
  ports:
    - name: controller
      port: {{ .Values.controller.service.port }}
      targetPort: controller
      protocol: TCP
  selector:
    app: {{ template "cosbench.name" . }}
    component: controller
    release: {{ .Release.Name }}
 # controller-serviceaccount.yaml
{{- if .Values.serviceAccounts.controller.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app: {{ template "cosbench.name" . }}
    chart: {{ template "cosbench.chart" . }}
    component: controller
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "cosbench.serviceAccountName.controller" . }}
{{- end }}
 # driver-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "cosbench.driver.fullname" . }}
  labels:
    app: {{ template "cosbench.name" . }}
    chart: {{ template "cosbench.chart" . }}
    component: driver
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
spec:
  clusterIP: None
  ports:
    - name: driver
      port: {{ .Values.driver.service.port }}
      targetPort: driver
      protocol: TCP
  selector:
    app: {{ template "cosbench.name" . }}
    component: driver
    release: {{ .Release.Name }}
 # driver-serviceaccount.yaml
{{- if .Values.serviceAccounts.driver.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app: {{ template "cosbench.name" . }}
    chart: {{ template "cosbench.chart" . }}
    component: driver
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "cosbench.serviceAccountName.driver" . }}
{{- end }}
 # driver-statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ template "cosbench.driver.fullname" . }}
  labels:
    app: {{ template "cosbench.name" . }}
    chart: {{ template "cosbench.chart" . }}
    component: driver
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
spec:
  serviceName: {{ template "cosbench.driver.fullname" . }}
  replicas: {{ .Values.driver.replicaCount }}
  selector:
    matchLabels:
        app: {{ template "cosbench.name" . }}
        component: driver
        release: "{{ .Release.Name }}"
  template:
    metadata:
      labels:
        app: {{ template "cosbench.name" . }}
        component: driver
        release: "{{ .Release.Name }}"
    spec:
      serviceAccountName: {{ template "cosbench.serviceAccountName.driver" . }}
      containers:
        - name: {{ template "cosbench.driver.fullname" . }}
          image: "{{ .Values.driver.image.repository }}:{{ .Values.driver.image.tag }}"
          imagePullPolicy: {{ .Values.driver.image.pullPolicy }}
          ports:
            - name: driver
              containerPort: 18088
              protocol: TCP
          env:
            - name: LOG_LEVEL
              value: "{{ .Values.driver.logLevel }}"
          args: ['java','-Dcosbench.tomcat.config=conf/driver-tomcat-server.xml','-server','-cp','main/*','org.eclipse.equinox.launcher.Main','-configuration','conf/.driver','-console','18089']
          resources:
{{ toYaml .Values.driver.resources | indent 12 }}
      affinity:
        podAntiAffinity:
        {{- if eq .Values.driver.antiAffinity "hard" }}
          requiredDuringSchedulingIgnoredDuringExecution:
          - topologyKey: "kubernetes.io/hostname"
            labelSelector:
              matchLabels:
                app:  {{ template "cosbench.name" . }}
                component: driver
                release: {{ .Release.Name | quote }}
        {{- else if eq .Values.driver.antiAffinity "soft" }}
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: {{ .Values.driver.antiAffinityWeight }}
            podAffinityTerm:
              topologyKey: "kubernetes.io/hostname"
              labelSelector:
                matchLabels:
                  app:  {{ template "cosbench.name" . }}
                  component: driver
                  release: {{ .Release.Name | quote }}
        {{- end }}
      {{- if .Values.driver.hostAliases }}
      hostAliases:
{{ toYaml .Values.driver.hostAliases | indent 8 }}
      {{- end }}

## Define serviceAccount names for components. Defaults to component's fully
## qualified name.
##
serviceAccounts:
  controller:
    create: true
    name:
  driver:
    create: true
    name:

# ------------------------------------------------------------------------------
# Controller:
# ------------------------------------------------------------------------------

controller:
  ## controller container image
  ##
  image:
    repository: zenko/zenko-cosbench
    tag: 0.0.6
    pullPolicy: IfNotPresent

  service:
    type: ClusterIP
    port: 19088

  ingress:
    enabled: false
    annotations: {}
      # kubernetes.io/ingress.class: nginx
      # kubernetes.io/tls-acme: "true"
    hosts: []
      # - cosbench.local
    tls: []
    #  - secretName: cosbench-tls
    #    hosts:
    #      - cosbench.local

  logLevel: DEBUG

# ------------------------------------------------------------------------------
# Driver:
# ------------------------------------------------------------------------------

driver:
  replicaCount: 1

  ## driver container image
  ##
  image:
    repository: zenko/zenko-cosbench
    tag: 0.0.6
    pullPolicy: IfNotPresent

  service:
    type: ClusterIP
    port: 18088

  ## driver log level
  ##
  logLevel: DEBUG

  ## Pod anitAffinity ["soft", "hard"]
  ##
  antiAffinity: soft
  antiAffinityWeight: 5

  ## hostAliases allow the modification of the hosts file inside a container
  ##
  hostAliases: []
  # - ip: "127.0.0.1"
  #   hostnames:
  #   - "cosbench.local"

  ## driver resource requests and limits
  ## Ref: http://kubernetes.io/docs/user-guide/compute-resources/
  ##
  resources:
    requests:
      cpu: 500m
      memory: 2Gi
    limits:
      cpu: 1
      memory: 4Gi
