conditional_validation.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/apis/networking"
  19. "k8s.io/kubernetes/pkg/features"
  20. )
  21. // ValidateConditionalNetworkPolicy validates conditionally valid fields.
  22. func ValidateConditionalNetworkPolicy(np, oldNP *networking.NetworkPolicy) field.ErrorList {
  23. var errs field.ErrorList
  24. // If the SCTPSupport feature is disabled, and the old object isn't using the SCTP feature, prevent the new object from using it
  25. if !utilfeature.DefaultFeatureGate.Enabled(features.SCTPSupport) && len(sctpFields(oldNP)) == 0 {
  26. for _, f := range sctpFields(np) {
  27. errs = append(errs, field.NotSupported(f, api.ProtocolSCTP, []string{string(api.ProtocolTCP), string(api.ProtocolUDP)}))
  28. }
  29. }
  30. return errs
  31. }
  32. func sctpFields(np *networking.NetworkPolicy) []*field.Path {
  33. if np == nil {
  34. return nil
  35. }
  36. fields := []*field.Path{}
  37. for iIndex, e := range np.Spec.Ingress {
  38. for pIndex, p := range e.Ports {
  39. if p.Protocol != nil && *p.Protocol == api.ProtocolSCTP {
  40. fields = append(fields, field.NewPath("spec.ingress").Index(iIndex).Child("ports").Index(pIndex).Child("protocol"))
  41. }
  42. }
  43. }
  44. for eIndex, e := range np.Spec.Egress {
  45. for pIndex, p := range e.Ports {
  46. if p.Protocol != nil && *p.Protocol == api.ProtocolSCTP {
  47. fields = append(fields, field.NewPath("spec.egress").Index(eIndex).Child("ports").Index(pIndex).Child("protocol"))
  48. }
  49. }
  50. }
  51. return fields
  52. }