From 8abf1efef4176092fe4db5ad9239e6d433ca57f2 Mon Sep 17 00:00:00 2001 From: sh2 Date: Thu, 27 Jun 2024 16:37:23 +0800 Subject: [PATCH] chore: cleanup and upgrade some api to v1 (#3644) * upgrade some api from v1a2 to v1 and cleanup Signed-off-by: shawnh2 * rm unused parentRef check Signed-off-by: shawnh2 --------- Signed-off-by: shawnh2 Co-authored-by: zirain --- internal/gatewayapi/contexts.go | 36 ++--- internal/gatewayapi/helpers_v1alpha2.go | 151 +-------------------- internal/gatewayapi/route.go | 4 +- internal/provider/kubernetes/controller.go | 16 +-- internal/provider/kubernetes/indexers.go | 12 +- internal/provider/kubernetes/routes.go | 18 +-- 6 files changed, 37 insertions(+), 200 deletions(-) diff --git a/internal/gatewayapi/contexts.go b/internal/gatewayapi/contexts.go index 414386c973d..6ecb2cf7318 100644 --- a/internal/gatewayapi/contexts.go +++ b/internal/gatewayapi/contexts.go @@ -206,9 +206,6 @@ func GetRouteType(route RouteContext) gwapiv1.Kind { return gwapiv1.Kind(rv.FieldByName("Kind").String()) } -// TODO: [v1alpha2-gwapiv1] This should not be required once all Route -// objects being implemented are of type gwapiv1. - // GetHostnames returns the hosts targeted by the Route object. func GetHostnames(route RouteContext) []string { rv := reflect.ValueOf(route).Elem() @@ -225,8 +222,6 @@ func GetHostnames(route RouteContext) []string { return hostnames } -// TODO: [v1alpha2-gwapiv1] This should not be required once all Route -// objects being implemented are of type gwapiv1. // GetParentReferences returns the ParentReference of the Route object. func GetParentReferences(route RouteContext) []gwapiv1.ParentReference { rv := reflect.ValueOf(route).Elem() @@ -256,11 +251,6 @@ func GetRouteParentContext(route RouteContext, forParentRef gwapiv1.ParentRefere return ctx } - isHTTPRoute := false - if rv.FieldByName("Kind").String() == KindHTTPRoute { - isHTTPRoute = true - } - var parentRef *gwapiv1.ParentReference specParentRefs := rv.FieldByName("Spec").FieldByName("ParentRefs") for i := 0; i < specParentRefs.Len(); i++ { @@ -275,32 +265,28 @@ func GetRouteParentContext(route RouteContext, forParentRef gwapiv1.ParentRefere } routeParentStatusIdx := -1 + defaultNamespace := gwapiv1.Namespace(metav1.NamespaceDefault) statusParents := rv.FieldByName("Status").FieldByName("Parents") for i := 0; i < statusParents.Len(); i++ { p := statusParents.Index(i).FieldByName("ParentRef").Interface().(gwapiv1.ParentReference) - if !isHTTPRoute { - p = UpgradeParentReference(p) - defaultNamespace := gwapiv1.Namespace(metav1.NamespaceDefault) - if forParentRef.Namespace == nil { - forParentRef.Namespace = &defaultNamespace - } - if p.Namespace == nil { - p.Namespace = &defaultNamespace - } + // For those non-v1 routes, their underlying type of `ParentReference` is v1 as well. + // So we can skip upgrading these routes for simplicity. + if forParentRef.Namespace == nil { + forParentRef.Namespace = &defaultNamespace + } + if p.Namespace == nil { + p.Namespace = &defaultNamespace } if reflect.DeepEqual(p, forParentRef) { routeParentStatusIdx = i break } } + if routeParentStatusIdx == -1 { - tmpPR := forParentRef - if !isHTTPRoute { - tmpPR = DowngradeParentReference(tmpPR) - } rParentStatus := gwapiv1a2.RouteParentStatus{ ControllerName: gwapiv1a2.GatewayController(rv.FieldByName("GatewayControllerName").String()), - ParentRef: tmpPR, + ParentRef: forParentRef, } statusParents.Set(reflect.Append(statusParents, reflect.ValueOf(rParentStatus))) routeParentStatusIdx = statusParents.Len() - 1 @@ -372,8 +358,8 @@ func GetBackendRef(b BackendRefContext) *gwapiv1.BackendRef { if br.IsValid() { backendRef := br.Interface().(gwapiv1.BackendRef) return &backendRef - } + backendRef := b.(gwapiv1.BackendRef) return &backendRef } diff --git a/internal/gatewayapi/helpers_v1alpha2.go b/internal/gatewayapi/helpers_v1alpha2.go index afa80413175..3b1dffde66f 100644 --- a/internal/gatewayapi/helpers_v1alpha2.go +++ b/internal/gatewayapi/helpers_v1alpha2.go @@ -17,125 +17,7 @@ import ( ) // TODO: [gwapiv1a2-gwapiv1] -// This file can be removed once TLSRoute graduates to gwapiv1. - -func GroupPtrV1Alpha2(group string) *gwapiv1a2.Group { - gwGroup := gwapiv1a2.Group(group) - return &gwGroup -} - -func KindPtrV1Alpha2(kind string) *gwapiv1a2.Kind { - gwKind := gwapiv1a2.Kind(kind) - return &gwKind -} - -func NamespacePtrV1Alpha2(namespace string) *gwapiv1a2.Namespace { - gwNamespace := gwapiv1a2.Namespace(namespace) - return &gwNamespace -} - -func SectionNamePtrV1Alpha2(sectionName string) *gwapiv1a2.SectionName { - gwSectionName := gwapiv1a2.SectionName(sectionName) - return &gwSectionName -} - -func PortNumPtrV1Alpha2(port int) *gwapiv1a2.PortNumber { - pn := gwapiv1a2.PortNumber(port) - return &pn -} - -func UpgradeParentReferences(old []gwapiv1a2.ParentReference) []gwapiv1.ParentReference { - newParentReferences := make([]gwapiv1.ParentReference, len(old)) - for i, o := range old { - newParentReferences[i] = UpgradeParentReference(o) - } - return newParentReferences -} - -// UpgradeParentReference converts gwapiv1a2.ParentReference to gwapiv1.ParentReference -func UpgradeParentReference(old gwapiv1a2.ParentReference) gwapiv1.ParentReference { - upgraded := gwapiv1.ParentReference{} - - if old.Group != nil { - upgraded.Group = GroupPtr(string(*old.Group)) - } - - if old.Kind != nil { - upgraded.Kind = KindPtr(string(*old.Kind)) - } - - if old.Namespace != nil { - upgraded.Namespace = NamespacePtr(string(*old.Namespace)) - } - - upgraded.Name = old.Name - - if old.SectionName != nil { - upgraded.SectionName = SectionNamePtr(string(*old.SectionName)) - } - - if old.Port != nil { - upgraded.Port = PortNumPtr(int32(*old.Port)) - } - - return upgraded -} - -func DowngradeParentReference(old gwapiv1.ParentReference) gwapiv1a2.ParentReference { - downgraded := gwapiv1a2.ParentReference{} - - if old.Group != nil { - downgraded.Group = GroupPtrV1Alpha2(string(*old.Group)) - } - - if old.Kind != nil { - downgraded.Kind = KindPtrV1Alpha2(string(*old.Kind)) - } - - if old.Namespace != nil { - downgraded.Namespace = NamespacePtrV1Alpha2(string(*old.Namespace)) - } - - downgraded.Name = old.Name - - if old.SectionName != nil { - downgraded.SectionName = SectionNamePtrV1Alpha2(string(*old.SectionName)) - } - - if old.Port != nil { - downgraded.Port = PortNumPtrV1Alpha2(int(*old.Port)) - } - - return downgraded -} - -func UpgradeRouteParentStatuses(routeParentStatuses []gwapiv1a2.RouteParentStatus) []gwapiv1.RouteParentStatus { - var res []gwapiv1.RouteParentStatus - - for _, rps := range routeParentStatuses { - res = append(res, gwapiv1.RouteParentStatus{ - ParentRef: UpgradeParentReference(rps.ParentRef), - ControllerName: rps.ControllerName, - Conditions: rps.Conditions, - }) - } - - return res -} - -func DowngradeRouteParentStatuses(routeParentStatuses []gwapiv1.RouteParentStatus) []gwapiv1a2.RouteParentStatus { - var res []gwapiv1a2.RouteParentStatus - - for _, rps := range routeParentStatuses { - res = append(res, gwapiv1a2.RouteParentStatus{ - ParentRef: DowngradeParentReference(rps.ParentRef), - ControllerName: rps.ControllerName, - Conditions: rps.Conditions, - }) - } - - return res -} +// This file can be removed once all routes graduates to gwapiv1. // UpgradeBackendRef converts gwapiv1a2.BackendRef to gwapiv1.BackendRef func UpgradeBackendRef(old gwapiv1a2.BackendRef) gwapiv1.BackendRef { @@ -161,34 +43,3 @@ func UpgradeBackendRef(old gwapiv1a2.BackendRef) gwapiv1.BackendRef { return upgraded } - -func DowngradeBackendRef(old gwapiv1.BackendRef) gwapiv1a2.BackendRef { - downgraded := gwapiv1a2.BackendRef{} - - if old.Group != nil { - downgraded.Group = GroupPtrV1Alpha2(string(*old.Group)) - } - - if old.Kind != nil { - downgraded.Kind = KindPtrV1Alpha2(string(*old.Kind)) - } - - if old.Namespace != nil { - downgraded.Namespace = NamespacePtrV1Alpha2(string(*old.Namespace)) - } - - downgraded.Name = old.Name - - if old.Port != nil { - downgraded.Port = PortNumPtrV1Alpha2(int(*old.Port)) - } - - return downgraded -} - -func NamespaceDerefOrAlpha(namespace *gwapiv1a2.Namespace, defaultNamespace string) string { - if namespace != nil && *namespace != "" { - return string(*namespace) - } - return defaultNamespace -} diff --git a/internal/gatewayapi/route.go b/internal/gatewayapi/route.go index db59bf551e2..ebebab2356d 100644 --- a/internal/gatewayapi/route.go +++ b/internal/gatewayapi/route.go @@ -228,7 +228,7 @@ func (t *Translator) processHTTPRouteRules(httpRoute *HTTPRouteContext, parentRe httpRoute.GetGeneration(), gwapiv1.RouteConditionResolvedRefs, metav1.ConditionFalse, - gwapiv1a2.RouteReasonResolvedRefs, + gwapiv1.RouteReasonResolvedRefs, "Mixed endpointslice address type between backendRefs is not supported") } @@ -1236,7 +1236,7 @@ func (t *Translator) processDestination(backendRefContext BackendRefContext, route.GetGeneration(), gwapiv1.RouteConditionResolvedRefs, metav1.ConditionFalse, - gwapiv1a2.RouteReasonResolvedRefs, + gwapiv1.RouteReasonResolvedRefs, err.Error()) } diff --git a/internal/provider/kubernetes/controller.go b/internal/provider/kubernetes/controller.go index 27a985456a4..343d4edc9a7 100644 --- a/internal/provider/kubernetes/controller.go +++ b/internal/provider/kubernetes/controller.go @@ -519,7 +519,7 @@ func (r *gatewayAPIReconciler) processSecurityPolicyObjectRefs( resourceMap.allAssociatedBackendRefs.Insert(gwapiv1.BackendObjectReference{ Group: backendRef.Group, Kind: backendRef.Kind, - Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace), + Namespace: gatewayapi.NamespacePtr(backendNamespace), Name: backendRef.Name, }) @@ -1641,11 +1641,11 @@ func (r *gatewayAPIReconciler) processEnvoyProxy(ep *egv1a1.EnvoyProxy, resource continue } for _, backendRef := range sink.OpenTelemetry.BackendRefs { - backendNamespace := gatewayapi.NamespaceDerefOrAlpha(backendRef.Namespace, ep.Namespace) + backendNamespace := gatewayapi.NamespaceDerefOr(backendRef.Namespace, ep.Namespace) resourceMap.allAssociatedBackendRefs.Insert(gwapiv1.BackendObjectReference{ Group: backendRef.BackendObjectReference.Group, Kind: backendRef.BackendObjectReference.Kind, - Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace), + Namespace: gatewayapi.NamespacePtr(backendNamespace), Name: backendRef.Name, }) } @@ -1659,11 +1659,11 @@ func (r *gatewayAPIReconciler) processEnvoyProxy(ep *egv1a1.EnvoyProxy, resource continue } for _, backendRef := range sink.OpenTelemetry.BackendRefs { - backendNamespace := gatewayapi.NamespaceDerefOrAlpha(backendRef.Namespace, ep.Namespace) + backendNamespace := gatewayapi.NamespaceDerefOr(backendRef.Namespace, ep.Namespace) resourceMap.allAssociatedBackendRefs.Insert(gwapiv1.BackendObjectReference{ Group: backendRef.BackendObjectReference.Group, Kind: backendRef.BackendObjectReference.Kind, - Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace), + Namespace: gatewayapi.NamespacePtr(backendNamespace), Name: backendRef.Name, }) } @@ -1672,11 +1672,11 @@ func (r *gatewayAPIReconciler) processEnvoyProxy(ep *egv1a1.EnvoyProxy, resource if telemetry.Tracing != nil { for _, backendRef := range telemetry.Tracing.Provider.BackendRefs { - backendNamespace := gatewayapi.NamespaceDerefOrAlpha(backendRef.Namespace, ep.Namespace) + backendNamespace := gatewayapi.NamespaceDerefOr(backendRef.Namespace, ep.Namespace) resourceMap.allAssociatedBackendRefs.Insert(gwapiv1.BackendObjectReference{ Group: backendRef.BackendObjectReference.Group, Kind: backendRef.BackendObjectReference.Kind, - Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace), + Namespace: gatewayapi.NamespacePtr(backendNamespace), Name: backendRef.Name, }) @@ -1851,7 +1851,7 @@ func (r *gatewayAPIReconciler) processEnvoyExtensionPolicyObjectRefs( resourceMap.allAssociatedBackendRefs.Insert(gwapiv1.BackendObjectReference{ Group: backendRef.Group, Kind: backendRef.Kind, - Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace), + Namespace: gatewayapi.NamespacePtr(backendNamespace), Name: backendRef.Name, }) diff --git a/internal/provider/kubernetes/indexers.go b/internal/provider/kubernetes/indexers.go index 9b4a3f17a1f..ba5bfd51db2 100644 --- a/internal/provider/kubernetes/indexers.go +++ b/internal/provider/kubernetes/indexers.go @@ -292,7 +292,7 @@ func addTLSRouteIndexers(ctx context.Context, mgr manager.Manager) error { // lookup the provided Gateway Name. gateways = append(gateways, types.NamespacedName{ - Namespace: gatewayapi.NamespaceDerefOrAlpha(parent.Namespace, tlsRoute.Namespace), + Namespace: gatewayapi.NamespaceDerefOr(parent.Namespace, tlsRoute.Namespace), Name: string(parent.Name), }.String(), ) @@ -319,7 +319,7 @@ func backendTLSRouteIndexFunc(rawObj client.Object) []string { // lookup the provided Gateway Name. backendRefs = append(backendRefs, types.NamespacedName{ - Namespace: gatewayapi.NamespaceDerefOrAlpha(backend.Namespace, tlsroute.Namespace), + Namespace: gatewayapi.NamespaceDerefOr(backend.Namespace, tlsroute.Namespace), Name: string(backend.Name), }.String(), ) @@ -342,7 +342,7 @@ func addTCPRouteIndexers(ctx context.Context, mgr manager.Manager) error { // lookup the provided Gateway Name. gateways = append(gateways, types.NamespacedName{ - Namespace: gatewayapi.NamespaceDerefOrAlpha(parent.Namespace, tcpRoute.Namespace), + Namespace: gatewayapi.NamespaceDerefOr(parent.Namespace, tcpRoute.Namespace), Name: string(parent.Name), }.String(), ) @@ -369,7 +369,7 @@ func backendTCPRouteIndexFunc(rawObj client.Object) []string { // lookup the provided Gateway Name. backendRefs = append(backendRefs, types.NamespacedName{ - Namespace: gatewayapi.NamespaceDerefOrAlpha(backend.Namespace, tcpRoute.Namespace), + Namespace: gatewayapi.NamespaceDerefOr(backend.Namespace, tcpRoute.Namespace), Name: string(backend.Name), }.String(), ) @@ -394,7 +394,7 @@ func addUDPRouteIndexers(ctx context.Context, mgr manager.Manager) error { // lookup the provided Gateway Name. gateways = append(gateways, types.NamespacedName{ - Namespace: gatewayapi.NamespaceDerefOrAlpha(parent.Namespace, udpRoute.Namespace), + Namespace: gatewayapi.NamespaceDerefOr(parent.Namespace, udpRoute.Namespace), Name: string(parent.Name), }.String(), ) @@ -421,7 +421,7 @@ func backendUDPRouteIndexFunc(rawObj client.Object) []string { // lookup the provided Gateway Name. backendRefs = append(backendRefs, types.NamespacedName{ - Namespace: gatewayapi.NamespaceDerefOrAlpha(backend.Namespace, udproute.Namespace), + Namespace: gatewayapi.NamespaceDerefOr(backend.Namespace, udproute.Namespace), Name: string(backend.Name), }.String(), ) diff --git a/internal/provider/kubernetes/routes.go b/internal/provider/kubernetes/routes.go index 32e2f470382..ad2638684cd 100644 --- a/internal/provider/kubernetes/routes.go +++ b/internal/provider/kubernetes/routes.go @@ -61,11 +61,11 @@ func (r *gatewayAPIReconciler) processTLSRoutes(ctx context.Context, gatewayName continue } - backendNamespace := gatewayapi.NamespaceDerefOrAlpha(backendRef.Namespace, tlsRoute.Namespace) + backendNamespace := gatewayapi.NamespaceDerefOr(backendRef.Namespace, tlsRoute.Namespace) resourceMap.allAssociatedBackendRefs.Insert(gwapiv1.BackendObjectReference{ Group: backendRef.BackendObjectReference.Group, Kind: backendRef.BackendObjectReference.Kind, - Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace), + Namespace: gatewayapi.NamespacePtr(backendNamespace), Name: backendRef.Name, }) @@ -144,7 +144,7 @@ func (r *gatewayAPIReconciler) processGRPCRoutes(ctx context.Context, gatewayNam resourceMap.allAssociatedBackendRefs.Insert(gwapiv1.BackendObjectReference{ Group: backendRef.BackendObjectReference.Group, Kind: backendRef.BackendObjectReference.Kind, - Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace), + Namespace: gatewayapi.NamespacePtr(backendNamespace), Name: backendRef.Name, }) @@ -279,7 +279,7 @@ func (r *gatewayAPIReconciler) processHTTPRoutes(ctx context.Context, gatewayNam resourceMap.allAssociatedBackendRefs.Insert(gwapiv1.BackendObjectReference{ Group: backendRef.BackendObjectReference.Group, Kind: backendRef.BackendObjectReference.Kind, - Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace), + Namespace: gatewayapi.NamespacePtr(backendNamespace), Name: backendRef.Name, }) @@ -346,7 +346,7 @@ func (r *gatewayAPIReconciler) processHTTPRoutes(ctx context.Context, gatewayNam resourceMap.allAssociatedBackendRefs.Insert(gwapiv1.BackendObjectReference{ Group: mirrorBackendRef.BackendObjectReference.Group, Kind: mirrorBackendRef.BackendObjectReference.Kind, - Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace), + Namespace: gatewayapi.NamespacePtr(backendNamespace), Name: mirrorBackendRef.Name, }) @@ -454,11 +454,11 @@ func (r *gatewayAPIReconciler) processTCPRoutes(ctx context.Context, gatewayName continue } - backendNamespace := gatewayapi.NamespaceDerefOrAlpha(backendRef.Namespace, tcpRoute.Namespace) + backendNamespace := gatewayapi.NamespaceDerefOr(backendRef.Namespace, tcpRoute.Namespace) resourceMap.allAssociatedBackendRefs.Insert(gwapiv1.BackendObjectReference{ Group: backendRef.BackendObjectReference.Group, Kind: backendRef.BackendObjectReference.Kind, - Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace), + Namespace: gatewayapi.NamespacePtr(backendNamespace), Name: backendRef.Name, }) @@ -533,11 +533,11 @@ func (r *gatewayAPIReconciler) processUDPRoutes(ctx context.Context, gatewayName continue } - backendNamespace := gatewayapi.NamespaceDerefOrAlpha(backendRef.Namespace, udpRoute.Namespace) + backendNamespace := gatewayapi.NamespaceDerefOr(backendRef.Namespace, udpRoute.Namespace) resourceMap.allAssociatedBackendRefs.Insert(gwapiv1.BackendObjectReference{ Group: backendRef.BackendObjectReference.Group, Kind: backendRef.BackendObjectReference.Kind, - Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace), + Namespace: gatewayapi.NamespacePtr(backendNamespace), Name: backendRef.Name, })