Skip to content

Commit 0fd8e9d

Browse files
authored
feat: add support for TCPRoute (#2564)
1 parent 7510e5c commit 0fd8e9d

File tree

18 files changed

+1072
-4
lines changed

18 files changed

+1072
-4
lines changed

config/rbac/role.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ rules:
9292
- grpcroutes/status
9393
- httproutes/status
9494
- referencegrants/status
95+
- tcproutes/status
9596
verbs:
9697
- get
9798
- update
@@ -101,6 +102,7 @@ rules:
101102
- gateways
102103
- grpcroutes
103104
- httproutes
105+
- tcproutes
104106
- referencegrants
105107
verbs:
106108
- get

docs/en/latest/concepts/gateway-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ By supporting Gateway API, the APISIX Ingress controller can realize richer func
5151
| GRPCRoute | Supported | Supported | Not supported | v1 |
5252
| ReferenceGrant | Supported | Not supported | Not supported | v1beta1 |
5353
| TLSRoute | Not supported | Not supported | Not supported | v1alpha2 |
54-
| TCPRoute | Not supported | Not supported | Not supported | v1alpha2 |
54+
| TCPRoute | Supported | Supported | Not supported | v1alpha2 |
5555
| UDPRoute | Not supported | Not supported | Not supported | v1alpha2 |
5656
| BackendTLSPolicy | Not supported | Not supported | Not supported | v1alpha3 |
5757

examples/httpbin/tcproute.yaml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
apiVersion: gateway.networking.k8s.io/v1
19+
kind: GatewayClass
20+
metadata:
21+
name: apisix
22+
spec:
23+
controllerName: "apisix.apache.org/apisix-ingress-controller"
24+
25+
---
26+
27+
apiVersion: apisix.apache.org/v1alpha1
28+
kind: GatewayProxy
29+
metadata:
30+
name: apisix-proxy-config
31+
spec:
32+
provider:
33+
type: ControlPlane
34+
controlPlane:
35+
endpoints:
36+
- ${ADMIN_ENDPOINT} # https://127.0.0.1:7443
37+
auth:
38+
type: AdminKey
39+
adminKey:
40+
value: "${ADMIN_KEY}"
41+
42+
---
43+
44+
apiVersion: gateway.networking.k8s.io/v1
45+
kind: Gateway
46+
metadata:
47+
name: apisix
48+
spec:
49+
gatewayClassName: apisix
50+
listeners:
51+
- name: foo
52+
protocol: TCP
53+
port: 80
54+
allowedRoutes:
55+
kinds:
56+
- kind: TCPRoute
57+
infrastructure:
58+
parametersRef:
59+
group: apisix.apache.org
60+
kind: GatewayProxy
61+
name: apisix-proxy-config
62+
---
63+
64+
apiVersion: gateway.networking.k8s.io/v1alpha2
65+
kind: TCPRoute
66+
metadata:
67+
name: tcp-app-1
68+
spec:
69+
parentRefs:
70+
- name: apisix
71+
sectionName: foo
72+
rules:
73+
- backendRefs:
74+
- name: httpbin
75+
port: 80
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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 translator
19+
20+
import (
21+
"fmt"
22+
23+
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
24+
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
25+
26+
adctypes "github.com/apache/apisix-ingress-controller/api/adc"
27+
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
28+
"github.com/apache/apisix-ingress-controller/internal/controller/label"
29+
"github.com/apache/apisix-ingress-controller/internal/id"
30+
"github.com/apache/apisix-ingress-controller/internal/provider"
31+
"github.com/apache/apisix-ingress-controller/internal/types"
32+
)
33+
34+
func newDefaultUpstreamWithoutScheme() *adctypes.Upstream {
35+
return &adctypes.Upstream{
36+
Metadata: adctypes.Metadata{
37+
Labels: map[string]string{
38+
"managed-by": "apisix-ingress-controller",
39+
},
40+
},
41+
Nodes: make(adctypes.UpstreamNodes, 0),
42+
}
43+
}
44+
45+
func (t *Translator) TranslateTCPRoute(tctx *provider.TranslateContext, tcpRoute *gatewayv1alpha2.TCPRoute) (*TranslateResult, error) {
46+
result := &TranslateResult{}
47+
rules := tcpRoute.Spec.Rules
48+
labels := label.GenLabel(tcpRoute)
49+
for ruleIndex, rule := range rules {
50+
service := adctypes.NewDefaultService()
51+
service.Labels = labels
52+
service.Name = adctypes.ComposeServiceNameWithStream(tcpRoute.Namespace, tcpRoute.Name, fmt.Sprintf("%d", ruleIndex))
53+
service.ID = id.GenID(service.Name)
54+
var (
55+
upstreams = make([]*adctypes.Upstream, 0)
56+
weightedUpstreams = make([]adctypes.TrafficSplitConfigRuleWeightedUpstream, 0)
57+
)
58+
for _, backend := range rule.BackendRefs {
59+
if backend.Namespace == nil {
60+
namespace := gatewayv1.Namespace(tcpRoute.Namespace)
61+
backend.Namespace = &namespace
62+
}
63+
upstream := newDefaultUpstreamWithoutScheme()
64+
upNodes, err := t.translateBackendRef(tctx, backend, DefaultEndpointFilter)
65+
if err != nil {
66+
continue
67+
}
68+
if len(upNodes) == 0 {
69+
continue
70+
}
71+
// TODO: Confirm BackendTrafficPolicy attachment with e2e test case.
72+
t.AttachBackendTrafficPolicyToUpstream(backend, tctx.BackendTrafficPolicies, upstream)
73+
upstream.Nodes = upNodes
74+
var (
75+
kind string
76+
port int32
77+
)
78+
if backend.Kind == nil {
79+
kind = types.KindService
80+
} else {
81+
kind = string(*backend.Kind)
82+
}
83+
if backend.Port != nil {
84+
port = int32(*backend.Port)
85+
}
86+
namespace := string(*backend.Namespace)
87+
name := string(backend.Name)
88+
upstreamName := adctypes.ComposeUpstreamNameForBackendRef(kind, namespace, name, port)
89+
upstream.Name = upstreamName
90+
upstream.ID = id.GenID(upstreamName)
91+
upstreams = append(upstreams, upstream)
92+
}
93+
94+
// Handle multiple backends with traffic-split plugin
95+
if len(upstreams) == 0 {
96+
// Create a default upstream if no valid backends
97+
upstream := adctypes.NewDefaultUpstream()
98+
service.Upstream = upstream
99+
} else if len(upstreams) == 1 {
100+
// Single backend - use directly as service upstream
101+
service.Upstream = upstreams[0]
102+
// remove the id and name of the service.upstream, adc schema does not need id and name for it
103+
service.Upstream.ID = ""
104+
service.Upstream.Name = ""
105+
} else {
106+
// Multiple backends - use traffic-split plugin
107+
service.Upstream = upstreams[0]
108+
// remove the id and name of the service.upstream, adc schema does not need id and name for it
109+
service.Upstream.ID = ""
110+
service.Upstream.Name = ""
111+
112+
upstreams = upstreams[1:]
113+
114+
if len(upstreams) > 0 {
115+
service.Upstreams = upstreams
116+
}
117+
118+
// Set weight in traffic-split for the default upstream
119+
weight := apiv2.DefaultWeight
120+
if rule.BackendRefs[0].Weight != nil {
121+
weight = int(*rule.BackendRefs[0].Weight)
122+
}
123+
weightedUpstreams = append(weightedUpstreams, adctypes.TrafficSplitConfigRuleWeightedUpstream{
124+
Weight: weight,
125+
})
126+
127+
// Set other upstreams in traffic-split using upstream_id
128+
for i, upstream := range upstreams {
129+
weight := apiv2.DefaultWeight
130+
// get weight from the backend refs starting from the second backend
131+
if i+1 < len(rule.BackendRefs) && rule.BackendRefs[i+1].Weight != nil {
132+
weight = int(*rule.BackendRefs[i+1].Weight)
133+
}
134+
weightedUpstreams = append(weightedUpstreams, adctypes.TrafficSplitConfigRuleWeightedUpstream{
135+
UpstreamID: upstream.ID,
136+
Weight: weight,
137+
})
138+
}
139+
140+
if len(weightedUpstreams) > 0 {
141+
if service.Plugins == nil {
142+
service.Plugins = make(map[string]any)
143+
}
144+
service.Plugins["traffic-split"] = &adctypes.TrafficSplitConfig{
145+
Rules: []adctypes.TrafficSplitConfigRule{
146+
{
147+
WeightedUpstreams: weightedUpstreams,
148+
},
149+
},
150+
}
151+
}
152+
}
153+
streamRoute := adctypes.NewDefaultStreamRoute()
154+
streamRouteName := adctypes.ComposeStreamRouteName(tcpRoute.Namespace, tcpRoute.Name, fmt.Sprintf("%d", ruleIndex))
155+
streamRoute.Name = streamRouteName
156+
streamRoute.ID = id.GenID(streamRouteName)
157+
streamRoute.Labels = labels
158+
// TODO: support remote_addr, server_addr, sni, server_port
159+
service.StreamRoutes = append(service.StreamRoutes, streamRoute)
160+
result.Services = append(result.Services, service)
161+
}
162+
return result, nil
163+
}

internal/controller/indexer/indexer.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
ctrl "sigs.k8s.io/controller-runtime"
2929
"sigs.k8s.io/controller-runtime/pkg/client"
3030
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
31+
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
3132

3233
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
3334
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
@@ -55,6 +56,7 @@ func SetupIndexer(mgr ctrl.Manager) error {
5556
for _, setup := range []func(ctrl.Manager) error{
5657
setupGatewayIndexer,
5758
setupHTTPRouteIndexer,
59+
setupTCPRouteIndexer,
5860
setupGRPCRouteIndexer,
5961
setupIngressIndexer,
6062
setupConsumerIndexer,
@@ -230,6 +232,26 @@ func setupHTTPRouteIndexer(mgr ctrl.Manager) error {
230232
return nil
231233
}
232234

235+
func setupTCPRouteIndexer(mgr ctrl.Manager) error {
236+
if err := mgr.GetFieldIndexer().IndexField(
237+
context.Background(),
238+
&gatewayv1alpha2.TCPRoute{},
239+
ParentRefs,
240+
TCPRouteParentRefsIndexFunc,
241+
); err != nil {
242+
return err
243+
}
244+
245+
if err := mgr.GetFieldIndexer().IndexField(
246+
context.Background(),
247+
&gatewayv1alpha2.TCPRoute{},
248+
ServiceIndexRef,
249+
TCPPRouteServiceIndexFunc,
250+
); err != nil {
251+
return err
252+
}
253+
return nil
254+
}
233255
func setupIngressClassIndexer(mgr ctrl.Manager) error {
234256
// create IngressClass index
235257
if err := mgr.GetFieldIndexer().IndexField(
@@ -482,6 +504,19 @@ func HTTPRouteParentRefsIndexFunc(rawObj client.Object) []string {
482504
return keys
483505
}
484506

507+
func TCPRouteParentRefsIndexFunc(rawObj client.Object) []string {
508+
tr := rawObj.(*gatewayv1alpha2.TCPRoute)
509+
keys := make([]string, 0, len(tr.Spec.ParentRefs))
510+
for _, ref := range tr.Spec.ParentRefs {
511+
ns := tr.GetNamespace()
512+
if ref.Namespace != nil {
513+
ns = string(*ref.Namespace)
514+
}
515+
keys = append(keys, GenIndexKey(ns, string(ref.Name)))
516+
}
517+
return keys
518+
}
519+
485520
func HTTPRouteServiceIndexFunc(rawObj client.Object) []string {
486521
hr := rawObj.(*gatewayv1.HTTPRoute)
487522
keys := make([]string, 0, len(hr.Spec.Rules))
@@ -500,6 +535,24 @@ func HTTPRouteServiceIndexFunc(rawObj client.Object) []string {
500535
return keys
501536
}
502537

538+
func TCPPRouteServiceIndexFunc(rawObj client.Object) []string {
539+
tr := rawObj.(*gatewayv1alpha2.TCPRoute)
540+
keys := make([]string, 0, len(tr.Spec.Rules))
541+
for _, rule := range tr.Spec.Rules {
542+
for _, backend := range rule.BackendRefs {
543+
namespace := tr.GetNamespace()
544+
if backend.Kind != nil && *backend.Kind != internaltypes.KindService {
545+
continue
546+
}
547+
if backend.Namespace != nil {
548+
namespace = string(*backend.Namespace)
549+
}
550+
keys = append(keys, GenIndexKey(namespace, string(backend.Name)))
551+
}
552+
}
553+
return keys
554+
}
555+
503556
func ApisixRouteServiceIndexFunc(cli client.Client) func(client.Object) []string {
504557
return func(obj client.Object) (keys []string) {
505558
ar := obj.(*apiv2.ApisixRoute)

0 commit comments

Comments
 (0)