mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 11:21:47 +00:00
Add validation of VolumeMounts
This commit is contained in:
parent
40c71e8292
commit
dd6b209617
@ -84,7 +84,10 @@ type VolumeMount struct {
|
||||
// Optional: Defaults to false (read-write).
|
||||
ReadOnly bool `yaml:"readOnly,omitempty" json:"readOnly,omitempty"`
|
||||
// Required.
|
||||
// Exactly one of the following must be set. If both are set, prefer MountPath.
|
||||
// DEPRECATED: Path will be removed in a future version of the API.
|
||||
MountPath string `yaml:"mountPath,omitempty" json:"mountPath,omitempty"`
|
||||
Path string `yaml:"Path,omitempty" json:"Path,omitempty"`
|
||||
// One of: "LOCAL" (local volume) or "HOST" (external mount from the host). Default: LOCAL.
|
||||
MountType string `yaml:"mountType,omitempty" json:"mountType,omitempty"`
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ const (
|
||||
ErrTypeInvalid ValidationErrorEnum = "invalid value"
|
||||
ErrTypeNotSupported ValidationErrorEnum = "unsupported value"
|
||||
ErrTypeDuplicate ValidationErrorEnum = "duplicate value"
|
||||
ErrTypeNotFound ValidationErrorEnum = "not found"
|
||||
)
|
||||
|
||||
// ValidationError is an implementation of the 'error' interface, which represents an error of validation.
|
||||
@ -61,6 +62,10 @@ func makeDuplicateError(field string, value interface{}) ValidationError {
|
||||
return ValidationError{ErrTypeDuplicate, field, value}
|
||||
}
|
||||
|
||||
func makeNotFoundError(field string, value interface{}) ValidationError {
|
||||
return ValidationError{ErrTypeNotFound, field, value}
|
||||
}
|
||||
|
||||
func validateVolumes(volumes []Volume) (util.StringSet, error) {
|
||||
allNames := util.StringSet{}
|
||||
for i := range volumes {
|
||||
@ -95,6 +100,28 @@ func validateEnv(vars []EnvVar) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateVolumeMounts(mounts []VolumeMount, volumes util.StringSet) error {
|
||||
for i := range mounts {
|
||||
mnt := &mounts[i] // so we can set default values
|
||||
if len(mnt.Name) == 0 {
|
||||
return makeInvalidError("VolumeMount.Name", mnt.Name)
|
||||
}
|
||||
if !volumes.Has(mnt.Name) {
|
||||
return makeNotFoundError("VolumeMount.Name", mnt.Name)
|
||||
}
|
||||
if len(mnt.MountPath) == 0 {
|
||||
// Backwards compat.
|
||||
if len(mnt.Path) == 0 {
|
||||
return makeInvalidError("VolumeMount.MountPath", mnt.MountPath)
|
||||
}
|
||||
glog.Warning("DEPRECATED: VolumeMount.Path has been replaced by VolumeMount.MountPath")
|
||||
mnt.MountPath = mnt.Path
|
||||
mnt.Path = ""
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateContainers(containers []Container, volumes util.StringSet) error {
|
||||
allNames := util.StringSet{}
|
||||
for i := range containers {
|
||||
@ -112,7 +139,9 @@ func validateContainers(containers []Container, volumes util.StringSet) error {
|
||||
if err := validateEnv(ctr.Env); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := validateVolumeMounts(ctr.VolumeMounts, volumes); err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO(thockin): finish validation.
|
||||
}
|
||||
return nil
|
||||
|
@ -82,6 +82,42 @@ func TestValidateEnv(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateVolumeMounts(t *testing.T) {
|
||||
volumes := util.NewStringSet("abc", "123", "abc-123")
|
||||
|
||||
successCase := []VolumeMount{
|
||||
{Name: "abc", MountPath: "/foo"},
|
||||
{Name: "123", MountPath: "/foo"},
|
||||
{Name: "abc-123", MountPath: "/bar"},
|
||||
}
|
||||
if err := validateVolumeMounts(successCase, volumes); err != nil {
|
||||
t.Errorf("expected success: %v", err)
|
||||
}
|
||||
|
||||
nonCanonicalCase := []VolumeMount{
|
||||
{Name: "abc", Path: "/foo"},
|
||||
}
|
||||
err := validateVolumeMounts(nonCanonicalCase, volumes)
|
||||
if err != nil {
|
||||
t.Errorf("expected success: %v", err)
|
||||
}
|
||||
if nonCanonicalCase[0].MountPath != "/foo" {
|
||||
t.Errorf("expected canonicalized values: %+v", nonCanonicalCase[0])
|
||||
}
|
||||
|
||||
errorCases := map[string][]VolumeMount{
|
||||
"empty name": {{Name: "", MountPath: "/foo"}},
|
||||
"name not found": {{Name: "", MountPath: "/foo"}},
|
||||
"empty mountpath": {{Name: "abc", MountPath: ""}},
|
||||
}
|
||||
for k, v := range errorCases {
|
||||
err := validateVolumeMounts(v, volumes)
|
||||
if err == nil {
|
||||
t.Errorf("expected failure for %s", k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateContainers(t *testing.T) {
|
||||
volumes := util.StringSet{}
|
||||
|
||||
@ -106,6 +142,9 @@ func TestValidateContainers(t *testing.T) {
|
||||
"invalid env var name": {
|
||||
{Name: "abc", Image: "image", Env: []EnvVar{{Name: "ev.1"}}},
|
||||
},
|
||||
"unknown volume name": {
|
||||
{Name: "abc", Image: "image", VolumeMounts: []VolumeMount{{Name: "anything", MountPath: "/foo"}}},
|
||||
},
|
||||
}
|
||||
for k, v := range errorCases {
|
||||
if err := validateContainers(v, volumes); err == nil {
|
||||
@ -136,6 +175,10 @@ func TestValidateManifest(t *testing.T) {
|
||||
{Name: "ev2", Value: "val2"},
|
||||
{Key: "EV3", Value: "val3"},
|
||||
},
|
||||
VolumeMounts: []VolumeMount{
|
||||
{Name: "vol1", MountPath: "/foo"},
|
||||
{Name: "vol1", Path: "/bar"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user