Merge pull request #24868 from rhatdan/kube

Kube volumes can not contain _
This commit is contained in:
openshift-merge-bot[bot]
2025-01-07 01:23:05 +00:00
committed by GitHub
2 changed files with 14 additions and 7 deletions

View File

@ -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 {

View File

@ -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" {