Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions api/adc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,18 +491,22 @@ func ComposeRouteName(namespace, name string, rule string) string {

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

buf.WriteString(namespace)
buf.WriteByte('_')
buf.WriteString(name)
buf.WriteByte('_')
buf.WriteString(rule)
buf.WriteString("_tcp")
buf.WriteByte('_')
buf.WriteString(typ)

return buf.String()
}
Expand Down Expand Up @@ -545,8 +549,8 @@ func ComposeServicesNameWithScheme(namespace, name string, rule string, scheme s
return buf.String()
}

func ComposeServiceNameWithStream(namespace, name string, rule string) string {
return ComposeServicesNameWithScheme(namespace, name, rule, "stream")
func ComposeServiceNameWithStream(namespace, name string, rule, typ string) string {
return ComposeServicesNameWithScheme(namespace, name, rule, typ)
}

func ComposeConsumerName(namespace, name string) string {
Expand Down
4 changes: 3 additions & 1 deletion config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ rules:
- httproutes/status
- referencegrants/status
- tcproutes/status
- udproutes/status
verbs:
- get
- update
Expand All @@ -103,8 +104,9 @@ rules:
- gateways
- grpcroutes
- httproutes
- tcproutes
- referencegrants
- tcproutes
- udproutes
verbs:
- get
- list
Expand Down
2 changes: 1 addition & 1 deletion docs/en/latest/concepts/gateway-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ By supporting Gateway API, the APISIX Ingress controller can realize richer func
| ReferenceGrant | Supported | Not supported | Not supported | v1beta1 |
| TLSRoute | Not supported | Not supported | Not supported | v1alpha2 |
| TCPRoute | Supported | Supported | Not supported | v1alpha2 |
| UDPRoute | Not supported | Not supported | Not supported | v1alpha2 |
| UDPRoute | Supported | Supported | Not supported | v1alpha2 |
| BackendTLSPolicy | Not supported | Not supported | Not supported | v1alpha3 |

## Examples
Expand Down
4 changes: 2 additions & 2 deletions internal/adc/translator/apisixroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,14 @@ func (t *Translator) translateStreamRule(tctx *provider.TranslateContext, ar *ap
t.loadRoutePlugins(tctx, ar, part.Plugins, plugins)

sr := adc.NewDefaultStreamRoute()
sr.Name = adc.ComposeStreamRouteName(ar.Namespace, ar.Name, part.Name)
sr.Name = adc.ComposeStreamRouteName(ar.Namespace, ar.Name, part.Name, part.Protocol)
sr.ID = id.GenID(sr.Name)
sr.ServerPort = part.Match.IngressPort
sr.SNI = part.Match.Host
sr.Plugins = plugins

svc := adc.NewDefaultService()
svc.Name = adc.ComposeServiceNameWithStream(ar.Namespace, ar.Name, part.Name)
svc.Name = adc.ComposeServiceNameWithStream(ar.Namespace, ar.Name, part.Name, part.Protocol)
svc.ID = id.GenID(svc.Name)
svc.StreamRoutes = append(svc.StreamRoutes, sr)

Expand Down
4 changes: 2 additions & 2 deletions internal/adc/translator/tcproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (t *Translator) TranslateTCPRoute(tctx *provider.TranslateContext, tcpRoute
for ruleIndex, rule := range rules {
service := adctypes.NewDefaultService()
service.Labels = labels
service.Name = adctypes.ComposeServiceNameWithStream(tcpRoute.Namespace, tcpRoute.Name, fmt.Sprintf("%d", ruleIndex))
service.Name = adctypes.ComposeServiceNameWithStream(tcpRoute.Namespace, tcpRoute.Name, fmt.Sprintf("%d", ruleIndex), "TCP")
service.ID = id.GenID(service.Name)
var (
upstreams = make([]*adctypes.Upstream, 0)
Expand Down Expand Up @@ -151,7 +151,7 @@ func (t *Translator) TranslateTCPRoute(tctx *provider.TranslateContext, tcpRoute
}
}
streamRoute := adctypes.NewDefaultStreamRoute()
streamRouteName := adctypes.ComposeStreamRouteName(tcpRoute.Namespace, tcpRoute.Name, fmt.Sprintf("%d", ruleIndex))
streamRouteName := adctypes.ComposeStreamRouteName(tcpRoute.Namespace, tcpRoute.Name, fmt.Sprintf("%d", ruleIndex), "TCP")
streamRoute.Name = streamRouteName
streamRoute.ID = id.GenID(streamRouteName)
streamRoute.Labels = labels
Expand Down
152 changes: 152 additions & 0 deletions internal/adc/translator/udproute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package translator

import (
"fmt"

gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

adctypes "github.com/apache/apisix-ingress-controller/api/adc"
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
"github.com/apache/apisix-ingress-controller/internal/controller/label"
"github.com/apache/apisix-ingress-controller/internal/id"
"github.com/apache/apisix-ingress-controller/internal/provider"
"github.com/apache/apisix-ingress-controller/internal/types"
)

func (t *Translator) TranslateUDPRoute(tctx *provider.TranslateContext, udpRoute *gatewayv1alpha2.UDPRoute) (*TranslateResult, error) {
result := &TranslateResult{}
rules := udpRoute.Spec.Rules
labels := label.GenLabel(udpRoute)
for ruleIndex, rule := range rules {
service := adctypes.NewDefaultService()
service.Labels = labels
service.Name = adctypes.ComposeServiceNameWithStream(udpRoute.Namespace, udpRoute.Name, fmt.Sprintf("%d", ruleIndex), "UDP")
service.ID = id.GenID(service.Name)
var (
upstreams = make([]*adctypes.Upstream, 0)
weightedUpstreams = make([]adctypes.TrafficSplitConfigRuleWeightedUpstream, 0)
)
for _, backend := range rule.BackendRefs {
if backend.Namespace == nil {
namespace := gatewayv1.Namespace(udpRoute.Namespace)
backend.Namespace = &namespace
}
upstream := newDefaultUpstreamWithoutScheme()
upNodes, err := t.translateBackendRef(tctx, backend, DefaultEndpointFilter)
if err != nil {
continue
}
if len(upNodes) == 0 {
continue
}
// TODO: Confirm BackendTrafficPolicy attachment with e2e test case.
t.AttachBackendTrafficPolicyToUpstream(backend, tctx.BackendTrafficPolicies, upstream)
upstream.Nodes = upNodes
var (
kind string
port int32
)
if backend.Kind == nil {
kind = types.KindService
} else {
kind = string(*backend.Kind)
}
if backend.Port != nil {
port = int32(*backend.Port)
}
namespace := string(*backend.Namespace)
name := string(backend.Name)
upstreamName := adctypes.ComposeUpstreamNameForBackendRef(kind, namespace, name, port)
upstream.Name = upstreamName
upstream.ID = id.GenID(upstreamName)
upstreams = append(upstreams, upstream)
}

// Handle multiple backends with traffic-split plugin
if len(upstreams) == 0 {
// Create a default upstream if no valid backends
upstream := adctypes.NewDefaultUpstream()
service.Upstream = upstream
} else if len(upstreams) == 1 {
// Single backend - use directly as service upstream
service.Upstream = upstreams[0]
// remove the id and name of the service.upstream, adc schema does not need id and name for it
service.Upstream.ID = ""
service.Upstream.Name = ""
} else {
// Multiple backends - use traffic-split plugin
service.Upstream = upstreams[0]
// remove the id and name of the service.upstream, adc schema does not need id and name for it
service.Upstream.ID = ""
service.Upstream.Name = ""

upstreams = upstreams[1:]

if len(upstreams) > 0 {
service.Upstreams = upstreams
}

// Set weight in traffic-split for the default upstream
weight := apiv2.DefaultWeight
if rule.BackendRefs[0].Weight != nil {
weight = int(*rule.BackendRefs[0].Weight)
}
weightedUpstreams = append(weightedUpstreams, adctypes.TrafficSplitConfigRuleWeightedUpstream{
Weight: weight,
})

// Set other upstreams in traffic-split using upstream_id
for i, upstream := range upstreams {
weight := apiv2.DefaultWeight
// get weight from the backend refs starting from the second backend
if i+1 < len(rule.BackendRefs) && rule.BackendRefs[i+1].Weight != nil {
weight = int(*rule.BackendRefs[i+1].Weight)
}
weightedUpstreams = append(weightedUpstreams, adctypes.TrafficSplitConfigRuleWeightedUpstream{
UpstreamID: upstream.ID,
Weight: weight,
})
}

if len(weightedUpstreams) > 0 {
if service.Plugins == nil {
service.Plugins = make(map[string]any)
}
service.Plugins["traffic-split"] = &adctypes.TrafficSplitConfig{
Rules: []adctypes.TrafficSplitConfigRule{
{
WeightedUpstreams: weightedUpstreams,
},
},
}
}
}
streamRoute := adctypes.NewDefaultStreamRoute()
streamRouteName := adctypes.ComposeStreamRouteName(udpRoute.Namespace, udpRoute.Name, fmt.Sprintf("%d", ruleIndex), "UDP")
streamRoute.Name = streamRouteName
streamRoute.ID = id.GenID(streamRouteName)
streamRoute.Labels = labels
// TODO: support remote_addr, server_addr, sni, server_port
service.StreamRoutes = append(service.StreamRoutes, streamRoute)
result.Services = append(result.Services, service)
}
return result, nil
}
54 changes: 54 additions & 0 deletions internal/controller/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func SetupIndexer(mgr ctrl.Manager) error {
&gatewayv1.HTTPRoute{}: setupHTTPRouteIndexer,
&gatewayv1.GRPCRoute{}: setupGRPCRouteIndexer,
&gatewayv1alpha2.TCPRoute{}: setupTCPRouteIndexer,
&gatewayv1alpha2.UDPRoute{}: setupUDPRouteIndexer,
&gatewayv1.GatewayClass{}: setupGatewayClassIndexer,
&v1alpha1.Consumer{}: setupConsumerIndexer,
&networkingv1.Ingress{}: setupIngressIndexer,
Expand Down Expand Up @@ -279,6 +280,28 @@ func setupTCPRouteIndexer(mgr ctrl.Manager) error {
}
return nil
}

func setupUDPRouteIndexer(mgr ctrl.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(
context.Background(),
&gatewayv1alpha2.UDPRoute{},
ParentRefs,
UDPRouteParentRefsIndexFunc,
); err != nil {
return err
}

if err := mgr.GetFieldIndexer().IndexField(
context.Background(),
&gatewayv1alpha2.UDPRoute{},
ServiceIndexRef,
UDPRouteServiceIndexFunc,
); err != nil {
return err
}
return nil
}

func setupIngressClassIndexer(mgr ctrl.Manager) error {
// create IngressClass index
if err := mgr.GetFieldIndexer().IndexField(
Expand Down Expand Up @@ -578,6 +601,19 @@ func TCPRouteParentRefsIndexFunc(rawObj client.Object) []string {
return keys
}

func UDPRouteParentRefsIndexFunc(rawObj client.Object) []string {
ur := rawObj.(*gatewayv1alpha2.UDPRoute)
keys := make([]string, 0, len(ur.Spec.ParentRefs))
for _, ref := range ur.Spec.ParentRefs {
ns := ur.GetNamespace()
if ref.Namespace != nil {
ns = string(*ref.Namespace)
}
keys = append(keys, GenIndexKey(ns, string(ref.Name)))
}
return keys
}

func HTTPRouteServiceIndexFunc(rawObj client.Object) []string {
hr := rawObj.(*gatewayv1.HTTPRoute)
keys := make([]string, 0, len(hr.Spec.Rules))
Expand Down Expand Up @@ -614,6 +650,24 @@ func TCPPRouteServiceIndexFunc(rawObj client.Object) []string {
return keys
}

func UDPRouteServiceIndexFunc(rawObj client.Object) []string {
ur := rawObj.(*gatewayv1alpha2.UDPRoute)
keys := make([]string, 0, len(ur.Spec.Rules))
for _, rule := range ur.Spec.Rules {
for _, backend := range rule.BackendRefs {
namespace := ur.GetNamespace()
if backend.Kind != nil && *backend.Kind != internaltypes.KindService {
continue
}
if backend.Namespace != nil {
namespace = string(*backend.Namespace)
}
keys = append(keys, GenIndexKey(namespace, string(backend.Name)))
}
}
return keys
}

func ApisixRouteServiceIndexFunc(cli client.Client) func(client.Object) []string {
return func(obj client.Object) (keys []string) {
ar := obj.(*apiv2.ApisixRoute)
Expand Down
Loading
Loading