-
Notifications
You must be signed in to change notification settings - Fork 368
feat(gateway-api): support TLSRoute #2594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c95d568
feat(gateway-api): support TLSoute
AlinsRan 13df298
lint
AlinsRan f18aa54
Update test/conformance/conformance_test.go
AlinsRan d9ad143
Update docs/en/latest/concepts/gateway-api.md
AlinsRan f5a1937
typo
AlinsRan 1f06729
remove blank lines
AlinsRan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| // 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) TranslateTLSRoute(tctx *provider.TranslateContext, tlsRoute *gatewayv1alpha2.TLSRoute) (*TranslateResult, error) { | ||
| result := &TranslateResult{} | ||
| rules := tlsRoute.Spec.Rules | ||
| labels := label.GenLabel(tlsRoute) | ||
| hosts := make([]string, 0, len(tlsRoute.Spec.Hostnames)) | ||
| for _, hostname := range tlsRoute.Spec.Hostnames { | ||
| hosts = append(hosts, string(hostname)) | ||
| } | ||
| for ruleIndex, rule := range rules { | ||
| service := adctypes.NewDefaultService() | ||
| service.Labels = labels | ||
| service.Name = adctypes.ComposeServiceNameWithStream(tlsRoute.Namespace, tlsRoute.Name, fmt.Sprintf("%d", ruleIndex), "TLS") | ||
| 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(tlsRoute.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, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for _, host := range hosts { | ||
| streamRoute := adctypes.NewDefaultStreamRoute() | ||
| streamRouteName := adctypes.ComposeStreamRouteName(tlsRoute.Namespace, tlsRoute.Name, fmt.Sprintf("%d", ruleIndex), "TLS") | ||
| streamRoute.Name = streamRouteName | ||
| streamRoute.ID = id.GenID(streamRouteName) | ||
| streamRoute.SNI = host | ||
| streamRoute.Labels = labels | ||
| service.StreamRoutes = append(service.StreamRoutes, streamRoute) | ||
|
|
||
| } | ||
| result.Services = append(result.Services, service) | ||
| } | ||
| return result, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // 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 indexer | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| internaltypes "github.com/apache/apisix-ingress-controller/internal/types" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" | ||
| ) | ||
|
|
||
| func setupTLSRouteIndexer(mgr ctrl.Manager) error { | ||
| if err := mgr.GetFieldIndexer().IndexField( | ||
| context.Background(), | ||
| &gatewayv1alpha2.TLSRoute{}, | ||
| ParentRefs, | ||
| TLSRouteParentRefsIndexFunc, | ||
| ); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := mgr.GetFieldIndexer().IndexField( | ||
| context.Background(), | ||
| &gatewayv1alpha2.TLSRoute{}, | ||
| ServiceIndexRef, | ||
| TLSPRouteServiceIndexFunc, | ||
| ); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func TLSRouteParentRefsIndexFunc(rawObj client.Object) []string { | ||
| tr := rawObj.(*gatewayv1alpha2.TLSRoute) | ||
| keys := make([]string, 0, len(tr.Spec.ParentRefs)) | ||
| for _, ref := range tr.Spec.ParentRefs { | ||
| ns := tr.GetNamespace() | ||
| if ref.Namespace != nil { | ||
| ns = string(*ref.Namespace) | ||
| } | ||
| keys = append(keys, GenIndexKey(ns, string(ref.Name))) | ||
| } | ||
| return keys | ||
| } | ||
|
|
||
| func TLSPRouteServiceIndexFunc(rawObj client.Object) []string { | ||
| tr := rawObj.(*gatewayv1alpha2.TLSRoute) | ||
| keys := make([]string, 0, len(tr.Spec.Rules)) | ||
| for _, rule := range tr.Spec.Rules { | ||
| for _, backend := range rule.BackendRefs { | ||
| namespace := tr.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 | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.