-
Notifications
You must be signed in to change notification settings - Fork 2
feat: fallback to endpoints when endpointslice disable #216
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
9 commits
Select commit
Hold shift + click to select a range
34b13bc
feat: fallback to endpoints when endpointslice disable
ronething f90f518
fix: r
ronething f376eb3
fix: r
ronething 2f9c3e3
fix: r
ronething 266f84b
fix: r
ronething e38182b
fix: review
ronething b8f14d3
fix: r
ronething d71c521
fix: r
ronething 9ba875c
fix: review
ronething 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,7 @@ import ( | |
| "github.com/apache/apisix-ingress-controller/internal/provider" | ||
| "github.com/apache/apisix-ingress-controller/internal/types" | ||
| "github.com/apache/apisix-ingress-controller/internal/utils" | ||
| pkgutils "github.com/apache/apisix-ingress-controller/pkg/utils" | ||
| ) | ||
|
|
||
| // HTTPRouteReconciler reconciles a GatewayClass object. | ||
|
|
@@ -67,18 +68,39 @@ type HTTPRouteReconciler struct { //nolint:revive | |
|
|
||
| Updater status.Updater | ||
| Readier readiness.ReadinessManager | ||
|
|
||
| // supportsEndpointSlice indicates whether the cluster supports EndpointSlice API | ||
| supportsEndpointSlice bool | ||
| } | ||
|
|
||
| // SetupWithManager sets up the controller with the Manager. | ||
| func (r *HTTPRouteReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
| r.genericEvent = make(chan event.GenericEvent, 100) | ||
|
|
||
| // Check and store EndpointSlice API support | ||
| r.supportsEndpointSlice = pkgutils.HasAPIResource(mgr, &discoveryv1.EndpointSlice{}) | ||
|
|
||
| bdr := ctrl.NewControllerManagedBy(mgr). | ||
| For(&gatewayv1.HTTPRoute{}). | ||
| WithEventFilter(predicate.GenerationChangedPredicate{}). | ||
| Watches(&discoveryv1.EndpointSlice{}, | ||
| WithEventFilter( | ||
| predicate.Or( | ||
| predicate.GenerationChangedPredicate{}, | ||
| predicate.NewPredicateFuncs(TypePredicate[*corev1.Endpoints]()), | ||
| )) | ||
ronething marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Conditionally watch EndpointSlice or Endpoints based on cluster API support | ||
| if r.supportsEndpointSlice { | ||
| bdr = bdr.Watches(&discoveryv1.EndpointSlice{}, | ||
| handler.EnqueueRequestsFromMapFunc(r.listHTTPRoutesByServiceBef), | ||
| ). | ||
| ) | ||
| } else { | ||
| r.Log.Info("EndpointSlice API not available, falling back to Endpoints API for service discovery") | ||
| bdr = bdr.Watches(&corev1.Endpoints{}, | ||
| handler.EnqueueRequestsFromMapFunc(r.listHTTPRoutesByServiceForEndpoints), | ||
| ) | ||
| } | ||
|
|
||
| bdr = bdr. | ||
| Watches(&v1alpha1.PluginConfig{}, | ||
| handler.EnqueueRequestsFromMapFunc(r.listHTTPRoutesByExtensionRef), | ||
| ). | ||
|
|
@@ -288,6 +310,36 @@ func (r *HTTPRouteReconciler) listHTTPRoutesByServiceBef(ctx context.Context, ob | |
| return requests | ||
| } | ||
|
|
||
| // listHTTPRoutesByServiceForEndpoints handles Endpoints objects and converts them to HTTPRoute reconcile requests. | ||
| // This function provides backward compatibility for Kubernetes 1.18 clusters that don't support EndpointSlice. | ||
| func (r *HTTPRouteReconciler) listHTTPRoutesByServiceForEndpoints(ctx context.Context, obj client.Object) []reconcile.Request { | ||
| endpoint, ok := obj.(*corev1.Endpoints) | ||
| if !ok { | ||
| r.Log.Error(fmt.Errorf("unexpected object type"), "failed to convert object to Endpoints") | ||
| return nil | ||
| } | ||
| namespace := endpoint.GetNamespace() | ||
| serviceName := endpoint.GetName() // For Endpoints, the name is the service name | ||
|
|
||
| hrList := &gatewayv1.HTTPRouteList{} | ||
| if err := r.List(ctx, hrList, client.MatchingFields{ | ||
| indexer.ServiceIndexRef: indexer.GenIndexKey(namespace, serviceName), | ||
| }); err != nil { | ||
| r.Log.Error(err, "failed to list httproutes by service", "service", serviceName) | ||
| return nil | ||
| } | ||
| requests := make([]reconcile.Request, 0, len(hrList.Items)) | ||
| for _, hr := range hrList.Items { | ||
| requests = append(requests, reconcile.Request{ | ||
| NamespacedName: client.ObjectKey{ | ||
| Namespace: hr.Namespace, | ||
| Name: hr.Name, | ||
| }, | ||
| }) | ||
| } | ||
| return requests | ||
| } | ||
|
|
||
| func (r *HTTPRouteReconciler) listHTTPRoutesByExtensionRef(ctx context.Context, obj client.Object) []reconcile.Request { | ||
| pluginconfig, ok := obj.(*v1alpha1.PluginConfig) | ||
| if !ok { | ||
|
|
@@ -520,19 +572,37 @@ func (r *HTTPRouteReconciler) processHTTPRouteBackendRefs(tctx *provider.Transla | |
| } | ||
| tctx.Services[targetNN] = &service | ||
|
|
||
| endpointSliceList := new(discoveryv1.EndpointSliceList) | ||
| if err := r.List(tctx, endpointSliceList, | ||
| client.InNamespace(targetNN.Namespace), | ||
| client.MatchingLabels{ | ||
| discoveryv1.LabelServiceName: targetNN.Name, | ||
| }, | ||
| ); err != nil { | ||
| r.Log.Error(err, "failed to list endpoint slices", "Service", targetNN) | ||
| terr = err | ||
| continue | ||
| // Conditionally collect EndpointSlice or Endpoints based on cluster API support | ||
| if r.supportsEndpointSlice { | ||
|
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. Can this logic be abstracted into a function for reuse.
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. updated. |
||
| endpointSliceList := new(discoveryv1.EndpointSliceList) | ||
| if err := r.List(tctx, endpointSliceList, | ||
| client.InNamespace(targetNN.Namespace), | ||
| client.MatchingLabels{ | ||
| discoveryv1.LabelServiceName: targetNN.Name, | ||
| }, | ||
| ); err != nil { | ||
| r.Log.Error(err, "failed to list endpoint slices", "Service", targetNN) | ||
| terr = err | ||
| continue | ||
| } | ||
| tctx.EndpointSlices[targetNN] = endpointSliceList.Items | ||
| } else { | ||
| // Fallback to Endpoints API for Kubernetes 1.18 compatibility | ||
| var endpoints corev1.Endpoints | ||
| if err := r.Get(tctx, targetNN, &endpoints); err != nil { | ||
| if client.IgnoreNotFound(err) != nil { | ||
| r.Log.Error(err, "failed to get endpoints", "Service", targetNN) | ||
| terr = err | ||
| continue | ||
| } | ||
| // If endpoints not found, create empty EndpointSlice list | ||
| tctx.EndpointSlices[targetNN] = []discoveryv1.EndpointSlice{} | ||
| } else { | ||
| // Convert Endpoints to EndpointSlice format for internal consistency | ||
| convertedEndpointSlices := pkgutils.ConvertEndpointsToEndpointSlice(&endpoints) | ||
| tctx.EndpointSlices[targetNN] = convertedEndpointSlices | ||
| } | ||
| } | ||
|
|
||
| tctx.EndpointSlices[targetNN] = endpointSliceList.Items | ||
| } | ||
| return terr | ||
| } | ||
|
|
||
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.
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.
When supportsEndpointSlice is true, it is redundant.
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.
updated.