-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid reconciling policies multiple times
`SriovNetworkNodePolicy` reconcile function lists all node policies and updates all SriovNetworkNodeState. Hence there is no need to resync all of them periodically. Avoid triggering multiple reconciliations when creating multiple policies in a row (that is what happens when using yaml files). Signed-off-by: Andrea Panattoni <[email protected]>
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
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 |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
"os" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
||
errs "github.com/pkg/errors" | ||
appsv1 "k8s.io/api/apps/v1" | ||
|
@@ -35,11 +36,15 @@ import ( | |
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
kscheme "k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/util/workqueue" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
|
||
dptypes "github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/types" | ||
|
||
|
@@ -49,6 +54,8 @@ import ( | |
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/render" | ||
) | ||
|
||
const nodePolicySyncEventName = "node-policy-sync-event" | ||
|
||
// SriovNetworkNodePolicyReconciler reconciles a SriovNetworkNodePolicy object | ||
type SriovNetworkNodePolicyReconciler struct { | ||
client.Client | ||
|
@@ -70,6 +77,10 @@ type SriovNetworkNodePolicyReconciler struct { | |
// - 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) | ||
if req.Name != nodePolicySyncEventName { | ||
reqLogger.Info("Skip reconciling") | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
reqLogger.Info("Reconciling") | ||
|
||
|
@@ -152,9 +163,28 @@ 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: namespace, | ||
Name: nodePolicySyncEventName, | ||
}}, time.Second) | ||
} | ||
|
||
squashAndDebounceHandler := handler.Funcs{ | ||
CreateFunc: func(e event.CreateEvent, q workqueue.RateLimitingInterface) { | ||
qHandler(q) | ||
}, | ||
UpdateFunc: func(e event.UpdateEvent, q workqueue.RateLimitingInterface) { | ||
qHandler(q) | ||
}, | ||
} | ||
|
||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&sriovnetworkv1.SriovNetworkNodePolicy{}). | ||
Watches(&source.Kind{Type: &sriovnetworkv1.SriovNetworkNodePolicy{}}, squashAndDebounceHandler). | ||
Complete(r) | ||
|
||
} | ||
|
||
func (r *SriovNetworkNodePolicyReconciler) syncDevicePluginConfigMap(ctx context.Context, pl *sriovnetworkv1.SriovNetworkNodePolicyList, nl *corev1.NodeList) error { | ||
|