-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[feat: gw api] Add finalizers for Rules CRD #4275
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
k8s-ci-robot
merged 1 commit into
kubernetes-sigs:main
from
shraddhabang:gwrulescrdfins
Jul 26, 2025
Merged
Changes from all commits
Commits
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
102 changes: 102 additions & 0 deletions
102
controllers/gateway/listenerrule_configuration_controller.go
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,102 @@ | ||
package gateway | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/go-logr/logr" | ||
"k8s.io/client-go/tools/record" | ||
elbv2gw "sigs.k8s.io/aws-load-balancer-controller/apis/gateway/v1beta1" | ||
"sigs.k8s.io/aws-load-balancer-controller/pkg/config" | ||
"sigs.k8s.io/aws-load-balancer-controller/pkg/gateway/constants" | ||
"sigs.k8s.io/aws-load-balancer-controller/pkg/gateway/routeutils" | ||
"sigs.k8s.io/aws-load-balancer-controller/pkg/k8s" | ||
"sigs.k8s.io/aws-load-balancer-controller/pkg/runtime" | ||
"sigs.k8s.io/aws-load-balancer-controller/pkg/shared_constants" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
) | ||
|
||
// NewListenerRuleConfigurationReconciler constructs a reconciler that responds to listener rule configuration changes | ||
func NewListenerRuleConfigurationReconciler(k8sClient client.Client, eventRecorder record.EventRecorder, controllerConfig config.ControllerConfig, finalizerManager k8s.FinalizerManager, logger logr.Logger) Reconciler { | ||
|
||
return &listenerRuleConfigurationReconciler{ | ||
k8sClient: k8sClient, | ||
eventRecorder: eventRecorder, | ||
logger: logger, | ||
finalizerManager: finalizerManager, | ||
workers: controllerConfig.GatewayClassMaxConcurrentReconciles, | ||
} | ||
} | ||
|
||
// listenerRuleConfigurationReconciler reconciles listener rule configurations | ||
type listenerRuleConfigurationReconciler struct { | ||
k8sClient client.Client | ||
logger logr.Logger | ||
eventRecorder record.EventRecorder | ||
finalizerManager k8s.FinalizerManager | ||
workers int | ||
} | ||
|
||
func (r *listenerRuleConfigurationReconciler) SetupWatches(_ context.Context, ctrl controller.Controller, mgr ctrl.Manager) error { | ||
|
||
if err := ctrl.Watch(source.Kind(mgr.GetCache(), &elbv2gw.ListenerRuleConfiguration{}, &handler.TypedEnqueueRequestForObject[*elbv2gw.ListenerRuleConfiguration]{})); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *listenerRuleConfigurationReconciler) Reconcile(ctx context.Context, req reconcile.Request) (ctrl.Result, error) { | ||
return runtime.HandleReconcileError(r.reconcile(ctx, req), r.logger) | ||
} | ||
|
||
func (r *listenerRuleConfigurationReconciler) reconcile(ctx context.Context, req reconcile.Request) error { | ||
listenerRuleConf := &elbv2gw.ListenerRuleConfiguration{} | ||
if err := r.k8sClient.Get(ctx, req.NamespacedName, listenerRuleConf); err != nil { | ||
return client.IgnoreNotFound(err) | ||
} | ||
|
||
r.logger.V(1).Info("Reconcile request for listener rule configuration", "cfg", listenerRuleConf) | ||
|
||
if listenerRuleConf.DeletionTimestamp == nil || listenerRuleConf.DeletionTimestamp.IsZero() { | ||
return r.handleUpdate(listenerRuleConf) | ||
} | ||
|
||
return r.handleDelete(listenerRuleConf) | ||
} | ||
|
||
func (r *listenerRuleConfigurationReconciler) handleUpdate(listenerRuleConf *elbv2gw.ListenerRuleConfiguration) error { | ||
if k8s.HasFinalizer(listenerRuleConf, shared_constants.ListenerRuleConfigurationFinalizer) { | ||
return nil | ||
} | ||
return r.finalizerManager.AddFinalizers(context.Background(), listenerRuleConf, shared_constants.ListenerRuleConfigurationFinalizer) | ||
} | ||
|
||
func (r *listenerRuleConfigurationReconciler) handleDelete(listenerRuleConf *elbv2gw.ListenerRuleConfiguration) error { | ||
if !k8s.HasFinalizer(listenerRuleConf, shared_constants.ListenerRuleConfigurationFinalizer) { | ||
return nil | ||
} | ||
|
||
inUse, err := routeutils.IsListenerRuleConfigInUse(context.Background(), listenerRuleConf, r.k8sClient) | ||
|
||
if err != nil { | ||
return fmt.Errorf("skipping finalizer removal due failure to verify if listener rule configuration [%+v] is in use. Error : %w ", k8s.NamespacedName(listenerRuleConf), err) | ||
} | ||
// if the listener rule configuration is still in use, we should not delete it | ||
if inUse { | ||
return fmt.Errorf("failed to remove finalizers as listener rule configuration [%+v] is still in use", k8s.NamespacedName(listenerRuleConf)) | ||
} | ||
return r.finalizerManager.RemoveFinalizers(context.Background(), listenerRuleConf, shared_constants.ListenerRuleConfigurationFinalizer) | ||
} | ||
|
||
func (r *listenerRuleConfigurationReconciler) SetupWithManager(_ context.Context, mgr ctrl.Manager) (controller.Controller, error) { | ||
return controller.New(constants.ListenerRuleConfigurationController, mgr, controller.Options{ | ||
MaxConcurrentReconciles: r.workers, | ||
Reconciler: r, | ||
}) | ||
|
||
} |
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,45 @@ | ||
package routeutils | ||
|
||
import ( | ||
"context" | ||
"k8s.io/apimachinery/pkg/types" | ||
elbv2gw "sigs.k8s.io/aws-load-balancer-controller/apis/gateway/v1beta1" | ||
"sigs.k8s.io/aws-load-balancer-controller/pkg/k8s" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func IsListenerRuleConfigInUse(ctx context.Context, listenerRuleConfig *elbv2gw.ListenerRuleConfiguration, k8sClient client.Client) (bool, error) { | ||
l7Routes, err := ListL7Routes(ctx, k8sClient) | ||
if err != nil { | ||
return false, err | ||
} | ||
filteredRoutesByListenerRuleCfg := FilterRoutesByListenerRuleCfg(l7Routes, listenerRuleConfig) | ||
|
||
return len(filteredRoutesByListenerRuleCfg) > 0, nil | ||
} | ||
|
||
// FilterRoutesByListenerRuleCfg filters a slice of routes based on ListenerRuleConfiguration reference. | ||
// Returns a new slice containing only routes that reference the specified ListenerRuleConfiguration. | ||
func FilterRoutesByListenerRuleCfg(routes []preLoadRouteDescriptor, ruleConfig *elbv2gw.ListenerRuleConfiguration) []preLoadRouteDescriptor { | ||
if ruleConfig == nil || len(routes) == 0 { | ||
return []preLoadRouteDescriptor{} | ||
} | ||
filteredRoutes := make([]preLoadRouteDescriptor, 0, len(routes)) | ||
for _, route := range routes { | ||
if isListenerRuleConfigReferredByRoute(route, k8s.NamespacedName(ruleConfig)) { | ||
filteredRoutes = append(filteredRoutes, route) | ||
} | ||
} | ||
return filteredRoutes | ||
} | ||
|
||
// isListenerRuleConfigReferredByRoute checks if a route references a specific ruleConfig. | ||
func isListenerRuleConfigReferredByRoute(route preLoadRouteDescriptor, ruleConfig types.NamespacedName) bool { | ||
for _, config := range route.GetListenerRuleConfigs() { | ||
namespace := route.GetRouteNamespacedName().Namespace | ||
if string(config.Name) == ruleConfig.Name && namespace == ruleConfig.Namespace { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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.