conditional_validation.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. "k8s.io/apimachinery/pkg/util/validation/field"
  16. utilfeature "k8s.io/apiserver/pkg/util/feature"
  17. api "k8s.io/kubernetes/pkg/apis/core"
  18. "k8s.io/kubernetes/pkg/features"
  19. )
  20. // ValidateConditionalService validates conditionally valid fields.
  21. func ValidateConditionalService(service, oldService *api.Service) field.ErrorList {
  22. var errs field.ErrorList
  23. // If the SCTPSupport feature is disabled, and the old object isn't using the SCTP feature, prevent the new object from using it
  24. if !utilfeature.DefaultFeatureGate.Enabled(features.SCTPSupport) && len(serviceSCTPFields(oldService)) == 0 {
  25. for _, f := range serviceSCTPFields(service) {
  26. errs = append(errs, field.NotSupported(f, api.ProtocolSCTP, []string{string(api.ProtocolTCP), string(api.ProtocolUDP)}))
  27. }
  28. }
  29. return errs
  30. }
  31. func serviceSCTPFields(service *api.Service) []*field.Path {
  32. if service == nil {
  33. return nil
  34. }
  35. fields := []*field.Path{}
  36. for pIndex, p := range service.Spec.Ports {
  37. if p.Protocol == api.ProtocolSCTP {
  38. fields = append(fields, field.NewPath("spec.ports").Index(pIndex).Child("protocol"))
  39. }
  40. }
  41. return fields
  42. }
  43. // ValidateConditionalEndpoints validates conditionally valid fields.
  44. func ValidateConditionalEndpoints(endpoints, oldEndpoints *api.Endpoints) field.ErrorList {
  45. var errs field.ErrorList
  46. // If the SCTPSupport feature is disabled, and the old object isn't using the SCTP feature, prevent the new object from using it
  47. if !utilfeature.DefaultFeatureGate.Enabled(features.SCTPSupport) && len(endpointsSCTPFields(oldEndpoints)) == 0 {
  48. for _, f := range endpointsSCTPFields(endpoints) {
  49. errs = append(errs, field.NotSupported(f, api.ProtocolSCTP, []string{string(api.ProtocolTCP), string(api.ProtocolUDP)}))
  50. }
  51. }
  52. return errs
  53. }
  54. func endpointsSCTPFields(endpoints *api.Endpoints) []*field.Path {
  55. if endpoints == nil {
  56. return nil
  57. }
  58. fields := []*field.Path{}
  59. for sIndex, s := range endpoints.Subsets {
  60. for pIndex, p := range s.Ports {
  61. if p.Protocol == api.ProtocolSCTP {
  62. fields = append(fields, field.NewPath("subsets").Index(sIndex).Child("ports").Index(pIndex).Child("protocol"))
  63. }
  64. }
  65. }
  66. return fields
  67. }
  68. // ValidateConditionalPodTemplate validates conditionally valid fields.
  69. // This should be called from Validate/ValidateUpdate for all resources containing a PodTemplateSpec
  70. func ValidateConditionalPodTemplate(podTemplate, oldPodTemplate *api.PodTemplateSpec, fldPath *field.Path) field.ErrorList {
  71. var (
  72. podSpec *api.PodSpec
  73. oldPodSpec *api.PodSpec
  74. )
  75. if podTemplate != nil {
  76. podSpec = &podTemplate.Spec
  77. }
  78. if oldPodTemplate != nil {
  79. oldPodSpec = &oldPodTemplate.Spec
  80. }
  81. return validateConditionalPodSpec(podSpec, oldPodSpec, fldPath.Child("spec"))
  82. }
  83. // ValidateConditionalPod validates conditionally valid fields.
  84. // This should be called from Validate/ValidateUpdate for all resources containing a Pod
  85. func ValidateConditionalPod(pod, oldPod *api.Pod, fldPath *field.Path) field.ErrorList {
  86. var (
  87. podSpec *api.PodSpec
  88. oldPodSpec *api.PodSpec
  89. )
  90. if pod != nil {
  91. podSpec = &pod.Spec
  92. }
  93. if oldPod != nil {
  94. oldPodSpec = &oldPod.Spec
  95. }
  96. return validateConditionalPodSpec(podSpec, oldPodSpec, fldPath.Child("spec"))
  97. }
  98. func validateConditionalPodSpec(podSpec, oldPodSpec *api.PodSpec, fldPath *field.Path) field.ErrorList {
  99. // Always make sure we have a non-nil current pod spec
  100. if podSpec == nil {
  101. podSpec = &api.PodSpec{}
  102. }
  103. errs := field.ErrorList{}
  104. // If the SCTPSupport feature is disabled, and the old object isn't using the SCTP feature, prevent the new object from using it
  105. if !utilfeature.DefaultFeatureGate.Enabled(features.SCTPSupport) && len(podSCTPFields(oldPodSpec, nil)) == 0 {
  106. for _, f := range podSCTPFields(podSpec, fldPath) {
  107. errs = append(errs, field.NotSupported(f, api.ProtocolSCTP, []string{string(api.ProtocolTCP), string(api.ProtocolUDP)}))
  108. }
  109. }
  110. return errs
  111. }
  112. func podSCTPFields(podSpec *api.PodSpec, fldPath *field.Path) []*field.Path {
  113. if podSpec == nil {
  114. return nil
  115. }
  116. fields := []*field.Path{}
  117. for cIndex, c := range podSpec.InitContainers {
  118. for pIndex, p := range c.Ports {
  119. if p.Protocol == api.ProtocolSCTP {
  120. fields = append(fields, fldPath.Child("initContainers").Index(cIndex).Child("ports").Index(pIndex).Child("protocol"))
  121. }
  122. }
  123. }
  124. for cIndex, c := range podSpec.Containers {
  125. for pIndex, p := range c.Ports {
  126. if p.Protocol == api.ProtocolSCTP {
  127. fields = append(fields, fldPath.Child("containers").Index(cIndex).Child("ports").Index(pIndex).Child("protocol"))
  128. }
  129. }
  130. }
  131. return fields
  132. }