Skip to content
This repository was archived by the owner on Sep 18, 2020. It is now read-only.

Commit bf66334

Browse files
committed
pkg/k8sutil: Add utility function for getting a node with retry. Add a generic retry function that retries on any error.
1 parent 53b8f94 commit bf66334

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

pkg/k8sutil/metadata.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ func NodeAnnotationCondition(selector fields.Selector) watch.ConditionFunc {
3535
}
3636
}
3737

38+
// GetNodeRetry gets a node object, retrying up to DefaultBackoff number of times if it fails
39+
func GetNodeRetry(nc v1core.NodeInterface, node string) (*v1api.Node, error) {
40+
var apiNode *v1api.Node
41+
err := RetryOnError(DefaultBackoff, func() error {
42+
n, getErr := nc.Get(node, v1meta.GetOptions{})
43+
if getErr != nil {
44+
return fmt.Errorf("failed to get node %q: %v", node, getErr)
45+
}
46+
47+
apiNode = n
48+
return nil
49+
})
50+
51+
return apiNode, err
52+
}
53+
3854
// UpdateNodeRetry calls f to update a node object in Kubernetes.
3955
// It will attempt to update the node by applying f to it up to DefaultBackoff
4056
// number of times.

pkg/k8sutil/retry.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,21 @@ func RetryOnConflict(backoff wait.Backoff, fn func() error) error {
7979
}
8080
return err
8181
}
82+
83+
// RetryOnError retries a function repeatedly with the specified backoff until it succeeds or times out
84+
func RetryOnError(backoff wait.Backoff, fn func() error) error {
85+
var lastErr error
86+
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
87+
lastErr := fn()
88+
if lastErr == nil {
89+
return true, nil
90+
}
91+
92+
return false, nil
93+
})
94+
95+
if err == wait.ErrWaitTimeout {
96+
err = lastErr
97+
}
98+
return err
99+
}

0 commit comments

Comments
 (0)