Skip to content

Commit

Permalink
add predicates for yawol-cloud-controller (node) to reduce the number…
Browse files Browse the repository at this point in the history
… of reconciles
  • Loading branch information
dergeberl committed May 30, 2023
1 parent 766a35e commit 5314b45
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package targetcontroller
import (
"context"
"encoding/json"
"reflect"
"regexp"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"strings"

yawolv1beta1 "github.com/stackitcloud/yawol/api/v1beta1"
Expand Down Expand Up @@ -84,9 +87,36 @@ func (r *NodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
func (r *NodeReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&coreV1.Node{}).
WithEventFilter(yawolNodePredicate()).
Complete(r)
}

func yawolNodePredicate() predicate.Predicate {
return predicate.Funcs{
CreateFunc: func(_ event.CreateEvent) bool {
return true
},
DeleteFunc: func(_ event.DeleteEvent) bool {
return true
},
UpdateFunc: func(updateEvent event.UpdateEvent) bool {
newNode := updateEvent.ObjectNew.(*coreV1.Node)
oldNode := updateEvent.ObjectOld.(*coreV1.Node)

if isNodeReady(*oldNode) != isNodeReady(*newNode) {
return true
}

return !reflect.DeepEqual(
getLoadBalancerEndpointFromNode(*oldNode, []coreV1.IPFamily{}),
getLoadBalancerEndpointFromNode(*newNode, []coreV1.IPFamily{}))
},
GenericFunc: func(_ event.GenericEvent) bool {
return false
},
}
}

func (r *NodeReconciler) patchEndpointsToLB(
ctx context.Context,
endpoints []yawolv1beta1.LoadBalancerEndpoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package targetcontroller
import (
"context"
"fmt"
predicatesEvent "sigs.k8s.io/controller-runtime/pkg/event"
"strings"
"time"

Expand All @@ -17,6 +18,105 @@ import (
. "github.com/onsi/gomega"
)

var _ = Describe("check controller-runtime predicate", func() {
nodeName := "node1"
conditionReady := v1.NodeCondition{

Type: v1.NodeReady,
Status: v1.ConditionTrue,
LastHeartbeatTime: metav1.Time{},
LastTransitionTime: metav1.Time{},
Reason: "Ready",
Message: "Ready",
}
conditionNotReady := v1.NodeCondition{

Type: v1.NodeReady,
Status: v1.ConditionFalse,
LastHeartbeatTime: metav1.Time{},
LastTransitionTime: metav1.Time{},
Reason: "Ready",
Message: "Ready",
}

baseNode := v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: nodeName,
Namespace: "default"},
Spec: v1.NodeSpec{},
Status: v1.NodeStatus{
Conditions: []v1.NodeCondition{
conditionReady,
},
Addresses: []v1.NodeAddress{
{
Type: v1.NodeInternalIP,
Address: "10.10.10.10",
}, {
Type: v1.NodeInternalIP,
Address: "2001:16b8:3015:1100::1b14",
},
},
},
}

It("should reconcile", func() {
By("change in ready condition", func() {
oldNode := baseNode.DeepCopy()
newNode := baseNode.DeepCopy()
newNode.Status.Conditions = []v1.NodeCondition{conditionNotReady}

event := predicatesEvent.UpdateEvent{
ObjectOld: oldNode,
ObjectNew: newNode,
}
Expect(yawolNodePredicate().Update(event)).To(BeTrue())
})
By("change in node addresses", func() {
oldNode := baseNode.DeepCopy()
newNode := baseNode.DeepCopy()
newNode.Status.Addresses = append(newNode.Status.Addresses, v1.NodeAddress{
Type: v1.NodeInternalIP,
Address: "10.10.10.11",
})

event := predicatesEvent.UpdateEvent{
ObjectOld: oldNode,
ObjectNew: newNode,
}
Expect(yawolNodePredicate().Update(event)).To(BeTrue())
})
By("create", func() {
event := predicatesEvent.CreateEvent{
Object: baseNode.DeepCopy(),
}
Expect(yawolNodePredicate().Create(event)).To(BeTrue())
})
By("delete", func() {
event := predicatesEvent.DeleteEvent{
Object: baseNode.DeepCopy(),
}
Expect(yawolNodePredicate().Delete(event)).To(BeTrue())
})
})

It("should not reconcile", func() {
By("change if no change in ready condition or node addresses", func() {
event := predicatesEvent.UpdateEvent{
ObjectOld: baseNode.DeepCopy(),
ObjectNew: baseNode.DeepCopy(),
}
Expect(yawolNodePredicate().Update(event)).To(BeFalse())
})
By("on generic event", func() {
event := predicatesEvent.GenericEvent{
Object: baseNode.DeepCopy(),
}
Expect(yawolNodePredicate().Generic(event)).To(BeFalse())
})
})
})

var _ = Describe("Check loadbalancer reconcile", Serial, Ordered, func() {
Context("run tests", func() {
ctx := context.Background()
Expand Down

0 comments on commit 5314b45

Please sign in to comment.