-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (66 loc) · 2.03 KB
/
main.go
File metadata and controls
79 lines (66 loc) · 2.03 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/spf13/cobra"
"github.com/Azure/AKSFlexNode/pkg/cmd/apply"
"github.com/Azure/AKSFlexNode/pkg/cmd/token"
"github.com/Azure/AKSFlexNode/pkg/config"
"github.com/Azure/AKSFlexNode/pkg/logger"
)
var (
configPath string
)
func main() {
rootCmd := &cobra.Command{
Use: "aks-flex-node",
Short: "AKS Flex Node Agent",
Long: "Azure Kubernetes Service Flex Node Agent for edge computing scenarios",
}
// Add global flags for configuration
rootCmd.PersistentFlags().StringVar(&configPath, "config", "", "Path to configuration JSON file (required)")
// Don't mark as required globally - we'll check in PersistentPreRunE for commands that need it
// Add commands
rootCmd.AddCommand(NewAgentCommand())
rootCmd.AddCommand(NewUnbootstrapCommand())
rootCmd.AddCommand(NewVersionCommand())
rootCmd.AddCommand(apply.Command)
rootCmd.AddCommand(token.Command)
// Set up context with signal handling
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
// Set up persistent pre-run to initialize config and logger
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
// Skip config loading for version command
if cmd.Name() == "version" {
return nil
}
if cmd.Name() == "apply" {
return nil
}
if cmd.Name() == "kubelogin" {
return nil
}
// For other commands, config is required
if configPath == "" {
return fmt.Errorf("config path is required for %s command", cmd.Name())
}
// Load config if specified
cfg, err := config.LoadConfig(configPath)
if err != nil {
return fmt.Errorf("failed to load config from %s: %w", configPath, err)
}
// Setup logger and update context
ctx := logger.SetupLogger(cmd.Context(), cfg.Agent.LogLevel, cfg.Agent.LogDir)
cmd.SetContext(ctx)
return nil
}
// Execute command with context
if err := rootCmd.ExecuteContext(ctx); err != nil {
fmt.Fprintf(os.Stderr, "Command execution failed: %v\n", err)
os.Exit(1)
}
}