1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package validation 18 19 import ( 20 "k8s.io/apimachinery/pkg/util/validation/field" 21 22 gatewayv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2" 23 ) 24 25 // ValidateTCPRoute validates TCPRoute according to the Gateway API specification. 26 // For additional details of the TCPRoute spec, refer to: 27 // https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1alpha2.TCPRoute 28 func ValidateTCPRoute(route *gatewayv1a2.TCPRoute) field.ErrorList { 29 return validateTCPRouteSpec(&route.Spec, field.NewPath("spec")) 30 } 31 32 // validateTCPRouteSpec validates that required fields of spec are set according to the 33 // TCPRoute specification. 34 func validateTCPRouteSpec(spec *gatewayv1a2.TCPRouteSpec, path *field.Path) field.ErrorList { 35 path = path.Child("rules") 36 var errs field.ErrorList 37 for i, rule := range spec.Rules { 38 for j, ref := range rule.BackendRefs { 39 // Avoid referencing to the loop variable. 40 ref := ref 41 errs = append(errs, validateBackendRefServicePort(&ref, path.Index(i).Child("backendRefs").Index(j))...) 42 errs = append(errs, validateParentRefs(spec.ParentRefs, path.Child("spec"))...) 43 } 44 } 45 return errs 46 } 47