Skip to content

Commit b2686dd

Browse files
github-actions[bot]Revolyssupronething
authored
feat: add support for TCPRoute (#2564) (#289)
Signed-off-by: Ashing Zheng <[email protected]> Co-authored-by: Ashish Tiwari <[email protected]> Co-authored-by: Ashing Zheng <[email protected]>
1 parent 749fbdb commit b2686dd

File tree

19 files changed

+1099
-5
lines changed

19 files changed

+1099
-5
lines changed

config/rbac/role.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ rules:
9393
- grpcroutes/status
9494
- httproutes/status
9595
- referencegrants/status
96+
- tcproutes/status
9697
verbs:
9798
- get
9899
- update
@@ -102,6 +103,7 @@ rules:
102103
- gateways
103104
- grpcroutes
104105
- httproutes
106+
- tcproutes
105107
- referencegrants
106108
verbs:
107109
- 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
@@ -29,6 +29,7 @@ import (
2929
ctrl "sigs.k8s.io/controller-runtime"
3030
"sigs.k8s.io/controller-runtime/pkg/client"
3131
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
32+
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
3233

3334
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
3435
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
@@ -62,6 +63,7 @@ func SetupIndexer(mgr ctrl.Manager) error {
6263
&gatewayv1.Gateway{}: setupGatewayIndexer,
6364
&gatewayv1.HTTPRoute{}: setupHTTPRouteIndexer,
6465
&gatewayv1.GRPCRoute{}: setupGRPCRouteIndexer,
66+
&gatewayv1alpha2.TCPRoute{}: setupTCPRouteIndexer,
6567
&gatewayv1.GatewayClass{}: setupGatewayClassIndexer,
6668
&v1alpha1.Consumer{}: setupConsumerIndexer,
6769
&networkingv1.Ingress{}: setupIngressIndexer,
@@ -257,6 +259,26 @@ func setupHTTPRouteIndexer(mgr ctrl.Manager) error {
257259
return nil
258260
}
259261

262+
func setupTCPRouteIndexer(mgr ctrl.Manager) error {
263+
if err := mgr.GetFieldIndexer().IndexField(
264+
context.Background(),
265+
&gatewayv1alpha2.TCPRoute{},
266+
ParentRefs,
267+
TCPRouteParentRefsIndexFunc,
268+
); err != nil {
269+
return err
270+
}
271+
272+
if err := mgr.GetFieldIndexer().IndexField(
273+
context.Background(),
274+
&gatewayv1alpha2.TCPRoute{},
275+
ServiceIndexRef,
276+
TCPPRouteServiceIndexFunc,
277+
); err != nil {
278+
return err
279+
}
280+
return nil
281+
}
260282
func setupIngressClassIndexer(mgr ctrl.Manager) error {
261283
// create IngressClass index
262284
if err := mgr.GetFieldIndexer().IndexField(
@@ -543,6 +565,19 @@ func HTTPRouteParentRefsIndexFunc(rawObj client.Object) []string {
543565
return keys
544566
}
545567

568+
func TCPRouteParentRefsIndexFunc(rawObj client.Object) []string {
569+
tr := rawObj.(*gatewayv1alpha2.TCPRoute)
570+
keys := make([]string, 0, len(tr.Spec.ParentRefs))
571+
for _, ref := range tr.Spec.ParentRefs {
572+
ns := tr.GetNamespace()
573+
if ref.Namespace != nil {
574+
ns = string(*ref.Namespace)
575+
}
576+
keys = append(keys, GenIndexKey(ns, string(ref.Name)))
577+
}
578+
return keys
579+
}
580+
546581
func HTTPRouteServiceIndexFunc(rawObj client.Object) []string {
547582
hr := rawObj.(*gatewayv1.HTTPRoute)
548583
keys := make([]string, 0, len(hr.Spec.Rules))
@@ -561,6 +596,24 @@ func HTTPRouteServiceIndexFunc(rawObj client.Object) []string {
561596
return keys
562597
}
563598

599+
func TCPPRouteServiceIndexFunc(rawObj client.Object) []string {
600+
tr := rawObj.(*gatewayv1alpha2.TCPRoute)
601+
keys := make([]string, 0, len(tr.Spec.Rules))
602+
for _, rule := range tr.Spec.Rules {
603+
for _, backend := range rule.BackendRefs {
604+
namespace := tr.GetNamespace()
605+
if backend.Kind != nil && *backend.Kind != internaltypes.KindService {
606+
continue
607+
}
608+
if backend.Namespace != nil {
609+
namespace = string(*backend.Namespace)
610+
}
611+
keys = append(keys, GenIndexKey(namespace, string(backend.Name)))
612+
}
613+
}
614+
return keys
615+
}
616+
564617
func ApisixRouteServiceIndexFunc(cli client.Client) func(client.Object) []string {
565618
return func(obj client.Object) (keys []string) {
566619
ar := obj.(*apiv2.ApisixRoute)

0 commit comments

Comments
 (0)