-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
110 lines (95 loc) · 2.78 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
package main
import (
"context"
"errors"
"fmt"
"github.com/jd-opensource/joylive-injector/pkg/log"
"go.uber.org/zap"
"net/http"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"github.com/jd-opensource/joylive-injector/pkg/admission"
_ "github.com/jd-opensource/joylive-injector/pkg/mutation"
"github.com/jd-opensource/joylive-injector/pkg/config"
"github.com/jd-opensource/joylive-injector/pkg/route"
"github.com/spf13/cobra"
)
var (
buildDate string
buildCommit string
versionTpl = `
Name: joylive-injector
Arch: %s
BuildDate: %s
BuildCommit: %s
`
)
var rootCmd = &cobra.Command{
Use: "joylive-injector",
Short: "JoyLive dynamic admission control tool",
Version: buildCommit,
Run: func(cmd *cobra.Command, args []string) {
log.InitLog()
admission.Setup()
route.Setup()
srv := &http.Server{
Handler: route.Router(),
Addr: config.Addr,
}
sigs := make(chan os.Signal)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
var shutdownOnce sync.Once
for range sigs {
log.Warn("Receiving the termination signal, graceful shutdown...")
shutdownOnce.Do(func() {
err := srv.Shutdown(context.Background())
if err != nil {
log.Error("Error:", zap.Error(err))
}
})
}
}()
if config.Cert != "" && config.Key != "" {
log.Infof("Listen TLS Server at %s", config.Addr)
err := srv.ListenAndServeTLS(config.Cert, config.Key)
if err != nil {
if errors.Is(err, http.ErrServerClosed) {
log.Info("server shutdown success.")
} else {
log.Error("Error:", zap.Error(err))
}
}
} else {
log.Infof("Listen HTTP Server at %s", config.Addr)
err := srv.ListenAndServe()
if err != nil {
if errors.Is(err, http.ErrServerClosed) {
log.Info("server shutdown success.")
} else {
log.Error("Error:", zap.Error(err))
}
}
}
},
}
func init() {
// version template
rootCmd.SetVersionTemplate(fmt.Sprintf(versionTpl, runtime.GOOS+"/"+runtime.GOARCH, buildDate, buildCommit))
// webhook
rootCmd.PersistentFlags().StringVar(&config.ConfigMountSubPath, "config-mount-sub-path", "config", "Config file mount sub path")
rootCmd.PersistentFlags().StringVarP(&config.Addr, "listen", "l", ":443", "Admission Controller listen address")
rootCmd.PersistentFlags().StringVar(&config.Cert, "cert", "", "Admission Controller TLS cert")
rootCmd.PersistentFlags().StringVar(&config.Key, "key", "", "Admission Controller TLS cert key")
rootCmd.PersistentFlags().StringVar(&config.MatchLabel, "match-label", "x-live", "Match label")
// admission config
rootCmd.PersistentFlags().StringVar(&config.InitContainerName, "init-container-name", "joylive-init-container", "Init container name")
}
func main() {
if err := rootCmd.Execute(); err != nil {
log.Fatal("Start error.", zap.Error(err))
}
}