From 022e199fad3a3e29d89fd964c233094363db0338 Mon Sep 17 00:00:00 2001
From: Janne Mareike Koschinski <janne@kuschku.de>
Date: Thu, 26 May 2022 16:08:07 +0200
Subject: [PATCH] feat: improve restic backup options

---
 .../{cronjob.yaml => cronjob_backup.yaml}     |  2 +-
 restic/templates/cronjob_forget.yaml          | 62 +++++++++++++++++++
 ...install-job.yaml => post-install-job.yaml} | 10 +--
 restic/values.yaml                            |  4 ++
 4 files changed, 69 insertions(+), 9 deletions(-)
 rename restic/templates/{cronjob.yaml => cronjob_backup.yaml} (97%)
 create mode 100644 restic/templates/cronjob_forget.yaml
 rename restic/templates/{pre-install-job.yaml => post-install-job.yaml} (86%)

diff --git a/restic/templates/cronjob.yaml b/restic/templates/cronjob_backup.yaml
similarity index 97%
rename from restic/templates/cronjob.yaml
rename to restic/templates/cronjob_backup.yaml
index 502d4d6..d28be75 100644
--- a/restic/templates/cronjob.yaml
+++ b/restic/templates/cronjob_backup.yaml
@@ -1,7 +1,7 @@
 apiVersion: batch/v1
 kind: CronJob
 metadata:
-  name: {{ include "restic-helm.fullname" . }}
+  name: {{ include "restic-helm.fullname" . }}-backup
   labels:
     {{- include "restic-helm.labels" . | nindent 4 }}
 spec:
diff --git a/restic/templates/cronjob_forget.yaml b/restic/templates/cronjob_forget.yaml
new file mode 100644
index 0000000..cbd2e83
--- /dev/null
+++ b/restic/templates/cronjob_forget.yaml
@@ -0,0 +1,62 @@
+apiVersion: batch/v1
+kind: CronJob
+metadata:
+  name: {{ include "restic-helm.fullname" . }}-forget
+  labels:
+    {{- include "restic-helm.labels" . | nindent 4 }}
+spec:
+  schedule: "{{ .Values.schedule }}"
+  jobTemplate:
+    metadata:
+      {{- with .Values.jobAnnotations }}
+      annotations:
+        {{- toYaml . | nindent 8 }}
+      {{- end }}
+      labels:
+        {{- include "restic-helm.labels" . | nindent 8 }}
+    spec:
+      template:
+        metadata:
+          {{- with .Values.podAnnotations }}
+          annotations:
+            {{- toYaml . | nindent 12 }}
+          {{- end }}
+          labels:
+            {{- include "restic-helm.labels" . | nindent 12 }}
+        spec:
+          {{- with .Values.imagePullSecrets }}
+          imagePullSecrets:
+            {{- toYaml . | nindent 12 }}
+          {{- end }}
+          securityContext:
+            {{- toYaml .Values.podSecurityContext | nindent 12 }}
+          containers:
+            - name: {{ .Chart.Name }}
+              securityContext:
+                {{- toYaml .Values.securityContext | nindent 16 }}
+              image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
+              imagePullPolicy: {{ .Values.image.pullPolicy }}
+              workingDir: "/sync"
+              args:
+                - "-r"
+                - "{{ .Values.repository.name }}"
+                - "forget"
+                - "--prune"
+                {{- toYaml .Values.keepOptions | nindent 16 }}
+              envFrom:
+                - secretRef:
+                    name: {{ include "restic-helm.fullname" . }}
+              resources:
+                {{- toYaml .Values.resources | nindent 16 }}
+          {{- with .Values.nodeSelector }}
+          nodeSelector:
+            {{- toYaml . | nindent 12 }}
+          {{- end }}
+          {{- with .Values.affinity }}
+          affinity:
+            {{- toYaml . | nindent 12 }}
+          {{- end }}
+          {{- with .Values.tolerations }}
+          tolerations:
+            {{- toYaml . | nindent 12 }}
+          {{- end }}
diff --git a/restic/templates/pre-install-job.yaml b/restic/templates/post-install-job.yaml
similarity index 86%
rename from restic/templates/pre-install-job.yaml
rename to restic/templates/post-install-job.yaml
index 9a8d92d..347202a 100644
--- a/restic/templates/pre-install-job.yaml
+++ b/restic/templates/post-install-job.yaml
@@ -1,9 +1,9 @@
 apiVersion: batch/v1
 kind: Job
 metadata:
-  name: {{ include "restic-helm.fullname" . }}
+  name: {{ include "restic-helm.fullname" . }}-init
   annotations:
-    "helm.sh/hook": pre-install
+    "helm.sh/hook": post-install
     "helm.sh/hook-weight": "-5"
     "helm.sh/hook-delete-policy": hook-succeeded
   labels:
@@ -25,9 +25,6 @@ spec:
       {{- end }}
       securityContext:
         {{- toYaml .Values.podSecurityContext | nindent 8 }}
-      volumes:
-        - name: data
-          {{- .Values.volume | nindent 10 }}
       containers:
         - name: {{ .Chart.Name }}
           securityContext:
@@ -44,9 +41,6 @@ spec:
                 name: {{ include "restic-helm.fullname" . }}
           resources:
             {{- toYaml .Values.resources | nindent 12 }}
-          volumeMounts:
-            - mountPath: "/sync"
-              name: data
       {{- with .Values.nodeSelector }}
       nodeSelector:
         {{- toYaml . | nindent 8 }}
diff --git a/restic/values.yaml b/restic/values.yaml
index 039b575..4fbdc68 100644
--- a/restic/values.yaml
+++ b/restic/values.yaml
@@ -12,6 +12,10 @@ volume: |-
 
 schedule: "0 0 * * *"
 
+keepOptions:
+  - "--keep-daily"
+  - "7"
+
 auth: |-
   B2_ACCOUNT_ID: "AzureDiamond"
   B2_ACCOUNT_KEY: "hunter2"
-- 
GitLab