-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.go
251 lines (216 loc) · 6.78 KB
/
watcher.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package watcher
import (
"context"
"fmt"
"log/slog"
"strconv"
"time"
"github.com/civo/civogo"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// Version is the current version of the this watcher
var Version string = "0.0.1"
const (
nodePoolLabelKey = "kubernetes.civo.com/civo-node-pool"
gpuResourceName = "nvidia.com/gpu"
)
type Watcher interface {
Run(ctx context.Context) error
}
type watcher struct {
client kubernetes.Interface
civoClient civogo.Clienter
clientCfgPath string
clusterID string
region string
apiKey string
apiURL string
nodeDesiredGPUCount int
rebootTimeWindowMinutes time.Duration
nodeSelector *metav1.LabelSelector
}
func NewWatcher(ctx context.Context, apiURL, apiKey, region, clusterID, nodePoolID, nodeDesiredGPUCount string, opts ...Option) (Watcher, error) {
w := &watcher{
clusterID: clusterID,
apiKey: apiKey,
apiURL: apiURL,
region: region,
}
for _, opt := range append(defaultOptions, opts...) {
opt(w)
}
if clusterID == "" {
return nil, fmt.Errorf("CIVO_CLUSTER_ID not set")
}
if nodePoolID == "" {
return nil, fmt.Errorf("CIVO_NODE_POOL_ID not set")
}
if w.civoClient == nil && apiKey == "" {
return nil, fmt.Errorf("CIVO_API_KEY not set")
}
n, err := strconv.Atoi(nodeDesiredGPUCount)
if err != nil {
return nil, fmt.Errorf("CIVO_NODE_DESIRED_GPU_COUNT has an invalid value, %s: %w", nodeDesiredGPUCount, err)
}
w.nodeDesiredGPUCount = n
w.nodeSelector = &metav1.LabelSelector{
MatchLabels: map[string]string{
nodePoolLabelKey: nodePoolID,
},
}
if err := w.setupKubernetesClient(); err != nil {
return nil, err
}
if err := w.setupCivoClient(); err != nil {
return nil, err
}
return w, nil
}
// setupKubernetesClient creates Kubernetes client based on the kubeconfig path.
// If kubeconfig path is not empty, the client will be created using that path.
// Otherwise, if the kubeconfig path is empty, the client will be created using the in-clustetr config.
func (w *watcher) setupKubernetesClient() (err error) {
if w.clientCfgPath != "" && w.client == nil {
cfg, err := clientcmd.BuildConfigFromFlags("", w.clientCfgPath)
if err != nil {
return fmt.Errorf("failed to build kubeconfig from path %q: %w", cfg, err)
}
w.client, err = kubernetes.NewForConfig(cfg)
if err != nil {
return fmt.Errorf("failed to create kubernetes API client: %w", err)
}
return nil
}
if w.client == nil {
cfg, err := rest.InClusterConfig()
if err != nil {
return fmt.Errorf("failed to load in-cluster kubeconfig: %w", err)
}
w.client, err = kubernetes.NewForConfig(cfg)
if err != nil {
return fmt.Errorf("failed to create kubernetes API client: %w", err)
}
}
return nil
}
func (w *watcher) setupCivoClient() error {
if w.civoClient != nil {
return nil
}
client, err := civogo.NewClientWithURL(w.apiKey, w.apiURL, w.region)
if err != nil {
return fmt.Errorf("failed to initialise civo client: %w", err)
}
userAgent := &civogo.Component{
ID: w.clusterID,
Name: "node-agent",
Version: Version,
}
client.SetUserAgent(userAgent)
w.civoClient = client
return nil
}
func (w *watcher) Run(ctx context.Context) error {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
slog.Info("Started the watcher process...")
if err := w.run(ctx); err != nil {
slog.Error("An error occurred while running the watcher process", "error", err)
}
case <-ctx.Done():
return nil
}
}
}
func (w *watcher) run(ctx context.Context) error {
nodes, err := w.client.CoreV1().Nodes().List(ctx, metav1.ListOptions{
LabelSelector: metav1.FormatLabelSelector(w.nodeSelector),
})
if err != nil {
return err
}
thresholdTime := time.Now().Add(-w.rebootTimeWindowMinutes * time.Minute)
for _, node := range nodes.Items {
if !isNodeDesiredGPU(&node, w.nodeDesiredGPUCount) || !isNodeReady(&node) {
slog.Info("Node is not ready, attempting to reboot", "node", node.GetName())
if isReadyOrNotReadyStatusChangedAfter(&node, thresholdTime) {
slog.Info("Skipping reboot because Ready/NotReady status was updated recently", "node", node.GetName())
continue
}
if err := w.rebootNode(node.GetName()); err != nil {
slog.Error("Failed to reboot Node", "node", node.GetName(), "error", err)
return fmt.Errorf("failed to reboot node: %w", err)
}
}
}
return nil
}
func isReadyOrNotReadyStatusChangedAfter(node *corev1.Node, thresholdTime time.Time) bool {
var lastChangedTime time.Time
for _, cond := range node.Status.Conditions {
if cond.Type == corev1.NodeReady {
if cond.LastTransitionTime.After(lastChangedTime) {
lastChangedTime = cond.LastTransitionTime.Time
}
}
}
slog.Info("Checking if Ready/NotReady status has changed recently",
"node", node.GetName(),
"lastTransitionTime", lastChangedTime.String(),
"thresholdTime", thresholdTime.String())
if lastChangedTime.IsZero() {
slog.Error("Node is in an invalid state, NodeReady condition not found", "node", node.GetName())
return false
}
return lastChangedTime.After(thresholdTime)
}
func isNodeReady(node *corev1.Node) bool {
for _, cond := range node.Status.Conditions {
if cond.Type == corev1.NodeReady {
slog.Info("Current Node status", "node", node.GetName(), "type", corev1.NodeReady, "status", cond.Status)
return cond.Status == corev1.ConditionTrue
}
}
slog.Info("NodeReady condition not found", "node", node.GetName())
return false
}
func isNodeDesiredGPU(node *corev1.Node, desired int) bool {
if desired == 0 {
slog.Info("Desired GPU count is set to 0", "node", node.GetName())
return true
}
quantity, exists := node.Status.Allocatable[gpuResourceName]
if !exists || quantity.IsZero() {
slog.Info("Allocatable GPU not found", "node", node.GetName())
return false
}
gpuCount, ok := quantity.AsInt64()
if !ok {
slog.Info("Failed to convert allocatable GPU quantity to int64", "node", node.GetName(), "quantity", quantity.String())
return false
}
slog.Info("Checking actual GPU count with desired",
"node", node.GetName(),
"actual", gpuCount,
"desired", strconv.Itoa(desired))
return gpuCount == int64(desired)
}
func (w *watcher) rebootNode(name string) error {
instance, err := w.civoClient.FindKubernetesClusterInstance(w.clusterID, name)
if err != nil {
return fmt.Errorf("failed to find instance, clusterID: %s, nodeName: %s: %w", w.clusterID, name, err)
}
_, err = w.civoClient.HardRebootInstance(instance.ID)
if err != nil {
return fmt.Errorf("failed to reboot instance, clusterID: %s, instanceID: %s: %w", w.clusterID, instance.ID, err)
}
slog.Info("Instance is rebooting", "instanceID", instance.ID, "node", name)
return nil
}