Skip to content

Commit

Permalink
Move VolumeBinding plugin args validation to apis/config/validation
Browse files Browse the repository at this point in the history
This PR also looses the check to allow zero since the API doc has
explained that value zero indicates no waiting.

Signed-off-by: Dave Chen <dave.chen@arm.com>
  • Loading branch information
chendave committed Mar 6, 2021
1 parent 4f93175 commit b8394c4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
12 changes: 12 additions & 0 deletions pkg/scheduler/apis/config/validation/validation_pluginargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,15 @@ func ValidateNodeAffinityArgs(args *config.NodeAffinityArgs) error {
}
return errors.Flatten(errors.NewAggregate(errs))
}

// ValidateVolumeBindingArgs validates that VolumeBindingArgs are set correctly.
func ValidateVolumeBindingArgs(args *config.VolumeBindingArgs) error {
var path *field.Path
var err error

if args.BindTimeoutSeconds < 0 {
err = field.Invalid(path.Child("bindTimeoutSeconds"), args.BindTimeoutSeconds, "invalid BindTimeoutSeconds, should not be a negative value")
}

return err
}
39 changes: 39 additions & 0 deletions pkg/scheduler/apis/config/validation/validation_pluginargs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -945,3 +945,42 @@ func TestValidateNodeAffinityArgs(t *testing.T) {
})
}
}

func TestValidateVolumeBindingArgs(t *testing.T) {
cases := []struct {
name string
args config.VolumeBindingArgs
wantErr error
}{
{
name: "zero is a valid config",
args: config.VolumeBindingArgs{
BindTimeoutSeconds: 0,
},
},
{
name: "positive value is valid config",
args: config.VolumeBindingArgs{
BindTimeoutSeconds: 10,
},
},
{
name: "negative value is invalid config ",
args: config.VolumeBindingArgs{
BindTimeoutSeconds: -10,
},
wantErr: &field.Error{
Type: field.ErrorTypeInvalid,
Field: "bindTimeoutSeconds",
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := ValidateVolumeBindingArgs(&tc.args)
if diff := cmp.Diff(tc.wantErr, err, ignoreBadValueDetail); diff != "" {
t.Errorf("ValidateVolumeBindingArgs returned err (-want,+got):\n%s", diff)
}
})
}
}
10 changes: 2 additions & 8 deletions pkg/scheduler/framework/plugins/volumebinding/volume_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"k8s.io/kubernetes/pkg/controller/volume/scheduling"
"k8s.io/kubernetes/pkg/features"
"k8s.io/kubernetes/pkg/scheduler/apis/config"
"k8s.io/kubernetes/pkg/scheduler/apis/config/validation"
"k8s.io/kubernetes/pkg/scheduler/framework"
)

Expand Down Expand Up @@ -286,7 +287,7 @@ func New(plArgs runtime.Object, fh framework.Handle) (framework.Plugin, error) {
if !ok {
return nil, fmt.Errorf("want args to be of type VolumeBindingArgs, got %T", plArgs)
}
if err := validateArgs(args); err != nil {
if err := validation.ValidateVolumeBindingArgs(args); err != nil {
return nil, err
}
podInformer := fh.SharedInformerFactory().Core().V1().Pods()
Expand All @@ -309,10 +310,3 @@ func New(plArgs runtime.Object, fh framework.Handle) (framework.Plugin, error) {
GenericEphemeralVolumeFeatureEnabled: utilfeature.DefaultFeatureGate.Enabled(features.GenericEphemeralVolume),
}, nil
}

func validateArgs(args *config.VolumeBindingArgs) error {
if args.BindTimeoutSeconds <= 0 {
return fmt.Errorf("invalid BindTimeoutSeconds: %d, must be positive integer", args.BindTimeoutSeconds)
}
return nil
}

0 comments on commit b8394c4

Please sign in to comment.