diff --git a/library/common-test/tests/pod/volume_hostPath_test.yaml b/library/common-test/tests/pod/volume_hostPath_test.yaml new file mode 100644 index 0000000000..6b82966403 --- /dev/null +++ b/library/common-test/tests/pod/volume_hostPath_test.yaml @@ -0,0 +1,108 @@ +suite: pod hostPath volume test +templates: + - common.yaml +tests: + - it: should pass with hostPath volume + set: + workload: + workload-name1: + enabled: true + primary: true + type: Deployment + podSpec: {} + persistence: + host-vol: + enabled: true + type: hostPath + hostPath: /some-path + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + contains: + path: spec.template.spec.volumes + content: + name: host-vol + hostPath: + path: /some-path + + - it: should pass with hostPath volume and type + set: + workload: + workload-name1: + enabled: true + primary: true + type: Deployment + podSpec: {} + persistence: + host-vol: + enabled: true + type: hostPath + hostPath: /some-path + hostPathType: DirectoryOrCreate + asserts: + - documentIndex: *deploymentDoc + isKind: + of: Deployment + - documentIndex: *deploymentDoc + contains: + path: spec.template.spec.volumes + content: + name: host-vol + hostPath: + path: /some-path + type: DirectoryOrCreate + +# Failures + - it: should fail without hostPath + set: + workload: + some-workload: + enabled: true + primary: true + type: Deployment + podSpec: {} + persistence: + volume1: + enabled: true + type: hostPath + hostPath: "" + asserts: + - failedTemplate: + errorMessage: Persistence - Expected non-empty on type + + - it: should fail with relative hostPath + set: + workload: + some-workload: + enabled: true + primary: true + type: Deployment + podSpec: {} + persistence: + volume1: + enabled: true + type: hostPath + hostPath: some-path + asserts: + - failedTemplate: + errorMessage: Persistence - Expected to start with a forward slash [/] on type + + - it: should fail with invalid hostPathType + set: + workload: + some-workload: + enabled: true + primary: true + type: Deployment + podSpec: {} + persistence: + volume1: + enabled: true + type: hostPath + hostPath: /some-path + 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-test/tests/pod/volume_nfs_test.yaml b/library/common-test/tests/pod/volume_nfs_test.yaml index 8c3d5af8e1..d5cc2accad 100644 --- a/library/common-test/tests/pod/volume_nfs_test.yaml +++ b/library/common-test/tests/pod/volume_nfs_test.yaml @@ -1,9 +1,11 @@ -suite: pod pvc volume test +suite: pod nfs volume test templates: - common.yaml tests: - - it: should pass with pvc volume + - it: should pass with nfs volume set: + some_path: /some-path + some_server: some-server workload: workload-name1: enabled: true @@ -11,9 +13,11 @@ tests: type: Deployment podSpec: {} persistence: - pvc-vol: + nfs-vol: enabled: true - type: pvc + type: nfs + path: "{{ .Values.some_path }}" + server: "{{ .Values.some_server }}" asserts: - documentIndex: &deploymentDoc 0 isKind: @@ -22,31 +26,60 @@ tests: contains: path: spec.template.spec.volumes content: - name: pvc-vol - persistentVolumeClaim: - claimName: release-name-common-test-pvc-vol + name: nfs-vol + nfs: + path: /some-path + server: some-server - - it: should pass with pvc volume with existing claim +# Failures + - it: should fail without path in nfs set: workload: - workload-name1: + some-workload: enabled: true primary: true type: Deployment podSpec: {} persistence: - pvc-vol: + volume1: enabled: true - type: pvc - existingClaim: some-existing-claim + type: nfs + path: "" asserts: - - documentIndex: &deploymentDoc 0 - isKind: - of: Deployment - - documentIndex: *deploymentDoc - contains: - path: spec.template.spec.volumes - content: - name: pvc-vol - persistentVolumeClaim: - claimName: some-existing-claim + - failedTemplate: + errorMessage: Persistence - Expected non-empty on type + + - it: should fail with path not starting with / in nfs + set: + workload: + some-workload: + enabled: true + primary: true + type: Deployment + podSpec: {} + persistence: + volume1: + enabled: true + type: nfs + path: some-relative-path + asserts: + - failedTemplate: + errorMessage: Persistence - Expected to start with a forward slash [/] on type + + - it: should fail without server in nfs + set: + workload: + some-workload: + enabled: true + primary: true + type: Deployment + podSpec: {} + persistence: + volume1: + enabled: true + type: nfs + path: /some-path + server: "" + asserts: + - failedTemplate: + errorMessage: Persistence - Expected non-empty on type diff --git a/library/common-test/tests/pod/volume_pvc_test.yaml b/library/common-test/tests/pod/volume_pvc_test.yaml index 175ebe6ce6..8c3d5af8e1 100644 --- a/library/common-test/tests/pod/volume_pvc_test.yaml +++ b/library/common-test/tests/pod/volume_pvc_test.yaml @@ -1,11 +1,9 @@ -suite: pod nfs volume test +suite: pod pvc volume test templates: - common.yaml tests: - - it: should pass with nfs volume + - it: should pass with pvc volume set: - some_path: /some-path - some_server: some-server workload: workload-name1: enabled: true @@ -13,11 +11,9 @@ tests: type: Deployment podSpec: {} persistence: - nfs-vol: + pvc-vol: enabled: true - type: nfs - path: "{{ .Values.some_path }}" - server: "{{ .Values.some_server }}" + type: pvc asserts: - documentIndex: &deploymentDoc 0 isKind: @@ -26,60 +22,31 @@ tests: contains: path: spec.template.spec.volumes content: - name: nfs-vol - nfs: - path: /some-path - server: some-server + name: pvc-vol + persistentVolumeClaim: + claimName: release-name-common-test-pvc-vol -# Failures - - it: should fail without path in nfs + - it: should pass with pvc volume with existing claim set: workload: - some-workload: + workload-name1: enabled: true primary: true type: Deployment podSpec: {} persistence: - volume1: + pvc-vol: enabled: true - type: nfs - path: "" + type: pvc + existingClaim: some-existing-claim asserts: - - failedTemplate: - errorMessage: Persistence - Expected non-empty [path] on type - - - it: should fail with path not starting with / in nfs - set: - workload: - some-workload: - enabled: true - primary: true - type: Deployment - podSpec: {} - persistence: - volume1: - enabled: true - type: nfs - path: some-relative-path - asserts: - - failedTemplate: - errorMessage: Persistence - Expected [path] to start with a forward slash [/] on type - - - it: should fail without server in nfs - set: - workload: - some-workload: - enabled: true - primary: true - type: Deployment - podSpec: {} - persistence: - volume1: - enabled: true - type: nfs - path: /some-path - server: "" - asserts: - - failedTemplate: - errorMessage: Persistence - Expected non-empty [server] on type + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + contains: + path: spec.template.spec.volumes + content: + name: pvc-vol + persistentVolumeClaim: + claimName: some-existing-claim diff --git a/library/common/1.0.0/docs/persistence.md b/library/common/1.0.0/docs/persistence.md index 55ea325db3..1c98cc2d00 100644 --- a/library/common/1.0.0/docs/persistence.md +++ b/library/common/1.0.0/docs/persistence.md @@ -26,6 +26,8 @@ | persistence.[volume-name].items | `list` | ❌ | ❌ | `[]` | Define a list of items for configmap/secret | | 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].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 | | persistence.[volume-name].targetSelector.[pod-name].[container-name].mountPath | `string` | ❌ | ✅ | `[volume-name].mountPath` | Define the mountPath for the container | @@ -105,4 +107,10 @@ persistence: path: path1 - key: key2 path: path2 + + hostpath-vol: + enabled: true + type: hostPath + hostPath: /path/to/host + 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 6fa4f8db45..dd158e8176 100644 --- a/library/common/1.0.0/templates/lib/pod/_volumes.tpl +++ b/library/common/1.0.0/templates/lib/pod/_volumes.tpl @@ -41,6 +41,7 @@ objectData: The object data to be used to render the Pod. {{- include "ix.v1.common.lib.pod.volume.pvc" (dict "rootCtx" $rootCtx "objectData" $persistence) | trim | nindent 0 -}} {{- else if eq "ixVolume" $type -}} {{- 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 -}} {{- include "ix.v1.common.lib.pod.volume.secret" (dict "rootCtx" $rootCtx "objectData" $persistence) | trim | nindent 0 -}} {{- else if eq "configmap" $type -}} diff --git a/library/common/1.0.0/templates/lib/pod/volumes/_hostPath.tpl b/library/common/1.0.0/templates/lib/pod/volumes/_hostPath.tpl new file mode 100644 index 0000000000..ff5d1c6747 --- /dev/null +++ b/library/common/1.0.0/templates/lib/pod/volumes/_hostPath.tpl @@ -0,0 +1,35 @@ +{{/* Returns hostPath Volume */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.pod.volume.hostPath" (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.hostPath" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData -}} + + {{- $hostPathType := "" -}} + {{- if $objectData.hostPathType -}} + {{- $hostPathType = tpl $objectData.hostPathType $rootCtx -}} + {{- end -}} + + {{- if not $objectData.hostPath -}} + {{- fail "Persistence - Expected non-empty on type" -}} + {{- end -}} + {{- $hostPath := tpl $objectData.hostPath $rootCtx -}} + + {{- if not (hasPrefix "/" $hostPath) -}} + {{- fail "Persistence - Expected 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 -}} diff --git a/library/common/1.0.0/templates/lib/pod/volumes/_nfs.tpl b/library/common/1.0.0/templates/lib/pod/volumes/_nfs.tpl index d1e845db34..e303f23445 100644 --- a/library/common/1.0.0/templates/lib/pod/volumes/_nfs.tpl +++ b/library/common/1.0.0/templates/lib/pod/volumes/_nfs.tpl @@ -9,16 +9,16 @@ objectData: The object data to be used to render the volume. {{- $objectData := .objectData -}} {{- if not $objectData.path -}} - {{- fail "Persistence - Expected non-empty [path] on type" -}} + {{- fail "Persistence - Expected non-empty on type" -}} {{- end -}} {{- $path := tpl $objectData.path $rootCtx -}} {{- if not (hasPrefix "/" $path) -}} - {{- fail "Persistence - Expected [path] to start with a forward slash [/] on type" -}} + {{- fail "Persistence - Expected to start with a forward slash [/] on type" -}} {{- end -}} {{- if not $objectData.server -}} - {{- fail "Persistence - Expected non-empty [server] on type" -}} + {{- fail "Persistence - Expected non-empty on type" -}} {{- end }} - name: {{ $objectData.shortName }} nfs: