Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
118 lines (105 loc) · 3.44 KB
/
Copy pathmain.go
File metadata and controls
118 lines (105 loc) · 3.44 KB
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
package main
import (
"context"
"errors"
"flag"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/civo/node-agent/pkg/health"
"github.com/civo/node-agent/pkg/metrics"
"github.com/civo/node-agent/pkg/operation"
"github.com/civo/node-agent/pkg/watcher"
)
var (
version = "0.2.0"
versionInfo = flag.Bool("version", false, "Print the driver version")
kubeconfigPath = flag.String("kubeconfig", "/etc/rancher/k3s/k3s.yaml", "Path to kubeconfig file (empty for in-cluster config)")
)
var (
apiURL = strings.TrimSpace(os.Getenv("CIVO_API_URL"))
apiKey = strings.TrimSpace(os.Getenv("CIVO_API_KEY"))
region = strings.TrimSpace(os.Getenv("CIVO_REGION"))
clusterID = strings.TrimSpace(os.Getenv("CIVO_CLUSTER_ID"))
nodePoolIDs = strings.TrimSpace(os.Getenv("CIVO_NODE_POOL_IDS"))
rebootWaitMinutes = strings.TrimSpace(os.Getenv("CIVO_NODE_REBOOT_WAIT_MINUTES"))
gpuRebootWaitMinutes = strings.TrimSpace(os.Getenv("CIVO_GPU_NODE_REBOOT_WAIT_MINUTES"))
maxRebootRetries = strings.TrimSpace(os.Getenv("CIVO_NODE_MAX_REBOOT_RETRIES"))
monitorOnly = strings.TrimSpace(os.Getenv("CIVO_NODE_AGENT_MONITOR_ONLY"))
metricsPort = strings.TrimSpace(os.Getenv("CIVO_NODE_AGENT_METRICS_PORT"))
)
const (
defaultMetricsPort = 9625
)
func run(ctx context.Context) error {
ctx, stop := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer stop()
executor, err := operation.NewCivoExecutor(clusterID,
operation.WithAPIConfig(apiKey, apiURL, region, version))
if err != nil {
return fmt.Errorf("failed to initialise executor: %w", err)
}
checkers := health.NewDefaultCheckers()
metrics.Register()
metrics.Info.WithLabelValues(version, clusterID).Set(1)
metricsServer := &http.Server{
Addr: ":" + metricsPortValue(metricsPort),
Handler: metrics.Handler(),
}
go func() {
slog.Info("Starting metrics server", "addr", metricsServer.Addr)
if err := metricsServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
slog.Error("Metrics server failed", "error", err)
stop()
}
}()
defer func() {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := metricsServer.Shutdown(shutdownCtx); err != nil {
slog.Error("Metrics server shutdown failed", "error", err)
}
}()
w, err := watcher.NewWatcher(
watcher.WithNodePoolIDs(nodePoolIDs),
watcher.WithKubernetesClientConfigPath(*kubeconfigPath),
watcher.WithExecutor(executor),
watcher.WithCheckers(checkers),
watcher.WithMonitorOnly(monitorOnly),
watcher.WithRebootWaitMinutes(rebootWaitMinutes),
watcher.WithGPURebootWaitMinutes(gpuRebootWaitMinutes),
watcher.WithMaxRebootRetries(maxRebootRetries),
)
if err != nil {
return err
}
return w.Run(ctx)
}
func main() {
flag.Parse()
if *versionInfo {
slog.Info("node-agent", "version", version)
return
}
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil).WithAttrs([]slog.Attr{
slog.String("clusterID", clusterID),
slog.String("region", region),
slog.String("nodePoolIDs", nodePoolIDs),
})))
if err := run(context.Background()); err != nil {
slog.Error("The node-agent encountered a critical error and will exit", "error", err)
os.Exit(1)
}
}
func metricsPortValue(s string) string {
if v, err := strconv.Atoi(s); err == nil && v >= 1024 && v <= 65535 {
return s
}
return strconv.Itoa(defaultMetricsPort)
}