-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathmain.go
More file actions
149 lines (126 loc) · 5.26 KB
/
main.go
File metadata and controls
149 lines (126 loc) · 5.26 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
// Package main provides the main entry point for the E2B on Kubernetes server.
package main
import (
"flag"
"net/http" // Added for pprof server
_ "net/http/pprof" // Added to register pprof handlers
"github.com/google/uuid"
"github.com/openkruise/agents/pkg/sandbox-manager/consts"
"github.com/openkruise/agents/pkg/utils"
"github.com/spf13/pflag"
zapRaw "go.uber.org/zap"
"go.uber.org/zap/zapcore"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"github.com/openkruise/agents/pkg/sandbox-manager/clients"
"github.com/openkruise/agents/pkg/servers/e2b"
"github.com/openkruise/agents/pkg/servers/e2b/models"
utilfeature "github.com/openkruise/agents/pkg/utils/feature"
)
func main() {
// Define variables for pprof configuration
var enablePprof bool
var pprofAddr string
// Define variables for server configuration
var port int
var e2bAdminKey string
var e2bEnableAuth bool
var domain string
var e2bMaxTimeout int
var sysNs string
var peerSelector string
var sandboxNamespace string
var sandboxLabelSelector string
var maxClaimWorkers int
var maxCreateQPS int
var extProcMaxConcurrency int
var kubeClientQPS float64
var kubeClientBurst int
utilfeature.DefaultMutableFeatureGate.AddFlag(pflag.CommandLine)
// Register the new pprof flags
pflag.BoolVar(&enablePprof, "enable-pprof", false, "Enable pprof profiling")
pflag.StringVar(&pprofAddr, "pprof-addr", ":6060", "The address the pprof debug maps to.")
// Register server configuration flags
pflag.IntVar(&port, "port", 8080, "The port the server listens on")
pflag.StringVar(&e2bAdminKey, "e2b-admin-key", "", "E2B admin API key (if empty, a random UUID will be generated)")
pflag.BoolVar(&e2bEnableAuth, "e2b-enable-auth", true, "Enable E2B authentication")
pflag.StringVar(&domain, "e2b-domain", "localhost", "E2B domain")
pflag.IntVar(&e2bMaxTimeout, "e2b-max-timeout", models.DefaultMaxTimeout, "E2B maximum timeout in seconds")
pflag.StringVar(&sysNs, "system-namespace", utils.DefaultSandboxDeployNamespace, "The namespace where the sandbox manager is running (required)")
pflag.StringVar(&peerSelector, "peer-selector", "", "Peer selector for sandbox manager (required)")
pflag.StringVar(&sandboxNamespace, "sandbox-namespace", "", "Namespace to filter sandbox-related custom resources (Sandbox, SandboxSet, Checkpoint, SandboxTemplate). Defaults to all.")
pflag.StringVar(&sandboxLabelSelector, "sandbox-label-selector", "", "Label selector to filter sandbox-related custom resources (Sandbox, SandboxSet, Checkpoint, SandboxTemplate). Defaults to all.")
pflag.IntVar(&maxClaimWorkers, "max-claim-workers", consts.DefaultClaimWorkers, "Maximum number of claim workers (0 uses default)")
pflag.IntVar(&maxCreateQPS, "max-create-qps", consts.DefaultCreateQPS, "Maximum QPS for sandbox creation (0 uses default)")
pflag.IntVar(&extProcMaxConcurrency, "ext-proc-max-concurrency", consts.DefaultExtProcConcurrency, "Maximum concurrency for external processor (0 uses default)")
pflag.Float64Var(&kubeClientQPS, "kube-client-qps", 500, "QPS for Kubernetes client")
pflag.IntVar(&kubeClientBurst, "kube-client-burst", 1000, "Burst for Kubernetes client")
opts := zap.Options{
Development: false,
}
opts.BindFlags(flag.CommandLine)
klog.InitFlags(nil)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
klog.SetLogger(zap.New(
zap.UseFlagOptions(&opts),
zap.RawZapOpts(zapRaw.AddCaller()),
zap.StacktraceLevel(zapcore.DPanicLevel),
))
// Start pprof server if enabled
if enablePprof {
go func() {
klog.Infof("Starting pprof server on %s", pprofAddr)
if err := http.ListenAndServe(pprofAddr, nil); err != nil {
klog.Errorf("Unable to start pprof server: %v", err)
}
}()
}
// Validate required flags
if sysNs == "" {
klog.Fatalf("--system-namespace is required")
}
if peerSelector == "" {
klog.Fatalf("--peer-selector is required")
}
// Generate admin key if not provided
if e2bAdminKey == "" {
e2bAdminKey = uuid.NewString()
}
// Validate positive values
if e2bMaxTimeout <= 0 {
klog.Fatalf("--e2b-max-timeout must be greater than 0")
}
if maxClaimWorkers < 0 {
klog.Fatalf("--max-claim-workers must be non-negative")
}
if maxCreateQPS < 0 {
klog.Fatalf("--max-create-qps must be non-negative")
}
if extProcMaxConcurrency < 0 {
klog.Fatalf("--ext-proc-max-concurrency must be non-negative")
}
if kubeClientQPS <= 0 {
klog.Fatalf("--kube-client-qps must be greater than 0")
}
if kubeClientBurst <= 0 {
klog.Fatalf("--kube-client-burst must be greater than 0")
}
// Initialize Kubernetes client and config
clientSet, err := clients.NewClientSetWithOptions(float32(kubeClientQPS), kubeClientBurst)
if err != nil {
klog.Fatalf("Failed to initialize Kubernetes client: %v", err)
}
sandboxController := e2b.NewController(domain, e2bAdminKey, sysNs, sandboxNamespace, sandboxLabelSelector, e2bMaxTimeout, maxClaimWorkers, maxCreateQPS, uint32(extProcMaxConcurrency),
port, e2bEnableAuth, clientSet)
if err := sandboxController.Init(); err != nil {
klog.Fatalf("Failed to initialize sandbox controller: %v", err)
}
// Start HTTP Server
sandboxCtx, err := sandboxController.Run(sysNs, peerSelector)
if err != nil {
klog.Fatalf("Failed to start sandbox controller: %v", err)
}
<-sandboxCtx.Done()
klog.Info("Sandbox controller stopped")
}