From f4b626b2ef0f2ece4eb15364ff82b964179bf1c5 Mon Sep 17 00:00:00 2001 From: Stavros Kois <47820033+stavros-k@users.noreply.github.com> Date: Mon, 26 Jun 2023 10:09:43 +0300 Subject: [PATCH] fix(devices): set the hostPathType when a hostPath matches some criteria (#477) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** ⚒️ Fixes # When someone tries to mount a device from host that does not exist, k8s will create a *directory* (on the host) in it's place That means that when they actually plug the usb it will still be a directory unless manually fixed. So when we detect one of those devices, lets set its hostPathType to avoid that. ![image](https://github.com/truecharts/library-charts/assets/47820033/4b3e7ce8-5412-4b5e-a5d6-2305d59a0685) We can extend the "filter" list once we figure out more things **⚙️ Type of change** - [ ] ⚙️ Feature/App addition - [x] 🪛 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?** **📃 Notes:** **✔️ Checklist:** - [x] ⚖️ My code follows the style guidelines of this project - [x] 👀 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 - [x] ⚠️ My changes generate no new warnings - [x] 🧪 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._ --- .../tests/pod/volume_device_test.yaml | 62 +++++++++++++++++++ .../templates/lib/pod/volumes/_device.tpl | 18 ++++++ 2 files changed, 80 insertions(+) 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) -}}