Skip to content

Commit 41f06aa

Browse files
committed
update
1 parent ebdbc64 commit 41f06aa

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

internal/actions/patch_node_handler.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package actions
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"reflect"
89
"strconv"
910
"time"
1011

1112
"github.com/sirupsen/logrus"
1213
v1 "k8s.io/api/core/v1"
13-
apierrors "k8s.io/apimachinery/pkg/api/errors"
1414
"k8s.io/client-go/kubernetes"
1515

1616
"github.com/castai/cluster-controller/internal/castai"
@@ -21,14 +21,16 @@ var _ ActionHandler = &PatchNodeHandler{}
2121

2222
func NewPatchNodeHandler(log logrus.FieldLogger, clientset kubernetes.Interface) *PatchNodeHandler {
2323
return &PatchNodeHandler{
24-
log: log,
25-
clientset: clientset,
24+
retryTimeout: 5 * time.Second, // default timeout for retrying node patching
25+
log: log,
26+
clientset: clientset,
2627
}
2728
}
2829

2930
type PatchNodeHandler struct {
30-
log logrus.FieldLogger
31-
clientset kubernetes.Interface
31+
retryTimeout time.Duration
32+
log logrus.FieldLogger
33+
clientset kubernetes.Interface
3234
}
3335

3436
func (h *PatchNodeHandler) Handle(ctx context.Context, action *castai.ClusterAction) error {
@@ -62,7 +64,7 @@ func (h *PatchNodeHandler) Handle(ctx context.Context, action *castai.ClusterAct
6264

6365
node, err := h.getNodeForPatching(ctx, req.NodeName, req.NodeID, req.ProviderId)
6466
if err != nil {
65-
if apierrors.IsNotFound(err) {
67+
if errors.Is(err, errNodeNotFound) {
6668
log.WithError(err).Infof("node not found, skipping patch")
6769
return nil
6870
}
@@ -119,7 +121,7 @@ func (h *PatchNodeHandler) getNodeForPatching(ctx context.Context, nodeName, nod
119121
var node *v1.Node
120122

121123
boff := waitext.DefaultExponentialBackoff()
122-
boff.Duration = 5 * time.Second
124+
boff.Duration = h.retryTimeout
123125

124126
err := waitext.Retry(
125127
ctx,

internal/actions/patch_node_handler_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package actions
33
import (
44
"context"
55
"testing"
6+
"time"
67

78
"github.com/google/uuid"
89
"github.com/samber/lo"
@@ -24,6 +25,7 @@ func TestPatchNodeHandler(t *testing.T) {
2425

2526
t.Run("patch successfully", func(t *testing.T) {
2627
nodeName := "node1"
28+
providerID := "provider-id-123"
2729
node := &v1.Node{
2830
ObjectMeta: metav1.ObjectMeta{
2931
Name: nodeName,
@@ -35,6 +37,7 @@ func TestPatchNodeHandler(t *testing.T) {
3537
},
3638
},
3739
Spec: v1.NodeSpec{
40+
ProviderID: providerID,
3841
Taints: []v1.Taint{
3942
{
4043
Key: "t1",
@@ -59,7 +62,8 @@ func TestPatchNodeHandler(t *testing.T) {
5962
action := &castai.ClusterAction{
6063
ID: uuid.New().String(),
6164
ActionPatchNode: &castai.ActionPatchNode{
62-
NodeName: "node1",
65+
NodeName: "node1",
66+
ProviderId: providerID,
6367
Labels: map[string]string{
6468
"-l1": "",
6569
"l2": "v2",
@@ -113,6 +117,7 @@ func TestPatchNodeHandler(t *testing.T) {
113117

114118
t.Run("skip patch when node not found", func(t *testing.T) {
115119
nodeName := "node1"
120+
nodeID := "node-id-123"
116121
node := &v1.Node{
117122
ObjectMeta: metav1.ObjectMeta{
118123
Name: nodeName,
@@ -124,11 +129,13 @@ func TestPatchNodeHandler(t *testing.T) {
124129
ID: uuid.New().String(),
125130
ActionPatchNode: &castai.ActionPatchNode{
126131
NodeName: "already-deleted-node",
132+
NodeID: nodeID,
127133
},
128134
}
129135
h := PatchNodeHandler{
130-
log: log,
131-
clientset: clientset,
136+
retryTimeout: time.Millisecond,
137+
log: log,
138+
clientset: clientset,
132139
}
133140

134141
err := h.Handle(context.Background(), action)
@@ -140,9 +147,13 @@ func TestPatchNodeHandler(t *testing.T) {
140147

141148
t.Run("cordoning node", func(t *testing.T) {
142149
nodeName := "node1"
150+
nodeID := "node-id-123"
143151
node := &v1.Node{
144152
ObjectMeta: metav1.ObjectMeta{
145153
Name: nodeName,
154+
Labels: map[string]string{
155+
castai.LabelNodeID: nodeID,
156+
},
146157
},
147158
Spec: v1.NodeSpec{
148159
Unschedulable: false,
@@ -159,6 +170,7 @@ func TestPatchNodeHandler(t *testing.T) {
159170
ID: uuid.New().String(),
160171
ActionPatchNode: &castai.ActionPatchNode{
161172
NodeName: "node1",
173+
NodeID: nodeID,
162174
Unschedulable: lo.ToPtr(true),
163175
},
164176
}

0 commit comments

Comments
 (0)