validation.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Copyright 2019 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. apivalidation "k8s.io/apimachinery/pkg/api/validation"
  16. unversionedvalidation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
  17. "k8s.io/apimachinery/pkg/util/validation/field"
  18. "k8s.io/kubernetes/pkg/apis/core"
  19. corevalidation "k8s.io/kubernetes/pkg/apis/core/validation"
  20. "k8s.io/kubernetes/pkg/apis/node"
  21. )
  22. // ValidateRuntimeClass validates the RuntimeClass
  23. func ValidateRuntimeClass(rc *node.RuntimeClass) field.ErrorList {
  24. allErrs := apivalidation.ValidateObjectMeta(&rc.ObjectMeta, false, apivalidation.NameIsDNSSubdomain, field.NewPath("metadata"))
  25. for _, msg := range apivalidation.NameIsDNSLabel(rc.Handler, false) {
  26. allErrs = append(allErrs, field.Invalid(field.NewPath("handler"), rc.Handler, msg))
  27. }
  28. if rc.Overhead != nil {
  29. allErrs = append(allErrs, validateOverhead(rc.Overhead, field.NewPath("overhead"))...)
  30. }
  31. if rc.Scheduling != nil {
  32. allErrs = append(allErrs, validateScheduling(rc.Scheduling, field.NewPath("scheduling"))...)
  33. }
  34. return allErrs
  35. }
  36. // ValidateRuntimeClassUpdate validates an update to the object
  37. func ValidateRuntimeClassUpdate(new, old *node.RuntimeClass) field.ErrorList {
  38. allErrs := apivalidation.ValidateObjectMetaUpdate(&new.ObjectMeta, &old.ObjectMeta, field.NewPath("metadata"))
  39. allErrs = append(allErrs, apivalidation.ValidateImmutableField(new.Handler, old.Handler, field.NewPath("handler"))...)
  40. return allErrs
  41. }
  42. func validateOverhead(overhead *node.Overhead, fldPath *field.Path) field.ErrorList {
  43. // reuse the ResourceRequirements validation logic
  44. return corevalidation.ValidateResourceRequirements(&core.ResourceRequirements{Limits: overhead.PodFixed}, fldPath)
  45. }
  46. func validateScheduling(s *node.Scheduling, fldPath *field.Path) field.ErrorList {
  47. var allErrs field.ErrorList
  48. if s.NodeSelector != nil {
  49. allErrs = append(allErrs, unversionedvalidation.ValidateLabels(s.NodeSelector, fldPath.Child("nodeSelector"))...)
  50. }
  51. allErrs = append(allErrs, validateTolerations(s.Tolerations, fldPath.Child("tolerations"))...)
  52. return allErrs
  53. }
  54. func validateTolerations(tolerations []core.Toleration, fldPath *field.Path) field.ErrorList {
  55. allErrs := corevalidation.ValidateTolerations(tolerations, fldPath.Child("tolerations"))
  56. // Ensure uniquenes of tolerations.
  57. tolerationSet := map[core.Toleration]bool{}
  58. for i, t := range tolerations {
  59. // listKey includes the toleration fields identified as listKeys in the API.
  60. listKey := core.Toleration{
  61. Key: t.Key,
  62. Operator: t.Operator,
  63. Value: t.Value,
  64. Effect: t.Effect,
  65. }
  66. if tolerationSet[listKey] {
  67. allErrs = append(allErrs, field.Duplicate(fldPath.Index(i), t))
  68. } else {
  69. tolerationSet[listKey] = true
  70. }
  71. }
  72. return allErrs
  73. }