Skip to content

Commit 6a6c658

Browse files
committed
f
1 parent 8da8356 commit 6a6c658

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package types
19+
20+
import (
21+
"github.com/samber/lo"
22+
"sigs.k8s.io/controller-runtime/pkg/client"
23+
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
24+
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
25+
)
26+
27+
type HTTPRouteAdapter struct {
28+
*gatewayv1.HTTPRoute
29+
}
30+
31+
func (r HTTPRouteAdapter) GetParentStatuses() []gatewayv1.RouteParentStatus {
32+
return r.Status.Parents
33+
}
34+
func (r HTTPRouteAdapter) GetParentRefs() []gatewayv1.ParentReference {
35+
return r.Spec.ParentRefs
36+
}
37+
func (r HTTPRouteAdapter) GetObject() client.Object {
38+
return r.HTTPRoute
39+
}
40+
41+
type GRPCRouteAdapter struct {
42+
*gatewayv1.GRPCRoute
43+
}
44+
45+
func (r GRPCRouteAdapter) GetParentStatuses() []gatewayv1.RouteParentStatus {
46+
return r.Status.Parents
47+
}
48+
func (r GRPCRouteAdapter) GetParentRefs() []gatewayv1.ParentReference {
49+
return r.Spec.ParentRefs
50+
}
51+
func (r GRPCRouteAdapter) GetObject() client.Object {
52+
return r.GRPCRoute
53+
}
54+
55+
type TCPRouteAdapter struct {
56+
*gatewayv1alpha2.TCPRoute
57+
}
58+
59+
func (r TCPRouteAdapter) GetParentStatuses() []gatewayv1.RouteParentStatus {
60+
return r.Status.Parents
61+
}
62+
63+
func (r TCPRouteAdapter) GetParentRefs() []gatewayv1.ParentReference {
64+
return r.Spec.ParentRefs
65+
}
66+
func (r TCPRouteAdapter) GetObject() client.Object {
67+
return r.TCPRoute
68+
}
69+
70+
type UDPRouteAdapter struct {
71+
*gatewayv1alpha2.UDPRoute
72+
}
73+
74+
func (r UDPRouteAdapter) GetParentStatuses() []gatewayv1.RouteParentStatus {
75+
return r.Status.Parents
76+
}
77+
func (r UDPRouteAdapter) GetParentRefs() []gatewayv1.ParentReference {
78+
return r.Spec.ParentRefs
79+
}
80+
func (r UDPRouteAdapter) GetObject() client.Object {
81+
return r.UDPRoute
82+
}
83+
84+
type TLSRouteAdapter struct {
85+
*gatewayv1alpha2.TLSRoute
86+
}
87+
88+
func (r TLSRouteAdapter) GetParentStatuses() []gatewayv1.RouteParentStatus {
89+
return r.Status.Parents
90+
}
91+
func (r TLSRouteAdapter) GetParentRefs() []gatewayv1.ParentReference {
92+
return r.Spec.ParentRefs
93+
}
94+
func (r TLSRouteAdapter) GetObject() client.Object {
95+
return r.TLSRoute
96+
}
97+
98+
type RouteAdapter interface {
99+
client.Object
100+
GetParentStatuses() []gatewayv1.RouteParentStatus
101+
GetParentRefs() []gatewayv1.ParentReference
102+
GetObject() client.Object
103+
}
104+
105+
func NewRouteAdapter(obj client.Object) RouteAdapter {
106+
switch r := obj.(type) {
107+
case *gatewayv1.HTTPRoute:
108+
return &HTTPRouteAdapter{HTTPRoute: r}
109+
case *gatewayv1.GRPCRoute:
110+
return &GRPCRouteAdapter{GRPCRoute: r}
111+
case *gatewayv1alpha2.TLSRoute:
112+
return &TLSRouteAdapter{TLSRoute: r}
113+
case *gatewayv1alpha2.TCPRoute:
114+
return &TCPRouteAdapter{TCPRoute: r}
115+
case *gatewayv1alpha2.UDPRoute:
116+
return &UDPRouteAdapter{UDPRoute: r}
117+
default:
118+
return nil
119+
}
120+
}
121+
122+
func NewRouteListAdapter(objList client.ObjectList) []RouteAdapter {
123+
switch r := objList.(type) {
124+
case *gatewayv1.HTTPRouteList:
125+
return lo.Map(r.Items, func(item gatewayv1.HTTPRoute, _ int) RouteAdapter {
126+
return &HTTPRouteAdapter{HTTPRoute: &item}
127+
})
128+
case *gatewayv1.GRPCRouteList:
129+
return lo.Map(r.Items, func(item gatewayv1.GRPCRoute, _ int) RouteAdapter {
130+
return &GRPCRouteAdapter{GRPCRoute: &item}
131+
})
132+
case *gatewayv1alpha2.TLSRouteList:
133+
return lo.Map(r.Items, func(item gatewayv1alpha2.TLSRoute, _ int) RouteAdapter {
134+
return &TLSRouteAdapter{TLSRoute: &item}
135+
})
136+
case *gatewayv1alpha2.TCPRouteList:
137+
return lo.Map(r.Items, func(item gatewayv1alpha2.TCPRoute, _ int) RouteAdapter {
138+
return &TCPRouteAdapter{TCPRoute: &item}
139+
})
140+
case *gatewayv1alpha2.UDPRouteList:
141+
return lo.Map(r.Items, func(item gatewayv1alpha2.UDPRoute, _ int) RouteAdapter {
142+
return &UDPRouteAdapter{UDPRoute: &item}
143+
})
144+
default:
145+
return nil
146+
}
147+
}

0 commit comments

Comments
 (0)