Skip to content

Commit 6866490

Browse files
authored
feat(gateway-api): add support for UDPRoute (#2578)
1 parent 5bb2afd commit 6866490

File tree

15 files changed

+907
-17
lines changed

15 files changed

+907
-17
lines changed

api/adc/types.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,18 +492,22 @@ func ComposeRouteName(namespace, name string, rule string) string {
492492

493493
// ComposeStreamRouteName uses namespace, name and rule name to compose
494494
// the stream_route name.
495-
func ComposeStreamRouteName(namespace, name string, rule string) string {
495+
func ComposeStreamRouteName(namespace, name string, rule string, typ string) string {
496+
if typ == "" {
497+
typ = "TCP"
498+
}
496499
// FIXME Use sync.Pool to reuse this buffer if the upstream
497500
// name composing code path is hot.
498-
p := make([]byte, 0, len(namespace)+len(name)+len(rule)+6)
501+
p := make([]byte, 0, len(namespace)+len(name)+len(rule)+len(typ)+3)
499502
buf := bytes.NewBuffer(p)
500503

501504
buf.WriteString(namespace)
502505
buf.WriteByte('_')
503506
buf.WriteString(name)
504507
buf.WriteByte('_')
505508
buf.WriteString(rule)
506-
buf.WriteString("_tcp")
509+
buf.WriteByte('_')
510+
buf.WriteString(typ)
507511

508512
return buf.String()
509513
}
@@ -546,8 +550,8 @@ func ComposeServicesNameWithScheme(namespace, name string, rule string, scheme s
546550
return buf.String()
547551
}
548552

549-
func ComposeServiceNameWithStream(namespace, name string, rule string) string {
550-
return ComposeServicesNameWithScheme(namespace, name, rule, "stream")
553+
func ComposeServiceNameWithStream(namespace, name string, rule, typ string) string {
554+
return ComposeServicesNameWithScheme(namespace, name, rule, typ)
551555
}
552556

553557
func ComposeConsumerName(namespace, name string) string {

config/rbac/role.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ rules:
9393
- httproutes/status
9494
- referencegrants/status
9595
- tcproutes/status
96+
- udproutes/status
9697
verbs:
9798
- get
9899
- update
@@ -102,8 +103,9 @@ rules:
102103
- gateways
103104
- grpcroutes
104105
- httproutes
105-
- tcproutes
106106
- referencegrants
107+
- tcproutes
108+
- udproutes
107109
verbs:
108110
- get
109111
- list

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

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

5858
## Examples

internal/adc/translator/apisixroute.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,14 +422,14 @@ func (t *Translator) translateStreamRule(tctx *provider.TranslateContext, ar *ap
422422
t.loadRoutePlugins(tctx, ar, part.Plugins, plugins)
423423

424424
sr := adc.NewDefaultStreamRoute()
425-
sr.Name = adc.ComposeStreamRouteName(ar.Namespace, ar.Name, part.Name)
425+
sr.Name = adc.ComposeStreamRouteName(ar.Namespace, ar.Name, part.Name, part.Protocol)
426426
sr.ID = id.GenID(sr.Name)
427427
sr.ServerPort = part.Match.IngressPort
428428
sr.SNI = part.Match.Host
429429
sr.Plugins = plugins
430430

431431
svc := adc.NewDefaultService()
432-
svc.Name = adc.ComposeServiceNameWithStream(ar.Namespace, ar.Name, part.Name)
432+
svc.Name = adc.ComposeServiceNameWithStream(ar.Namespace, ar.Name, part.Name, part.Protocol)
433433
svc.ID = id.GenID(svc.Name)
434434
svc.StreamRoutes = append(svc.StreamRoutes, sr)
435435

internal/adc/translator/tcproute.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (t *Translator) TranslateTCPRoute(tctx *provider.TranslateContext, tcpRoute
4949
for ruleIndex, rule := range rules {
5050
service := adctypes.NewDefaultService()
5151
service.Labels = labels
52-
service.Name = adctypes.ComposeServiceNameWithStream(tcpRoute.Namespace, tcpRoute.Name, fmt.Sprintf("%d", ruleIndex))
52+
service.Name = adctypes.ComposeServiceNameWithStream(tcpRoute.Namespace, tcpRoute.Name, fmt.Sprintf("%d", ruleIndex), "TCP")
5353
service.ID = id.GenID(service.Name)
5454
var (
5555
upstreams = make([]*adctypes.Upstream, 0)
@@ -151,7 +151,7 @@ func (t *Translator) TranslateTCPRoute(tctx *provider.TranslateContext, tcpRoute
151151
}
152152
}
153153
streamRoute := adctypes.NewDefaultStreamRoute()
154-
streamRouteName := adctypes.ComposeStreamRouteName(tcpRoute.Namespace, tcpRoute.Name, fmt.Sprintf("%d", ruleIndex))
154+
streamRouteName := adctypes.ComposeStreamRouteName(tcpRoute.Namespace, tcpRoute.Name, fmt.Sprintf("%d", ruleIndex), "TCP")
155155
streamRoute.Name = streamRouteName
156156
streamRoute.ID = id.GenID(streamRouteName)
157157
streamRoute.Labels = labels
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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 (t *Translator) TranslateUDPRoute(tctx *provider.TranslateContext, udpRoute *gatewayv1alpha2.UDPRoute) (*TranslateResult, error) {
35+
result := &TranslateResult{}
36+
rules := udpRoute.Spec.Rules
37+
labels := label.GenLabel(udpRoute)
38+
for ruleIndex, rule := range rules {
39+
service := adctypes.NewDefaultService()
40+
service.Labels = labels
41+
service.Name = adctypes.ComposeServiceNameWithStream(udpRoute.Namespace, udpRoute.Name, fmt.Sprintf("%d", ruleIndex), "UDP")
42+
service.ID = id.GenID(service.Name)
43+
var (
44+
upstreams = make([]*adctypes.Upstream, 0)
45+
weightedUpstreams = make([]adctypes.TrafficSplitConfigRuleWeightedUpstream, 0)
46+
)
47+
for _, backend := range rule.BackendRefs {
48+
if backend.Namespace == nil {
49+
namespace := gatewayv1.Namespace(udpRoute.Namespace)
50+
backend.Namespace = &namespace
51+
}
52+
upstream := newDefaultUpstreamWithoutScheme()
53+
upNodes, err := t.translateBackendRef(tctx, backend, DefaultEndpointFilter)
54+
if err != nil {
55+
continue
56+
}
57+
if len(upNodes) == 0 {
58+
continue
59+
}
60+
// TODO: Confirm BackendTrafficPolicy attachment with e2e test case.
61+
t.AttachBackendTrafficPolicyToUpstream(backend, tctx.BackendTrafficPolicies, upstream)
62+
upstream.Nodes = upNodes
63+
var (
64+
kind string
65+
port int32
66+
)
67+
if backend.Kind == nil {
68+
kind = types.KindService
69+
} else {
70+
kind = string(*backend.Kind)
71+
}
72+
if backend.Port != nil {
73+
port = int32(*backend.Port)
74+
}
75+
namespace := string(*backend.Namespace)
76+
name := string(backend.Name)
77+
upstreamName := adctypes.ComposeUpstreamNameForBackendRef(kind, namespace, name, port)
78+
upstream.Name = upstreamName
79+
upstream.ID = id.GenID(upstreamName)
80+
upstreams = append(upstreams, upstream)
81+
}
82+
83+
// Handle multiple backends with traffic-split plugin
84+
if len(upstreams) == 0 {
85+
// Create a default upstream if no valid backends
86+
upstream := adctypes.NewDefaultUpstream()
87+
service.Upstream = upstream
88+
} else if len(upstreams) == 1 {
89+
// Single backend - use directly as service upstream
90+
service.Upstream = upstreams[0]
91+
// remove the id and name of the service.upstream, adc schema does not need id and name for it
92+
service.Upstream.ID = ""
93+
service.Upstream.Name = ""
94+
} else {
95+
// Multiple backends - use traffic-split plugin
96+
service.Upstream = upstreams[0]
97+
// remove the id and name of the service.upstream, adc schema does not need id and name for it
98+
service.Upstream.ID = ""
99+
service.Upstream.Name = ""
100+
101+
upstreams = upstreams[1:]
102+
103+
if len(upstreams) > 0 {
104+
service.Upstreams = upstreams
105+
}
106+
107+
// Set weight in traffic-split for the default upstream
108+
weight := apiv2.DefaultWeight
109+
if rule.BackendRefs[0].Weight != nil {
110+
weight = int(*rule.BackendRefs[0].Weight)
111+
}
112+
weightedUpstreams = append(weightedUpstreams, adctypes.TrafficSplitConfigRuleWeightedUpstream{
113+
Weight: weight,
114+
})
115+
116+
// Set other upstreams in traffic-split using upstream_id
117+
for i, upstream := range upstreams {
118+
weight := apiv2.DefaultWeight
119+
// get weight from the backend refs starting from the second backend
120+
if i+1 < len(rule.BackendRefs) && rule.BackendRefs[i+1].Weight != nil {
121+
weight = int(*rule.BackendRefs[i+1].Weight)
122+
}
123+
weightedUpstreams = append(weightedUpstreams, adctypes.TrafficSplitConfigRuleWeightedUpstream{
124+
UpstreamID: upstream.ID,
125+
Weight: weight,
126+
})
127+
}
128+
129+
if len(weightedUpstreams) > 0 {
130+
if service.Plugins == nil {
131+
service.Plugins = make(map[string]any)
132+
}
133+
service.Plugins["traffic-split"] = &adctypes.TrafficSplitConfig{
134+
Rules: []adctypes.TrafficSplitConfigRule{
135+
{
136+
WeightedUpstreams: weightedUpstreams,
137+
},
138+
},
139+
}
140+
}
141+
}
142+
streamRoute := adctypes.NewDefaultStreamRoute()
143+
streamRouteName := adctypes.ComposeStreamRouteName(udpRoute.Namespace, udpRoute.Name, fmt.Sprintf("%d", ruleIndex), "UDP")
144+
streamRoute.Name = streamRouteName
145+
streamRoute.ID = id.GenID(streamRouteName)
146+
streamRoute.Labels = labels
147+
// TODO: support remote_addr, server_addr, sni, server_port
148+
service.StreamRoutes = append(service.StreamRoutes, streamRoute)
149+
result.Services = append(result.Services, service)
150+
}
151+
return result, nil
152+
}

internal/controller/indexer/indexer.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func SetupIndexer(mgr ctrl.Manager) error {
5757
setupGatewayIndexer,
5858
setupHTTPRouteIndexer,
5959
setupTCPRouteIndexer,
60+
setupUDPRouteIndexer,
6061
setupGRPCRouteIndexer,
6162
setupIngressIndexer,
6263
setupConsumerIndexer,
@@ -252,6 +253,28 @@ func setupTCPRouteIndexer(mgr ctrl.Manager) error {
252253
}
253254
return nil
254255
}
256+
257+
func setupUDPRouteIndexer(mgr ctrl.Manager) error {
258+
if err := mgr.GetFieldIndexer().IndexField(
259+
context.Background(),
260+
&gatewayv1alpha2.UDPRoute{},
261+
ParentRefs,
262+
UDPRouteParentRefsIndexFunc,
263+
); err != nil {
264+
return err
265+
}
266+
267+
if err := mgr.GetFieldIndexer().IndexField(
268+
context.Background(),
269+
&gatewayv1alpha2.UDPRoute{},
270+
ServiceIndexRef,
271+
UDPRouteServiceIndexFunc,
272+
); err != nil {
273+
return err
274+
}
275+
return nil
276+
}
277+
255278
func setupIngressClassIndexer(mgr ctrl.Manager) error {
256279
// create IngressClass index
257280
if err := mgr.GetFieldIndexer().IndexField(
@@ -517,6 +540,19 @@ func TCPRouteParentRefsIndexFunc(rawObj client.Object) []string {
517540
return keys
518541
}
519542

543+
func UDPRouteParentRefsIndexFunc(rawObj client.Object) []string {
544+
ur := rawObj.(*gatewayv1alpha2.UDPRoute)
545+
keys := make([]string, 0, len(ur.Spec.ParentRefs))
546+
for _, ref := range ur.Spec.ParentRefs {
547+
ns := ur.GetNamespace()
548+
if ref.Namespace != nil {
549+
ns = string(*ref.Namespace)
550+
}
551+
keys = append(keys, GenIndexKey(ns, string(ref.Name)))
552+
}
553+
return keys
554+
}
555+
520556
func HTTPRouteServiceIndexFunc(rawObj client.Object) []string {
521557
hr := rawObj.(*gatewayv1.HTTPRoute)
522558
keys := make([]string, 0, len(hr.Spec.Rules))
@@ -553,6 +589,24 @@ func TCPPRouteServiceIndexFunc(rawObj client.Object) []string {
553589
return keys
554590
}
555591

592+
func UDPRouteServiceIndexFunc(rawObj client.Object) []string {
593+
ur := rawObj.(*gatewayv1alpha2.UDPRoute)
594+
keys := make([]string, 0, len(ur.Spec.Rules))
595+
for _, rule := range ur.Spec.Rules {
596+
for _, backend := range rule.BackendRefs {
597+
namespace := ur.GetNamespace()
598+
if backend.Kind != nil && *backend.Kind != internaltypes.KindService {
599+
continue
600+
}
601+
if backend.Namespace != nil {
602+
namespace = string(*backend.Namespace)
603+
}
604+
keys = append(keys, GenIndexKey(namespace, string(backend.Name)))
605+
}
606+
}
607+
return keys
608+
}
609+
556610
func ApisixRouteServiceIndexFunc(cli client.Client) func(client.Object) []string {
557611
return func(obj client.Object) (keys []string) {
558612
ar := obj.(*apiv2.ApisixRoute)

0 commit comments

Comments
 (0)