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

{{/* Helm required labels */}}
{{- define "kanister-operator.helmLabels" -}}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
chart: {{ template "kanister-operator.chart" . }}
app: {{ template "kanister-operator.name" . }}
{{- end -}}

{{/*
Create the name of the service account to use
*/}}
{{- define "kanister-operator.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
    {{ default (include "kanister-operator.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}
 # deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "kanister-operator.fullname" . }}
  labels:
{{ include "kanister-operator.helmLabels" . | indent 4 }}
spec:
  replicas: 1
  selector:
    matchLabels:
      release: {{ .Release.Name }}
      app: {{ template "kanister-operator.name" . }}
  template:
    metadata:
      labels:
{{ include "kanister-operator.helmLabels" . | indent 8}}
    spec:
      serviceAccountName: {{ template "kanister-operator.serviceAccountName" . }}
      containers:
      - name: {{ template "kanister-operator.fullname" . }}
        image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- if .Values.resources }}
        resources:
{{ toYaml .Values.resources | indent 12 }}
{{- end }}
 # profile.yaml
{{ if .Values.create }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "profile.profileName" . }}-creds
  namespace: {{ .Release.Namespace }}
  labels:
{{ include "profile.helmLabels" . | indent 4 }}
type: Opaque
data:
  access_key_id: {{ required "Cloud provider API key is required when configuring a profile." .Values.s3.accessKey | b64enc | quote }}
  secret_access_key: {{ required "Cloud provider API secret is required when configuring a profile." .Values.s3.secretKey | b64enc | quote }}
---
apiVersion: cr.kanister.io/v1alpha1
kind: Profile
metadata:
  name: {{ template "profile.profileName" . }}
  namespace: {{ .Release.Namespace }}
  labels:
{{ include "profile.helmLabels" . | indent 4 }}
location:
  type: s3Compliant
  s3Compliant:
    bucket: {{ required "S3 compatible bucket is required when configuring a profile." .Values.s3.bucket | quote }}
    endpoint: {{ .Values.s3.endpoint | quote }}
    prefix: {{ .Values.s3.prefix | quote }}
    region: {{ .Values.s3.region | quote }}
credential:
  type: keyPair
  keyPair:
    idField: access_key_id
    secretField: secret_access_key
    secret:
      apiVersion: v1
      name: {{ template "profile.profileName" . }}-creds
      namespace: {{ .Release.Namespace }}
skipSSLVerify: {{ not .Values.verifySSL }}
{{ end }}
 # rbac.yaml
{{- if .Values.rbac.create }}
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  labels:
{{ include "kanister-operator.helmLabels" . | indent 4 }}
  name: {{ template "kanister-operator.fullname" . }}-cluster-role
rules:
- apiGroups:
  - ""
  resources:
  - events
  verbs:
  - create
- apiGroups:
  - apiextensions.k8s.io
  resources:
  - customresourcedefinitions
  verbs:
  - '*'
- apiGroups:
  - cr.kanister.io
  resources:
  - '*'
  verbs:
  - '*'
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  labels:
{{ include "kanister-operator.helmLabels" . | indent 4 }}
  name: {{ template "kanister-operator.fullname" . }}-edit-role
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: edit
subjects:
- kind: ServiceAccount
  name: {{ template "kanister-operator.serviceAccountName" . }}
  namespace: {{ .Release.Namespace }}
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  labels:
{{ include "kanister-operator.helmLabels" . | indent 4 }}
  name: {{ template "kanister-operator.fullname" . }}-cr-role
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ template "kanister-operator.fullname" . }}-cluster-role
subjects:
- kind: ServiceAccount
  name: {{ template "kanister-operator.serviceAccountName" . }}
  namespace: {{ .Release.Namespace }}
{{- end }}
 # serviceaccount.yaml
{{- if .Values.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
{{ include "kanister-operator.helmLabels" . | indent 4 }}
  name: {{ template "kanister-operator.serviceAccountName" . }}
{{- end }}

# Default values for kanister-profile.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
defaultProfile: true
defaultProfileName: default-profile
profileName:
# s3 properties
s3:
  bucket:
  endpoint:
  prefix:
  region:
  accessKey:
  secretKey:
verifySSL: true
