From a12f0b384038c7f3f5ba4ac8487a87b8d0b58a44 Mon Sep 17 00:00:00 2001 From: Michael Weiss Date: Fri, 5 Jan 2024 18:12:58 +0100 Subject: [PATCH] first commit --- .helmignore | 23 +++++++ Chart.yaml | 24 +++++++ README.md | 51 +++++++++++++++ templates/_helpers.tpl | 51 +++++++++++++++ templates/cronjob.yaml | 140 +++++++++++++++++++++++++++++++++++++++++ templates/job.yaml | 111 ++++++++++++++++++++++++++++++++ templates/secret.yaml | 52 +++++++++++++++ values.yaml | 52 +++++++++++++++ 8 files changed, 504 insertions(+) create mode 100644 .helmignore create mode 100644 Chart.yaml create mode 100644 README.md create mode 100644 templates/_helpers.tpl create mode 100644 templates/cronjob.yaml create mode 100644 templates/job.yaml create mode 100644 templates/secret.yaml create mode 100644 values.yaml diff --git a/.helmignore b/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/Chart.yaml b/Chart.yaml new file mode 100644 index 0000000..9967e7e --- /dev/null +++ b/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: rustic +description: fast, encrypted and deduplicated backups + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "v0.6.1" diff --git a/README.md b/README.md new file mode 100644 index 0000000..8ef5303 --- /dev/null +++ b/README.md @@ -0,0 +1,51 @@ +# Rustic + +[Rustic](https://rustic.cli.rs/) is a backup tool that provides fast, encrypted and deduplicated backups. + +It reads and writes the [restic](https://github.com/restic/restic) repo format described in the [design document](https://github.com/restic/restic/blob/master/doc/design.rst) and can be used as a restic replacement in most cases. + +## Introduction + +This chart will set up rustic as a Kubernetes CronJob + +## Installing + +### Prerequisites + +1. Kubernetes >=1.29 or 1.28 (with featureGate `SidecarContainers` enabled) +2. Helm v3 (Tested with v3.11.2) +3. Rustic's chart repository: `helm repo add rustic https://rustic` + + +Bare minimum `values.yml`: +``` +hostname: + +rustic: + encryption_secret: "***" + backup_volume: + hostPath: + path: /path/to/backupfolder + +s3: + repo: + endpoint: s3.fr-par.scw.cloud + access_key_id: + secret_access_key: + region: fr-par + bucket: k8s-data-cold +``` + +### Deploying rustic + +``` +helm install rustic rustic/rustic --set rustic.init=true -f values.yml +``` + +If there is already an initialized restic repository in the Bucket omit `--set rustic.init=true`: + +``` +helm install rustic rustic/rustic -f values.yml +``` + +### Examples \ No newline at end of file diff --git a/templates/_helpers.tpl b/templates/_helpers.tpl new file mode 100644 index 0000000..96256ea --- /dev/null +++ b/templates/_helpers.tpl @@ -0,0 +1,51 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "rustic.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 "rustic.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 "rustic.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "rustic.labels" -}} +helm.sh/chart: {{ include "rustic.chart" . }} +{{ include "rustic.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "rustic.selectorLabels" -}} +app.kubernetes.io/name: {{ include "rustic.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} diff --git a/templates/cronjob.yaml b/templates/cronjob.yaml new file mode 100644 index 0000000..7331458 --- /dev/null +++ b/templates/cronjob.yaml @@ -0,0 +1,140 @@ +apiVersion: batch/v1 +kind: CronJob +metadata: + name: {{ include "rustic.fullname" . }} + labels: + {{- include "rustic.labels" . | nindent 4 }} +spec: + schedule: "{{ .Values.cronjob.schedule }}" + failedJobsHistoryLimit: {{ .Values.cronjob.failedJobsHistoryLimit }} + successfulJobsHistoryLimit: {{ .Values.cronjob.successfulJobsHistoryLimit }} + jobTemplate: + spec: + template: + metadata: + labels: + {{- include "rustic.labels" . | nindent 12 }} + spec: + automountServiceAccountToken: false + hostname: {{ .Values.hostname }} + restartPolicy: {{ .Values.cronjob.restartPolicy }} + initContainers: + - name: rclone + image: "{{ .Values.rclone.image.repository }}:{{ .Values.rclone.image.tag }}" + imagePullPolicy: {{ .Values.rclone.image.pullPolicy }} + restartPolicy: Always + args: + - serve + - restic + - -v + - --addr + - :8080 + - repo:{{ .Values.s3.repo.bucket }} + volumeMounts: + - mountPath: /config/rclone + name: rclone-config + startupProbe: + httpGet: + path: / + port: 8080 + httpHeaders: + - name: Accept + value: application/vnd.x.restic.rest.v2 + failureThreshold: 30 + periodSeconds: 1 + resources: + {{- toYaml .Values.rclone.resources | nindent 16 }} + securityContext: + {{- toYaml .Values.rclone.securityContext | nindent 16 }} + {{- if .Values.s3.repo_hot }} + - name: rclone-hot + image: "{{ .Values.rclone.image.repository }}:{{ .Values.rclone.image.tag }}" + imagePullPolicy: {{ .Values.rclone.image.pullPolicy }} + restartPolicy: Always + args: + - serve + - restic + - -v + - --addr + - :8081 + - repo-hot:{{ .Values.s3.repo_hot.bucket }} + volumeMounts: + - mountPath: /config/rclone + name: rclone-hot-config + startupProbe: + httpGet: + path: / + port: 8081 + httpHeaders: + - name: Accept + value: application/vnd.x.restic.rest.v2 + failureThreshold: 30 + periodSeconds: 1 + resources: + {{- toYaml .Values.rclone.resources | nindent 16 }} + securityContext: + {{- toYaml .Values.rclone.securityContext | nindent 16 }} + {{- end }} + containers: + - name: {{ .Chart.Name }} + image: "{{ .Values.rustic.image.repository }}:{{ .Values.rustic.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.rustic.image.pullPolicy }} + args: + - backup + - --password-file + - "/etc/rustic/.rustic" + - --repository + - rest:http://localhost:8080/ + {{- if .Values.s3.repo_hot }} + - --repo-hot + - rest:http://localhost:8081/ + {{- end }} + {{- if .Values.persistence }} + - --cache-dir + - /cache + {{- end }} + - "/backup" + volumeMounts: + - mountPath: /backup + name: backup-folder + readOnly: true + - mountPath: /etc/rustic + name: rustic-encryption-secret + {{- if .Values.persistence }} + - name: cache + mountPath: /cache + {{- end }} + resources: + {{- toYaml .Values.rustic.resources | nindent 16 }} + securityContext: + {{- toYaml .Values.rustic.securityContext | nindent 16 }} + volumes: + - name: backup-folder + {{- toYaml .Values.rustic.backup_volume | nindent 14 }} + - name: rustic-encryption-secret + secret: + secretName: {{ include "rustic.fullname" . }}-encryption-secret + {{- with .Values.persistence }} + - name: cache + {{- toYaml . | nindent 14 }} + {{- end }} + - name: rclone-config + secret: + secretName: {{ include "rustic.fullname" . }}-rclone + {{- if .Values.s3.repo_hot }} + - name: rclone-hot-config + secret: + secretName: {{ include "rustic.fullname" . }}-rclone-hot + {{- end }} + {{- 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/templates/job.yaml b/templates/job.yaml new file mode 100644 index 0000000..1cf7cf7 --- /dev/null +++ b/templates/job.yaml @@ -0,0 +1,111 @@ +{{- if eq .Values.rustic.init true }} +apiVersion: batch/v1 +kind: Job +metadata: + name: {{ include "rustic.fullname" . }}-init + labels: + {{- include "rustic.labels" . | nindent 4 }} +spec: + ttlSecondsAfterFinished: 100 + template: + metadata: + labels: + {{- include "rustic.labels" . | nindent 8 }} + spec: + automountServiceAccountToken: false + restartPolicy: OnFailure + initContainers: + - name: rclone + image: "{{ .Values.rclone.image.repository }}:{{ .Values.rclone.image.tag }}" + imagePullPolicy: {{ .Values.rclone.image.pullPolicy }} + restartPolicy: Always + args: + - serve + - restic + - -v + - --addr + - :8080 + - repo:{{ .Values.s3.repo.bucket }} + volumeMounts: + - mountPath: /config/rclone + name: rclone-config + startupProbe: + tcpSocket: + port: 8080 + failureThreshold: 30 + periodSeconds: 1 + resources: + {{- toYaml .Values.rclone.resources | nindent 16 }} + securityContext: + {{- toYaml .Values.rclone.securityContext | nindent 16 }} + {{- if .Values.s3.repo_hot }} + - name: rclone-hot + image: "{{ .Values.rclone.image.repository }}:{{ .Values.rclone.image.tag }}" + imagePullPolicy: {{ .Values.rclone.image.pullPolicy }} + restartPolicy: Always + args: + - serve + - restic + - -v + - --addr + - :8081 + - repo-hot:{{ .Values.s3.repo_hot.bucket }} + volumeMounts: + - mountPath: /config/rclone + name: rclone-hot-config + startupProbe: + tcpSocket: + port: 8081 + failureThreshold: 30 + periodSeconds: 1 + resources: + {{- toYaml .Values.rclone.resources | nindent 16 }} + securityContext: + {{- toYaml .Values.rclone.securityContext | nindent 16 }} + {{- end }} + containers: + - name: {{ .Chart.Name }} + image: "{{ .Values.rustic.image.repository }}:{{ .Values.rustic.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.rustic.image.pullPolicy }} + args: + - init + - --password-file + - "/etc/rustic/.rustic" + - --repository + - rest:http://localhost:8080/ + {{- if .Values.s3.repo_hot }} + - --repo-hot + - rest:http://localhost:8081/ + {{- end }} + volumeMounts: + - mountPath: /etc/rustic + name: rustic-encryption-secret + resources: + {{- toYaml .Values.rustic.resources | nindent 16 }} + securityContext: + {{- toYaml .Values.rustic.securityContext | nindent 16 }} + volumes: + - name: rustic-encryption-secret + secret: + secretName: {{ include "rustic.fullname" . }}-encryption-secret + - name: rclone-config + secret: + secretName: {{ include "rustic.fullname" . }}-rclone + {{- if .Values.s3.repo_hot }} + - name: rclone-hot-config + secret: + secretName: {{ include "rustic.fullname" . }}-rclone-hot + {{- end }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 12 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 12 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 12 }} + {{- end }} +{{- end }} \ No newline at end of file diff --git a/templates/secret.yaml b/templates/secret.yaml new file mode 100644 index 0000000..c9334ec --- /dev/null +++ b/templates/secret.yaml @@ -0,0 +1,52 @@ +apiVersion: v1 +kind: Secret +metadata: + name: {{ include "rustic.fullname" . }}-encryption-secret +type: Opaque +stringData: + .rustic: "{{ .Values.rustic.encryption_secret }}" + +--- +apiVersion: v1 +kind: Secret +metadata: + name: {{ include "rustic.fullname" . }}-rclone +type: Opaque +stringData: + rclone.conf: | + [repo] + type = s3 + provider = + env_auth = false + endpoint = {{ .Values.s3.repo.endpoint }} + access_key_id = {{ .Values.s3.repo.access_key_id }} + secret_access_key = {{ .Values.s3.repo.secret_access_key }} + region = {{ .Values.s3.repo.region }} + location_constraint = + acl = private + server_side_encryption = + storage_class = {{ .Values.s3.repo.storage_class }} + +{{- if .Values.s3.repo_hot }} +--- +apiVersion: v1 +kind: Secret +metadata: + name: {{ include "rustic.fullname" . }}-rclone-hot +type: Opaque +stringData: + rclone.conf: | + [repo-hot] + type = s3 + provider = + env_auth = false + endpoint = {{ .Values.s3.repo_hot.endpoint }} + access_key_id = {{ .Values.s3.repo_hot.access_key_id }} + secret_access_key = {{ .Values.s3.repo_hot.secret_access_key }} + region = {{ .Values.s3.repo_hot.region }} + location_constraint = + acl = private + server_side_encryption = + storage_class = {{ .Values.s3.repo_hot.storage_class }} + +{{- end }} \ No newline at end of file diff --git a/values.yaml b/values.yaml new file mode 100644 index 0000000..1e385ba --- /dev/null +++ b/values.yaml @@ -0,0 +1,52 @@ +# Default values for rustic. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +cronjob: + schedule: "0 1 * * *" + failedJobsHistoryLimit: 3 + successfulJobsHistoryLimit: 1 + restartPolicy: OnFailure + +nameOverride: "" +fullnameOverride: "" + +rclone: + image: + repository: rclone/rclone + pullPolicy: IfNotPresent + tag: 1.65.0 + resources: {} + securityContext: + runAsUser: 1009 + runAsGroup: 1009 + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + +rustic: + image: + repository: rusticdocker/rustic + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "" + init: false + resources: + requests: + memory: 50Mi + cpu: 100m + limits: + memory: 192Mi + cpu: 200m + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + +nodeSelector: {} + +tolerations: [] + +affinity: {}