Skip to content

Commit 8bfdcf7

Browse files
abaguasytsarev
authored andcommitted
implement TCPRoute
Signed-off-by: Andre Aguas <andre.aguas@protonmail.com>
1 parent be1e033 commit 8bfdcf7

24 files changed

+648
-178
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ deploy-local-cluster:
235235
$(MAKE) deploy-k8gb-with-helm
236236

237237
@echo -e "\n$(YELLOW)Install Gateway API CRDs $(NC)"
238-
kubectl apply --server-side -f https://github.com/kubernetes-sigs/gateway-api/releases/download/$(GATEWAY_API_VERSION)/standard-install.yaml
238+
kubectl apply --server-side -f https://github.com/kubernetes-sigs/gateway-api/releases/download/$(GATEWAY_API_VERSION)/experimental-install.yaml
239239

240240
@echo -e "\n$(YELLOW)Install Istio CRDs $(NC)"
241241
kubectl create namespace istio-system --dry-run=client -o yaml | kubectl apply -f -

controllers/refresolver/gatewayapi/adapters.go

Lines changed: 4 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
2121
/* Adapters for Gateway API resources
2222
This file contains adapters for Gateway API resources.
2323
We need to fetch the ParentRefs and BackendRefs fields. However, different Gateway API resources exposed different types for their spec fields
24-
RouteSpec and RouteRule interfaces are used to provide a common interface for the different Gateway API resources.
24+
RouteAdapter and RouteRule interfaces are used to provide a common interface for the different Gateway API resources.
2525
*/
2626

2727
import (
2828
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
2929
)
3030

31-
// RouteSpec provides access to common route spec fields shared by all Gateway API's route resources (HTTPRoute, GRPCRoute, etc.)
32-
type RouteSpec interface {
33-
GetHostnames() []gatewayapiv1.Hostname
31+
// RouteAdapter provides access to common route spec fields shared by all Gateway API's route resources (HTTPRoute, GRPCRoute, etc.)
32+
type RouteAdapter interface {
33+
GetHostnames() ([]gatewayapiv1.Hostname, error)
3434
GetName() string
3535
GetNamespace() string
3636
GetParentRefs() []gatewayapiv1.ParentReference
@@ -41,97 +41,3 @@ type RouteSpec interface {
4141
type RouteRule interface {
4242
GetBackendRefs() []gatewayapiv1.BackendRef
4343
}
44-
45-
// HTTPRouteAdapter adapts HTTPRoute to the RouteSpec interface
46-
type HTTPRouteAdapter struct {
47-
route *gatewayapiv1.HTTPRoute
48-
}
49-
50-
// NewHTTPRouteAdapter creates a new HTTPRoute adapter
51-
func NewHTTPRouteAdapter(route *gatewayapiv1.HTTPRoute) *HTTPRouteAdapter {
52-
return &HTTPRouteAdapter{route: route}
53-
}
54-
55-
func (a *HTTPRouteAdapter) GetHostnames() []gatewayapiv1.Hostname {
56-
return a.route.Spec.Hostnames
57-
}
58-
59-
func (a *HTTPRouteAdapter) GetParentRefs() []gatewayapiv1.ParentReference {
60-
return a.route.Spec.ParentRefs
61-
}
62-
63-
func (a *HTTPRouteAdapter) GetRules() []RouteRule {
64-
rules := make([]RouteRule, len(a.route.Spec.Rules))
65-
for i := range a.route.Spec.Rules {
66-
rules[i] = &HTTPRouteRuleAdapter{rule: &a.route.Spec.Rules[i]}
67-
}
68-
return rules
69-
}
70-
71-
func (a *HTTPRouteAdapter) GetName() string {
72-
return a.route.Name
73-
}
74-
75-
func (a *HTTPRouteAdapter) GetNamespace() string {
76-
return a.route.Namespace
77-
}
78-
79-
// HTTPRouteRuleAdapter adapts HTTPRouteRule to the RouteRule interface
80-
type HTTPRouteRuleAdapter struct {
81-
rule *gatewayapiv1.HTTPRouteRule
82-
}
83-
84-
func (a *HTTPRouteRuleAdapter) GetBackendRefs() []gatewayapiv1.BackendRef {
85-
refs := make([]gatewayapiv1.BackendRef, len(a.rule.BackendRefs))
86-
for i := range a.rule.BackendRefs {
87-
refs[i] = a.rule.BackendRefs[i].BackendRef
88-
}
89-
return refs
90-
}
91-
92-
// GRPCRouteAdapter adapts GRPCRoute to the RouteSpec interface
93-
type GRPCRouteAdapter struct {
94-
route *gatewayapiv1.GRPCRoute
95-
}
96-
97-
// NewGRPCRouteAdapter creates a new GRPCRoute adapter
98-
func NewGRPCRouteAdapter(route *gatewayapiv1.GRPCRoute) *GRPCRouteAdapter {
99-
return &GRPCRouteAdapter{route: route}
100-
}
101-
102-
func (a *GRPCRouteAdapter) GetHostnames() []gatewayapiv1.Hostname {
103-
return a.route.Spec.Hostnames
104-
}
105-
106-
func (a *GRPCRouteAdapter) GetParentRefs() []gatewayapiv1.ParentReference {
107-
return a.route.Spec.ParentRefs
108-
}
109-
110-
func (a *GRPCRouteAdapter) GetRules() []RouteRule {
111-
rules := make([]RouteRule, len(a.route.Spec.Rules))
112-
for i := range a.route.Spec.Rules {
113-
rules[i] = &GRPCRouteRuleAdapter{rule: &a.route.Spec.Rules[i]}
114-
}
115-
return rules
116-
}
117-
118-
func (a *GRPCRouteAdapter) GetName() string {
119-
return a.route.Name
120-
}
121-
122-
func (a *GRPCRouteAdapter) GetNamespace() string {
123-
return a.route.Namespace
124-
}
125-
126-
// GRPCRouteRuleAdapter adapts GRPCRouteRule to the RouteRule interface
127-
type GRPCRouteRuleAdapter struct {
128-
rule *gatewayapiv1.GRPCRouteRule
129-
}
130-
131-
func (a *GRPCRouteRuleAdapter) GetBackendRefs() []gatewayapiv1.BackendRef {
132-
refs := make([]gatewayapiv1.BackendRef, len(a.rule.BackendRefs))
133-
for i := range a.rule.BackendRefs {
134-
refs[i] = a.rule.BackendRefs[i].BackendRef
135-
}
136-
return refs
137-
}

controllers/refresolver/gatewayapi/gatewayapi.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
var log = logging.Logger()
3636

3737
// GetGateway retrieves the gateway referenced by a route resource
38-
func GetGateway(route RouteSpec, k8sClient client.Client) (*gatewayapiv1.Gateway, error) {
38+
func GetGateway(route RouteAdapter, k8sClient client.Client) (*gatewayapiv1.Gateway, error) {
3939
var gateways []types.NamespacedName
4040
for _, parentRef := range route.GetParentRefs() {
4141
if parentRef.Kind != nil && string(*parentRef.Kind) != "Gateway" {
@@ -76,8 +76,11 @@ func GetGateway(route RouteSpec, k8sClient client.Client) (*gatewayapiv1.Gateway
7676
}
7777

7878
// GetServersFromRoute retrieves the GSLB server configuration from a route resource
79-
func GetServersFromRoute(route RouteSpec) ([]*k8gbv1beta1.Server, error) {
80-
hostnames := route.GetHostnames()
79+
func GetServersFromRoute(route RouteAdapter) ([]*k8gbv1beta1.Server, error) {
80+
hostnames, err := route.GetHostnames()
81+
if err != nil {
82+
return nil, err
83+
}
8184
if len(hostnames) < 1 {
8285
return nil, fmt.Errorf("can't find hosts in route %s", route.GetName())
8386
}

controllers/refresolver/gatewayapi/gatewayapi_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestGetGslbExposedIPs(t *testing.T) {
7979
for _, tt := range tests {
8080
t.Run(tt.name, func(t *testing.T) {
8181
// arrange
82-
gateway := utils.FileToGatewayAPIGateway(tt.gatewayYaml)
82+
gateway := utils.FileToGatewayApiGateway(tt.gatewayYaml)
8383

8484
// act
8585
IPs, err := GetGslbExposedIPs(gateway, tt.annotations, []utils.DNSServer{})
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package gatewayapigrpcroute
2+
3+
/*
4+
Copyright 2021-2025 The k8gb Contributors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
19+
*/
20+
21+
import (
22+
"github.com/k8gb-io/k8gb/controllers/refresolver/gatewayapi"
23+
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
24+
)
25+
26+
// GRPCRouteAdapter adapts GRPCRoute to the gatewayapi.RouteAdapter interface
27+
type GRPCRouteAdapter struct {
28+
grpcRoute *gatewayapiv1.GRPCRoute
29+
}
30+
31+
// NewGRPCRouteAdapter creates a new GRPCRoute adapter
32+
func NewGRPCRouteAdapter(grpcRoute *gatewayapiv1.GRPCRoute) *GRPCRouteAdapter {
33+
return &GRPCRouteAdapter{grpcRoute: grpcRoute}
34+
}
35+
36+
func (a *GRPCRouteAdapter) GetHostnames() ([]gatewayapiv1.Hostname, error) {
37+
return a.grpcRoute.Spec.Hostnames, nil
38+
}
39+
40+
func (a *GRPCRouteAdapter) GetParentRefs() []gatewayapiv1.ParentReference {
41+
return a.grpcRoute.Spec.ParentRefs
42+
}
43+
44+
func (a *GRPCRouteAdapter) GetRules() []gatewayapi.RouteRule {
45+
rules := make([]gatewayapi.RouteRule, len(a.grpcRoute.Spec.Rules))
46+
for i := range a.grpcRoute.Spec.Rules {
47+
rules[i] = &GRPCRouteRuleAdapter{rule: &a.grpcRoute.Spec.Rules[i]}
48+
}
49+
return rules
50+
}
51+
52+
func (a *GRPCRouteAdapter) GetName() string {
53+
return a.grpcRoute.Name
54+
}
55+
56+
func (a *GRPCRouteAdapter) GetNamespace() string {
57+
return a.grpcRoute.Namespace
58+
}
59+
60+
// GRPCRouteRuleAdapter adapts GRPCRouteRule to the RouteRule interface
61+
type GRPCRouteRuleAdapter struct {
62+
rule *gatewayapiv1.GRPCRouteRule
63+
}
64+
65+
func (a *GRPCRouteRuleAdapter) GetBackendRefs() []gatewayapiv1.BackendRef {
66+
refs := make([]gatewayapiv1.BackendRef, len(a.rule.BackendRefs))
67+
for i := range a.rule.BackendRefs {
68+
refs[i] = a.rule.BackendRefs[i].BackendRef
69+
}
70+
return refs
71+
}

controllers/refresolver/gatewayapigrpcroute/gatewayapigrpcroute.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
var log = logging.Logger()
3737

3838
type ReferenceResolver struct {
39-
grpcRoute *gatewayapiv1.GRPCRoute
39+
grpcRoute *GRPCRouteAdapter
4040
gateway *gatewayapiv1.Gateway
4141
}
4242

@@ -52,14 +52,14 @@ func NewReferenceResolver(gslb *k8gbv1beta1.Gslb, k8sClient client.Client) (*Ref
5252
}
5353
grpcRoute := grpcRouteList[0]
5454

55-
route := gatewayapi.NewGRPCRouteAdapter(&grpcRoute)
56-
gateway, err := gatewayapi.GetGateway(route, k8sClient)
55+
grpcRouteAdapter := NewGRPCRouteAdapter(&grpcRoute)
56+
gateway, err := gatewayapi.GetGateway(grpcRouteAdapter, k8sClient)
5757
if err != nil {
5858
return nil, err
5959
}
6060

6161
return &ReferenceResolver{
62-
grpcRoute: &grpcRoute,
62+
grpcRoute: grpcRouteAdapter,
6363
gateway: gateway,
6464
}, nil
6565
}
@@ -105,8 +105,7 @@ func getGslbGRPCRouteRef(gslb *k8gbv1beta1.Gslb, k8sClient client.Client) ([]gat
105105

106106
// GetServers retrieves the GSLB server configuration from the GRPCRoute resource
107107
func (rr *ReferenceResolver) GetServers() ([]*k8gbv1beta1.Server, error) {
108-
route := gatewayapi.NewGRPCRouteAdapter(rr.grpcRoute)
109-
return gatewayapi.GetServersFromRoute(route)
108+
return gatewayapi.GetServersFromRoute(rr.grpcRoute)
110109
}
111110

112111
// GetGslbExposedIPs retrieves the load balancer IP address of the GSLB

controllers/refresolver/gatewayapigrpcroute/gatewayapigrpcroute_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ func TestGetServers(t *testing.T) {
109109
for _, test := range tests {
110110
t.Run(test.name, func(t *testing.T) {
111111
// arrange
112-
grpcRoute := utils.FileToGatewayAPIGRPCRoute(test.grpcRouteFile)
112+
grpcRoute := utils.FileToGatewayApiGrpcRoute(test.grpcRouteFile)
113113
resolver := ReferenceResolver{
114-
grpcRoute: grpcRoute,
114+
grpcRoute: NewGRPCRouteAdapter(grpcRoute),
115115
}
116116

117117
// act
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package gatewayapihttproute
2+
3+
/*
4+
Copyright 2021-2025 The k8gb Contributors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
19+
*/
20+
21+
import (
22+
"github.com/k8gb-io/k8gb/controllers/refresolver/gatewayapi"
23+
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
24+
)
25+
26+
// HTTPRouteAdapter adapts HTTPRoute to the RouteAdapter interface
27+
type HTTPRouteAdapter struct {
28+
httpRoute *gatewayapiv1.HTTPRoute
29+
}
30+
31+
// NewHTTPRouteAdapter creates a new HTTPRoute adapter
32+
func NewHTTPRouteAdapter(httpRoute *gatewayapiv1.HTTPRoute) *HTTPRouteAdapter {
33+
return &HTTPRouteAdapter{httpRoute: httpRoute}
34+
}
35+
36+
func (a *HTTPRouteAdapter) GetHostnames() ([]gatewayapiv1.Hostname, error) {
37+
return a.httpRoute.Spec.Hostnames, nil
38+
}
39+
40+
func (a *HTTPRouteAdapter) GetParentRefs() []gatewayapiv1.ParentReference {
41+
return a.httpRoute.Spec.ParentRefs
42+
}
43+
44+
func (a *HTTPRouteAdapter) GetRules() []gatewayapi.RouteRule {
45+
rules := make([]gatewayapi.RouteRule, len(a.httpRoute.Spec.Rules))
46+
for i := range a.httpRoute.Spec.Rules {
47+
rules[i] = &HTTPRouteRuleAdapter{rule: &a.httpRoute.Spec.Rules[i]}
48+
}
49+
return rules
50+
}
51+
52+
func (a *HTTPRouteAdapter) GetName() string {
53+
return a.httpRoute.Name
54+
}
55+
56+
func (a *HTTPRouteAdapter) GetNamespace() string {
57+
return a.httpRoute.Namespace
58+
}
59+
60+
// HTTPRouteRuleAdapter adapts HTTPRouteRule to the RouteRule interface
61+
type HTTPRouteRuleAdapter struct {
62+
rule *gatewayapiv1.HTTPRouteRule
63+
}
64+
65+
func (a *HTTPRouteRuleAdapter) GetBackendRefs() []gatewayapiv1.BackendRef {
66+
refs := make([]gatewayapiv1.BackendRef, len(a.rule.BackendRefs))
67+
for i := range a.rule.BackendRefs {
68+
refs[i] = a.rule.BackendRefs[i].BackendRef
69+
}
70+
return refs
71+
}

controllers/refresolver/gatewayapihttproute/gatewayapihttproute.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
var log = logging.Logger()
3737

3838
type ReferenceResolver struct {
39-
httpRoute *gatewayapiv1.HTTPRoute
39+
httpRoute *HTTPRouteAdapter
4040
gateway *gatewayapiv1.Gateway
4141
}
4242

@@ -52,14 +52,14 @@ func NewReferenceResolver(gslb *k8gbv1beta1.Gslb, k8sClient client.Client) (*Ref
5252
}
5353
httpRoute := httpRouteList[0]
5454

55-
route := gatewayapi.NewHTTPRouteAdapter(&httpRoute)
56-
gateway, err := gatewayapi.GetGateway(route, k8sClient)
55+
httpRouteAdapter := NewHTTPRouteAdapter(&httpRoute)
56+
gateway, err := gatewayapi.GetGateway(httpRouteAdapter, k8sClient)
5757
if err != nil {
5858
return nil, err
5959
}
6060

6161
return &ReferenceResolver{
62-
httpRoute: &httpRoute,
62+
httpRoute: httpRouteAdapter,
6363
gateway: gateway,
6464
}, nil
6565
}
@@ -105,8 +105,7 @@ func getGslbHTTPRouteRef(gslb *k8gbv1beta1.Gslb, k8sClient client.Client) ([]gat
105105

106106
// GetServers retrieves the GSLB server configuration from the HTTPRoute resource
107107
func (rr *ReferenceResolver) GetServers() ([]*k8gbv1beta1.Server, error) {
108-
route := gatewayapi.NewHTTPRouteAdapter(rr.httpRoute)
109-
return gatewayapi.GetServersFromRoute(route)
108+
return gatewayapi.GetServersFromRoute(rr.httpRoute)
110109
}
111110

112111
// GetGslbExposedIPs retrieves the load balancer IP address of the GSLB

0 commit comments

Comments
 (0)