validation.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. Copyright 2017 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. "fmt"
  16. "strings"
  17. apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
  18. "k8s.io/apimachinery/pkg/util/validation/field"
  19. apivalidation "k8s.io/kubernetes/pkg/apis/core/validation"
  20. "k8s.io/kubernetes/pkg/apis/scheduling"
  21. )
  22. // ValidatePriorityClass tests whether required fields in the PriorityClass are
  23. // set correctly.
  24. func ValidatePriorityClass(pc *scheduling.PriorityClass) field.ErrorList {
  25. allErrs := field.ErrorList{}
  26. allErrs = append(allErrs, apivalidation.ValidateObjectMeta(&pc.ObjectMeta, false, apimachineryvalidation.NameIsDNSSubdomain, field.NewPath("metadata"))...)
  27. // If the priorityClass starts with a system prefix, it must be one of the
  28. // predefined system priority classes.
  29. if strings.HasPrefix(pc.Name, scheduling.SystemPriorityClassPrefix) {
  30. if is, err := scheduling.IsKnownSystemPriorityClass(pc); !is {
  31. allErrs = append(allErrs, field.Forbidden(field.NewPath("metadata", "name"), "priority class names with '"+scheduling.SystemPriorityClassPrefix+"' prefix are reserved for system use only. error: "+err.Error()))
  32. }
  33. } else if pc.Value > scheduling.HighestUserDefinablePriority {
  34. // Non-system critical priority classes are not allowed to have a value larger than HighestUserDefinablePriority.
  35. allErrs = append(allErrs, field.Forbidden(field.NewPath("value"), fmt.Sprintf("maximum allowed value of a user defined priority is %v", scheduling.HighestUserDefinablePriority)))
  36. }
  37. if pc.PreemptionPolicy != nil {
  38. allErrs = append(allErrs, apivalidation.ValidatePreemptionPolicy(pc.PreemptionPolicy, field.NewPath("preemptionPolicy"))...)
  39. }
  40. return allErrs
  41. }
  42. // ValidatePriorityClassUpdate tests if required fields in the PriorityClass are
  43. // set and are valid. PriorityClass does not allow updating Name, and Value.
  44. func ValidatePriorityClassUpdate(pc, oldPc *scheduling.PriorityClass) field.ErrorList {
  45. allErrs := apivalidation.ValidateObjectMetaUpdate(&pc.ObjectMeta, &oldPc.ObjectMeta, field.NewPath("metadata"))
  46. // Name is immutable and is checked by the ObjectMeta validator.
  47. if pc.Value != oldPc.Value {
  48. allErrs = append(allErrs, field.Forbidden(field.NewPath("Value"), "may not be changed in an update."))
  49. }
  50. // PreemptionPolicy is immutable and is checked by the ObjectMeta validator.
  51. allErrs = append(allErrs, apivalidation.ValidateImmutableField(pc.PreemptionPolicy, oldPc.PreemptionPolicy, field.NewPath("preemptionPolicy"))...)
  52. return allErrs
  53. }