Skip to content

Commit

Permalink
chore: cleanup and upgrade some api to v1 (#3644)
Browse files Browse the repository at this point in the history
* upgrade some api from v1a2 to v1 and cleanup

Signed-off-by: shawnh2 <[email protected]>

* rm unused parentRef check

Signed-off-by: shawnh2 <[email protected]>

---------

Signed-off-by: shawnh2 <[email protected]>
Co-authored-by: zirain <[email protected]>
  • Loading branch information
shawnh2 and zirain authored Jun 27, 2024
1 parent 22984d7 commit 8abf1ef
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 200 deletions.
36 changes: 11 additions & 25 deletions internal/gatewayapi/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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++ {
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
151 changes: 1 addition & 150 deletions internal/gatewayapi/helpers_v1alpha2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions internal/gatewayapi/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -1236,7 +1236,7 @@ func (t *Translator) processDestination(backendRefContext BackendRefContext,
route.GetGeneration(),
gwapiv1.RouteConditionResolvedRefs,
metav1.ConditionFalse,
gwapiv1a2.RouteReasonResolvedRefs,
gwapiv1.RouteReasonResolvedRefs,
err.Error())
}

Expand Down
16 changes: 8 additions & 8 deletions internal/provider/kubernetes/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})

Expand Down Expand Up @@ -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,
})
}
Expand All @@ -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,
})
}
Expand All @@ -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,
})

Expand Down Expand Up @@ -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,
})

Expand Down
12 changes: 6 additions & 6 deletions internal/provider/kubernetes/indexers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
Expand All @@ -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(),
)
Expand All @@ -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(),
)
Expand All @@ -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(),
)
Expand All @@ -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(),
)
Expand All @@ -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(),
)
Expand Down
Loading

0 comments on commit 8abf1ef

Please sign in to comment.