From ecd882f9f7b675baa954089b5509a8f65eb6ad48 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Wed, 18 Dec 2024 09:03:48 -0500 Subject: [PATCH] Kube volumes can not container _ Need to substiture all _ to - for k8s support. Signed-off-by: Daniel J Walsh --- libpod/kube.go | 15 +++++++++++---- test/system/710-kube.bats | 6 +++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/libpod/kube.go b/libpod/kube.go index bfdc9bf84a..0257bbf26d 100644 --- a/libpod/kube.go +++ b/libpod/kube.go @@ -1188,7 +1188,7 @@ func generateKubePersistentVolumeClaim(v *ContainerNamedVolume) (v1.VolumeMount, ro := slices.Contains(v.Options, "ro") // To avoid naming conflicts with any host path mounts, add a unique suffix to the volume's name. - vName := strings.ToLower(v.Name) + vName := fixKubeVolumeName(v.Name) name := vName + "-pvc" vm := v1.VolumeMount{} @@ -1262,6 +1262,15 @@ func isHostPathDirectory(hostPathSource string) (bool, error) { return info.Mode().IsDir(), nil } +func fixKubeVolumeName(source string) string { + // Trim trailing slashes, + // Replace slashes with dashes. + // Replace underscores with dashes. + // Force all letters to lower case + // Thus, /mnt/data/ will become mnt-data + return strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(strings.Trim(source, "/"), "/", "-"), "_", "-")) +} + func convertVolumePathToName(hostSourcePath string) (string, error) { if len(hostSourcePath) == 0 { return "", errors.New("hostSourcePath must be specified to generate volume name") @@ -1273,9 +1282,7 @@ func convertVolumePathToName(hostSourcePath string) (string, error) { // add special case name return "root", nil } - // First, trim trailing slashes, then replace slashes with dashes. - // Thus, /mnt/data/ will become mnt-data - return strings.ToLower(strings.ReplaceAll(strings.Trim(hostSourcePath, "/"), "/", "-")), nil + return fixKubeVolumeName(hostSourcePath), nil } func determineCapAddDropFromCapabilities(defaultCaps, containerCaps []string) *v1.Capabilities { diff --git a/test/system/710-kube.bats b/test/system/710-kube.bats index c34d373f88..4c0bb80790 100644 --- a/test/system/710-kube.bats +++ b/test/system/710-kube.bats @@ -99,12 +99,12 @@ status | = | null KUBE=$PODMAN_TMPDIR/kube.yaml source=$PODMAN_TMPDIR/Upper/Case/Path mkdir -p ${source} - run_podman create --name $cname -v $source:/mnt -v UPPERCASEVolume:/volume $IMAGE + run_podman create --name $cname -v $source:/mnt -v UPPERCASE_Volume:/volume $IMAGE run_podman kube generate $cname -f $KUBE - assert "$(< $KUBE)" =~ "name: uppercasevolume-pvc" "Lowercase volume name" + assert "$(< $KUBE)" =~ "name: uppercase-volume-pvc" "Lowercase volume name" assert "$(< $KUBE)" =~ "upper-case-path" "Lowercase volume paths" run_podman rm $cname - run_podman volume rm UPPERCASEVolume + run_podman volume rm UPPERCASE_Volume } @test "podman kube generate - pod" {