feat(common): Add volumeSnapshotClass class and spawner (#591)

**Description**
With some CSI's we want to add, it's nice to also be able to spawn
volumeSnapshotClass objects

⚒️ Fixes  #590

**⚙️ Type of change**

- [x] ⚙️ Feature/App addition
- [ ] 🪛 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?**
<!--
Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration
-->

**📃 Notes:**
<!-- Please enter any other relevant information here -->

**✔️ Checklist:**

- [ ] ⚖️ My code follows the style guidelines of this project
- [ ] 👀 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
- [ ] ⚠️ My changes generate no new warnings
- [ ] 🧪 I have added tests to this description that prove my fix is
effective or that my feature works
- [ ] ⬆️ I increased versions for any altered app according to semantic
versioning

** App addition**

If this PR is an app addition please make sure you have done the
following.

- [ ] 🪞 I have opened a PR on
[truecharts/containers](https://github.com/truecharts/containers) adding
the container to TrueCharts mirror repo.
- [ ] 🖼️ 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._

---------

Co-authored-by: Stavros kois <s.kois@outlook.com>
This commit is contained in:
Kjeld Schouten
2023-11-18 21:35:06 +01:00
committed by GitHub
parent 1043bf066b
commit 456e44a708
13 changed files with 407 additions and 10 deletions

View File

@@ -35,7 +35,7 @@ tests:
- documentIndex: &volumeSnapshotDoc 0
isKind:
of: VolumeSnapshot
- documentIndex: &volumeSnapshotDoc 0
- documentIndex: *volumeSnapshotDoc
isAPIVersion:
of: snapshot.storage.k8s.io/v1
- documentIndex: *volumeSnapshotDoc

View File

@@ -71,7 +71,7 @@ tests:
volumeSnapshotContentName: some-name
asserts:
- failedTemplate:
errorMessage: VolumeSnapshot - Expected non empty [name]
errorMessage: Volume Snapshot - Expected non empty [name]
- it: should fail without source object
set:
@@ -80,7 +80,7 @@ tests:
enabled: true
asserts:
- failedTemplate:
errorMessage: VolumeSnapshot - Expected non empty [source]
errorMessage: Volume Snapshot - Expected non empty [source]
- it: should fail without a valid source
set:
@@ -91,7 +91,7 @@ tests:
invalid: invalid
asserts:
- failedTemplate:
errorMessage: VolumeSnapshot - Expected exactly one of the valid source types [volumeSnapshotContentName, persistentVolumeClaimName]. Found [0]
errorMessage: Volume Snapshot - Expected exactly one of the valid source types [volumeSnapshotContentName, persistentVolumeClaimName]. Found [0]
- it: should fail with more than one valid source
set:
@@ -103,4 +103,14 @@ tests:
persistentVolumeClaimName: some-pvc-name
asserts:
- failedTemplate:
errorMessage: VolumeSnapshot - Expected exactly one of the valid source types [volumeSnapshotContentName, persistentVolumeClaimName]. Found [2]
errorMessage: Volume Snapshot - Expected exactly one of the valid source types [volumeSnapshotContentName, persistentVolumeClaimName]. Found [2]
- it: should fail with empty enabled
set:
volumeSnapshots:
- name: example1
enabled:
asserts:
- failedTemplate:
errorMessage: Volume Snapshot - Expected the defined key [enabled] in [volumeSnapshots.0] to not be empty

View File

@@ -0,0 +1,105 @@
suite: volumeSnapshotClass 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 volumeSnapshotClass 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 }}"
volumeSnapshotClass:
class1:
enabled: true
driver: csi-hostpath-snapshots
labels:
label1: "{{ .Values.label1 }}"
label2: label2
annotations:
annotation1: "{{ .Values.annotation1 }}"
annotation2: annotation2
class2:
enabled: true
isDefault: true
driver: csi-hostpath-snapshots
labels:
label1: "{{ .Values.label1 }}"
label2: label2
annotations:
annotation1: "{{ .Values.annotation1 }}"
annotation2: annotation2
asserts:
- documentIndex: &volumeSnapshotDoc 0
isKind:
of: VolumeSnapshotClass
- documentIndex: *volumeSnapshotDoc
isAPIVersion:
of: snapshot.storage.k8s.io/v1
- documentIndex: *volumeSnapshotDoc
equal:
path: metadata.annotations
value:
annotation1: annotation1
annotation2: annotation2
g_annotation1: global_annotation1
g_annotation2: global_annotation2
snapshot.storage.kubernetes.io/is-default-class: "false"
- documentIndex: *volumeSnapshotDoc
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
- documentIndex: &otherVolumeSnapshotDoc 1
isKind:
of: VolumeSnapshotClass
- documentIndex: *otherVolumeSnapshotDoc
isAPIVersion:
of: snapshot.storage.k8s.io/v1
- documentIndex: *otherVolumeSnapshotDoc
equal:
path: metadata.annotations
value:
annotation1: annotation1
annotation2: annotation2
g_annotation1: global_annotation1
g_annotation2: global_annotation2
snapshot.storage.kubernetes.io/is-default-class: "true"
- documentIndex: *otherVolumeSnapshotDoc
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

View File

@@ -0,0 +1,37 @@
suite: volumeSnapshotClass name test
templates:
- common.yaml
release:
name: test-release-name
namespace: test-release-namespace
tests:
- it: should generate correct name
set:
volumeSnapshotClass:
class1:
enabled: true
driver: csi-hostpath-snapshots
class2:
enabled: true
driver: csi-hostpath-snapshots
asserts:
- documentIndex: &volumeSnapshotDoc 0
isKind:
of: VolumeSnapshotClass
- documentIndex: *volumeSnapshotDoc
isAPIVersion:
of: snapshot.storage.k8s.io/v1
- documentIndex: *volumeSnapshotDoc
equal:
path: metadata.name
value: test-release-name-common-test-class1
- documentIndex: &otherVolumeSnapshotDoc 1
isKind:
of: VolumeSnapshotClass
- documentIndex: *otherVolumeSnapshotDoc
isAPIVersion:
of: snapshot.storage.k8s.io/v1
- documentIndex: *otherVolumeSnapshotDoc
equal:
path: metadata.name
value: test-release-name-common-test-class2

View File

@@ -0,0 +1,88 @@
suite: volumeSnapshotClass spec test
templates:
- common.yaml
release:
name: test-release-name
namespace: test-release-namespace
tests:
- it: should generate correct spec
set:
some_driver: csi-hostpath-snapshots
some_key: parameter1
some_value: value1
volumeSnapshotClass:
example1:
enabled: true
driver: csi-hostpath-snapshots
deletionPolicy: Delete
example2:
enabled: true
driver: "{{ .Values.some_driver }}"
parameters:
"{{ .Values.some_key }}": "{{ .Values.some_value }}"
parameter2: 5
asserts:
- documentIndex: &volumeSnapshotDoc 0
isKind:
of: VolumeSnapshotClass
- documentIndex: *volumeSnapshotDoc
isAPIVersion:
of: snapshot.storage.k8s.io/v1
- documentIndex: *volumeSnapshotDoc
equal:
path: driver
value: csi-hostpath-snapshots
- documentIndex: *volumeSnapshotDoc
equal:
path: deletionPolicy
value: Delete
- documentIndex: &otherVolumeSnapshotDoc 1
isKind:
of: VolumeSnapshotClass
- documentIndex: *otherVolumeSnapshotDoc
isAPIVersion:
of: snapshot.storage.k8s.io/v1
- documentIndex: *otherVolumeSnapshotDoc
equal:
path: driver
value: csi-hostpath-snapshots
- documentIndex: *otherVolumeSnapshotDoc
equal:
path: deletionPolicy
value: Retain
- documentIndex: *otherVolumeSnapshotDoc
equal:
path: parameters
value:
parameter1: value1
parameter2: "5"
# Failures
- it: should fail with invalid deletionPolicy
set:
volumeSnapshotClass:
class1:
enabled: true
driver: csi-hostpath-snapshots
deletionPolicy: Invalid
asserts:
- failedTemplate:
errorMessage: Volume Snapshot Class - Expected [deletionPolicy] to be one of [Retain, Delete], but got [Invalid]
- it: should fail without driver
set:
volumeSnapshotClass:
class1:
enabled: true
asserts:
- failedTemplate:
errorMessage: Volume Snapshot Class - Expected non empty [driver]
- it: should fail with empty enabled
set:
volumeSnapshotClass:
class1:
enabled:
asserts:
- failedTemplate:
errorMessage: Volume Snapshot Class - Expected the defined key [enabled] in [volumeSnapshotClass.class1] to not be empty

View File

@@ -0,0 +1,45 @@
{{/* volumesnapshotclass Class */}}
{{/* Call this template:
{{ include "tc.v1.common.class.volumesnapshotclass" (dict "rootCtx" $ "objectData" $objectData) }}
rootCtx: The root context of the chart.
objectData:
name: The name of the volumesnapshotclass.
labels: The labels of the volumesnapshotclass.
annotations: The annotations of the volumesnapshotclass.
*/}}
{{- define "tc.v1.common.class.volumesnapshotclass" -}}
{{- $rootCtx := .rootCtx -}}
{{- $objectData := .objectData -}}
{{- $isDefault := false -}}
{{- if (kindIs "bool" $objectData.isDefault) -}}
{{- $isDefault = $objectData.isDefault -}}
{{- end }}
---
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
name: {{ $objectData.name }}
{{- $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:
{{- . | nindent 4 }}
{{- end -}}
{{- $annotations := (mustMerge ($objectData.annotations | default dict) (include "tc.v1.common.lib.metadata.allAnnotations" $rootCtx | fromYaml)) }}
annotations:
snapshot.storage.kubernetes.io/is-default-class: {{ $isDefault | quote }}
{{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "annotations" $annotations) | trim) }}
{{- . | nindent 4 }}
{{- end }}
driver: {{ tpl $objectData.driver $rootCtx }}
deletionPolicy: {{ $objectData.deletionPolicy | default "Retain" }}
{{- with $objectData.parameters }}
parameters:
{{- range $k, $v := . }}
{{ tpl $k $rootCtx }}: {{ (tpl ($v | toString) $rootCtx) | quote }}
{{- end -}}
{{- end -}}
{{- end -}}

View File

@@ -11,7 +11,7 @@ objectData:
{{- $objectData := .objectData -}}
{{- if not $objectData.source -}}
{{- fail "VolumeSnapshot - Expected non empty [source]" -}}
{{- fail "Volume Snapshot - Expected non empty [source]" -}}
{{- end -}}
{{- $sourceTypes := (list "volumeSnapshotContentName" "persistentVolumeClaimName") -}}
@@ -23,7 +23,7 @@ objectData:
{{- end -}}
{{- if ne $sourceCount 1 -}}
{{- fail (printf "VolumeSnapshot - Expected exactly one of the valid source types [%s]. Found [%d]" (join ", " $sourceTypes) $sourceCount) -}}
{{- fail (printf "Volume Snapshot - Expected exactly one of the valid source types [%s]. Found [%d]" (join ", " $sourceTypes) $sourceCount) -}}
{{- end -}}
{{- end -}}

View File

@@ -0,0 +1,15 @@
{{- define "tc.v1.common.lib.volumesnapshotclass.validation" -}}
{{- $rootCtx := .rootCtx -}}
{{- $objectData := .objectData -}}
{{- $validPolicies := (list "Retain" "Delete") -}}
{{- if $objectData.deletionPolicy -}}
{{- if not (mustHas $objectData.deletionPolicy $validPolicies) -}}
{{- fail (printf "Volume Snapshot Class - Expected [deletionPolicy] to be one of [%s], but got [%s]" (join ", " $validPolicies) $objectData.deletionPolicy) -}}
{{- end -}}
{{- end -}}
{{- if not $objectData.driver -}}
{{- fail "Volume Snapshot Class - Expected non empty [driver]" -}}
{{- end -}}
{{- end -}}

View File

@@ -49,6 +49,9 @@
{{/* Render volumeSnapshot(s) */}}
{{- include "tc.v1.common.spawner.volumesnapshot" . | nindent 0 -}}
{{/* Render volumeSnapshotClass(s) */}}
{{- include "tc.v1.common.spawner.volumesnapshotclass" . | nindent 0 -}}
{{/* Render ingress(s) */}}
{{- include "tc.v1.common.spawner.ingress" . | nindent 0 -}}

View File

@@ -85,7 +85,7 @@
{{/* Perform validations */}} {{/* volumesnapshots have a max name length of 253 */}}
{{- include "tc.v1.common.lib.chart.names.validation" (dict "name" $snapshotName "length" 253) -}}
{{- include "tc.v1.common.lib.metadata.validation" (dict "objectData" $volSnapData "caller" "PVC - VolumeSnapshot") -}}
{{- include "tc.v1.common.lib.metadata.validation" (dict "objectData" $volSnapData "caller" "PVC - Volume Snapshot") -}}
{{/* Set the name of the volumesnapshot */}}
{{- $_ := set $volSnapData "name" $snapshotName -}}

View File

@@ -6,12 +6,35 @@
{{- define "tc.v1.common.spawner.volumesnapshot" -}}
{{- $fullname := include "tc.v1.common.lib.chart.names.fullname" $ -}}
{{- range $volumesnapshot := .Values.volumeSnapshots -}}
{{- range $idx, $volumesnapshot := .Values.volumeSnapshots -}}
{{- $enabled := false -}}
{{- if hasKey $volumesnapshot "enabled" -}}
{{- if not (kindIs "invalid" $volumesnapshot.enabled) -}}
{{- $enabled = $volumesnapshot.enabled -}}
{{- else -}}
{{- fail (printf "Volume Snapshot - Expected the defined key [enabled] in [volumeSnapshots.%v] to not be empty" $idx) -}}
{{- end -}}
{{- end -}}
{{- if kindIs "string" $enabled -}}
{{- $enabled = tpl $enabled $ -}}
{{/* After tpl it becomes a string, not a bool */}}
{{- if eq $enabled "true" -}}
{{- $enabled = true -}}
{{- else if eq $enabled "false" -}}
{{- $enabled = false -}}
{{- end -}}
{{- end -}}
{{- if $enabled -}}
{{/* Create a copy of the volumesnapshot */}}
{{- $objectData := (mustDeepCopy $volumesnapshot) -}}
{{- if not $objectData.name -}}
{{- fail "VolumeSnapshot - Expected non empty [name]" -}}
{{- fail "Volume Snapshot - Expected non empty [name]" -}}
{{- end -}}
{{- $objectName := (printf "%s-%s" $fullname $volumesnapshot.name) -}}
@@ -33,6 +56,8 @@
{{/* Call class to create the object */}}
{{- include "tc.v1.common.class.volumesnapshot" (dict "rootCtx" $ "objectData" $objectData) -}}
{{- end -}}
{{- end -}}
{{- end -}}

View File

@@ -0,0 +1,59 @@
{{/* volumesnapshotclass Spawwner */}}
{{/* Call this template:
{{ include "tc.v1.common.spawner.volumesnapshotclass" $ -}}
*/}}
{{- define "tc.v1.common.spawner.volumesnapshotclass" -}}
{{- $fullname := include "tc.v1.common.lib.chart.names.fullname" $ -}}
{{- range $name, $volumesnapshotclass := .Values.volumeSnapshotClass -}}
{{- $enabled := false -}}
{{- if hasKey $volumesnapshotclass "enabled" -}}
{{- if not (kindIs "invalid" $volumesnapshotclass.enabled) -}}
{{- $enabled = $volumesnapshotclass.enabled -}}
{{- else -}}
{{- fail (printf "Volume Snapshot Class - Expected the defined key [enabled] in [volumeSnapshotClass.%s] to not be empty" $name) -}}
{{- end -}}
{{- end -}}
{{- if kindIs "string" $enabled -}}
{{- $enabled = tpl $enabled $ -}}
{{/* After tpl it becomes a string, not a bool */}}
{{- if eq $enabled "true" -}}
{{- $enabled = true -}}
{{- else if eq $enabled "false" -}}
{{- $enabled = false -}}
{{- end -}}
{{- end -}}
{{- if $enabled -}}
{{/* Create a copy of the volumesnapshotclass */}}
{{- $objectData := (mustDeepCopy $volumesnapshotclass) -}}
{{- $objectName := (printf "%s-%s" $fullname $name) -}}
{{- if hasKey $objectData "expandObjectName" -}}
{{- if not $objectData.expandObjectName -}}
{{- $objectName = $name -}}
{{- end -}}
{{- end -}}
{{/* Perform validations */}} {{/* volumesnapshotclasss 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.volumesnapshotclass.validation" (dict "objectData" $objectData) -}}
{{- include "tc.v1.common.lib.metadata.validation" (dict "objectData" $objectData "caller" "Volume Snapshot Class") -}}
{{/* Set the name of the volumesnapshotclass */}}
{{- $_ := set $objectData "name" $objectName -}}
{{- $_ := set $objectData "shortName" $name -}}
{{/* Call class to create the object */}}
{{- include "tc.v1.common.class.volumesnapshotclass" (dict "rootCtx" $ "objectData" $objectData) -}}
{{- end -}}
{{- end -}}
{{- end -}}

View File

@@ -320,6 +320,7 @@ persistenceList: []
deviceList: []
volumeSnapshotClass: []
volumeSnapshots: []
# volumeSnapshots:
# - name: "mycustomsnapshot"
@@ -704,6 +705,15 @@ webhook:
# volumeBindingMode: Immediate
# mountOptions: []
# # -- create volumeSnapshotClass on demand
# volumeSnapshotClass:
# example:
# enabled: true
# driver: some.driver.io
# deletionPolicy: Retain
# isDefault: false
# parameters: {}
metrics:
main:
enabled: false