forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
274 lines (233 loc) · 9.17 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"errors"
"flag"
"fmt"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"github.com/kubermatic/kubermatic/api/pkg/leaderelection"
kubermaticlog "github.com/kubermatic/kubermatic/api/pkg/log"
"github.com/kubermatic/kubermatic/api/pkg/metrics"
metricserver "github.com/kubermatic/kubermatic/api/pkg/metrics/server"
"github.com/kubermatic/kubermatic/api/pkg/pprof"
"github.com/kubermatic/kubermatic/api/pkg/provider"
"github.com/kubermatic/kubermatic/api/pkg/signals"
"github.com/kubermatic/kubermatic/api/pkg/util/workerlabel"
seedvalidation "github.com/kubermatic/kubermatic/api/pkg/validation/seed"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/rest"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
ctrlruntimecache "sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/manager"
ctrlruntimelog "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)
const (
controllerName = "kubermatic-master-controller-manager"
)
type controllerRunOptions struct {
kubeconfig string
masterURL string
internalAddr string
seedvalidationHook seedvalidation.WebhookOpts
workerName string
}
type controllerContext struct {
ctx context.Context
mgr manager.Manager
log *zap.SugaredLogger
workerCount int
workerName string
workerNameLabelSelector labels.Selector
seedsGetter provider.SeedsGetter
seedKubeconfigGetter provider.SeedKubeconfigGetter
labelSelectorFunc func(*metav1.ListOptions)
namespace string
}
func main() {
var g run.Group
ctrlCtx := &controllerContext{}
runOpts := controllerRunOptions{}
klog.InitFlags(nil)
pprofOpts := &pprof.Opts{}
pprofOpts.AddFlags(flag.CommandLine)
logOpts := kubermaticlog.NewDefaultOptions()
logOpts.AddFlags(flag.CommandLine)
runOpts.seedvalidationHook.AddFlags(flag.CommandLine)
flag.StringVar(&runOpts.kubeconfig, "kubeconfig", "", "Path to a kubeconfig.")
flag.StringVar(&runOpts.masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
flag.StringVar(&runOpts.workerName, "worker-name", "", "The name of the worker that will only processes resources with label=worker-name.")
flag.IntVar(&ctrlCtx.workerCount, "worker-count", 4, "Number of workers which process the clusters in parallel.")
flag.StringVar(&runOpts.internalAddr, "internal-address", "127.0.0.1:8085", "The address on which the /metrics endpoint will be served.")
flag.StringVar(&ctrlCtx.namespace, "namespace", "kubermatic", "The namespace kubermatic runs in, uses to determine where to look for datacenter custom resources.")
addFlags(flag.CommandLine)
flag.Parse()
ctrlruntimelog.SetLogger(ctrlruntimelog.ZapLogger(false))
rawLog := kubermaticlog.New(logOpts.Debug, logOpts.Format)
log := rawLog.Sugar()
defer func() {
if err := log.Sync(); err != nil {
fmt.Println(err)
}
}()
kubermaticlog.Logger = log
ctrlCtx.log = log
ctrlCtx.workerName = runOpts.workerName
selector, err := workerlabel.LabelSelector(runOpts.workerName)
if err != nil {
log.Fatalw("failed to create the label selector for the given worker", "workerName", runOpts.workerName, zap.Error(err))
}
ctrlCtx.workerNameLabelSelector = selector
// register the global error metric. Ensures that runtime.HandleError() increases the error metric
metrics.RegisterRuntimErrorMetricCounter("kubermatic_master_controller_manager", prometheus.DefaultRegisterer)
// register an operating system signals and context on which we will gracefully close the app
stopCh := signals.SetupSignalHandler()
// prepare a context to use throughout the controller manager
ctx, ctxCancel := context.WithCancel(context.Background())
defer ctxCancel()
ctrlCtx.ctx = ctx
// load kubeconfig and create API client; kubeconfig can be empty if no Seed CRD migrations need to run
var cfg *restclient.Config
if runOpts.kubeconfig == "" {
cfg, err = clientcmd.BuildConfigFromFlags("", "")
if err != nil {
log.Fatalw("failed to load in-cluster credentials", zap.Error(err))
}
} else {
kubeconfig, err := clientcmd.LoadFromFile(runOpts.kubeconfig)
if err != nil {
log.Fatalw("failed to read the kubeconfig", zap.Error(err))
}
config := clientcmd.NewNonInteractiveClientConfig(
*kubeconfig,
kubeconfig.CurrentContext,
&clientcmd.ConfigOverrides{CurrentContext: kubeconfig.CurrentContext},
nil,
)
cfg, err = config.ClientConfig()
if err != nil {
log.Fatalw("failed to create client", zap.Error(err))
}
}
ctrlCtx.labelSelectorFunc = func(listOpts *metav1.ListOptions) {
listOpts.LabelSelector = selector.String()
}
// create a new cache that we can start independently of the manager, so that other
// components work even if the manager has not yet been started
cache, err := ctrlruntimecache.New(cfg, ctrlruntimecache.Options{})
if err != nil {
log.Fatalw("failed to create cache", zap.Error(err))
}
go func() {
if err := cache.Start(ctx.Done()); err != nil {
log.Fatalw("failed to start cache", zap.Error(err))
}
}()
if !cache.WaitForCacheSync(ctx.Done()) {
log.Fatal("failed to wait for caches to synchronize")
}
mgr, err := manager.New(cfg, manager.Options{
MetricsBindAddress: "",
// inject the handcrafted cache into the manager
NewCache: func(config *rest.Config, opts ctrlruntimecache.Options) (ctrlruntimecache.Cache, error) {
return &unstartableCache{cache}, nil
},
})
if err != nil {
log.Fatalw("failed to create Controller Manager instance", zap.Error(err))
}
ctrlCtx.mgr = mgr
if err := mgr.Add(pprofOpts); err != nil {
log.Fatalw("Failed to add pprof endpoint", zap.Error(err))
}
// these two getters rely on the ctrlruntime manager being started; they are
// only used inside controllers
ctrlCtx.seedsGetter, err = seedsGetterFactory(ctx, mgr.GetClient(), ctrlCtx.namespace)
if err != nil {
log.Fatalw("failed to construct seedsGetter", zap.Error(err))
}
ctrlCtx.seedKubeconfigGetter, err = seedKubeconfigGetterFactory(ctx, mgr.GetClient(), runOpts)
if err != nil {
log.Fatalw("failed to construct seedKubeconfigGetter", zap.Error(err))
}
if runOpts.seedvalidationHook.CertFile != "" || runOpts.seedvalidationHook.KeyFile != "" {
if err := setupSeedValidationWebhook(ctx, mgr, log, runOpts, ctrlCtx); err != nil {
log.Fatalw("failed to start seed validation webhook", zap.Error(err))
}
} else {
log.Info("the validatingAdmissionWebhook server can not be started because seed-admissionwebhook-cert-file and seed-admissionwebhook-key-file are empty")
}
if err := createAllControllers(ctrlCtx); err != nil {
log.Fatalw("could not create all controllers", zap.Error(err))
}
if err := mgr.Add(metricserver.New(runOpts.internalAddr)); err != nil {
log.Fatalw("failed to add metrics server", zap.Error(err))
}
// This group is forever waiting in a goroutine for signals to stop
{
g.Add(func() error {
select {
case <-stopCh:
return errors.New("a user has requested to stop the controller")
case <-ctx.Done():
return errors.New("parent context has been closed - propagating the request")
}
}, func(err error) {
ctxCancel()
})
}
// This group is running the actual controller logic
{
// This group is running the actual controller logic
leaderCtx, stopLeaderElection := context.WithCancel(ctx)
defer stopLeaderElection()
g.Add(func() error {
electionName := controllerName + "-leader-election"
if runOpts.workerName != "" {
electionName += "-" + runOpts.workerName
}
return leaderelection.RunAsLeader(leaderCtx, log, cfg, mgr.GetEventRecorderFor(controllerName), electionName, func(ctx context.Context) error {
if err := runMigrations(ctx, mgr.GetClient(), log, runOpts, ctrlCtx); err != nil {
return err
}
log.Info("starting the master-controller-manager...")
if err := mgr.Start(ctx.Done()); err != nil {
return fmt.Errorf("the controller-manager stopped with an error: %v", err)
}
return nil
})
}, func(err error) {
stopLeaderElection()
})
}
if err := g.Run(); err != nil {
log.Fatalw("cannot start the master-controller-manager", zap.Error(err))
}
}
// unstartableCache is used to prevent the ctrlruntime manager from starting the
// cache *again*, just after we started and initialized it.
type unstartableCache struct {
ctrlruntimecache.Cache
}
func (m *unstartableCache) Start(_ <-chan struct{}) error {
return nil
}
func (m *unstartableCache) WaitForCacheSync(_ <-chan struct{}) bool {
return true
}