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

{{/*
Create the name of the service account to use for creating or deleting the ark server
*/}}
{{- define "ark.serverServiceAccount" -}}
{{- if .Values.serviceAccount.server.create -}}
    {{ default (printf "%s-%s" (include "ark.fullname" .) "server") .Values.serviceAccount.server.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccount.server.name }}
{{- end -}}
{{- end -}}

{{/*
Create the name for the credentials secret.
*/}}
{{- define "ark.secretName" -}}
{{- if .Values.credentials.existingSecret -}}
  {{- .Values.credentials.existingSecret -}}
{{- else -}}
  {{- template "ark.fullname" . -}}
{{- end -}}
{{- end -}}
 # backups.yaml
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: backups.ark.heptio.com
  labels:
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
  annotations:
    "helm.sh/hook": crd-install
    "helm.sh/hook-delete-policy": "before-hook-creation"
spec:
  group: ark.heptio.com
  version: v1
  scope: Namespaced
  names:
    plural: backups
    kind: Backup
 # backupstoragelocation.yaml
{{- $root := . }}
{{- with .Values.configuration }}
{{- with .backupStorageLocation }}
apiVersion: ark.heptio.com/v1
kind: BackupStorageLocation
metadata:
  name: default
  labels:
    chart: {{ template "ark.chart" $root }}
    heritage: {{ $root.Release.Service }}
    release: {{ $root.Release.Name }}
    app: {{ template "ark.name" $root }}
spec:
  provider: {{ .name  }}
  objectStorage:
    bucket: {{ .bucket  }}
    {{- with .prefix }}
    prefix: {{ . }}
    {{- end }}
{{- with .config }}
  config:
  {{- with .region }}
    region: {{ . }}
  {{- end }}
  {{- with .s3ForcePathStyle }}
    s3ForcePathStyle: {{ . | quote }}
  {{- end }}
  {{- with .s3Url }}
    s3Url: {{ . }}
  {{- end }}
  {{- with .kmsKeyId }}
    kmsKeyId: {{ . }}
  {{- end }}
  {{- with .resourceGroup }}
    resourceGroup: {{ . }}
  {{- end }}
  {{- with .storageAccount }}
    storageAccount: {{ . }}
  {{- end }}
  {{- if .publicUrl }}
  {{- with .publicUrl }}
    publicUrl: {{ . }}
  {{- end }}
  {{- end }}
{{- end }}
{{- end }}
{{- end }}
 # backupstoragelocations.yaml
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: backupstoragelocations.ark.heptio.com
  labels:
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
  annotations:
    "helm.sh/hook": crd-install
    "helm.sh/hook-delete-policy": "before-hook-creation"
spec:
  group: ark.heptio.com
  version: v1
  scope: Namespaced
  names:
    plural: backupstoragelocations
    kind: BackupStorageLocation
 # deletebackuprequests.yaml
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: deletebackuprequests.ark.heptio.com
  labels:
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
  annotations:
    "helm.sh/hook": crd-install
    "helm.sh/hook-delete-policy": "before-hook-creation"
spec:
  group: ark.heptio.com
  version: v1
  scope: Namespaced
  names:
    plural: deletebackuprequests
    kind: DeleteBackupRequest
 # deployment.yaml
{{- if .Values.configuration.provider -}}
{{- $provider := .Values.configuration.provider -}}
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: {{ template "ark.fullname" . }}
  labels:
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
spec:
  replicas: 1
  selector:
    matchLabels:
      release: {{ .Release.Name }}
      app: {{ template "ark.name" . }}
  template:
    metadata:
      labels:
        release: {{ .Release.Name }}
        app: {{ template "ark.name" . }}
      {{- if or .Values.podAnnotations .Values.metrics.enabled }}
      annotations:
{{- if .Values.podAnnotations }}
{{ toYaml .Values.podAnnotations | indent 8 }}
{{- end }}
{{- if .Values.metrics.enabled }}
{{ toYaml .Values.metrics.podAnnotations | indent 8 }}
{{- end }}
      {{- end }}
    spec:
      restartPolicy: Always
      serviceAccountName: {{ template "ark.serverServiceAccount" . }}
      containers:
        - name: ark
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.metrics.enabled }}
          ports:
            - name: monitoring
              containerPort: 8085
          {{- end }}
          command:
            - /ark
          args:
            - server
          {{- with .Values.configuration }}
            {{- with .backupSyncPeriod }}
            - --backup-sync-period={{ . }}
            {{- end }}
            {{- with .resticTimeout }}
            - --restic-timeout={{ . }}
            {{- end }}
            {{- if .restoreOnlyMode }}
            - --restore-only
            {{- end }}
            {{- with .restoreResourcePriorities }}
            - --restore-resource-priorities={{ . }}
            {{- end }}
          {{- end }}
          {{- if eq $provider "azure" }}
          envFrom:
            - secretRef:
                name: {{ template "ark.secretName" . }}
          {{- end }}
          {{- with .Values.resources }}
          resources:
{{ toYaml . | indent 12 }}
          {{- end }}
          volumeMounts:
            - name: plugins
              mountPath: /plugins
        {{- if and .Values.credentials.useSecret (or (eq $provider "aws") (eq $provider "gcp")) }}
            - name: cloud-credentials
              mountPath: /credentials
            - name: scratch
              mountPath: /scratch
          env:
          {{- if eq $provider "aws" }}
            - name: AWS_SHARED_CREDENTIALS_FILE
          {{- else }}
            - name: GOOGLE_APPLICATION_CREDENTIALS
          {{- end }}
              value: /credentials/cloud
            - name: ARK_SCRATCH_DIR
              value: /scratch
          {{ if .Values.configuration.extraEnvVars }}
          {{- range $key, $value := .Values.configuration.extraEnvVars }}
            - name: {{ default "none" $key }}
              value: {{ default "none" $value }}
          {{- end }}
          {{- end }}
        {{- end }}
{{- if .Values.initContainers }}
      initContainers:
{{ toYaml .Values.initContainers | indent 8 }}
{{- end }}
      volumes:
        {{- if and .Values.credentials.useSecret (or (eq $provider "aws") (eq $provider "gcp")) }}
        - name: cloud-credentials
          secret:
            secretName: {{ template "ark.secretName" . }}
        {{- end }}
        - name: plugins
          emptyDir: {}
        - name: scratch
          emptyDir: {}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
{{- end -}}
 # downloadrequests.yaml
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: downloadrequests.ark.heptio.com
  labels:
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
  annotations:
    "helm.sh/hook": crd-install
    "helm.sh/hook-delete-policy": "before-hook-creation"
spec:
  group: ark.heptio.com
  version: v1
  scope: Namespaced
  names:
    plural: downloadrequests
    kind: DownloadRequest
 # podvolumebackups.yaml
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: podvolumebackups.ark.heptio.com
  labels:
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
  annotations:
    "helm.sh/hook": crd-install
    "helm.sh/hook-delete-policy": "before-hook-creation"
spec:
  group: ark.heptio.com
  version: v1
  scope: Namespaced
  names:
    plural: podvolumebackups
    kind: PodVolumeBackup
 # podvolumerestores.yaml
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: podvolumerestores.ark.heptio.com
  labels:
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
  annotations:
    "helm.sh/hook": crd-install
    "helm.sh/hook-delete-policy": "before-hook-creation"
spec:
  group: ark.heptio.com
  version: v1
  scope: Namespaced
  names:
    plural: podvolumerestores
    kind: PodVolumeRestore
 # rbac.yaml
{{- if .Values.rbac.create }}
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: {{ template "ark.fullname" . }}-server
  labels:
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
subjects:
  - kind: ServiceAccount
    namespace: {{ .Release.Namespace }}
    name: {{ template "ark.serverServiceAccount" . }}
roleRef:
  kind: ClusterRole
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io
{{- end }}
 # restic-daemonset.yaml
{{- if .Values.deployRestic }}
{{- $provider := .Values.configuration.provider -}}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: restic
  labels:
    app: {{ template "ark.name" . }}
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
spec:
  selector:
    matchLabels:
      name: restic
  template:
    metadata:
      labels:
        name: restic
      {{- with .Values.podAnnotations }}
      annotations:
{{ toYaml . | indent 8 }}
      {{- end }}
    spec:
      {{- if .Values.serviceAccount.server.create }}
      serviceAccountName: {{ template "ark.serverServiceAccount" . }}
      {{- end }}
      securityContext:
        runAsUser: 0
      volumes:
        {{- if and .Values.credentials.useSecret (or (eq $provider "aws") (eq $provider "gcp")) }}
        - name: cloud-credentials
          secret:
            secretName: {{ template "ark.secretName" . }}
        {{- end }}
        - name: host-pods
          hostPath:
            path: /var/lib/kubelet/pods
        - name: scratch
          emptyDir: {}
      containers:
        - name: ark
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - /ark
          args:
            - restic
            - server
          volumeMounts:
            {{- if and .Values.credentials.useSecret (or (eq $provider "aws") (eq $provider "gcp")) }}
            - name: cloud-credentials
              mountPath: /credentials
            {{- end }}
            - name: host-pods
              mountPath: /host_pods
              mountPropagation: HostToContainer
            - name: scratch
              mountPath: /scratch
          {{- if and .Values.credentials.useSecret (eq $provider "azure") }}
          envFrom:
            - secretRef:
                name: {{ template "ark.secretName" . }}
          {{- end }}
          env:
            - name: HEPTIO_ARK_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: ARK_SCRATCH_DIR
              value: /scratch
            {{- if eq $provider "aws" }}
            - name: AWS_SHARED_CREDENTIALS_FILE
              value: /credentials/cloud
            {{- end }}
            {{- if eq $provider "gcp" }}
            - name: GOOGLE_APPLICATION_CREDENTIALS
              value: /credentials/cloud
            {{- end }}
            {{- if eq $provider "minio" }}
            - name: AWS_SHARED_CREDENTIALS_FILE
              value: /credentials/cloud
            {{- end }}
          {{- with .Values.resources }}
          resources:
{{ toYaml . | indent 12 }}
          {{- end }}
{{- end }}
 # resticrepositories.yaml
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: resticrepositories.ark.heptio.com
  labels:
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
  annotations:
    "helm.sh/hook": crd-install
    "helm.sh/hook-delete-policy": "before-hook-creation"
spec:
  group: ark.heptio.com
  version: v1
  scope: Namespaced
  names:
    plural: resticrepositories
    kind: ResticRepository
 # restores.yaml
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: restores.ark.heptio.com
  labels:
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
  annotations:
    "helm.sh/hook": crd-install
    "helm.sh/hook-delete-policy": "before-hook-creation"
spec:
  group: ark.heptio.com
  version: v1
  scope: Namespaced
  names:
    plural: restores
    kind: Restore
 # schedule.yaml
{{- $root := . }}
{{- range $scheduleName, $schedule := .Values.schedules }}
apiVersion: ark.heptio.com/v1
kind: Schedule
metadata:
  name: {{ template "ark.fullname" $root }}-{{ $scheduleName }}
  labels:
    chart: {{ template "ark.chart" $root }}
    heritage: {{ $root.Release.Service }}
    release: {{ $root.Release.Name }}
    app: {{ template "ark.name" $root }}
spec:
  schedule: {{ $schedule.schedule | quote }}
{{- with $schedule.template }}
  template:
{{ toYaml . | indent 8 }}
{{- end }}
---
{{- end }}
 # schedules.yaml
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: schedules.ark.heptio.com
  labels:
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
  annotations:
    "helm.sh/hook": crd-install
    "helm.sh/hook-delete-policy": "before-hook-creation"
spec:
  group: ark.heptio.com
  version: v1
  scope: Namespaced
  names:
    plural: schedules
    kind: Schedule
 # secret.yaml
{{- if and .Values.credentials.useSecret (not .Values.credentials.existingSecret) -}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "ark.fullname" . }}
  labels:
    app: {{ template "ark.name" . }}
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
type: Opaque
data:
{{- range $key, $value := .Values.credentials.secretContents }}
  {{ $key }}: {{ $value | b64enc | quote }}
{{- end }}
{{- end -}}
 # service.yaml
{{- if .Values.metrics.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "ark.fullname" . }}
  labels:
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
spec:
  type: ClusterIP
  ports:
    - name: monitoring
      port: 8085
      targetPort: monitoring
  selector:
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
{{- end }}
 # serviceaccount-server.yaml
{{- if .Values.serviceAccount.server.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "ark.serverServiceAccount" . }}
  labels:
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
{{- end }}
 # servicemonitor.yaml
{{- if and .Values.metrics.enabled .Values.metrics.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "ark.fullname" . }}
  labels:
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
    {{- if .Values.metrics.serviceMonitor.additionalLabels }}
{{ toYaml .Values.metrics.serviceMonitor.additionalLabels | indent 4 }}
    {{- end }}
spec:
  selector:
    matchLabels:
      release: {{ .Release.Name }}
      app: {{ template "ark.name" . }}
  endpoints:
  - port: monitoring
    interval: {{ .Values.metrics.scrapeInterval }}
{{- end }}
 # volumesnapshotlocation.yaml
{{- $root := . }}
{{- with .Values.configuration }}
{{- with .volumeSnapshotLocation }}
apiVersion: ark.heptio.com/v1
kind: VolumeSnapshotLocation
metadata:
  name: default
  labels:
    chart: {{ template "ark.chart" $root }}
    heritage: {{ $root.Release.Service }}
    release: {{ $root.Release.Name }}
    app: {{ template "ark.name" $root }}
spec:
  provider: {{ .name  }}
  objectStorage:
    bucket: {{ .bucket  }}
{{ with .config }}
  config:
  {{- with .region }}
    region: {{ . }}
  {{- end }}
  {{- with .apitimeout }}
    apiTimeout: {{ . }}
  {{- end }}
{{- end }}
{{- end }}
{{- end }}
 # volumesnapshotlocations.yaml
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: volumesnapshotlocations.ark.heptio.com
  labels:
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
  annotations:
    "helm.sh/hook": crd-install
    "helm.sh/hook-delete-policy": "before-hook-creation"
spec:
  group: ark.heptio.com
  version: v1
  scope: Namespaced
  names:
    plural: volumesnapshotlocations
    kind: VolumeSnapshotLocation

image:
  repository: gcr.io/heptio-images/ark
  tag: v0.10.2
  pullPolicy: IfNotPresent

# Only kube2iam/kiam: change the AWS_ACCOUNT_ID and HEPTIO_ARK_ROLE_NAME
podAnnotations: {}
#  iam.amazonaws.com/role: arn:aws:iam::<AWS_ACCOUNT_ID>:role/<HEPTIO_ARK_ROLE_NAME>
#  prometheus.io/scrape: "true"
#  prometheus.io/port: "8085"
#  prometheus.io/path: "/metrics"

rbac:
  create: true

resources: {}

# this is the k8s spec block for initContainers:
initContainers: []
  # - name:
  #   image:
  #   volumeMounts:
  #     - name: plugins
  #       mountPath: /target


serviceAccount:
  server:
    create: true
    name:

tolerations: []

nodeSelector: {}

## Parameters for the ' default' Config resource
## See https://heptio.github.io/ark/v0.9.0/config-definition
configuration:
  provider:

  volumeSnapshotLocation: {}
  #  name:
  #  config:
  #    region:
  #    apiTimeout:

  backupStorageLocation:
    name:
    bucket:
    # prefix:
    config: {}
    #  region:
    #  s3ForcePathStyle:
    #  s3Url:
    #  kmsKeyId:
    #  resourceGroup:
    #  storageAccount:
    #  publicUrl:

  backupSyncPeriod: 60m
  resticTimeout: 1h
  restoreResourcePriorities: namespaces,persistentvolumes,persistentvolumeclaims,secrets,configmaps,serviceaccounts,limitranges,pods
  restoreOnlyMode: false
  # additional key/value pairs to be used as environment variables such as "AWS_CLUSTER_NAME: 'yourcluster.domain.tld'"
  extraEnvVars: {}

# Eg:
# schedules:
#   mybackup:
#     schedule: "0 0 * * *"
#     template:
#       ttl: "240h"
#       includedNamespaces:
#        - foo
schedules: {}

credentials:
  existingSecret:
  useSecret: true
  secretContents: {}

deployRestic: false

metrics:
  enabled: false
  scrapeInterval: 30s

  # Pod annotations for Prometheus
  podAnnotations:
    prometheus.io/scrape: "true"
    prometheus.io/port: "8085"

  serviceMonitor:
    enabled: false
    additionalLabels: {}
