-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid reconciling SriovNetworkNodePolicies multiple times
- Loading branch information
1 parent
7f1af63
commit f405450
Showing
1 changed file
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -20,9 +20,14 @@ import ( | |
"context" | ||
"encoding/json" | ||
"fmt" | ||
"k8s.io/client-go/util/workqueue" | ||
"os" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
||
errs "github.com/pkg/errors" | ||
appsv1 "k8s.io/api/apps/v1" | ||
|
@@ -49,6 +54,8 @@ import ( | |
render "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/render" | ||
) | ||
|
||
const nodePolicySyncEventName = "node-policy-sync-event" | ||
|
||
// SriovNetworkNodePolicyReconciler reconciles a SriovNetworkNodePolicy object | ||
type SriovNetworkNodePolicyReconciler struct { | ||
client.Client | ||
|
@@ -69,8 +76,12 @@ type SriovNetworkNodePolicyReconciler struct { | |
// For more details, check Reconcile and its Result here: | ||
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile | ||
func (r *SriovNetworkNodePolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
reqLogger := log.FromContext(ctx).WithValues("sriovnetworknodepolicy", req.NamespacedName) | ||
// Only handle node-policy-sync-event | ||
if req.Name != nodePolicySyncEventName || req.Namespace != "" { | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
reqLogger := log.FromContext(ctx) | ||
reqLogger.Info("Reconciling") | ||
|
||
defaultPolicy := &sriovnetworkv1.SriovNetworkNodePolicy{} | ||
|
@@ -155,8 +166,34 @@ func (r *SriovNetworkNodePolicyReconciler) Reconcile(ctx context.Context, req ct | |
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *SriovNetworkNodePolicyReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
qHandler := func(q workqueue.RateLimitingInterface) { | ||
q.AddAfter(reconcile.Request{NamespacedName: types.NamespacedName{ | ||
Namespace: "", | ||
Name: nodePolicySyncEventName, | ||
}}, time.Second) | ||
} | ||
|
||
delayedEventHandler := handler.Funcs{ | ||
CreateFunc: func(e event.CreateEvent, q workqueue.RateLimitingInterface) { | ||
log.Log.WithName("SriovNetworkNodePolicy"). | ||
Info("Enqueuing sync for create event", "resource", e.Object.GetName()) | ||
qHandler(q) | ||
}, | ||
UpdateFunc: func(e event.UpdateEvent, q workqueue.RateLimitingInterface) { | ||
log.Log.WithName("SriovNetworkNodePolicy"). | ||
Info("Enqueuing sync for update event", "resource", e.ObjectNew.GetName()) | ||
qHandler(q) | ||
}, | ||
DeleteFunc: func(e event.DeleteEvent, q workqueue.RateLimitingInterface) { | ||
log.Log.WithName("SriovNetworkNodePolicy"). | ||
Info("Enqueuing sync for delete event", "resource", e.Object.GetName()) | ||
qHandler(q) | ||
}, | ||
} | ||
|
||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&sriovnetworkv1.SriovNetworkNodePolicy{}). | ||
Watches(&source.Kind{Type: &sriovnetworkv1.SriovNetworkNodePolicy{}}, delayedEventHandler). | ||
Complete(r) | ||
} | ||
|
||
|