-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcommands.go
More file actions
462 lines (403 loc) · 13.6 KB
/
commands.go
File metadata and controls
462 lines (403 loc) · 13.6 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package main
import (
"context"
"fmt"
"log/slog"
"os"
"sync"
"sync/atomic"
"time"
"github.com/spf13/cobra"
"github.com/Azure/AKSFlexNode/pkg/bootstrapper"
"github.com/Azure/AKSFlexNode/pkg/config"
"github.com/Azure/AKSFlexNode/pkg/daemon"
"github.com/Azure/AKSFlexNode/pkg/drift"
"github.com/Azure/AKSFlexNode/pkg/logger"
"github.com/Azure/AKSFlexNode/pkg/spec"
"github.com/Azure/AKSFlexNode/pkg/status"
"github.com/Azure/unbounded/pkg/agent/goalstates"
)
// Version information variables (set at build time)
var (
Version = "dev"
GitCommit = "unknown"
BuildTime = "unknown"
)
func NewAgentCommand() *cobra.Command {
var configPath string
cmd := &cobra.Command{
Use: "agent",
Short: "Run the AKS Flex Node daemon",
Long: "Run the long-lived AKS Flex Node daemon with automatic status tracking and self-recovery. This command is intended to be launched by systemd after bootstrap.",
RunE: func(cmd *cobra.Command, args []string) error {
cfg, logger, err := initConfigAndLogger(configPath)
if err != nil {
return err
}
return runAgentDaemon(cmd.Context(), cfg, logger)
},
}
cmd.Flags().StringVar(&configPath, "config", "", "Path to configuration JSON file (required)")
_ = cmd.MarkFlagRequired("config")
return cmd
}
func NewBootstrapCommand() *cobra.Command {
var configPath string
var azureConfigSource string
cmd := &cobra.Command{
Use: "bootstrap",
Short: "Bootstrap the node and start the agent service",
Long: "Install the systemd unit, bootstrap the nspawn-based AKS worker node, then enable and start the agent daemon through systemd.",
RunE: func(cmd *cobra.Command, args []string) error {
cfg, logger, err := initConfigAndLogger(configPath)
if err != nil {
return err
}
return runBootstrap(cmd.Context(), cfg, logger, azureConfigSource)
},
}
cmd.Flags().StringVar(&configPath, "config", "", "Path to configuration JSON file (required)")
cmd.Flags().StringVar(&azureConfigSource, "azure-config-source", "", "Source Azure CLI config directory containing auth files")
_ = cmd.MarkFlagRequired("config")
return cmd
}
func NewUnbootstrapCommand() *cobra.Command {
var configPath string
cmd := &cobra.Command{
Use: "unbootstrap",
Short: "Remove AKS node configuration and Arc connection",
Long: "Clean up and remove all AKS node components and Arc registration from this machine",
RunE: func(cmd *cobra.Command, args []string) error {
l := logger.CreateLogger("info", "")
if err := daemon.UninstallService(cmd.Context(), l); err != nil {
return err
}
cfg, logger, err := initConfigAndLogger(configPath)
if err != nil {
return err
}
return runUnbootstrap(cmd.Context(), cfg, logger)
},
}
cmd.Flags().StringVar(&configPath, "config", "", "Path to configuration JSON file (required)")
_ = cmd.MarkFlagRequired("config")
return cmd
}
func NewVersionCommand() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Show version information",
Long: "Display version, build commit, and build time information",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("AKS Flex Node Agent\n")
fmt.Printf("Version: %s\n", Version)
fmt.Printf("Git Commit: %s\n", GitCommit)
fmt.Printf("Build Time: %s\n", BuildTime)
},
}
}
func initConfigAndLogger(configPath string) (*config.Config, *slog.Logger, error) {
cfg, err := config.LoadConfig(configPath)
if err != nil {
return nil, nil, fmt.Errorf("failed to load config from %s: %w", configPath, err)
}
l := logger.CreateLogger(cfg.Agent.LogLevel, cfg.Agent.LogDir)
return cfg, l, nil
}
func runBootstrap(ctx context.Context, cfg *config.Config, logger *slog.Logger, azureConfigSource string) error {
if err := daemon.InstallService(ctx, logger, azureConfigSource); err != nil {
return err
}
if err := spec.EnsureRuntimeDir(); err != nil {
return err
}
machineName := goalstates.NSpawnMachineKube1
bootstrapExecutor := bootstrapper.New(cfg, logger, machineName)
result, err := bootstrapExecutor.Bootstrap(ctx)
if err != nil {
return err
}
if err := handleExecutionResult(result, "bootstrap", logger); err != nil {
return err
}
logger.Info("Bootstrap completed successfully, starting agent systemd service...")
if err := daemon.EnableAndStartService(ctx, logger); err != nil {
return err
}
printBootstrapNextSteps()
return nil
}
func printBootstrapNextSteps() {
fmt.Println()
fmt.Println("AKS Flex Node agent service started successfully.")
fmt.Println()
fmt.Println("Next steps:")
fmt.Println(" Check service status: systemctl status aks-flex-node-agent")
fmt.Println(" View service logs: journalctl -u aks-flex-node-agent -f")
fmt.Println(" Stop agent: systemctl stop aks-flex-node-agent")
}
func runAgentDaemon(ctx context.Context, cfg *config.Config, logger *slog.Logger) error {
if err := spec.EnsureRuntimeDir(); err != nil {
return err
}
machineName := goalstates.NSpawnMachineKube1
return runDaemonLoop(ctx, cfg, logger, machineName)
}
func runUnbootstrap(ctx context.Context, cfg *config.Config, logger *slog.Logger) error {
if err := spec.EnsureRuntimeDir(); err != nil {
return err
}
machineName := goalstates.NSpawnMachineKube1
bootstrapExecutor := bootstrapper.New(cfg, logger, machineName)
result, err := bootstrapExecutor.Unbootstrap(ctx)
if err != nil {
return err
}
return handleExecutionResult(result, "unbootstrap", logger)
}
func runDaemonLoop(ctx context.Context, cfg *config.Config, logger *slog.Logger, machineName string) error {
statusFilePath := spec.StatusFilePath
if _, err := os.Stat(statusFilePath); err == nil {
logger.Info("Removing stale status file from previous daemon session...")
status.RemoveStatusFileBestEffortAtPath(logger, statusFilePath)
}
removed, err := spec.RemoveManagedClusterSpecSnapshot()
if err != nil {
logger.Warn("Failed to remove stale managed cluster spec snapshot", "error", err)
} else if removed {
logger.Info("Removed stale managed cluster spec snapshot successfully")
}
logger.Info("Starting periodic status collection daemon", "statusInterval", "1m", "bootstrapCheckInterval", "2m", "specCollectionInterval", "10m")
var cfgMu sync.RWMutex
var bootstrapInProgress int32
if err := collectAndWriteStatus(ctx, cfg, logger, statusFilePath, machineName); err != nil {
logger.Error("Failed to collect initial status", "error", err)
}
driftEnabled := cfg != nil && cfg.IsDriftDetectionAndRemediationEnabled()
if !driftEnabled {
logger.Info("Drift detection and remediation is disabled by config")
}
var detectors []drift.Detector
if driftEnabled {
detectors = drift.DefaultDetectors()
if err := collectAndWriteManagedClusterSpec(ctx, cfg, logger); err != nil {
logger.Warn("Failed to collect initial managed cluster spec", "error", err)
} else {
cfgSnap := snapshotConfig(cfg, &cfgMu)
if err := drift.DetectAndRemediateFromFiles(ctx, cfgSnap, logger, &bootstrapInProgress, detectors, machineName); err != nil {
logger.Warn("Initial drift detection after spec collection failed", "error", err)
} else {
logger.Info("Initial drift detection after spec collection completed successfully")
}
}
}
var wg sync.WaitGroup
startDaemonLoops(ctx, cfg, statusFilePath, logger, &cfgMu, &bootstrapInProgress, detectors, driftEnabled, &wg, machineName)
<-ctx.Done()
logger.Info("Daemon shutting down due to context cancellation")
wg.Wait()
return ctx.Err()
}
func startDaemonLoops(
ctx context.Context,
cfg *config.Config,
statusFilePath string,
logger *slog.Logger,
cfgMu *sync.RWMutex,
bootstrapInProgress *int32,
detectors []drift.Detector,
driftEnabled bool,
wg *sync.WaitGroup,
machineName string,
) {
if wg == nil {
return
}
if driftEnabled {
wg.Add(3)
} else {
wg.Add(2)
}
startStatusCollectionLoop(ctx, cfg, statusFilePath, logger, cfgMu, wg, machineName)
startBootstrapHealthCheckLoop(ctx, cfg, logger, cfgMu, bootstrapInProgress, wg, machineName)
if driftEnabled {
startNodeDriftDetectionAndRemediationLoop(ctx, cfg, logger, cfgMu, bootstrapInProgress, detectors, wg, machineName)
}
}
func snapshotConfig(cfg *config.Config, cfgMu *sync.RWMutex) *config.Config {
if cfg == nil {
return nil
}
if cfgMu != nil {
cfgMu.RLock()
defer cfgMu.RUnlock()
}
return cfg.DeepCopy()
}
func startStatusCollectionLoop(
ctx context.Context,
cfg *config.Config,
statusFilePath string,
logger *slog.Logger,
cfgMu *sync.RWMutex,
wg *sync.WaitGroup,
machineName string,
) {
go func() {
defer wg.Done()
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
logger.Info("Starting periodic status collection")
cfgSnap := snapshotConfig(cfg, cfgMu)
err := collectAndWriteStatus(ctx, cfgSnap, logger, statusFilePath, machineName)
if err != nil {
logger.Error("Failed to collect status", "error", err)
continue
}
logger.Info("Status collection completed successfully")
}
}
}()
}
func startBootstrapHealthCheckLoop(
ctx context.Context,
cfg *config.Config,
logger *slog.Logger,
cfgMu *sync.RWMutex,
bootstrapInProgress *int32,
wg *sync.WaitGroup,
machineName string,
) {
go func() {
defer wg.Done()
ticker := time.NewTicker(2 * time.Minute)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
logger.Info("Starting bootstrap health check")
if !atomic.CompareAndSwapInt32(bootstrapInProgress, 0, 1) {
logger.Warn("Bootstrap already in progress, skipping this interval")
continue
}
func() {
defer atomic.StoreInt32(bootstrapInProgress, 0)
cfgSnap := snapshotConfig(cfg, cfgMu)
err := checkAndBootstrap(ctx, cfgSnap, logger, machineName)
if err != nil {
logger.Error("Auto-bootstrap check failed", "error", err)
return
}
logger.Info("Bootstrap health check completed")
}()
}
}
}()
}
func startNodeDriftDetectionAndRemediationLoop(
ctx context.Context,
cfg *config.Config,
logger *slog.Logger,
cfgMu *sync.RWMutex,
bootstrapInProgress *int32,
detectors []drift.Detector,
wg *sync.WaitGroup,
machineName string,
) {
go func() {
defer wg.Done()
ticker := time.NewTicker(10 * time.Minute)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
logger.Info("Starting periodic managed cluster spec collection")
cfgSnap := snapshotConfig(cfg, cfgMu)
err := collectAndWriteManagedClusterSpec(ctx, cfgSnap, logger)
if err != nil {
logger.Warn("Failed to collect managed cluster spec", "error", err)
continue
}
logger.Info("Managed cluster spec collection completed")
if err := drift.DetectAndRemediateFromFiles(ctx, cfgSnap, logger, bootstrapInProgress, detectors, machineName); err != nil {
logger.Warn("Drift detection after spec collection failed", "error", err)
} else {
logger.Info("Drift detection after spec collection completed")
}
}
}
}()
}
func collectAndWriteManagedClusterSpec(ctx context.Context, cfg *config.Config, logger *slog.Logger) error {
collector := spec.NewManagedClusterSpecCollector(cfg, logger)
_, err := collector.Collect(ctx)
return err
}
func checkAndBootstrap(ctx context.Context, cfg *config.Config, logger *slog.Logger, machineName string) error {
collector := status.NewCollector(cfg, logger, Version, machineName)
needsBootstrap := collector.NeedsBootstrap(ctx)
if !needsBootstrap {
return nil
}
logger.Info("Node requires re-bootstrapping, initiating auto-bootstrap...")
if cfg != nil && cfg.IsDriftDetectionAndRemediationEnabled() {
if err := collectAndWriteManagedClusterSpec(ctx, cfg, logger); err != nil {
logger.Warn("Failed to refresh managed cluster spec before auto-bootstrap", "error", err)
}
if changed, oldV, newV, err := spec.OverrideKubernetesVersionFromManagedClusterSpec(cfg); err == nil && changed {
logger.Info("Overriding Kubernetes version from managed cluster spec", "old", oldV, "new", newV)
}
}
bootstrapExecutor := bootstrapper.New(cfg, logger, machineName)
result, err := bootstrapExecutor.Bootstrap(ctx)
if err != nil {
status.RemoveStatusFileBestEffort(logger)
return fmt.Errorf("auto-bootstrap failed: %w", err)
}
if err := handleExecutionResult(result, "auto-bootstrap", logger); err != nil {
status.RemoveStatusFileBestEffort(logger)
return fmt.Errorf("auto-bootstrap execution failed: %w", err)
}
logger.Info("Auto-bootstrap completed successfully")
return nil
}
func collectAndWriteStatus(ctx context.Context, cfg *config.Config, logger *slog.Logger, statusFilePath string, machineName string) error {
collector := status.NewCollector(cfg, logger, Version, machineName)
nodeStatus, err := collector.CollectStatus(ctx)
if err != nil {
return fmt.Errorf("failed to collect node status: %w", err)
}
if nodeStatus != nil {
nodeStatus.LastUpdatedBy = status.LastUpdatedByStatusCollectionLoop
nodeStatus.LastUpdatedReason = status.LastUpdatedReasonPeriodicStatusLoop
}
err = status.WriteStatusToFile(statusFilePath, nodeStatus)
if err != nil {
return fmt.Errorf("failed to write status to file: %w", err)
}
logger.Debug("Status written", "path", statusFilePath)
return nil
}
func handleExecutionResult(result *bootstrapper.ExecutionResult, operation string, logger *slog.Logger) error {
if result == nil {
return fmt.Errorf("%s result is nil", operation)
}
if result.Success {
logger.Info("operation completed successfully", "operation", operation, "duration", result.Duration, "steps", result.StepCount)
return nil
}
if operation == "unbootstrap" {
logger.Warn("operation completed with some failures", "operation", operation, "error", result.Error, "duration", result.Duration)
return nil
}
return fmt.Errorf("%s failed: %s", operation, result.Error)
}