-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (103 loc) · 3.84 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
//go:generate go run pkg/codegen/cleanup/main.go
//go:generate go run pkg/codegen/main.go
package main
import (
"context"
"flag"
"fmt"
"os"
"github.com/cnrancher/rancher-flat-network/pkg/admission"
"github.com/cnrancher/rancher-flat-network/pkg/controller/endpoints"
"github.com/cnrancher/rancher-flat-network/pkg/controller/endpointslice"
"github.com/cnrancher/rancher-flat-network/pkg/controller/flatnetworkip"
"github.com/cnrancher/rancher-flat-network/pkg/controller/flatnetworksubnet"
"github.com/cnrancher/rancher-flat-network/pkg/controller/ingress"
"github.com/cnrancher/rancher-flat-network/pkg/controller/namespace"
"github.com/cnrancher/rancher-flat-network/pkg/controller/pod"
"github.com/cnrancher/rancher-flat-network/pkg/controller/service"
"github.com/cnrancher/rancher-flat-network/pkg/controller/workload"
"github.com/cnrancher/rancher-flat-network/pkg/controller/wrangler"
"github.com/cnrancher/rancher-flat-network/pkg/logserver"
"github.com/cnrancher/rancher-flat-network/pkg/utils"
"github.com/rancher/wrangler/v3/pkg/kubeconfig"
"github.com/rancher/wrangler/v3/pkg/signals"
"github.com/sirupsen/logrus"
)
var (
masterURL string
kubeConfigFile string
port int
address string
cert string
key string
worker int
webhookServer bool
version bool
versionString string
debug bool
)
func init() {
if utils.GitCommit != "" {
versionString = fmt.Sprintf("%v - %v", utils.Version, utils.GitCommit)
} else {
versionString = utils.Version
}
}
func main() {
flag.StringVar(&kubeConfigFile, "kubeconfig", "", "Kube-config file (optional)")
flag.StringVar(&masterURL, "master", "", "The address of the Kubernetes API server (optional)")
flag.IntVar(&port, "port", 443, "Webhook server port")
flag.StringVar(&address, "bind-address", "0.0.0.0", "Webhook server bind address")
flag.StringVar(&cert, "tls-cert-file", "/etc/webhook/certs/tls.crt", "Webhook server TLS x509 certificate")
flag.StringVar(&key, "tls-private-key-file", "/etc/webhook/certs/tls.key", "Webhook server TLS x509 private key")
flag.BoolVar(&webhookServer, "webhook-server", true, "Enable the Admission Webhook server")
flag.BoolVar(&debug, "debug", false, "Enable debug log output")
flag.BoolVar(&version, "v", false, "Output version")
flag.IntVar(&worker, "worker", 5, "Worker number (1-50)")
flag.Parse()
utils.SetupLogrus(debug)
if worker > 50 || worker < 1 {
logrus.Warnf("invalid worker num: %v, should be 1-50, set to default: 5", worker)
worker = 5
}
if debug || os.Getenv("CATTLE_DEV_MODE") != "" {
logrus.SetLevel(logrus.DebugLevel)
logrus.Debugf("debug output enabled")
}
if version {
logrus.Infof("rancher-flat-network-operator %v", versionString)
return
}
logrus.Infof("starting rancher-flat-network operator %v", versionString)
ctx := signals.SetupSignalContext()
logserver.StartServerWithDefaults(ctx)
cfg, err := kubeconfig.GetNonInteractiveClientConfig(kubeConfigFile).ClientConfig()
if err != nil {
logrus.Fatalf("Error building kubeconfig: %v", err)
}
wctx := wrangler.NewContextOrDie(cfg)
wctx.WaitForCacheSyncOrDie(ctx)
// Register handlers
flatnetworkip.Register(ctx, wctx)
flatnetworksubnet.Register(ctx, wctx)
service.Register(ctx, wctx)
pod.Register(ctx, wctx)
ingress.Register(ctx, wctx)
workload.Register(ctx, wctx)
endpoints.Register(ctx, wctx)
endpointslice.Register(ctx, wctx)
namespace.Register(ctx, wctx)
wctx.OnLeader(func(ctx context.Context) error {
logrus.Infof("pod [%v] is leader, starting handlers", utils.Hostname())
// Start controller when this pod becomes leader.
return wctx.StartHandler(ctx, worker)
})
wctx.Run(ctx)
if webhookServer {
webhook := admission.NewAdmissionWebhookServer(address, port, cert, key, wctx)
if err := webhook.Run(ctx); err != nil {
logrus.Fatalf("failed to start webhook: %v", err)
}
}
select {}
}