Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kube volumes can not contain _ #24868

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions libpod/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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, "/"), "/", "-"), "_", "-"))
}

Comment on lines +1265 to +1273
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the perfect candidate for a unit test.

func convertVolumePathToName(hostSourcePath string) (string, error) {
if len(hostSourcePath) == 0 {
return "", errors.New("hostSourcePath must be specified to generate volume name")
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions test/system/710-kube.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
Loading