Skip to content

Commit

Permalink
Make path argument variadic in PathOnHost and `PathInsideMountpoint…
Browse files Browse the repository at this point in the history
…Pod` (#344)

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ı <[email protected]>
  • Loading branch information
unexge authored Jan 17, 2025
1 parent a6001ba commit 2e98bab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 13 additions & 4 deletions pkg/podmounter/mppod/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
2 changes: 2 additions & 0 deletions pkg/podmounter/mppod/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ 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) {
podPath := "/var/lib/kubelet/pods/46efe8aa-75d9-4b12-8fdd-0ce0c2cabd99"
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"))
}

0 comments on commit 2e98bab

Please sign in to comment.