Skip to content

Commit 52ff32c

Browse files
Revolyssupronething
authored andcommitted
feat(gateway-api): add support for UDPRoute (#2578)
Signed-off-by: Ashing Zheng <[email protected]>
1 parent 6f44812 commit 52ff32c

File tree

15 files changed

+901
-14
lines changed

15 files changed

+901
-14
lines changed

api/adc/types.go

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

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

500503
buf.WriteString(namespace)
501504
buf.WriteByte('_')
502505
buf.WriteString(name)
503506
buf.WriteByte('_')
504507
buf.WriteString(rule)
505-
buf.WriteString("_tcp")
508+
buf.WriteByte('_')
509+
buf.WriteString(typ)
506510

507511
return buf.String()
508512
}
@@ -545,8 +549,8 @@ func ComposeServicesNameWithScheme(namespace, name string, rule string, scheme s
545549
return buf.String()
546550
}
547551

548-
func ComposeServiceNameWithStream(namespace, name string, rule string) string {
549-
return ComposeServicesNameWithScheme(namespace, name, rule, "stream")
552+
func ComposeServiceNameWithStream(namespace, name string, rule, typ string) string {
553+
return ComposeServicesNameWithScheme(namespace, name, rule, typ)
550554
}
551555

552556
func ComposeConsumerName(namespace, name string) string {

config/rbac/role.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ rules:
9494
- httproutes/status
9595
- referencegrants/status
9696
- tcproutes/status
97+
- udproutes/status
9798
verbs:
9899
- get
99100
- update
@@ -103,8 +104,9 @@ rules:
103104
- gateways
104105
- grpcroutes
105106
- httproutes
106-
- tcproutes
107107
- referencegrants
108+
- tcproutes
109+
- udproutes
108110
verbs:
109111
- get
110112
- 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
@@ -446,14 +446,14 @@ func (t *Translator) translateStreamRule(tctx *provider.TranslateContext, ar *ap
446446
t.loadRoutePlugins(tctx, ar, part.Plugins, plugins)
447447

448448
sr := adc.NewDefaultStreamRoute()
449-
sr.Name = adc.ComposeStreamRouteName(ar.Namespace, ar.Name, part.Name)
449+
sr.Name = adc.ComposeStreamRouteName(ar.Namespace, ar.Name, part.Name, part.Protocol)
450450
sr.ID = id.GenID(sr.Name)
451451
sr.ServerPort = part.Match.IngressPort
452452
sr.SNI = part.Match.Host
453453
sr.Plugins = plugins
454454

455455
svc := adc.NewDefaultService()
456-
svc.Name = adc.ComposeServiceNameWithStream(ar.Namespace, ar.Name, part.Name)
456+
svc.Name = adc.ComposeServiceNameWithStream(ar.Namespace, ar.Name, part.Name, part.Protocol)
457457
svc.ID = id.GenID(svc.Name)
458458
svc.StreamRoutes = append(svc.StreamRoutes, sr)
459459

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
@@ -64,6 +64,7 @@ func SetupIndexer(mgr ctrl.Manager) error {
6464
&gatewayv1.HTTPRoute{}: setupHTTPRouteIndexer,
6565
&gatewayv1.GRPCRoute{}: setupGRPCRouteIndexer,
6666
&gatewayv1alpha2.TCPRoute{}: setupTCPRouteIndexer,
67+
&gatewayv1alpha2.UDPRoute{}: setupUDPRouteIndexer,
6768
&gatewayv1.GatewayClass{}: setupGatewayClassIndexer,
6869
&v1alpha1.Consumer{}: setupConsumerIndexer,
6970
&networkingv1.Ingress{}: setupIngressIndexer,
@@ -279,6 +280,28 @@ func setupTCPRouteIndexer(mgr ctrl.Manager) error {
279280
}
280281
return nil
281282
}
283+
284+
func setupUDPRouteIndexer(mgr ctrl.Manager) error {
285+
if err := mgr.GetFieldIndexer().IndexField(
286+
context.Background(),
287+
&gatewayv1alpha2.UDPRoute{},
288+
ParentRefs,
289+
UDPRouteParentRefsIndexFunc,
290+
); err != nil {
291+
return err
292+
}
293+
294+
if err := mgr.GetFieldIndexer().IndexField(
295+
context.Background(),
296+
&gatewayv1alpha2.UDPRoute{},
297+
ServiceIndexRef,
298+
UDPRouteServiceIndexFunc,
299+
); err != nil {
300+
return err
301+
}
302+
return nil
303+
}
304+
282305
func setupIngressClassIndexer(mgr ctrl.Manager) error {
283306
// create IngressClass index
284307
if err := mgr.GetFieldIndexer().IndexField(
@@ -578,6 +601,19 @@ func TCPRouteParentRefsIndexFunc(rawObj client.Object) []string {
578601
return keys
579602
}
580603

604+
func UDPRouteParentRefsIndexFunc(rawObj client.Object) []string {
605+
ur := rawObj.(*gatewayv1alpha2.UDPRoute)
606+
keys := make([]string, 0, len(ur.Spec.ParentRefs))
607+
for _, ref := range ur.Spec.ParentRefs {
608+
ns := ur.GetNamespace()
609+
if ref.Namespace != nil {
610+
ns = string(*ref.Namespace)
611+
}
612+
keys = append(keys, GenIndexKey(ns, string(ref.Name)))
613+
}
614+
return keys
615+
}
616+
581617
func HTTPRouteServiceIndexFunc(rawObj client.Object) []string {
582618
hr := rawObj.(*gatewayv1.HTTPRoute)
583619
keys := make([]string, 0, len(hr.Spec.Rules))
@@ -614,6 +650,24 @@ func TCPPRouteServiceIndexFunc(rawObj client.Object) []string {
614650
return keys
615651
}
616652

653+
func UDPRouteServiceIndexFunc(rawObj client.Object) []string {
654+
ur := rawObj.(*gatewayv1alpha2.UDPRoute)
655+
keys := make([]string, 0, len(ur.Spec.Rules))
656+
for _, rule := range ur.Spec.Rules {
657+
for _, backend := range rule.BackendRefs {
658+
namespace := ur.GetNamespace()
659+
if backend.Kind != nil && *backend.Kind != internaltypes.KindService {
660+
continue
661+
}
662+
if backend.Namespace != nil {
663+
namespace = string(*backend.Namespace)
664+
}
665+
keys = append(keys, GenIndexKey(namespace, string(backend.Name)))
666+
}
667+
}
668+
return keys
669+
}
670+
617671
func ApisixRouteServiceIndexFunc(cli client.Client) func(client.Object) []string {
618672
return func(obj client.Object) (keys []string) {
619673
ar := obj.(*apiv2.ApisixRoute)

0 commit comments

Comments
 (0)