validation.go 6.5 KB

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