-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (75 loc) · 1.82 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime/pprof"
"strconv"
"strings"
"net/http"
_ "net/http/pprof"
"github.com/urandom/kd/ext"
"github.com/urandom/kd/k8s"
"github.com/urandom/kd/ui"
"github.com/urandom/kd/ui/presenter"
"k8s.io/klog"
)
var (
configF = flag.String("kubeconfig", "", "path to kubeconfig file")
cpuProfileF = flag.String("cpuprofile", "", "write cpu profile to file")
cpuPortF = flag.Int("cpuport", 0, "serve cpu profile on port")
extensionPathF = flag.String("extensions", "", "path(s) to extensions directory. Comma separated")
)
func main() {
klog.InitFlags(flag.CommandLine)
flag.Parse()
if *cpuProfileF != "" {
f, err := os.Create(*cpuProfileF)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if *cpuPortF != 0 {
go func() {
log.Println(http.ListenAndServe("localhost:"+strconv.Itoa(*cpuPortF), nil))
}()
}
if err := setupLogging(); err != nil {
log.Fatal(err)
}
loader, err := ext.NewLoader(strings.Split(*extensionPathF, ",")...)
if err != nil {
log.Fatal(err)
}
manager := ext.NewManager(loader)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
p := presenter.NewMain(ui.New(), manager, func() (*k8s.Client, error) { return k8s.New(ctx, *configF) })
if err := p.Run(); err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
func configPath() string {
if *configF == "" {
config := os.Getenv("KUBECONFIG")
if config == "" {
return filepath.Join(os.Getenv("HOME"), ".kube", "config")
}
return config
}
return *configF
}
func setupLogging() error {
f, err := os.OpenFile("/tmp/kd.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return fmt.Errorf("opening log file: %w", err)
}
log.SetOutput(f)
return nil
}