From cd9c7cccd4be2f2a0a157a02fcc8f9543bf8e039 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Sat, 25 Nov 2023 15:59:50 +0200 Subject: [PATCH] spec --- cspell.config.yaml | 3 +- .../tests/veleroSchedule/metadata_test.yaml | 63 +++++++++++++ .../tests/veleroSchedule/names_test.yaml | 37 ++++++++ .../tests/veleroSchedule/spec_test.yaml | 94 +++++++++++++++++++ .../templates/class/velero/_schedule.tpl | 14 +-- .../lib/velero/_scheduleValidation.tpl | 8 ++ .../templates/spawner/velero/_schedule.tpl | 15 ++- library/common/values.yaml | 1 + 8 files changed, 219 insertions(+), 16 deletions(-) create mode 100644 library/common-test/tests/veleroSchedule/metadata_test.yaml create mode 100644 library/common-test/tests/veleroSchedule/names_test.yaml create mode 100644 library/common-test/tests/veleroSchedule/spec_test.yaml diff --git a/cspell.config.yaml b/cspell.config.yaml index 6b06eb75..526c795b 100644 --- a/cspell.config.yaml +++ b/cspell.config.yaml @@ -1,2 +1,3 @@ words: -- tailscale + - tailscale + - Velero diff --git a/library/common-test/tests/veleroSchedule/metadata_test.yaml b/library/common-test/tests/veleroSchedule/metadata_test.yaml new file mode 100644 index 00000000..8f37a932 --- /dev/null +++ b/library/common-test/tests/veleroSchedule/metadata_test.yaml @@ -0,0 +1,63 @@ +suite: velero schedule metadata test +templates: + - common.yaml +chart: + appVersion: &appVer v9.9.9 +release: + name: test-release-name + namespace: test-release-namespace +tests: + - it: should pass with storageClass created with labels and annotations + set: + label1: label1 + label2: global_label2 + annotation1: annotation1 + annotation2: global_annotation2 + global: + labels: + g_label1: global_label1 + g_label2: "{{ .Values.label2 }}" + annotations: + g_annotation1: global_annotation1 + g_annotation2: "{{ .Values.annotation2 }}" + schedules: + my-sched: + enabled: true + labels: + label1: "{{ .Values.label1 }}" + label2: label2 + annotations: + annotation1: "{{ .Values.annotation1 }}" + annotation2: annotation2 + schedule: "0 0 * * *" + asserts: + - documentIndex: &scheduleDoc 0 + isKind: + of: Schedule + - documentIndex: *scheduleDoc + isAPIVersion: + of: velero.io/v1 + - documentIndex: *scheduleDoc + equal: + path: metadata.annotations + value: + annotation1: annotation1 + annotation2: annotation2 + g_annotation1: global_annotation1 + g_annotation2: global_annotation2 + - documentIndex: *scheduleDoc + equal: + path: metadata.labels + value: + app: common-test-1.0.0 + release: test-release-name + helm-revision: "0" + helm.sh/chart: common-test-1.0.0 + app.kubernetes.io/name: common-test + app.kubernetes.io/instance: test-release-name + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/version: *appVer + g_label1: global_label1 + g_label2: global_label2 + label1: label1 + label2: label2 diff --git a/library/common-test/tests/veleroSchedule/names_test.yaml b/library/common-test/tests/veleroSchedule/names_test.yaml new file mode 100644 index 00000000..0671bbd1 --- /dev/null +++ b/library/common-test/tests/veleroSchedule/names_test.yaml @@ -0,0 +1,37 @@ +suite: velero schedule name test +templates: + - common.yaml +release: + name: test-release-name + namespace: test-release-namespace +tests: + - it: should generate correct name + set: + schedules: + my-sched1: + enabled: true + schedule: "0 0 * * *" + my-sched2: + enabled: true + schedule: "0 0 * * *" + asserts: + - documentIndex: &scheduleDoc 0 + isKind: + of: Schedule + - documentIndex: *scheduleDoc + isAPIVersion: + of: velero.io/v1 + - documentIndex: *scheduleDoc + equal: + path: metadata.name + value: test-release-name-common-test-my-sched1 + - documentIndex: &otherScheduleDoc 1 + isKind: + of: Schedule + - documentIndex: *otherScheduleDoc + isAPIVersion: + of: velero.io/v1 + - documentIndex: *otherScheduleDoc + equal: + path: metadata.name + value: test-release-name-common-test-my-sched2 diff --git a/library/common-test/tests/veleroSchedule/spec_test.yaml b/library/common-test/tests/veleroSchedule/spec_test.yaml new file mode 100644 index 00000000..d8cd3c8c --- /dev/null +++ b/library/common-test/tests/veleroSchedule/spec_test.yaml @@ -0,0 +1,94 @@ +suite: velero schedule spec test +templates: + - common.yaml +release: + name: test-release-name + namespace: test-release-namespace +tests: + - it: should generate correct spec with provided provider and credential + set: + schedules: + my-sched: + enabled: true + schedule: "0 2 * * *" + asserts: + - documentIndex: &scheduleDoc 0 + isKind: + of: Schedule + - documentIndex: *scheduleDoc + isAPIVersion: + of: velero.io/v1 + - documentIndex: *scheduleDoc + equal: + path: spec + value: + schedule: "0 2 * * *" + + - it: should generate correct spec with useOwnerReferencesInBackup + set: + schedules: + my-sched: + enabled: true + schedule: "0 2 * * *" + useOwnerReferencesInBackup: true + asserts: + - documentIndex: &scheduleDoc 0 + isKind: + of: Schedule + - documentIndex: *scheduleDoc + isAPIVersion: + of: velero.io/v1 + - documentIndex: *scheduleDoc + equal: + path: spec + value: + schedule: "0 2 * * *" + useOwnerReferencesInBackup: true + + - it: should generate correct spec with template + set: + schedules: + my-sched: + enabled: true + schedule: "0 2 * * *" + template: + ttl: 720h + includeClusterResources: true + snapshotVolumes: true + asserts: + - documentIndex: &scheduleDoc 0 + isKind: + of: Schedule + - documentIndex: *scheduleDoc + isAPIVersion: + of: velero.io/v1 + - documentIndex: *scheduleDoc + equal: + path: spec + value: + schedule: "0 2 * * *" + template: + ttl: 720h + includeClusterResources: true + snapshotVolumes: true + + # Failures + - it: should fail without schedule + set: + schedules: + my-sched: + enabled: true + asserts: + - failedTemplate: + errorMessage: Velero Schedule - Expected non-empty [schedule] + + - it: should fail with useOwnerReferencesInBackup not a bool + set: + schedules: + my-sched: + enabled: true + schedule: "0 2 * * *" + useOwnerReferencesInBackup: "true" + asserts: + - failedTemplate: + errorMessage: Velero Schedule - Expected [useOwnerReferencesInBackup] to be a boolean diff --git a/library/common/templates/class/velero/_schedule.tpl b/library/common/templates/class/velero/_schedule.tpl index af6e5a91..ff5419e0 100644 --- a/library/common/templates/class/velero/_schedule.tpl +++ b/library/common/templates/class/velero/_schedule.tpl @@ -16,10 +16,10 @@ objectData: {{- $objectData := .objectData }} --- apiVersion: velero.io/v1 -kind: schedule +kind: Schedule metadata: name: {{ $objectData.name }} - namespace: {{ include "tc.v1.common.lib.metadata.namespace" (dict "rootCtx" $rootCtx "objectData" $objectData "caller" "schedule") }} + namespace: {{ include "tc.v1.common.lib.metadata.namespace" (dict "rootCtx" $rootCtx "objectData" $objectData "caller" "Schedule") }} {{- $labels := (mustMerge ($objectData.labels | default dict) (include "tc.v1.common.lib.metadata.allLabels" $rootCtx | fromYaml)) -}} {{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $labels) | trim) }} labels: @@ -31,12 +31,12 @@ metadata: {{- . | nindent 4 }} {{- end }} spec: -{{- if $objectData.useOwnerReferencesInBackup }} - useOwnerReferencesInBackup: {{ $objectData.useOwnerReferencesInBackup }} -{{- end }} schedule: {{ $objectData.schedule | quote }} -{{- with $objectData.template }} + {{- if (kindIs "bool" $objectData.useOwnerReferencesInBackup) }} + useOwnerReferencesInBackup: {{ $objectData.useOwnerReferencesInBackup }} + {{- end -}} + {{- with $objectData.template }} template: {{- toYaml . | nindent 4 }} -{{- end }} + {{- end -}} {{- end -}} diff --git a/library/common/templates/lib/velero/_scheduleValidation.tpl b/library/common/templates/lib/velero/_scheduleValidation.tpl index 381d2a57..94d88524 100644 --- a/library/common/templates/lib/velero/_scheduleValidation.tpl +++ b/library/common/templates/lib/velero/_scheduleValidation.tpl @@ -10,5 +10,13 @@ objectData: {{- $rootCtx := .rootCtx -}} {{- $objectData := .objectData -}} + {{- if not $objectData.schedule -}} + {{- fail "Velero Schedule - Expected non-empty [schedule]" -}} + {{- end -}} + {{- if (hasKey $objectData "useOwnerReferencesInBackup") -}} + {{- if not (kindIs "bool" $objectData.useOwnerReferencesInBackup) -}} + {{ fail "Velero Schedule - Expected [useOwnerReferencesInBackup] to be a boolean" -}} + {{- end -}} + {{- end -}} {{- end -}} diff --git a/library/common/templates/spawner/velero/_schedule.tpl b/library/common/templates/spawner/velero/_schedule.tpl index 5488e6ad..85259fc7 100644 --- a/library/common/templates/spawner/velero/_schedule.tpl +++ b/library/common/templates/spawner/velero/_schedule.tpl @@ -6,18 +6,17 @@ {{- define "tc.v1.common.spawner.velero.schedule" -}} {{- $fullname := include "tc.v1.common.lib.chart.names.fullname" $ -}} - {{- range $schedule := .Values.schedule -}} + {{- range $name, $schedule := .Values.schedules -}} {{- $enabled := false -}} {{- if hasKey $schedule "enabled" -}} {{- if not (kindIs "invalid" $schedule.enabled) -}} {{- $enabled = $schedule.enabled -}} {{- else -}} - {{- fail (printf "schedule - Expected the defined key [enabled] in [schedule.%s] to not be empty" $schedule.name) -}} + {{- fail (printf "Schedule - Expected the defined key [enabled] in [schedules.%s] to not be empty" $name) -}} {{- end -}} {{- end -}} - {{- if kindIs "string" $enabled -}} {{- $enabled = tpl $enabled $ -}} @@ -34,25 +33,25 @@ {{/* Create a copy of the schedule */}} {{- $objectData := (mustDeepCopy $schedule) -}} - {{- $objectName := (printf "%s-%s" $fullname $schedule.name) -}} + {{- $objectName := (printf "%s-%s" $fullname $name) -}} {{- if hasKey $objectData "expandObjectName" -}} {{- if not $objectData.expandObjectName -}} - {{- $objectName = $schedule.name -}} + {{- $objectName = $name -}} {{- end -}} {{- end -}} {{/* Perform validations */}} {{/* schedules have a max name length of 253 */}} {{- include "tc.v1.common.lib.chart.names.validation" (dict "name" $objectName "length" 253) -}} {{- include "tc.v1.common.lib.velero.schedule.validation" (dict "objectData" $objectData) -}} - {{- include "tc.v1.common.lib.metadata.validation" (dict "objectData" $objectData "caller" "schedule") -}} + {{- include "tc.v1.common.lib.metadata.validation" (dict "objectData" $objectData "caller" "Schedule") -}} {{/* Set the name of the schedule */}} {{- $_ := set $objectData "name" $objectName -}} - {{- $_ := set $objectData "shortName" $schedule.name -}} + {{- $_ := set $objectData "shortName" $name -}} {{/* Set namespace to velero location or itself, just in case its used from within velero */}} {{- $operator := index $.Values.operator "velero" -}} - {{- $namespace := $operator.namespace | default ( include "tc.v1.common.lib.metadata.namespace" (dict "rootCtx" $ "objectData" $objectData "caller" "backupstoragelocation") ) -}} + {{- $namespace := $operator.namespace | default (include "tc.v1.common.lib.metadata.namespace" (dict "rootCtx" $ "objectData" $objectData "caller" "Schedule")) -}} {{- $_ := set $objectData "namespace" $namespace -}} {{/* Call class to create the object */}} diff --git a/library/common/values.yaml b/library/common/values.yaml index ad74d577..09758679 100644 --- a/library/common/values.yaml +++ b/library/common/values.yaml @@ -788,6 +788,7 @@ volumeSnapshotLocation: [] # # project: # Backup schedules to create. +schedules: {} # Eg: # schedules: # mybackup: