-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add check node deleted action (#32)
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package actions | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
"github.com/sirupsen/logrus" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
|
||
"github.com/castai/cluster-controller/castai" | ||
) | ||
|
||
type checkNodeDeletedConfig struct { | ||
retries uint64 | ||
retryWait time.Duration | ||
} | ||
|
||
func newCheckNodeDeletedHandler(log logrus.FieldLogger, clientset kubernetes.Interface) ActionHandler { | ||
return &checkNodeDeletedHandler{ | ||
log: log, | ||
clientset: clientset, | ||
cfg: checkNodeDeletedConfig{ | ||
retries: 5, | ||
retryWait: 1 * time.Second, | ||
}, | ||
} | ||
} | ||
|
||
type checkNodeDeletedHandler struct { | ||
log logrus.FieldLogger | ||
clientset kubernetes.Interface | ||
cfg checkNodeDeletedConfig | ||
} | ||
|
||
func (h *checkNodeDeletedHandler) Handle(ctx context.Context, data interface{}) error { | ||
req, ok := data.(*castai.ActionCheckNodeDeleted) | ||
if !ok { | ||
return fmt.Errorf("unexpected type %T for check node deleted handler", data) | ||
} | ||
|
||
log := h.log.WithField("node_name", req.NodeName) | ||
log.Info("checking if node is deleted") | ||
|
||
b := backoff.WithContext(backoff.WithMaxRetries(backoff.NewConstantBackOff(h.cfg.retryWait), h.cfg.retries), ctx) | ||
return backoff.Retry(func() error { | ||
n, err := h.clientset.CoreV1().Nodes().Get(ctx, req.NodeName, metav1.GetOptions{}) | ||
if apierrors.IsNotFound(err) { | ||
return nil | ||
} | ||
if n != nil { | ||
return backoff.Permanent(errors.New("node is not deleted")) | ||
} | ||
return err | ||
}, b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package actions | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/require" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/fake" | ||
|
||
"github.com/castai/cluster-controller/castai" | ||
) | ||
|
||
func TestCheckNodeDeletedHandler(t *testing.T) { | ||
r := require.New(t) | ||
|
||
log := logrus.New() | ||
log.SetLevel(logrus.DebugLevel) | ||
|
||
t.Run("return error when node is not deleted", func(t *testing.T) { | ||
nodeName := "node1" | ||
node := &v1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: nodeName, | ||
}, | ||
} | ||
clientset := fake.NewSimpleClientset(node) | ||
|
||
h := checkNodeDeletedHandler{ | ||
log: log, | ||
clientset: clientset, | ||
cfg: checkNodeDeletedConfig{}, | ||
} | ||
|
||
req := &castai.ActionCheckNodeDeleted{ | ||
NodeName: "node1", | ||
} | ||
|
||
err := h.Handle(context.Background(), req) | ||
r.EqualError(err, "node is not deleted") | ||
}) | ||
|
||
t.Run("handle check successfully when node is not found", func(t *testing.T) { | ||
clientset := fake.NewSimpleClientset() | ||
|
||
h := checkNodeDeletedHandler{ | ||
log: log, | ||
clientset: clientset, | ||
cfg: checkNodeDeletedConfig{}, | ||
} | ||
|
||
req := &castai.ActionCheckNodeDeleted{ | ||
NodeName: "node1", | ||
} | ||
|
||
err := h.Handle(context.Background(), req) | ||
r.NoError(err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters