-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (68 loc) · 1.95 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
package main
import (
"flag"
"os"
"os/signal"
"strconv"
"syscall"
"github.com/sirupsen/logrus"
"github.com/verloop/nsync/controller"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
var kubeconfig string
var tickInterval uint
flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
flag.UintVar(&tickInterval, "tickinterval", 30, "time in seconds after which we should force a resync")
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
logrus.WithError(err).Fatalln("failed to build config from flag")
}
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
logrus.WithError(err).Fatalln("clientset error")
}
nc := controller.NewNamespaceController(clientset, tickInterval)
logrus.Info("Starting server")
err = nc.Start()
if err != nil {
logrus.WithError(err).Fatalln("start error")
}
handleSigTerm(nc)
logrus.Info("Stopped server")
}
func handleSigTerm(namespaceController *controller.NamespaceController) {
deathRay := make(chan os.Signal, 2)
signal.Notify(deathRay, syscall.SIGINT, syscall.SIGTERM)
<-deathRay
logrus.Info("shutting down")
exitCode := 0
if err := namespaceController.Stop(); err != nil {
logrus.WithError(err).Info("error during shutdown")
exitCode = 1
}
logrus.WithField("exit_code", exitCode).Info("exiting")
os.Exit(exitCode)
}
func init() {
// Log as JSON instead of the default ASCII formatter.
logrus.SetFormatter(&logrus.JSONFormatter{
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "timestamp",
logrus.FieldKeyLevel: "severity",
logrus.FieldKeyMsg: "message",
},
})
// Output to stdout instead of the default stderr
logrus.SetOutput(os.Stdout)
debug, _ := strconv.ParseBool(os.Getenv("DEBUG"))
if debug {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
}