diff --git a/library/common-test/tests/pod/volume_device_test.yaml b/library/common-test/tests/pod/volume_device_test.yaml index 3e314d25..ab785b0c 100644 --- a/library/common-test/tests/pod/volume_device_test.yaml +++ b/library/common-test/tests/pod/volume_device_test.yaml @@ -56,6 +56,68 @@ tests: path: /dev/something type: BlockDevice + - it: should pass with specific devices volume + set: + workload: + workload-name1: + enabled: true + primary: true + type: Deployment + podSpec: {} + persistence: + tty-vol: + enabled: true + type: device + hostPath: /dev/ttyUSB0 + sda-vol: + enabled: true + type: device + hostPath: /dev/sda + hda-vol: + enabled: true + type: device + hostPath: /dev/hda + nvme-vol: + enabled: true + type: device + hostPath: /dev/nvme0n1 + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + contains: + path: spec.template.spec.volumes + content: + name: tty-vol + hostPath: + path: /dev/ttyUSB0 + type: CharDevice + - documentIndex: *deploymentDoc + contains: + path: spec.template.spec.volumes + content: + name: sda-vol + hostPath: + path: /dev/sda + type: BlockDevice + - documentIndex: *deploymentDoc + contains: + path: spec.template.spec.volumes + content: + name: hda-vol + hostPath: + path: /dev/hda + type: BlockDevice + - documentIndex: *deploymentDoc + contains: + path: spec.template.spec.volumes + content: + name: nvme-vol + hostPath: + path: /dev/nvme0n1 + type: BlockDevice + # Failures - it: should fail without hostPath set: diff --git a/library/common/templates/lib/pod/volumes/_device.tpl b/library/common/templates/lib/pod/volumes/_device.tpl index 8f0e277d..c1af6940 100644 --- a/library/common/templates/lib/pod/volumes/_device.tpl +++ b/library/common/templates/lib/pod/volumes/_device.tpl @@ -22,6 +22,24 @@ objectData: The object data to be used to render the volume. {{- fail "Persistence - Expected to start with a forward slash [/] on type" -}} {{- end -}} + {{- $charDevices := (list "tty") -}} + {{- if not $hostPathType -}} + {{- range $char := $charDevices -}} + {{- if hasPrefix (printf "/dev/%v" $char) $hostPath -}} + {{- $hostPathType = "CharDevice" -}} + {{- end -}} + {{- end -}} + {{- end -}} + + {{- $blockDevices := (list "sd" "hd" "nvme") -}} + {{- if not $hostPathType -}} + {{- range $block := $blockDevices -}} + {{- if hasPrefix (printf "/dev/%v" $block) $hostPath -}} + {{- $hostPathType = "BlockDevice" -}} + {{- end -}} + {{- end -}} + {{- 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) -}}