From a46f2330fda387a89d16df75c96a10add7f1c040 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Sat, 10 Feb 2024 11:48:02 +0200 Subject: [PATCH] chore(common): fix tests and cleanup --- .../tests/lists/volumeScheduleList_test.yaml | 47 +++++++++++++++++++ .../tests/veleroSchedule/spec_test.yaml | 6 ++- library/common/Chart.yaml | 2 +- .../templates/class/velero/_schedule.tpl | 33 ++++++++----- .../templates/lib/velero/_providerSecret.tpl | 8 ++-- 5 files changed, 75 insertions(+), 21 deletions(-) create mode 100644 library/common-test/tests/lists/volumeScheduleList_test.yaml diff --git a/library/common-test/tests/lists/volumeScheduleList_test.yaml b/library/common-test/tests/lists/volumeScheduleList_test.yaml new file mode 100644 index 000000000..02dc321b8 --- /dev/null +++ b/library/common-test/tests/lists/volumeScheduleList_test.yaml @@ -0,0 +1,47 @@ +suite: volumeSnapshotList spec test +templates: + - common.yaml +release: + name: test-release-name + namespace: test-release-namespace +tests: + - it: should generate correct spec + set: + schedulesList: + - name: example1 + enabled: true + schedule: "0 2 * * *" + - name: example2 + 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 * * * + template: + includedNamespaces: + - test-release-namespace + - documentIndex: &scheduleDoc 1 + isKind: + of: Schedule + - documentIndex: *scheduleDoc + isAPIVersion: + of: velero.io/v1 + - documentIndex: *scheduleDoc + equal: + path: spec + value: + schedule: "0 2 * * *" + useOwnerReferencesInBackup: true + template: + includedNamespaces: + - test-release-namespace diff --git a/library/common-test/tests/veleroSchedule/spec_test.yaml b/library/common-test/tests/veleroSchedule/spec_test.yaml index 835a88127..6543f1c00 100644 --- a/library/common-test/tests/veleroSchedule/spec_test.yaml +++ b/library/common-test/tests/veleroSchedule/spec_test.yaml @@ -50,7 +50,7 @@ tests: includedNamespaces: - test-release-namespace - - it: should generate correct spec with template + - it: should generate correct spec with template and override on includedNamespaces set: schedules: my-sched: @@ -60,6 +60,8 @@ tests: ttl: 720h includeClusterResources: true snapshotVolumes: true + includedNamespaces: + - some-namespace asserts: - documentIndex: &scheduleDoc 0 isKind: @@ -77,7 +79,7 @@ tests: includeClusterResources: true snapshotVolumes: true includedNamespaces: - - test-release-namespace + - some-namespace # Failures - it: should fail without schedule diff --git a/library/common/Chart.yaml b/library/common/Chart.yaml index 5fff25079..1414947e1 100644 --- a/library/common/Chart.yaml +++ b/library/common/Chart.yaml @@ -15,4 +15,4 @@ maintainers: name: common sources: null type: library -version: 17.3.9 +version: 17.3.10 diff --git a/library/common/templates/class/velero/_schedule.tpl b/library/common/templates/class/velero/_schedule.tpl index 2c4fb0fef..9445a8387 100644 --- a/library/common/templates/class/velero/_schedule.tpl +++ b/library/common/templates/class/velero/_schedule.tpl @@ -15,12 +15,14 @@ objectData: {{- $rootCtx := .rootCtx -}} {{- $objectData := .objectData }} {{- $namespace := (include "tc.v1.common.lib.metadata.namespace" (dict "rootCtx" $rootCtx "objectData" $objectData "caller" "Schedule")) -}} + + {{/* Get existing BSLs */}} {{- $lookupBSL := (lookup "velero.io/v1" "BackupStorageLocation" "" "") -}} - {{- if and $lookupBSL $lookupBSL.items -}} - {{- $lookupBSL = $lookupBSL.items -}} - {{- end -}} - {{- range $BSL := $lookupBSL -}} - {{- $namespace = $BSL.metadata.namespace -}} + {{/* TODO: Why we only keep one namespace of the found BSLs? + In this case we can just get the `| first` item + */}} + {{- range $bsl := $lookupBSL.items -}} + {{- $namespace = $bsl.metadata.namespace -}} {{- end }} --- apiVersion: velero.io/v1 @@ -44,7 +46,7 @@ spec: useOwnerReferencesInBackup: {{ $objectData.useOwnerReferencesInBackup }} {{- end }} template: - {{- if not $objectData.template }} + {{- if not $objectData.template }} includeClusterResources: true includedNamespaces: - {{ include "tc.v1.common.lib.metadata.namespace" (dict "rootCtx" $rootCtx "objectData" $objectData "caller" "Schedule") }} @@ -56,10 +58,18 @@ spec: - matchLabels: owner: helm name: {{ $rootCtx.Release.Name }} - {{- end -}} - - {{- with $objectData.template }} + {{- end -}} + {{- with $objectData.template }} + {{/* + TODO: This toYaml should **not** be here if any of the checks + below renders when a key is present. Currently, checks below + only render when a key is not present, which is mostly safe + to use along with the toYaml. + */}} {{- toYaml . | nindent 4 }} + {{- if not (hasKey . "includeClusterResources") }} + includeClusterResources: true + {{- end -}} {{- if not .orLabelSelectors }} orLabelSelectors: - matchLabels: @@ -74,8 +84,5 @@ spec: includedNamespaces: - {{ include "tc.v1.common.lib.metadata.namespace" (dict "rootCtx" $rootCtx "objectData" $objectData "caller" "Schedule") }} {{- end -}} - {{- if not (hasKey . "includeClusterResources") }} - includeClusterResources: true - {{- end -}} - {{- end -}} + {{- end -}} {{- end -}} diff --git a/library/common/templates/lib/velero/_providerSecret.tpl b/library/common/templates/lib/velero/_providerSecret.tpl index 626e1d3c9..5eddc1f6d 100644 --- a/library/common/templates/lib/velero/_providerSecret.tpl +++ b/library/common/templates/lib/velero/_providerSecret.tpl @@ -4,16 +4,14 @@ {{- $prefix := .prefix -}} {{- $creds := "" -}} - {{- $provider := "" -}} + {{/* Make sure provider is a string */}} + {{- $provider := $objectData.provider | toString -}} {{/* Use credential provider when creating secret for VSL */}} - {{/* Make sure provider is a string */}} {{- if eq $prefix "vsl" -}} {{- $provider = $objectData.credential.provider | toString -}} - {{- else -}} - {{- $provider = $objectData.provider | toString -}} {{- end -}} - + {{- if and (eq $provider "aws") $objectData.credential.aws -}} {{- $creds = (include "tc.v1.common.lib.velero.provider.aws.secret" (dict "creds" $objectData.credential.aws) | fromYaml).data -}}