-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
147 lines (127 loc) · 3.61 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
package main
import (
"flag"
"time"
"github.com/SAP/stewardci-core/pkg/k8s"
"github.com/SAP/stewardci-core/pkg/metrics"
"github.com/SAP/stewardci-core/pkg/signals"
tenantctl "github.com/SAP/stewardci-core/pkg/tenantctl"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
klog "k8s.io/klog/v2"
"knative.dev/pkg/system"
)
const (
// resyncPeriod is the period between full resyncs performed
// by the controller.
resyncPeriod = 1 * time.Minute
// metricsPort is the TCP port number to be used by the metrics
// HTTP server.
metricsPort = 9090
)
var (
kubeconfig string
burst, qps, threadiness int
heartbeatInterval time.Duration
heartbeatLogging bool
heartbeatLogLevel int
k8sAPIRequestTimeout time.Duration
)
func init() {
klog.InitFlags(nil)
flag.StringVar(
&kubeconfig,
"kubeconfig",
"",
"The path to a kubeconfig file configuring access to the Kubernetes cluster."+
" If not specified or empty, assume running in-cluster.",
)
flag.IntVar(
&qps,
"qps",
5,
"The queries per seconds (QPS) for Kubernetes API client-side rate limiting.",
)
flag.IntVar(
&burst,
"burst",
10,
"The size of the burst bucket for Kubernetes API client-side rate limiting.",
)
flag.IntVar(
&threadiness,
"threadiness",
2,
"The maximum number of reconciliations performed by the controller in parallel.",
)
flag.DurationVar(
&heartbeatInterval,
"heartbeat-interval",
1*time.Minute,
"The interval of controller heartbeats.",
)
flag.BoolVar(
&heartbeatLogging,
"heartbeat-logging",
true,
"Whether controller heartbeats should be logged.",
)
flag.IntVar(
&heartbeatLogLevel,
"heartbeat-log-level",
3,
"The log level to be used for controller heartbeats.",
)
flag.DurationVar(
&k8sAPIRequestTimeout,
"k8s-api-request-timeout",
15*time.Minute,
"The maximum length of time to wait before giving up on a server request. A value of zero means no timeout.",
)
flag.Parse()
}
func main() {
defer klog.Flush()
system.Namespace() // ensure that namespace is set in environment
var config *rest.Config
var err error
if kubeconfig == "" {
klog.Infof("In cluster")
config, err = rest.InClusterConfig()
if err != nil {
klog.Exitf("failed to load kubeconfig: %s; Hint: You can use parameter '-kubeconfig' for local testing", err.Error())
}
} else {
klog.Infof("Outside cluster")
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
klog.Exitln(err.Error())
}
}
klog.V(3).Infof("Create Factory (resync period: %s, QPS: %d, burst: %d, k8s-api-request-timeout: %s)", resyncPeriod.String(), qps, burst, k8sAPIRequestTimeout.String())
config.QPS = float32(qps)
config.Burst = burst
config.Timeout = k8sAPIRequestTimeout
factory := k8s.NewClientFactory(config, resyncPeriod)
klog.V(2).Infof("Provide metrics on http://0.0.0.0:%d/metrics", metricsPort)
metrics.StartServer(metricsPort)
klog.V(3).Infof("Create Controller")
controllerOpts := tenantctl.ControllerOpts{
HeartbeatInterval: heartbeatInterval,
}
if heartbeatLogging {
tmp := klog.Level(heartbeatLogLevel)
controllerOpts.HeartbeatLogLevel = &tmp
}
controller := tenantctl.NewController(factory, controllerOpts)
klog.V(3).Infof("Create Signal Handlers")
stopCh := signals.SetupShutdownSignalHandler()
signals.SetupThreadDumpSignalHandler()
klog.V(2).Infof("Start Informer")
factory.StewardInformerFactory().Start(stopCh)
klog.V(2).Infof("Run controller (threadiness=%d)", threadiness)
if err = controller.Run(threadiness, stopCh); err != nil {
klog.Fatalf("Error running controller: %s", err.Error())
}
}