Skip to content

Commit

Permalink
fix node pod probe event handler bug (#1116)
Browse files Browse the repository at this point in the history
Signed-off-by: liheng.zms <liheng.zms@alibaba-inc.com>

Signed-off-by: liheng.zms <liheng.zms@alibaba-inc.com>
  • Loading branch information
zmberg authored Jan 3, 2023
1 parent fb95062 commit 69512c6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
5 changes: 0 additions & 5 deletions pkg/controller/nodepodprobe/node_pod_probe_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"flag"
"reflect"
"strings"
"time"

appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
"github.com/openkruise/kruise/pkg/features"
Expand All @@ -47,10 +46,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/source"
)

var (
nodePodProbeCreationDelayAfterNodeReady = time.Second * 30
)

func init() {
flag.IntVar(&concurrentReconciles, "nodepodprobe-workers", concurrentReconciles, "Max concurrent workers for NodePodProbe controller.")
}
Expand Down
19 changes: 6 additions & 13 deletions pkg/controller/nodepodprobe/nodepodprobe_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package nodepodprobe
import (
"context"
"reflect"
"time"

appsalphav1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -170,6 +169,8 @@ func (e *enqueueRequestForNode) Update(evt event.UpdateEvent, q workqueue.RateLi
}
if node.DeletionTimestamp != nil {
e.nodeDelete(node, q)
} else {
e.nodeCreate(node, q)
}
}

Expand All @@ -187,13 +188,9 @@ func (e *enqueueRequestForNode) nodeCreate(node *corev1.Node, q workqueue.RateLi
if errors.IsNotFound(err) {
klog.Infof("Node create event for nodePodProbe %v", node.Name)
namespacedName := types.NamespacedName{Name: node.Name}
if isReady, delay := getNodeReadyAndDelayTime(node); !isReady {
if !isNodeReady(node) {
klog.Infof("Skip to enqueue Node %s with not nodePodProbe, for not ready yet.", node.Name)
return
} else if delay > 0 {
klog.Infof("Enqueue Node %s with not nodePodProbe after %v.", node.Name, delay)
q.AddAfter(reconcile.Request{NamespacedName: namespacedName}, delay)
return
}
klog.Infof("Enqueue Node %s with not nodePodProbe.", node.Name)
q.Add(reconcile.Request{NamespacedName: namespacedName})
Expand All @@ -211,14 +208,10 @@ func (e *enqueueRequestForNode) nodeDelete(node *corev1.Node, q workqueue.RateLi
q.Add(reconcile.Request{NamespacedName: types.NamespacedName{Name: node.Name}})
}

func getNodeReadyAndDelayTime(node *corev1.Node) (bool, time.Duration) {
func isNodeReady(node *corev1.Node) bool {
_, condition := nodeutil.GetNodeCondition(&node.Status, corev1.NodeReady)
if condition == nil || condition.Status != corev1.ConditionTrue {
return false, 0
}
delay := nodePodProbeCreationDelayAfterNodeReady - time.Since(condition.LastTransitionTime.Time)
if delay > 0 {
return true, delay
return false
}
return true, 0
return true
}
59 changes: 59 additions & 0 deletions pkg/controller/nodepodprobe/nodepodprobe_event_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2022 The Kruise Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package nodepodprobe

import (
"context"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/event"
)

var (
demoNode = corev1.Node{
ObjectMeta: metav1.ObjectMeta{Name: "node1"},
Status: corev1.NodeStatus{
Phase: corev1.NodeRunning,
Conditions: []corev1.NodeCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionTrue,
},
},
},
}
)

func TestNodeEventHandler(t *testing.T) {
fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build()
handler := enqueueRequestForNode{Reader: fakeClient}
demo := demoNode.DeepCopy()
_ = fakeClient.Create(context.TODO(), demo)
// create
createQ := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
createEvt := event.CreateEvent{
Object: demo,
}
handler.Create(createEvt, createQ)
if createQ.Len() != 1 {
t.Errorf("unexpected create event handle queue size, expected 1 actual %d", createQ.Len())
}
}

0 comments on commit 69512c6

Please sign in to comment.