Skip to content

Commit d829c76

Browse files
authored
Merge pull request #419 from e0ne/nic-cluster-policy-node-reconcile
Watch for node label change event in NicClusterPolicy reconcile loop
2 parents 0088a8c + 29efa09 commit d829c76

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

controllers/nicclusterpolicy_controller.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ import (
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
"k8s.io/apimachinery/pkg/runtime"
3030
"k8s.io/apimachinery/pkg/types"
31+
"k8s.io/client-go/util/workqueue"
3132
ctrl "sigs.k8s.io/controller-runtime"
33+
"sigs.k8s.io/controller-runtime/pkg/builder"
3234
"sigs.k8s.io/controller-runtime/pkg/client"
35+
"sigs.k8s.io/controller-runtime/pkg/event"
3336
"sigs.k8s.io/controller-runtime/pkg/handler"
3437
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3538
"sigs.k8s.io/controller-runtime/pkg/source"
@@ -249,20 +252,33 @@ func (r *NicClusterPolicyReconciler) SetupWithManager(mgr ctrl.Manager) error {
249252
}
250253
r.stateManager = stateManager
251254

252-
builder := ctrl.NewControllerManagedBy(mgr).
255+
ctl := ctrl.NewControllerManagedBy(mgr).
253256
For(&mellanoxv1alpha1.NicClusterPolicy{}).
254257
// Watch for changes to primary resource NicClusterPolicy
255258
Watches(&source.Kind{Type: &mellanoxv1alpha1.NicClusterPolicy{}}, &handler.EnqueueRequestForObject{})
256259

257-
// Watch for changes to secondary resource DaemonSet and requeue the owner NicClusterPolicy
260+
// we always add object with a same(static) key to the queue to reduce
261+
// reconciliation count
262+
updateEnqueue := handler.Funcs{
263+
UpdateFunc: func(e event.UpdateEvent, q workqueue.RateLimitingInterface) {
264+
q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
265+
Name: consts.NicClusterPolicyResourceName,
266+
}})
267+
},
268+
}
269+
270+
// Watch for "feature.node.kubernetes.io/pci-15b3.present" label applying
271+
nodePredicates := builder.WithPredicates(MlnxLabelChangedPredicate{})
272+
ctl = ctl.Watches(&source.Kind{Type: &corev1.Node{}}, updateEnqueue, nodePredicates)
273+
258274
ws := stateManager.GetWatchSources()
259275
r.Log.V(consts.LogLevelInfo).Info("Watch Sources", "Kind:", ws)
260276
for i := range ws {
261-
builder = builder.Watches(ws[i], &handler.EnqueueRequestForOwner{
277+
ctl = ctl.Watches(ws[i], &handler.EnqueueRequestForOwner{
262278
IsController: true,
263279
OwnerType: &mellanoxv1alpha1.NicClusterPolicy{},
264280
})
265281
}
266282

267-
return builder.Complete(r)
283+
return ctl.Complete(r)
268284
}

controllers/predicate.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2022 NVIDIA CORPORATION & AFFILIATES
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controllers
18+
19+
import (
20+
"sigs.k8s.io/controller-runtime/pkg/event"
21+
"sigs.k8s.io/controller-runtime/pkg/predicate"
22+
23+
"github.com/Mellanox/network-operator/pkg/nodeinfo"
24+
)
25+
26+
type MlnxLabelChangedPredicate struct {
27+
predicate.Funcs
28+
}
29+
30+
func (p MlnxLabelChangedPredicate) hasMlnxLabel(labels map[string]string) bool {
31+
// We don't need to check label value because NFD doesn't set it to "false"
32+
_, exist := labels[nodeinfo.NodeLabelMlnxNIC]
33+
return exist
34+
}
35+
36+
func (p MlnxLabelChangedPredicate) Update(e event.UpdateEvent) bool {
37+
return p.hasMlnxLabel(e.ObjectOld.GetLabels()) != p.hasMlnxLabel(e.ObjectNew.GetLabels())
38+
}

controllers/upgrade_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func (r *UpgradeReconciler) SetupWithManager(mgr ctrl.Manager) error {
293293
return ok
294294
}))
295295

296-
// ignore all update events for nodes except when annotations where changed
296+
// Watch for spec and annotation changes
297297
nodePredicates := builder.WithPredicates(predicate.AnnotationChangedPredicate{})
298298

299299
return ctrl.NewControllerManagedBy(mgr).

0 commit comments

Comments
 (0)