Skip to content

Commit

Permalink
Add predicate func for pod controller and node controller
Browse files Browse the repository at this point in the history
Update node predicate func, filter all the conditions compare.
Add pod predicate func, remove host network check in the beginning of reconcile.

Signed-off-by: Xie Zheng <[email protected]>
  • Loading branch information
zhengxiexie committed Oct 12, 2024
1 parent 7223696 commit f7caed5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
12 changes: 5 additions & 7 deletions pkg/controllers/node/node_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func (r *NodeReconciler) Start(mgr ctrl.Manager) error {
return nil
}

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

// If only the heartbeat time, transition time and resource version has changed, and other properties unchanged, ignore the update
// If only the condition, resource version, allocatable, capacity have changed, and other properties unchanged, ignore the update
if len(newNode.Status.Conditions) > 0 && len(oldNode.Status.Conditions) > 0 {
if newNode.ResourceVersion != oldNode.ResourceVersion &&
newNode.Status.Conditions[0].LastHeartbeatTime != oldNode.Status.Conditions[0].LastHeartbeatTime &&
newNode.Status.Conditions[0].LastTransitionTime != oldNode.Status.Conditions[0].LastTransitionTime {
oldNode.Status.Conditions[0].LastHeartbeatTime = newNode.Status.Conditions[0].LastHeartbeatTime
oldNode.Status.Conditions[0].LastTransitionTime = newNode.Status.Conditions[0].LastTransitionTime
if newNode.ResourceVersion != oldNode.ResourceVersion {
oldNode.Status.Allocatable = newNode.Status.Allocatable
oldNode.Status.Capacity = newNode.Status.Capacity
oldNode.Status.Conditions = newNode.Status.Conditions
return !reflect.DeepEqual(oldNode.Status, newNode.Status)
}
}
Expand Down
44 changes: 32 additions & 12 deletions pkg/controllers/pod/pod_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
log.Error(err, "unable to fetch pod", "req", req.NamespacedName)
return common.ResultNormal, client.IgnoreNotFound(err)
}
if pod.Spec.HostNetwork {
log.Info("skipping handling hostnetwork pod", "pod", req.NamespacedName)
return common.ResultNormal, nil
}
if len(pod.Spec.NodeName) == 0 {
log.Info("pod is not scheduled on node yet, skipping", "pod", req.NamespacedName)
return common.ResultNormal, nil
Expand Down Expand Up @@ -147,14 +143,7 @@ func (r *PodReconciler) GetNodeByName(nodeName string) (*model.HostTransportNode
func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1.Pod{}).
WithEventFilter(
predicate.Funcs{
DeleteFunc: func(e event.DeleteEvent) bool {
// Suppress Delete events to avoid filtering them out in the Reconcile function
return false
},
},
).
WithEventFilter(PredicateFuncsPod).
WithOptions(
controller.Options{
MaxConcurrentReconciles: common.NumReconcile(),
Expand Down Expand Up @@ -264,3 +253,34 @@ func (r *PodReconciler) GetSubnetPathForPod(ctx context.Context, pod *v1.Pod) (s
func podIsDeleted(pod *v1.Pod) bool {
return !pod.ObjectMeta.DeletionTimestamp.IsZero() || pod.Status.Phase == "Succeeded" || pod.Status.Phase == "Failed"
}

// PredicateFuncsPod filters out events where pod.Spec.HostNetwork is true
var PredicateFuncsPod = predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
oldPod, okOld := e.ObjectOld.(*v1.Pod)
newPod, okNew := e.ObjectNew.(*v1.Pod)
if !okOld || !okNew {
return true
}

if oldPod.Spec.HostNetwork && newPod.Spec.HostNetwork {
return false
}
return true
},
CreateFunc: func(e event.CreateEvent) bool {
pod, ok := e.Object.(*v1.Pod)
if !ok {
return true
}

if pod.Spec.HostNetwork {
return false
}
return true
},
DeleteFunc: func(e event.DeleteEvent) bool { return true },
GenericFunc: func(e event.GenericEvent) bool {
return true
},
}

0 comments on commit f7caed5

Please sign in to comment.