Skip to content

Commit 263c237

Browse files
authored
Merge pull request vmware-tanzu#718 from zhengxiexie/zhengxie/node_filter_new
Add predicate func for pod controller and node controller +1
2 parents 1020f36 + 6ec818d commit 263c237

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

pkg/controllers/node/node_controller.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ func (r *NodeReconciler) Start(mgr ctrl.Manager) error {
9797
return nil
9898
}
9999

100-
// PredicateFuncsNode filters out events where only resourceVersion, lastHeartbeatTime, or lastTransitionTime have changed
101100
var PredicateFuncsNode = predicate.Funcs{
102101
UpdateFunc: func(e event.UpdateEvent) bool {
103102
oldNode, okOld := e.ObjectOld.(*v1.Node)
@@ -106,13 +105,12 @@ var PredicateFuncsNode = predicate.Funcs{
106105
return true
107106
}
108107

109-
// If only the heartbeat time, transition time and resource version has changed, and other properties unchanged, ignore the update
108+
// If only the condition, resource version, allocatable, capacity have changed, and other properties unchanged, ignore the update
110109
if len(newNode.Status.Conditions) > 0 && len(oldNode.Status.Conditions) > 0 {
111-
if newNode.ResourceVersion != oldNode.ResourceVersion &&
112-
newNode.Status.Conditions[0].LastHeartbeatTime != oldNode.Status.Conditions[0].LastHeartbeatTime &&
113-
newNode.Status.Conditions[0].LastTransitionTime != oldNode.Status.Conditions[0].LastTransitionTime {
114-
oldNode.Status.Conditions[0].LastHeartbeatTime = newNode.Status.Conditions[0].LastHeartbeatTime
115-
oldNode.Status.Conditions[0].LastTransitionTime = newNode.Status.Conditions[0].LastTransitionTime
110+
if newNode.ResourceVersion != oldNode.ResourceVersion {
111+
oldNode.Status.Allocatable = newNode.Status.Allocatable
112+
oldNode.Status.Capacity = newNode.Status.Capacity
113+
oldNode.Status.Conditions = newNode.Status.Conditions
116114
return !reflect.DeepEqual(oldNode.Status, newNode.Status)
117115
}
118116
}

pkg/controllers/pod/pod_controller.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
ctrl "sigs.k8s.io/controller-runtime"
1919
"sigs.k8s.io/controller-runtime/pkg/client"
2020
"sigs.k8s.io/controller-runtime/pkg/controller"
21+
"sigs.k8s.io/controller-runtime/pkg/event"
22+
"sigs.k8s.io/controller-runtime/pkg/predicate"
2123

2224
"github.com/vmware-tanzu/nsx-operator/pkg/controllers/common"
2325
"github.com/vmware-tanzu/nsx-operator/pkg/logger"
@@ -64,10 +66,6 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
6466
log.Error(err, "unable to fetch Pod", "Pod", req.NamespacedName)
6567
return common.ResultRequeue, err
6668
}
67-
if pod.Spec.HostNetwork {
68-
log.Info("skipping handling hostnetwork pod", "pod", req.NamespacedName)
69-
return common.ResultNormal, nil
70-
}
7169
if len(pod.Spec.NodeName) == 0 {
7270
log.Info("pod is not scheduled on node yet, skipping", "pod", req.NamespacedName)
7371
return common.ResultNormal, nil
@@ -135,6 +133,7 @@ func (r *PodReconciler) GetNodeByName(nodeName string) (*model.HostTransportNode
135133
func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
136134
return ctrl.NewControllerManagedBy(mgr).
137135
For(&v1.Pod{}).
136+
WithEventFilter(PredicateFuncsPod).
138137
WithOptions(
139138
controller.Options{
140139
MaxConcurrentReconciles: common.NumReconcile(),
@@ -267,3 +266,34 @@ func (r *PodReconciler) deleteSubnetPortByPodName(ctx context.Context, ns string
267266
log.Info("successfully deleted nsxSubnetPort for Pod", "namespace", ns, "name", name)
268267
return nil
269268
}
269+
270+
// PredicateFuncsPod filters out events where pod.Spec.HostNetwork is true
271+
var PredicateFuncsPod = predicate.Funcs{
272+
UpdateFunc: func(e event.UpdateEvent) bool {
273+
oldPod, okOld := e.ObjectOld.(*v1.Pod)
274+
newPod, okNew := e.ObjectNew.(*v1.Pod)
275+
if !okOld || !okNew {
276+
return true
277+
}
278+
279+
if oldPod.Spec.HostNetwork && newPod.Spec.HostNetwork {
280+
return false
281+
}
282+
return true
283+
},
284+
CreateFunc: func(e event.CreateEvent) bool {
285+
pod, ok := e.Object.(*v1.Pod)
286+
if !ok {
287+
return true
288+
}
289+
290+
if pod.Spec.HostNetwork {
291+
return false
292+
}
293+
return true
294+
},
295+
DeleteFunc: func(e event.DeleteEvent) bool { return true },
296+
GenericFunc: func(e event.GenericEvent) bool {
297+
return true
298+
},
299+
}

0 commit comments

Comments
 (0)