Skip to content

Commit

Permalink
core: do not watch resourceversion and heartbeat node updates
Browse files Browse the repository at this point in the history
currently node predicate watches for resourceversion and
heartbeat, which are not critical things so we should
look for the reconcile logic and unnecessary
increasing the predicate cache size,
So with the PR we will ignore conidering for a reconcile
of cephcluster if updates are not cruical

closes: #14070

Signed-off-by: parth-gr <paarora@redhat.com>
  • Loading branch information
parth-gr committed May 10, 2024
1 parent 5e3d623 commit 1e21458
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkg/operator/ceph/cluster/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,36 @@ import (
"context"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/clusterd"
discoverDaemon "github.com/rook/rook/pkg/daemon/discover"
"github.com/rook/rook/pkg/operator/ceph/controller"
"github.com/rook/rook/pkg/operator/k8sutil"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

func compareNodes(objOld, objNew *corev1.Node) string {
// do not watch node if only resourceversion got changed
resourceQtyComparer := cmpopts.IgnoreFields(v1.ObjectMeta{}, "ResourceVersion")
diff := cmp.Diff(objOld.Spec, objNew.Spec, resourceQtyComparer)

// do not watch node if only LastHeartbeatTime got changed
resourceQtyComparer = cmpopts.IgnoreFields(corev1.NodeCondition{}, "LastHeartbeatTime")
diff1 := cmp.Diff(objOld.Spec, objNew.Spec, resourceQtyComparer)

if diff == "" && diff1 == "" {
return diff
}
return "not-equal"
}

// predicateForNodeWatcher is the predicate function to trigger reconcile on Node events
func predicateForNodeWatcher(ctx context.Context, client client.Client, context *clusterd.Context) predicate.Funcs {
return predicate.Funcs{
Expand All @@ -43,6 +60,13 @@ func predicateForNodeWatcher(ctx context.Context, client client.Client, context
},

UpdateFunc: func(e event.UpdateEvent) bool {
objOld := e.ObjectOld.(*corev1.Node)
objNew := e.ObjectNew.(*corev1.Node)
diff := compareNodes(objOld, objNew)
if diff == "" {
return false
}

clientCluster := newClientCluster(client, e.ObjectNew.GetNamespace(), context)
return clientCluster.onK8sNode(ctx, e.ObjectNew)
},
Expand Down
37 changes: 37 additions & 0 deletions pkg/operator/ceph/cluster/predicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package cluster

import (
"testing"
"time"

cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestIsHotPlugCM(t *testing.T) {
Expand All @@ -40,3 +42,38 @@ func TestIsHotPlugCM(t *testing.T) {
cm.Labels["app"] = "rook-discover"
assert.True(t, isHotPlugCM(cm))
}

func TestCompareNodes(t *testing.T) {
oldobj := corev1.Node{}
newobj := corev1.Node{}

result := compareNodes(&oldobj, &newobj)
assert.Equal(t, result, "")

oldobj.ResourceVersion = "123"
newobj.ResourceVersion = "145"
result = compareNodes(&oldobj, &newobj)
assert.Equal(t, result, "")

oldobj.Status = corev1.NodeStatus{
Conditions: []corev1.NodeCondition{
{
LastHeartbeatTime: metav1.NewTime(time.Now()),
},
},
}
newobj.Status = corev1.NodeStatus{
Conditions: []corev1.NodeCondition{
{
LastHeartbeatTime: metav1.NewTime(<-time.After(5)),
},
},
}
result = compareNodes(&oldobj, &newobj)
assert.Equal(t, result, "")

oldobj.Spec.PodCIDR = "123"
newobj.Spec.PodCIDR = "145"
result = compareNodes(&oldobj, &newobj)
assert.Equal(t, result, "not-equal")
}

0 comments on commit 1e21458

Please sign in to comment.