From 4fa4d36d4481c732e911188e944a182ede8bc2e5 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Wed, 8 Feb 2023 17:59:46 +0200 Subject: [PATCH] finish volumes --- .../tests/pod/volume_hostPath_test.yaml | 6 +- .../tests/pod/volume_ixVolume_test.yaml | 137 ++++++++++++++++++ library/common/1.0.0/docs/persistence.md | 7 + .../1.0.0/templates/lib/pod/_volumes.tpl | 1 + .../templates/lib/pod/volumes/_ixVolume.tpl | 48 ++++++ 5 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 library/common-test/tests/pod/volume_ixVolume_test.yaml create mode 100644 library/common/1.0.0/templates/lib/pod/volumes/_ixVolume.tpl diff --git a/library/common-test/tests/pod/volume_hostPath_test.yaml b/library/common-test/tests/pod/volume_hostPath_test.yaml index 6b82966403..9c60fa0fc7 100644 --- a/library/common-test/tests/pod/volume_hostPath_test.yaml +++ b/library/common-test/tests/pod/volume_hostPath_test.yaml @@ -29,6 +29,8 @@ tests: - it: should pass with hostPath volume and type set: + some_path: /some-path + some_type: DirectoryOrCreate workload: workload-name1: enabled: true @@ -39,8 +41,8 @@ tests: host-vol: enabled: true type: hostPath - hostPath: /some-path - hostPathType: DirectoryOrCreate + hostPath: "{{ .Values.some_path }}" + hostPathType: "{{ .Values.some_type }}" asserts: - documentIndex: *deploymentDoc isKind: diff --git a/library/common-test/tests/pod/volume_ixVolume_test.yaml b/library/common-test/tests/pod/volume_ixVolume_test.yaml new file mode 100644 index 0000000000..60d61abc5f --- /dev/null +++ b/library/common-test/tests/pod/volume_ixVolume_test.yaml @@ -0,0 +1,137 @@ +suite: pod ixVolume volume test +templates: + - common.yaml +tests: + - it: should pass with ixVolume volume + set: + workload: + workload-name1: + enabled: true + primary: true + type: Deployment + podSpec: {} + ixVolumes: + - /mnt/pool/ix-applications/ix-app + persistence: + ix-vol: + enabled: true + type: ixVolume + datasetName: ix-app + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + contains: + path: spec.template.spec.volumes + content: + name: ix-vol + hostPath: + path: /mnt/pool/ix-applications/ix-app + + - it: should pass with hostPath volume and type + set: + some_dataset: ix-app + some_type: DirectoryOrCreate + workload: + workload-name1: + enabled: true + primary: true + type: Deployment + podSpec: {} + ixVolumes: + - /mnt/pool/ix-applications/ix-app + persistence: + ix-vol: + enabled: true + type: ixVolume + datasetName: "{{ .Values.some_dataset }}" + hostPathType: "{{ .Values.some_type }}" + asserts: + - documentIndex: *deploymentDoc + isKind: + of: Deployment + - documentIndex: *deploymentDoc + contains: + path: spec.template.spec.volumes + content: + name: ix-vol + hostPath: + path: /mnt/pool/ix-applications/ix-app + type: DirectoryOrCreate + +# Failures + - it: should fail without datasetName + set: + workload: + some-workload: + enabled: true + primary: true + type: Deployment + podSpec: {} + persistence: + volume1: + enabled: true + type: ixVolume + datasetname: "" + asserts: + - failedTemplate: + errorMessage: Persistence - Expected non-empty on type + + - it: should fail with empty ixVolumes + set: + workload: + some-workload: + enabled: true + primary: true + type: Deployment + podSpec: {} + ixVolumes: [] + persistence: + volume1: + enabled: true + type: ixVolume + datasetName: ix-app + asserts: + - failedTemplate: + errorMessage: Persistence - Expected non-empty in values on type + + - it: should fail with empty ixVolumes + set: + workload: + some-workload: + enabled: true + primary: true + type: Deployment + podSpec: {} + ixVolumes: + - /mnt/pool/ix-applications/ix-wrong-app + - /mnt/pool/ix-applications/ix-other-app + persistence: + volume1: + enabled: true + type: ixVolume + datasetName: ix-app + asserts: + - failedTemplate: + errorMessage: Persistence - Expected [ix-app] to exist on list, but list contained [/mnt/pool/ix-applications/ix-wrong-app, /mnt/pool/ix-applications/ix-other-app] on type + + - it: should fail with invalid hostPathType + set: + workload: + some-workload: + enabled: true + primary: true + type: Deployment + podSpec: {} + ixVolumes: + - /mnt/pool/ix-applications/ix-app + persistence: + volume1: + enabled: true + type: ixVolume + datasetName: ix-app + hostPathType: invalid + asserts: + - failedTemplate: + errorMessage: Persistence - Expected to be one of [DirectoryOrCreate, Directory, FileOrCreate, File, Socket, CharDevice, BlockDevice], but got [invalid] diff --git a/library/common/1.0.0/docs/persistence.md b/library/common/1.0.0/docs/persistence.md index 1c98cc2d00..7a7b5bf0b9 100644 --- a/library/common/1.0.0/docs/persistence.md +++ b/library/common/1.0.0/docs/persistence.md @@ -27,6 +27,7 @@ | persistence.[volume-name].items.key | `string` | ✅ | ✅ | `""` | Define the key of the configmap/secret | | persistence.[volume-name].items.path | `string` | ✅ | ✅ | `""` | Define the path | | persistence.[volume-name].hostPath | `string` | ✅(On hostPath type) | ✅ | `""` | Define the hostPath | +| persistence.[volume-name].datasetName | `string` | ✅(On ixVolume type) | ✅ | `""` | Define the datasetName | | persistence.[volume-name].hostPathType | `string` | ❌ | ✅ | `""` | Define the hostPathType | | persistence.[volume-name].targetSelector.[pod-name] | `dict` | ❌ | ❌ | `{}` | Define a dict named after the pod to define the volume | | persistence.[volume-name].targetSelector.[pod-name].[container-name] | `dict` | ❌ | ❌ | `{}` | Define a dict named after the container to mount the volume | @@ -113,4 +114,10 @@ persistence: type: hostPath hostPath: /path/to/host hostPathType: DirectoryOrCreate + + ix-vol: + enabled: true + type: ixVolume + datasetName: ix-app + hostPathType: DirectoryOrCreate ``` diff --git a/library/common/1.0.0/templates/lib/pod/_volumes.tpl b/library/common/1.0.0/templates/lib/pod/_volumes.tpl index dd158e8176..69a3580467 100644 --- a/library/common/1.0.0/templates/lib/pod/_volumes.tpl +++ b/library/common/1.0.0/templates/lib/pod/_volumes.tpl @@ -40,6 +40,7 @@ objectData: The object data to be used to render the Pod. {{- if eq "pvc" $type -}} {{- include "ix.v1.common.lib.pod.volume.pvc" (dict "rootCtx" $rootCtx "objectData" $persistence) | trim | nindent 0 -}} {{- else if eq "ixVolume" $type -}} + {{- include "ix.v1.common.lib.pod.volume.ixVolume" (dict "rootCtx" $rootCtx "objectData" $persistence) | trim | nindent 0 -}} {{- else if eq "hostPath" $type -}} {{- include "ix.v1.common.lib.pod.volume.hostPath" (dict "rootCtx" $rootCtx "objectData" $persistence) | trim | nindent 0 -}} {{- else if eq "secret" $type -}} diff --git a/library/common/1.0.0/templates/lib/pod/volumes/_ixVolume.tpl b/library/common/1.0.0/templates/lib/pod/volumes/_ixVolume.tpl new file mode 100644 index 0000000000..50882a0427 --- /dev/null +++ b/library/common/1.0.0/templates/lib/pod/volumes/_ixVolume.tpl @@ -0,0 +1,48 @@ +{{/* Returns ixVolume Volume */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.pod.volume.ixVolume" (dict "rootCtx" $ "objectData" $objectData) }} +rootCtx: The root context of the template. It is used to access the global context. +objectData: The object data to be used to render the volume. +*/}} +{{- define "ix.v1.common.lib.pod.volume.ixVolume" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData -}} + + {{- $hostPathType := "" -}} + {{- if $objectData.hostPathType -}} + {{- $hostPathType = tpl $objectData.hostPathType $rootCtx -}} + {{- end -}} + + {{- if not $objectData.datasetName -}} + {{- fail "Persistence - Expected non-empty on type" -}} + {{- end -}} + {{- $datasetName := tpl $objectData.datasetName $rootCtx -}} + + {{- if not $rootCtx.Values.ixVolumes -}} + {{- fail "Persistence - Expected non-empty in values on type" -}} + {{- end -}} + + {{- $hostPath := "" -}} + {{- range $idx, $normalizedHostPath := $rootCtx.Values.ixVolumes -}} + {{- if eq $datasetName (base $normalizedHostPath) -}} + {{- $hostPath = $normalizedHostPath -}} + {{- else -}} + {{- fail (printf "Persistence - Expected [%s] to exist on list, but list contained [%s] on type" $datasetName (join ", " $rootCtx.Values.ixVolumes )) -}} + {{- end -}} + {{- end -}} + + {{- if not (hasPrefix "/" $hostPath) -}} + {{- fail "Persistence - Expected normalized path from to start with a forward slash [/] on type" -}} + {{- end -}} + + {{- $types := (list "DirectoryOrCreate" "Directory" "FileOrCreate" "File" "Socket" "CharDevice" "BlockDevice") -}} + {{- if and $hostPathType (not (mustHas $hostPathType $types)) -}} + {{- fail (printf "Persistence - Expected to be one of [%s], but got [%s]" (join ", " $types) $hostPathType) -}} + {{- end }} +- name: {{ $objectData.shortName }} + hostPath: + path: {{ $hostPath }} + {{- with $hostPathType }} + type: {{ $hostPathType }} + {{- end -}} +{{- end -}}