validation.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Copyright 2014 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. "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/api/resource"
  19. "k8s.io/apimachinery/pkg/util/sets"
  20. "k8s.io/apimachinery/pkg/util/validation"
  21. "k8s.io/apimachinery/pkg/util/validation/field"
  22. "k8s.io/kubernetes/pkg/apis/core/helper"
  23. v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
  24. )
  25. const isNegativeErrorMsg string = `must be greater than or equal to 0`
  26. const isNotIntegerErrorMsg string = `must be an integer`
  27. func ValidateResourceRequirements(requirements *v1.ResourceRequirements, fldPath *field.Path) field.ErrorList {
  28. allErrs := field.ErrorList{}
  29. limPath := fldPath.Child("limits")
  30. reqPath := fldPath.Child("requests")
  31. for resourceName, quantity := range requirements.Limits {
  32. fldPath := limPath.Key(string(resourceName))
  33. // Validate resource name.
  34. allErrs = append(allErrs, validateContainerResourceName(string(resourceName), fldPath)...)
  35. // Validate resource quantity.
  36. allErrs = append(allErrs, ValidateResourceQuantityValue(string(resourceName), quantity, fldPath)...)
  37. }
  38. for resourceName, quantity := range requirements.Requests {
  39. fldPath := reqPath.Key(string(resourceName))
  40. // Validate resource name.
  41. allErrs = append(allErrs, validateContainerResourceName(string(resourceName), fldPath)...)
  42. // Validate resource quantity.
  43. allErrs = append(allErrs, ValidateResourceQuantityValue(string(resourceName), quantity, fldPath)...)
  44. // Check that request <= limit.
  45. limitQuantity, exists := requirements.Limits[resourceName]
  46. if exists {
  47. // For GPUs, not only requests can't exceed limits, they also can't be lower, i.e. must be equal.
  48. if quantity.Cmp(limitQuantity) != 0 && !v1helper.IsOvercommitAllowed(resourceName) {
  49. allErrs = append(allErrs, field.Invalid(reqPath, quantity.String(), fmt.Sprintf("must be equal to %s limit", resourceName)))
  50. } else if quantity.Cmp(limitQuantity) > 0 {
  51. allErrs = append(allErrs, field.Invalid(reqPath, quantity.String(), fmt.Sprintf("must be less than or equal to %s limit", resourceName)))
  52. }
  53. }
  54. }
  55. return allErrs
  56. }
  57. func validateContainerResourceName(value string, fldPath *field.Path) field.ErrorList {
  58. allErrs := validateResourceName(value, fldPath)
  59. if len(strings.Split(value, "/")) == 1 {
  60. if !helper.IsStandardContainerResourceName(value) {
  61. return append(allErrs, field.Invalid(fldPath, value, "must be a standard resource for containers"))
  62. }
  63. } else if !v1helper.IsNativeResource(v1.ResourceName(value)) {
  64. if !v1helper.IsExtendedResourceName(v1.ResourceName(value)) {
  65. return append(allErrs, field.Invalid(fldPath, value, "doesn't follow extended resource name standard"))
  66. }
  67. }
  68. return allErrs
  69. }
  70. // ValidateResourceQuantityValue enforces that specified quantity is valid for specified resource
  71. func ValidateResourceQuantityValue(resource string, value resource.Quantity, fldPath *field.Path) field.ErrorList {
  72. allErrs := field.ErrorList{}
  73. allErrs = append(allErrs, ValidateNonnegativeQuantity(value, fldPath)...)
  74. if helper.IsIntegerResourceName(resource) {
  75. if value.MilliValue()%int64(1000) != int64(0) {
  76. allErrs = append(allErrs, field.Invalid(fldPath, value, isNotIntegerErrorMsg))
  77. }
  78. }
  79. return allErrs
  80. }
  81. // Validates that a Quantity is not negative
  82. func ValidateNonnegativeQuantity(value resource.Quantity, fldPath *field.Path) field.ErrorList {
  83. allErrs := field.ErrorList{}
  84. if value.Cmp(resource.Quantity{}) < 0 {
  85. allErrs = append(allErrs, field.Invalid(fldPath, value.String(), isNegativeErrorMsg))
  86. }
  87. return allErrs
  88. }
  89. // Validate compute resource typename.
  90. // Refer to docs/design/resources.md for more details.
  91. func validateResourceName(value string, fldPath *field.Path) field.ErrorList {
  92. allErrs := field.ErrorList{}
  93. for _, msg := range validation.IsQualifiedName(value) {
  94. allErrs = append(allErrs, field.Invalid(fldPath, value, msg))
  95. }
  96. if len(allErrs) != 0 {
  97. return allErrs
  98. }
  99. if len(strings.Split(value, "/")) == 1 {
  100. if !helper.IsStandardResourceName(value) {
  101. return append(allErrs, field.Invalid(fldPath, value, "must be a standard resource type or fully qualified"))
  102. }
  103. }
  104. return allErrs
  105. }
  106. func ValidatePodLogOptions(opts *v1.PodLogOptions) field.ErrorList {
  107. allErrs := field.ErrorList{}
  108. if opts.TailLines != nil && *opts.TailLines < 0 {
  109. allErrs = append(allErrs, field.Invalid(field.NewPath("tailLines"), *opts.TailLines, isNegativeErrorMsg))
  110. }
  111. if opts.LimitBytes != nil && *opts.LimitBytes < 1 {
  112. allErrs = append(allErrs, field.Invalid(field.NewPath("limitBytes"), *opts.LimitBytes, "must be greater than 0"))
  113. }
  114. switch {
  115. case opts.SinceSeconds != nil && opts.SinceTime != nil:
  116. allErrs = append(allErrs, field.Forbidden(field.NewPath(""), "at most one of `sinceTime` or `sinceSeconds` may be specified"))
  117. case opts.SinceSeconds != nil:
  118. if *opts.SinceSeconds < 1 {
  119. allErrs = append(allErrs, field.Invalid(field.NewPath("sinceSeconds"), *opts.SinceSeconds, "must be greater than 0"))
  120. }
  121. }
  122. return allErrs
  123. }
  124. func AccumulateUniqueHostPorts(containers []v1.Container, accumulator *sets.String, fldPath *field.Path) field.ErrorList {
  125. allErrs := field.ErrorList{}
  126. for ci, ctr := range containers {
  127. idxPath := fldPath.Index(ci)
  128. portsPath := idxPath.Child("ports")
  129. for pi := range ctr.Ports {
  130. idxPath := portsPath.Index(pi)
  131. port := ctr.Ports[pi].HostPort
  132. if port == 0 {
  133. continue
  134. }
  135. str := fmt.Sprintf("%d/%s", port, ctr.Ports[pi].Protocol)
  136. if accumulator.Has(str) {
  137. allErrs = append(allErrs, field.Duplicate(idxPath.Child("hostPort"), str))
  138. } else {
  139. accumulator.Insert(str)
  140. }
  141. }
  142. }
  143. return allErrs
  144. }