-
Notifications
You must be signed in to change notification settings - Fork 366
fix: support filter endpoint when translate backend ref. #2451
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -283,7 +283,7 @@ func (t *Translator) fillHTTPRoutePolicies(routes []*adctypes.Route, policies [] | |
| } | ||
| } | ||
|
|
||
| func (t *Translator) translateEndpointSlice(portName *string, weight int, endpointSlices []discoveryv1.EndpointSlice) adctypes.UpstreamNodes { | ||
| func (t *Translator) translateEndpointSlice(portName *string, weight int, endpointSlices []discoveryv1.EndpointSlice, endpointFilter func(*discoveryv1.Endpoint) bool) adctypes.UpstreamNodes { | ||
| var nodes adctypes.UpstreamNodes | ||
| if len(endpointSlices) == 0 { | ||
| return nodes | ||
|
|
@@ -294,6 +294,9 @@ func (t *Translator) translateEndpointSlice(portName *string, weight int, endpoi | |
| continue | ||
| } | ||
| for _, endpoint := range endpointSlice.Endpoints { | ||
| if endpointFilter != nil && !endpointFilter(&endpoint) { | ||
| continue | ||
| } | ||
| for _, addr := range endpoint.Addresses { | ||
| node := adctypes.UpstreamNode{ | ||
| Host: addr, | ||
|
|
@@ -312,11 +315,19 @@ func (t *Translator) translateEndpointSlice(portName *string, weight int, endpoi | |
| return nodes | ||
| } | ||
|
|
||
| func (t *Translator) TranslateBackendRef(tctx *provider.TranslateContext, ref gatewayv1.BackendRef) (adctypes.UpstreamNodes, error) { | ||
| return t.translateBackendRef(tctx, ref) | ||
| func DefaultEndpointFilter(endpoint *discoveryv1.Endpoint) bool { | ||
| if endpoint.Conditions.Ready != nil && !*endpoint.Conditions.Ready { | ||
| log.Debugw("skip not ready endpoint", zap.Any("endpoint", endpoint)) | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func (t *Translator) TranslateBackendRefWithFilter(tctx *provider.TranslateContext, ref gatewayv1.BackendRef, endpointFilter func(*discoveryv1.Endpoint) bool) (adctypes.UpstreamNodes, error) { | ||
| return t.translateBackendRef(tctx, ref, endpointFilter) | ||
| } | ||
|
|
||
| func (t *Translator) translateBackendRef(tctx *provider.TranslateContext, ref gatewayv1.BackendRef) (adctypes.UpstreamNodes, error) { | ||
| func (t *Translator) translateBackendRef(tctx *provider.TranslateContext, ref gatewayv1.BackendRef, endpointFilter func(*discoveryv1.Endpoint) bool) (adctypes.UpstreamNodes, error) { | ||
|
Comment on lines
+326
to
+330
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two functions are exactly the same. Can they be combined into one function?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, only the code that retrieves apisix standalone nodes calls the We can consider making a unified adjustment in the next PR. |
||
| if ref.Kind != nil && *ref.Kind != "Service" { | ||
| return adctypes.UpstreamNodes{}, fmt.Errorf("kind %s is not supported", *ref.Kind) | ||
| } | ||
|
|
@@ -363,7 +374,7 @@ func (t *Translator) translateBackendRef(tctx *provider.TranslateContext, ref ga | |
| } | ||
|
|
||
| endpointSlices := tctx.EndpointSlices[key] | ||
| return t.translateEndpointSlice(portName, weight, endpointSlices), nil | ||
| return t.translateEndpointSlice(portName, weight, endpointSlices, endpointFilter), nil | ||
| } | ||
|
|
||
| // calculateHTTPRoutePriority calculates the priority of the HTTP route. | ||
|
|
@@ -475,7 +486,7 @@ func (t *Translator) TranslateHTTPRoute(tctx *provider.TranslateContext, httpRou | |
| namespace := gatewayv1.Namespace(httpRoute.Namespace) | ||
| backend.Namespace = &namespace | ||
| } | ||
| upNodes, err := t.translateBackendRef(tctx, backend.BackendRef) | ||
| upNodes, err := t.translateBackendRef(tctx, backend.BackendRef, DefaultEndpointFilter) | ||
| if err != nil { | ||
| backendErr = err | ||
| continue | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method seems to cover the functionality of
translateEndpointSliceForIngress. It seems thattranslateEndpointSliceForIngresscan be removed or calledtranslateEndpointSlicein it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is still a need for more clarification. It may not be appropriate to make adjustments in this PR, as it involves more code changes.