mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
Validate that volume mountpoints are unique
This commit is contained in:
parent
6cd8e5c0e6
commit
24aade64f5
@ -1107,6 +1107,7 @@ func validateSecretKeySelector(s *api.SecretKeySelector, fldPath *field.Path) fi
|
||||
|
||||
func validateVolumeMounts(mounts []api.VolumeMount, volumes sets.String, fldPath *field.Path) field.ErrorList {
|
||||
allErrs := field.ErrorList{}
|
||||
mountpoints := sets.NewString()
|
||||
|
||||
for i, mnt := range mounts {
|
||||
idxPath := fldPath.Index(i)
|
||||
@ -1120,6 +1121,10 @@ func validateVolumeMounts(mounts []api.VolumeMount, volumes sets.String, fldPath
|
||||
} else if strings.Contains(mnt.MountPath, ":") {
|
||||
allErrs = append(allErrs, field.Invalid(idxPath.Child("mountPath"), mnt.MountPath, "must not contain ':'"))
|
||||
}
|
||||
if mountpoints.Has(mnt.MountPath) {
|
||||
allErrs = append(allErrs, field.Invalid(idxPath.Child("mountPath"), mnt.MountPath, "must be unique"))
|
||||
}
|
||||
mountpoints.Insert(mnt.MountPath)
|
||||
}
|
||||
return allErrs
|
||||
}
|
||||
|
@ -1124,18 +1124,19 @@ func TestValidateVolumeMounts(t *testing.T) {
|
||||
|
||||
successCase := []api.VolumeMount{
|
||||
{Name: "abc", MountPath: "/foo"},
|
||||
{Name: "123", MountPath: "/foo"},
|
||||
{Name: "abc-123", MountPath: "/bar"},
|
||||
{Name: "123", MountPath: "/bar"},
|
||||
{Name: "abc-123", MountPath: "/baz"},
|
||||
}
|
||||
if errs := validateVolumeMounts(successCase, volumes, field.NewPath("field")); len(errs) != 0 {
|
||||
t.Errorf("expected success: %v", errs)
|
||||
}
|
||||
|
||||
errorCases := map[string][]api.VolumeMount{
|
||||
"empty name": {{Name: "", MountPath: "/foo"}},
|
||||
"name not found": {{Name: "", MountPath: "/foo"}},
|
||||
"empty mountpath": {{Name: "abc", MountPath: ""}},
|
||||
"colon mountpath": {{Name: "abc", MountPath: "foo:bar"}},
|
||||
"empty name": {{Name: "", MountPath: "/foo"}},
|
||||
"name not found": {{Name: "", MountPath: "/foo"}},
|
||||
"empty mountpath": {{Name: "abc", MountPath: ""}},
|
||||
"colon mountpath": {{Name: "abc", MountPath: "foo:bar"}},
|
||||
"mountpath collision": {{Name: "foo", MountPath: "/path/a"}, {Name: "bar", MountPath: "/path/a"}},
|
||||
}
|
||||
for k, v := range errorCases {
|
||||
if errs := validateVolumeMounts(v, volumes, field.NewPath("field")); len(errs) == 0 {
|
||||
|
Loading…
Reference in New Issue
Block a user