-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
70 lines (57 loc) · 1.44 KB
/
main.go
File metadata and controls
70 lines (57 loc) · 1.44 KB
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
package main
import (
"context"
"database/sql"
"flag"
"log"
"os"
"os/signal"
"syscall"
"time"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
_ "github.com/go-sql-driver/mysql"
)
var (
masterURL string
kubeconfig string
)
func main() {
var kubeconfig string
flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
var masterURL string
flag.StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
var dsn string
flag.StringVar(&dsn, "dsn", "", "The DSN used to connect to MySQL")
flag.Parse()
cfg, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfig)
if err != nil {
log.Fatalf("Error building Kubernetes config: %v", err)
}
client, err := rest.HTTPClientFor(cfg)
if err != nil {
log.Fatalf("Error building Kubernetes client: %v", err)
}
db, err := sql.Open("mysql", dsn)
if err == nil {
db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(1)
db.SetMaxIdleConns(1)
err = db.Ping()
}
if err != nil {
log.Fatalf("Error connecting to MySQL: %v", err)
}
c := make(chan os.Signal, 2)
ctx, cancel := context.WithCancel(context.Background())
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
// Cancel on first signal.
<-c
cancel()
// Force quit on second signal.
<-c
os.Exit(1)
}()
NewController(cfg, client, db).Run(ctx)
}