forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (120 loc) · 5.28 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
/*
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"
"flag"
"fmt"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"github.com/kubermatic/kubermatic/api/pkg/controller/operator/common"
masterctrl "github.com/kubermatic/kubermatic/api/pkg/controller/operator/master"
seedctrl "github.com/kubermatic/kubermatic/api/pkg/controller/operator/seed"
seedcontrollerlifecycle "github.com/kubermatic/kubermatic/api/pkg/controller/shared/seed-controller-lifecycle"
operatorv1alpha1 "github.com/kubermatic/kubermatic/api/pkg/crd/operator/v1alpha1"
kubermaticlog "github.com/kubermatic/kubermatic/api/pkg/log"
"github.com/kubermatic/kubermatic/api/pkg/pprof"
"github.com/kubermatic/kubermatic/api/pkg/provider"
"github.com/kubermatic/kubermatic/api/pkg/signals"
certmanagerv1alpha2 "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha2"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/manager"
ctrllog "sigs.k8s.io/controller-runtime/pkg/runtime/log"
// Do not import "sigs.k8s.io/controller-runtime/pkg" to prevent
// duplicate kubeconfig flags being defined.
)
type controllerRunOptions struct {
kubeconfig string
namespace string
internalAddr string
workerCount int
workerName string
}
func main() {
ctx := context.Background()
klog.InitFlags(nil)
pprofOpts := &pprof.Opts{}
pprofOpts.AddFlags(flag.CommandLine)
logOpts := kubermaticlog.NewDefaultOptions()
logOpts.AddFlags(flag.CommandLine)
opt := &controllerRunOptions{}
flag.StringVar(&opt.kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if outside of cluster.")
flag.StringVar(&opt.namespace, "namespace", "", "The namespace the operator runs in, uses to determine where to look for KubermaticConfigurations.")
flag.IntVar(&opt.workerCount, "worker-count", 4, "Number of workers which process reconcilings in parallel.")
flag.StringVar(&opt.internalAddr, "internal-address", "127.0.0.1:8085", "The address on which the /metrics endpoint will be served")
flag.StringVar(&opt.workerName, "worker-name", "", "The name of the worker that will only processes resources with label=worker-name.")
flag.Parse()
rawLog := kubermaticlog.New(logOpts.Debug, logOpts.Format).Named(opt.workerName)
log := rawLog.Sugar()
defer func() {
if err := log.Sync(); err != nil {
fmt.Println(err)
}
}()
// update global logger instance
kubermaticlog.Logger = log
// set the logger used by sigs.k8s.io/controller-runtime
ctrllog.SetLogger(zapr.NewLogger(rawLog.WithOptions(zap.AddCallerSkip(1))))
if len(opt.namespace) == 0 {
log.Fatal("-namespace is a mandatory flag")
}
v := common.NewDefaultVersions()
log.With("kubermatic", v.Kubermatic, "ui", v.UI).Infof("Moin, moin, I'm the Kubermatic %s Operator and these are the versions I work with.", v.KubermaticEdition)
config, err := clientcmd.BuildConfigFromFlags("", opt.kubeconfig)
if err != nil {
log.Fatalw("Failed to build config", zap.Error(err))
}
mgr, err := manager.New(config, manager.Options{MetricsBindAddress: opt.internalAddr})
if err != nil {
log.Fatalw("Failed to create Controller Manager instance: %v", err)
}
if err := mgr.Add(pprofOpts); err != nil {
log.Fatalw("Failed to add pprof endpoint", zap.Error(err))
}
if err := operatorv1alpha1.AddToScheme(mgr.GetScheme()); err != nil {
log.Fatalw("Failed to register scheme", zap.Stringer("api", operatorv1alpha1.SchemeGroupVersion), zap.Error(err))
}
if err := certmanagerv1alpha2.AddToScheme(mgr.GetScheme()); err != nil {
log.Fatalw("Failed to register scheme", zap.Stringer("api", certmanagerv1alpha2.SchemeGroupVersion), zap.Error(err))
}
seedsGetter, err := seedsGetterFactory(ctx, mgr.GetClient(), opt)
if err != nil {
log.Fatalw("Failed to construct seedsGetter", zap.Error(err))
}
seedKubeconfigGetter, err := provider.SeedKubeconfigGetterFactory(ctx, mgr.GetClient())
if err != nil {
log.Fatalw("Failed to construct seedKubeconfigGetter", zap.Error(err))
}
if err := masterctrl.Add(ctx, mgr, log, opt.namespace, opt.workerCount, opt.workerName); err != nil {
log.Fatalw("Failed to add operator-master controller", zap.Error(err))
}
seedOperatorControllerFactory := func(ctx context.Context, mgr manager.Manager, seedManagerMap map[string]manager.Manager) (string, error) {
return seedctrl.ControllerName, seedctrl.Add(
ctx,
log,
opt.namespace,
mgr,
seedManagerMap,
seedsGetter,
opt.workerCount,
opt.workerName,
)
}
if err := seedcontrollerlifecycle.Add(ctx, log, mgr, opt.namespace, seedsGetter, seedKubeconfigGetter, seedOperatorControllerFactory); err != nil {
log.Fatalw("Failed to create seed-lifecycle controller", zap.Error(err))
}
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
log.Fatalw("Cannot start manager", zap.Error(err))
}
}