-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (144 loc) · 3.55 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
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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
bmk "github.com/uppercaveman/k8s-gpu-device-plugin/benchmark"
"github.com/uppercaveman/k8s-gpu-device-plugin/config"
l "github.com/uppercaveman/k8s-gpu-device-plugin/modules/log"
"github.com/uppercaveman/k8s-gpu-device-plugin/modules/util"
"github.com/uppercaveman/k8s-gpu-device-plugin/plugin"
"github.com/uppercaveman/k8s-gpu-device-plugin/server"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.uber.org/zap"
)
func init() {
prometheus.MustRegister(collectors.NewBuildInfoCollector())
}
func main() {
pflag.String("configFile", "config", "name of config file (without extension)")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
// 默认配置
config.SetDefaultConfig()
viper.AddConfigPath(".")
viper.SetConfigName(viper.GetString("configFile"))
viper.SetConfigType("yml")
err := viper.ReadInConfig()
if err != nil {
log.Printf("fatal error config file: %s \n", err.Error())
}
cfg := new(config.Config)
err = viper.Unmarshal(cfg)
if err != nil {
log.Panic("fatal unmarshal config", err.Error())
return
}
// log
err = l.InitLogger(*cfg.Log, "k8s-gpu-device-plugin")
if err != nil {
log.Panic("init logger failed", err.Error())
return
}
l.Logger.Info("Starting k8s-gpu-device-plugin Server...")
// plugin manager Ready
pluginReady := &util.CloseOnce{
C: make(chan struct{}),
}
pluginReady.Close = func() {
pluginReady.Once.Do(func() {
close(pluginReady.C)
})
}
// plugin manager
pluginManager := plugin.NewPluginManager(cfg.MigStrategy, pluginReady)
// web server
webServer := server.New(cfg.WebListenAddress, pluginManager)
ctxWeb, cancelWeb := context.WithCancel(context.Background())
var g run.Group
{
// Termination handler.
term := make(chan os.Signal, 1)
signal.Notify(term, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
cancel := make(chan struct{})
g.Add(
func() error {
select {
case sig := <-term:
switch sig {
case syscall.SIGINT:
log.Println("messaged SIGINT, exiting gracefully...")
case syscall.SIGTERM:
log.Println("messaged SIGTERM, exiting gracefully...")
case syscall.SIGHUP:
log.Println("messaged SIGHUP, exiting gracefully...")
case syscall.SIGQUIT:
log.Println("messaged SIGQUIT, exiting gracefully...")
default:
log.Printf("messaged %s, exiting gracefully...", sig.String())
}
case <-cancel:
pluginReady.Close()
log.Println("canceled, exiting gracefully...")
}
return nil
},
func(err error) {
close(cancel)
},
)
}
{
// Plugin Manager.
g.Add(
func() error {
pluginManager.Start()
return nil
},
func(err error) {
pluginManager.Stop()
},
)
}
{
// Web Server.
g.Add(
func() error {
<-pluginReady.C
if err := webServer.Run(ctxWeb); err != nil {
return fmt.Errorf("error starting web server : %s", err)
}
return nil
},
func(err error) {
cancelWeb()
},
)
}
// Benchmark.
if cfg.Benchmark {
// benchmark
bench, err := bmk.NewBenchmark(l.Logger.With(zap.String("component", "benchmark")), "")
if err != nil {
log.Fatal("new benchmark err : ", err.Error())
os.Exit(1)
}
if err := bench.Run(); err != nil {
log.Fatal(err.Error())
os.Exit(1)
}
defer bench.Stop()
}
if err := g.Run(); err != nil {
log.Fatal(err.Error())
os.Exit(1)
}
log.Println("see you next time!")
}