validation.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package validation
  14. import (
  15. "errors"
  16. "fmt"
  17. "k8s.io/api/core/v1"
  18. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  19. "k8s.io/apimachinery/pkg/util/sets"
  20. "k8s.io/apimachinery/pkg/util/validation"
  21. v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
  22. schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
  23. )
  24. // ValidatePolicy checks for errors in the Config
  25. // It does not return early so that it can find as many errors as possible
  26. func ValidatePolicy(policy schedulerapi.Policy) error {
  27. var validationErrors []error
  28. for _, priority := range policy.Priorities {
  29. if priority.Weight <= 0 || priority.Weight >= schedulerapi.MaxWeight {
  30. validationErrors = append(validationErrors, fmt.Errorf("Priority %s should have a positive weight applied to it or it has overflown", priority.Name))
  31. }
  32. }
  33. binders := 0
  34. extenderManagedResources := sets.NewString()
  35. for _, extender := range policy.ExtenderConfigs {
  36. if len(extender.PrioritizeVerb) > 0 && extender.Weight <= 0 {
  37. validationErrors = append(validationErrors, fmt.Errorf("Priority for extender %s should have a positive weight applied to it", extender.URLPrefix))
  38. }
  39. if extender.BindVerb != "" {
  40. binders++
  41. }
  42. for _, resource := range extender.ManagedResources {
  43. errs := validateExtendedResourceName(resource.Name)
  44. if len(errs) != 0 {
  45. validationErrors = append(validationErrors, errs...)
  46. }
  47. if extenderManagedResources.Has(string(resource.Name)) {
  48. validationErrors = append(validationErrors, fmt.Errorf("Duplicate extender managed resource name %s", string(resource.Name)))
  49. }
  50. extenderManagedResources.Insert(string(resource.Name))
  51. }
  52. }
  53. if binders > 1 {
  54. validationErrors = append(validationErrors, fmt.Errorf("Only one extender can implement bind, found %v", binders))
  55. }
  56. return utilerrors.NewAggregate(validationErrors)
  57. }
  58. // validateExtendedResourceName checks whether the specified name is a valid
  59. // extended resource name.
  60. func validateExtendedResourceName(name v1.ResourceName) []error {
  61. var validationErrors []error
  62. for _, msg := range validation.IsQualifiedName(string(name)) {
  63. validationErrors = append(validationErrors, errors.New(msg))
  64. }
  65. if len(validationErrors) != 0 {
  66. return validationErrors
  67. }
  68. if !v1helper.IsExtendedResourceName(name) {
  69. validationErrors = append(validationErrors, fmt.Errorf("%s is an invalid extended resource name", name))
  70. }
  71. return validationErrors
  72. }