From 2e98bab6f5518ec7ebdd6c8460df5fc52872db4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Varl=C4=B1?= Date: Fri, 17 Jan 2025 13:17:16 +0000 Subject: [PATCH] Make path argument variadic in `PathOnHost` and `PathInsideMountpointPod` (#344) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is to make this API nicer to use by having a similar semantics to `filepath.Join` --- By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice. --------- Signed-off-by: Burak Varlı --- pkg/podmounter/mppod/path.go | 17 +++++++++++++---- pkg/podmounter/mppod/path_test.go | 2 ++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/pkg/podmounter/mppod/path.go b/pkg/podmounter/mppod/path.go index 179d47b2..78c5827b 100644 --- a/pkg/podmounter/mppod/path.go +++ b/pkg/podmounter/mppod/path.go @@ -16,12 +16,21 @@ const CommunicationDirName = "comm" // PathOnHost returns the full path on the host that refers to `path` inside Mountpoint Pod. // This function should be used in the CSI Driver Node Pod which uses `hostPath` volume to mount kubelet. -func PathOnHost(podPathOnHost string, path string) string { - return filepath.Join(podPathOnHost, "/volumes/kubernetes.io~empty-dir/", CommunicationDirName, path) +func PathOnHost(podPathOnHost string, path ...string) string { + parts := append([]string{ + podPathOnHost, + "/volumes/kubernetes.io~empty-dir/", + CommunicationDirName, + }, path...) + return filepath.Join(parts...) } // PathInsideMountpointPod returns the full path that refers to `path` inside Mountpoint Pod. // This function should be used in the Mountpoint Pod. -func PathInsideMountpointPod(path string) string { - return filepath.Join("/", CommunicationDirName, path) +func PathInsideMountpointPod(path ...string) string { + parts := append([]string{ + "/", + CommunicationDirName, + }, path...) + return filepath.Join(parts...) } diff --git a/pkg/podmounter/mppod/path_test.go b/pkg/podmounter/mppod/path_test.go index 7288b55b..15cd296e 100644 --- a/pkg/podmounter/mppod/path_test.go +++ b/pkg/podmounter/mppod/path_test.go @@ -12,6 +12,7 @@ func TestGeneratingPathsInsideMountpointPod(t *testing.T) { assert.Equals(t, "/comm/mount.sock", mppod.PathInsideMountpointPod("mount.sock")) assert.Equals(t, "/comm/mount.sock", mppod.PathInsideMountpointPod("/mount.sock")) assert.Equals(t, "/comm/sa-token/web-identity.token", mppod.PathInsideMountpointPod("./sa-token/web-identity.token")) + assert.Equals(t, "/comm/sa-token/web-identity.token", mppod.PathInsideMountpointPod("sa-token", "web-identity.token")) } func TestGeneratingPathsForMountpointPodOnHost(t *testing.T) { @@ -19,4 +20,5 @@ func TestGeneratingPathsForMountpointPodOnHost(t *testing.T) { assert.Equals(t, filepath.Join(podPath, "/volumes/kubernetes.io~empty-dir/comm/mount.sock"), mppod.PathOnHost(podPath, "mount.sock")) assert.Equals(t, filepath.Join(podPath, "/volumes/kubernetes.io~empty-dir/comm/mount.sock"), mppod.PathOnHost(podPath, "/mount.sock")) assert.Equals(t, filepath.Join(podPath, "/volumes/kubernetes.io~empty-dir/comm/sa-token/web-identity.token"), mppod.PathOnHost(podPath, "./sa-token/web-identity.token")) + assert.Equals(t, filepath.Join(podPath, "/volumes/kubernetes.io~empty-dir/comm/sa-token/web-identity.token"), mppod.PathOnHost(podPath, "sa-token", "./web-identity.token")) }