From cec6a4cd3eb6a00a066207b8a53233419061ea7d Mon Sep 17 00:00:00 2001 From: Stavros Kois <47820033+stavros-k@users.noreply.github.com> Date: Wed, 27 Mar 2024 01:16:42 +0200 Subject: [PATCH] fix(storageClass): fix storage class and add tests (#767) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** ⚒️ Fixes # **⚙️ Type of change** - [ ] ⚙️ Feature/App addition - [x] 🪛 Bugfix - [ ] ⚠️ Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] 🔃 Refactor of current code **🧪 How Has This Been Tested?** **📃 Notes:** **✔️ Checklist:** - [x] ⚖️ My code follows the style guidelines of this project - [x] 👀 I have performed a self-review of my own code - [ ] #️⃣ I have commented my code, particularly in hard-to-understand areas - [ ] 📄 I have made corresponding changes to the documentation - [x] ⚠️ My changes generate no new warnings - [x] 🧪 I have added tests to this description that prove my fix is effective or that my feature works - [x] ⬆️ I increased versions for any altered app according to semantic versioning - [x] I made sure the title starts with `feat(chart-name):`, `fix(chart-name):` or `chore(chart-name):` **➕ App addition** If this PR is an app addition please make sure you have done the following. - [ ] 🖼️ I have added an icon in the Chart's root directory called `icon.png` --- _Please don't blindly check all the boxes. Read them and only check those that apply. Those checkboxes are there for the reviewer to see what is this all about and the status of this PR with a quick glance._ --- .../tests/storageClass/metadata_test.yaml | 1 + .../tests/storageClass/spec_test.yaml | 35 ++++++++++++++++++- library/common/Chart.yaml | 2 +- .../common/templates/class/_storageClass.tpl | 9 +++-- .../lib/storage/_storageClassValidation.tpl | 6 ++++ 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/library/common-test/tests/storageClass/metadata_test.yaml b/library/common-test/tests/storageClass/metadata_test.yaml index f9f511d3..0dd9a109 100644 --- a/library/common-test/tests/storageClass/metadata_test.yaml +++ b/library/common-test/tests/storageClass/metadata_test.yaml @@ -52,6 +52,7 @@ tests: g_annotation1: global_annotation1 g_annotation2: global_annotation2 some-annotation: some-value + storageclass.kubernetes.io/is-default-class: "false" - documentIndex: *storageClassDoc equal: path: metadata.labels diff --git a/library/common-test/tests/storageClass/spec_test.yaml b/library/common-test/tests/storageClass/spec_test.yaml index d55436a2..b96f63cc 100644 --- a/library/common-test/tests/storageClass/spec_test.yaml +++ b/library/common-test/tests/storageClass/spec_test.yaml @@ -34,6 +34,28 @@ tests: equal: path: volumeBindingMode value: Immediate + - documentIndex: *storageClassDoc + equal: + path: metadata.annotations + value: + storageclass.kubernetes.io/is-default-class: "false" + + - it: should generate correct spec with isDefault set + set: + storageClass: + example1: + enabled: true + provisioner: some.provisioner.io + isDefault: true + asserts: + - documentIndex: *storageClassDoc + isKind: + of: StorageClass + - documentIndex: *storageClassDoc + equal: + path: metadata.annotations + value: + storageclass.kubernetes.io/is-default-class: "true" - it: should generate correct spec with non-default reclaim policy set: @@ -153,7 +175,7 @@ tests: - option1 - option2=value2 -# Failures + # Failures - it: should fail without provisioner set: storageClass: @@ -185,3 +207,14 @@ tests: asserts: - failedTemplate: errorMessage: Storage Class - Expected [volumeBindingMode] to be one of [WaitForFirstConsumer, Immediate], but got [invalid] + + - it: should fail with isDefault not a boolean + set: + storageClass: + example1: + enabled: true + provisioner: some.provisioner.io + isDefault: invalid + asserts: + - failedTemplate: + errorMessage: Storage Class - Expected [isDefault] to be [boolean], but got [string] diff --git a/library/common/Chart.yaml b/library/common/Chart.yaml index 15a63a86..5a46d853 100644 --- a/library/common/Chart.yaml +++ b/library/common/Chart.yaml @@ -15,7 +15,7 @@ maintainers: name: common sources: null type: library -version: 20.2.7 +version: 20.2.8 annotations: artifacthub.io/category: "integration-delivery" artifacthub.io/license: "BUSL-1.1" diff --git a/library/common/templates/class/_storageClass.tpl b/library/common/templates/class/_storageClass.tpl index b90de092..f9002aa4 100644 --- a/library/common/templates/class/_storageClass.tpl +++ b/library/common/templates/class/_storageClass.tpl @@ -14,11 +14,15 @@ objectData: {{- $rootCtx := .rootCtx -}} {{- $objectData := .objectData -}} + {{- $isDefaultClass := false -}} + {{- if (hasKey $objectData "isDefault") -}} + {{- $isDefaultClass = $objectData.isDefault -}} + {{- end -}} + {{- $allowVolExpand := true -}} {{- if not (kindIs "invalid" $objectData.allowVolumeExpansion) -}} {{- $allowVolExpand = $objectData.allowVolumeExpansion -}} {{- end }} - --- apiVersion: storage.k8s.io/v1 kind: StorageClass @@ -29,7 +33,8 @@ metadata: labels: {{- . | nindent 4 }} {{- end -}} - {{- $annotations := (mustMerge ($objectData.annotations | default dict) ( dict 'storageclass.kubernetes.io/is-default-class' ( $objectData.isDefaultClass | default "false" ) ) (include "tc.v1.common.lib.metadata.allAnnotations" $rootCtx | fromYaml)) -}} + {{- $annotations := (mustMerge ($objectData.annotations | default dict) (include "tc.v1.common.lib.metadata.allAnnotations" $rootCtx | fromYaml)) -}} + {{- $_ := set $annotations "storageclass.kubernetes.io/is-default-class" ($isDefaultClass | toString) -}} {{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "annotations" $annotations) | trim) }} annotations: {{- . | nindent 4 }} diff --git a/library/common/templates/lib/storage/_storageClassValidation.tpl b/library/common/templates/lib/storage/_storageClassValidation.tpl index b8c5b49f..5ddfc2c8 100644 --- a/library/common/templates/lib/storage/_storageClassValidation.tpl +++ b/library/common/templates/lib/storage/_storageClassValidation.tpl @@ -6,6 +6,12 @@ {{- fail "Storage Class - Expected non-empty [provisioner]" -}} {{- end -}} + {{- if (hasKey $objectData "isDefault") -}} + {{- if not (kindIs "bool" $objectData.isDefault) -}} + {{- fail (printf "Storage Class - Expected [isDefault] to be [boolean], but got [%s]" (kindOf $objectData.isDefault)) -}} + {{- end -}} + {{- end -}} + {{- $validPolicies := (list "Retain" "Delete") -}} {{- if $objectData.reclaimPolicy -}} {{- if not (mustHas $objectData.reclaimPolicy $validPolicies) -}}