-
Notifications
You must be signed in to change notification settings - Fork 3
/
kubernetes_helpers.go
50 lines (41 loc) · 1.29 KB
/
kubernetes_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package actions
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/cenkalti/backoff/v4"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apitypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/client-go/kubernetes"
)
func patchNode(ctx context.Context, clientset kubernetes.Interface, node *v1.Node, changeFn func(*v1.Node) error) error {
oldData, err := json.Marshal(node)
if err != nil {
return fmt.Errorf("marshaling old data: %w", err)
}
if err := changeFn(node); err != nil {
return err
}
newData, err := json.Marshal(node)
if err != nil {
return fmt.Errorf("marshaling new data: %w", err)
}
patch, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, node)
if err != nil {
return fmt.Errorf("creating patch for node: %w", err)
}
err = backoff.Retry(func() error {
_, err = clientset.CoreV1().Nodes().Patch(ctx, node.Name, apitypes.StrategicMergePatchType, patch, metav1.PatchOptions{})
return err
}, defaultBackoff(ctx))
if err != nil {
return fmt.Errorf("patching node: %w", err)
}
return nil
}
func defaultBackoff(ctx context.Context) backoff.BackOffContext {
return backoff.WithContext(backoff.WithMaxRetries(backoff.NewConstantBackOff(500*time.Millisecond), 5), ctx) // nolint:gomnd
}