-
Notifications
You must be signed in to change notification settings - Fork 227
/
root.go
108 lines (94 loc) · 3.03 KB
/
root.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
// Package controller starts the fleet controller.
package controller
import (
"context"
"log"
"net/http"
"os"
"path"
"runtime/pprof"
"strconv"
"time"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/wait"
command "github.com/rancher/fleet/internal/cmd"
"github.com/rancher/fleet/internal/cmd/controller/agent"
"github.com/rancher/fleet/pkg/durations"
"github.com/rancher/fleet/pkg/version"
)
var (
debugConfig command.DebugConfig
)
type FleetManager struct {
Kubeconfig string `usage:"Kubeconfig file"`
Namespace string `usage:"namespace to watch" default:"cattle-fleet-system" env:"NAMESPACE"`
DisableGitops bool `usage:"disable gitops components" name:"disable-gitops"`
DisableBootstrap bool `usage:"disable local cluster components" name:"disable-bootstrap"`
}
func (f *FleetManager) Run(cmd *cobra.Command, args []string) error {
setupDebug()
setupCpuPprof(cmd.Context())
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil)) // nolint:gosec // Debugging only
}()
if err := start(cmd.Context(), f.Namespace, f.Kubeconfig, f.DisableGitops, f.DisableBootstrap); err != nil {
return err
}
<-cmd.Context().Done()
return nil
}
func App() *cobra.Command {
cmd := command.Command(&FleetManager{}, cobra.Command{
Version: version.FriendlyVersion(),
})
return command.AddDebug(cmd, &debugConfig)
}
// setupDebug parses debug flags and configures the relevant log levels
func setupDebug() {
debugConfig.MustSetupDebug()
// if debug is enabled in controller, enable in agents too (unless otherwise specified)
propagateDebug, _ := strconv.ParseBool(os.Getenv("FLEET_PROPAGATE_DEBUG_SETTINGS_TO_AGENTS"))
if propagateDebug && debugConfig.Debug {
agent.DebugEnabled = true
agent.DebugLevel = debugConfig.DebugLevel
}
}
// setupCpuPprof starts a goroutine that captures a cpu pprof profile
// into FLEET_CPU_PPROF_DIR every FLEET_CPU_PPROF_PERIOD
func setupCpuPprof(ctx context.Context) {
if dir, ok := os.LookupEnv("FLEET_CPU_PPROF_DIR"); ok {
go func() {
var pprofCpuFile *os.File
period := durations.DefaultCpuPprofPeriod
if customPeriod, err := time.ParseDuration(os.Getenv("FLEET_CPU_PPROF_PERIOD")); err == nil {
period = customPeriod
}
wait.UntilWithContext(ctx, func(ctx context.Context) {
stopCpuPprof(pprofCpuFile)
pprofCpuFile = startCpuPprof(dir)
}, period)
}()
}
}
// stopCpuPprof concludes a cpu pprof capture, if any is ongoing
func stopCpuPprof(f *os.File) {
pprof.StopCPUProfile()
if f != nil {
err := f.Close()
if err != nil {
log.Println("could not close CPU profile file ", err)
}
}
}
// startCpuPprof starts a pprof cpu capture into a timestamp-prefixed file in dir
func startCpuPprof(dir string) *os.File {
name := time.Now().UTC().Format("2006-01-02_15_04_05") + ".pprof.fleetcontroller.samples.cpu.pb.gz"
f, err := os.Create(path.Join(dir, name))
if err != nil {
log.Println("could not create CPU profile: ", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Println("could not start CPU profile: ", err)
}
return f
}