Skip to content

Commit 737bf16

Browse files
committed
Mocks for k8s client/node drainer
1 parent ce7586c commit 737bf16

File tree

4 files changed

+276
-18
lines changed

4 files changed

+276
-18
lines changed

internal/k8s/kubernetes.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:generate mockgen -destination ./mock/kubernetes.go . Client
2+
13
package k8s
24

35
import (
@@ -44,32 +46,49 @@ const (
4446
DefaultMaxRetriesK8SOperation = 5
4547
)
4648

49+
type Client interface {
50+
PatchNode(ctx context.Context, node *v1.Node, changeFn func(*v1.Node)) error
51+
PatchNodeStatus(ctx context.Context, name string, patch []byte) error
52+
EvictPod(ctx context.Context, pod v1.Pod, podEvictRetryDelay time.Duration, version schema.GroupVersion) error
53+
CordonNode(ctx context.Context, node *v1.Node) error
54+
GetNodeByIDs(ctx context.Context, nodeName, nodeID, providerID string) (*v1.Node, error)
55+
ExecuteBatchPodActions(
56+
ctx context.Context,
57+
pods []*v1.Pod,
58+
action func(context.Context, v1.Pod) error,
59+
actionName string,
60+
) ([]*v1.Pod, []PodActionFailure)
61+
DeletePod(ctx context.Context, options metav1.DeleteOptions, pod v1.Pod, podDeleteRetries int, podDeleteRetryDelay time.Duration) error
62+
Clientset() kubernetes.Interface
63+
Log() logrus.FieldLogger
64+
}
65+
4766
// Client provides Kubernetes operations with common dependencies.
48-
type Client struct {
67+
type client struct {
4968
clientset kubernetes.Interface
5069
log logrus.FieldLogger
5170
}
5271

5372
// NewClient creates a new K8s client with the given dependencies.
54-
func NewClient(clientset kubernetes.Interface, log logrus.FieldLogger) *Client {
55-
return &Client{
73+
func NewClient(clientset kubernetes.Interface, log logrus.FieldLogger) Client {
74+
return &client{
5675
clientset: clientset,
5776
log: log,
5877
}
5978
}
6079

6180
// Clientset returns the underlying kubernetes.Interface.
62-
func (c *Client) Clientset() kubernetes.Interface {
81+
func (c *client) Clientset() kubernetes.Interface {
6382
return c.clientset
6483
}
6584

6685
// Log returns the logger.
67-
func (c *Client) Log() logrus.FieldLogger {
86+
func (c *client) Log() logrus.FieldLogger {
6887
return c.log
6988
}
7089

7190
// PatchNode patches a node with the given change function.
72-
func (c *Client) PatchNode(ctx context.Context, node *v1.Node, changeFn func(*v1.Node)) error {
91+
func (c *client) PatchNode(ctx context.Context, node *v1.Node, changeFn func(*v1.Node)) error {
7392
logger := logger.FromContext(ctx, c.log)
7493
oldData, err := json.Marshal(node)
7594
if err != nil {
@@ -108,7 +127,7 @@ func (c *Client) PatchNode(ctx context.Context, node *v1.Node, changeFn func(*v1
108127
}
109128

110129
// PatchNodeStatus patches the status of a node.
111-
func (c *Client) PatchNodeStatus(ctx context.Context, name string, patch []byte) error {
130+
func (c *client) PatchNodeStatus(ctx context.Context, name string, patch []byte) error {
112131
logger := logger.FromContext(ctx, c.log)
113132

114133
err := waitext.Retry(
@@ -134,7 +153,7 @@ func (c *Client) PatchNodeStatus(ctx context.Context, name string, patch []byte)
134153
return nil
135154
}
136155

137-
func (c *Client) CordonNode(ctx context.Context, node *v1.Node) error {
156+
func (c *client) CordonNode(ctx context.Context, node *v1.Node) error {
138157
if node.Spec.Unschedulable {
139158
return nil
140159
}
@@ -149,7 +168,7 @@ func (c *Client) CordonNode(ctx context.Context, node *v1.Node) error {
149168
}
150169

151170
// GetNodeByIDs retrieves a node by name and validates its ID and provider ID.
152-
func (c *Client) GetNodeByIDs(ctx context.Context, nodeName, nodeID, providerID string) (*v1.Node, error) {
171+
func (c *client) GetNodeByIDs(ctx context.Context, nodeName, nodeID, providerID string) (*v1.Node, error) {
153172
if nodeID == "" && providerID == "" {
154173
return nil, fmt.Errorf("node and provider IDs are empty %w", ErrAction)
155174
}
@@ -178,7 +197,7 @@ func (c *Client) GetNodeByIDs(ctx context.Context, nodeName, nodeID, providerID
178197
// It does internal throttling to avoid spawning a goroutine-per-pod on large lists.
179198
// Returns two sets of pods - the ones that successfully executed the action and the ones that failed.
180199
// actionName might be used to distinguish what is the operation (for logs, debugging, etc.) but is optional.
181-
func (c *Client) ExecuteBatchPodActions(
200+
func (c *client) ExecuteBatchPodActions(
182201
ctx context.Context,
183202
pods []*v1.Pod,
184203
action func(context.Context, v1.Pod) error,
@@ -250,7 +269,7 @@ func (c *Client) ExecuteBatchPodActions(
250269

251270
// EvictPod evicts a pod from a k8s node. Error handling is based on eviction api documentation:
252271
// https://kubernetes.io/docs/tasks/administer-cluster/safely-drain-node/#the-eviction-api
253-
func (c *Client) EvictPod(ctx context.Context, pod v1.Pod, podEvictRetryDelay time.Duration, version schema.GroupVersion) error {
272+
func (c *client) EvictPod(ctx context.Context, pod v1.Pod, podEvictRetryDelay time.Duration, version schema.GroupVersion) error {
254273
logger := logger.FromContext(ctx, c.log)
255274

256275
b := waitext.NewConstantBackoff(podEvictRetryDelay)
@@ -306,7 +325,7 @@ func (c *Client) EvictPod(ctx context.Context, pod v1.Pod, podEvictRetryDelay ti
306325
}
307326

308327
// DeletePod deletes a pod from the cluster.
309-
func (c *Client) DeletePod(ctx context.Context, options metav1.DeleteOptions, pod v1.Pod, podDeleteRetries int, podDeleteRetryDelay time.Duration) error {
328+
func (c *client) DeletePod(ctx context.Context, options metav1.DeleteOptions, pod v1.Pod, podDeleteRetries int, podDeleteRetryDelay time.Duration) error {
310329
logger := logger.FromContext(ctx, c.log)
311330

312331
b := waitext.NewConstantBackoff(podDeleteRetryDelay)

internal/k8s/mock/kubernetes.go

Lines changed: 170 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/nodes/mock/kubernetes.go

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)