Skip to content

Commit

Permalink
fix(storageClass): fix storage class and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stavros-k committed Mar 26, 2024
1 parent 5e8d220 commit 335e2d2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions library/common-test/tests/storageClass/metadata_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 34 additions & 1 deletion library/common-test/tests/storageClass/spec_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -153,7 +175,7 @@ tests:
- option1
- option2=value2

# Failures
# Failures
- it: should fail without provisioner
set:
storageClass:
Expand Down Expand Up @@ -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]
2 changes: 1 addition & 1 deletion library/common/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 7 additions & 2 deletions library/common/templates/class/_storageClass.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) -}}
Expand Down

0 comments on commit 335e2d2

Please sign in to comment.