-
Notifications
You must be signed in to change notification settings - Fork 157
/
seed_controller_lifecycle.go
295 lines (249 loc) · 9.15 KB
/
seed_controller_lifecycle.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
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 seedcontrollerlifecycle
import (
"context"
"fmt"
"reflect"
"go.uber.org/zap"
controllerutil "github.com/kubermatic/kubermatic/api/pkg/controller/util"
predicateutil "github.com/kubermatic/kubermatic/api/pkg/controller/util/predicate"
kubermaticv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
"github.com/kubermatic/kubermatic/api/pkg/provider"
"github.com/prometheus/client_golang/prometheus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/cache"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const (
ControllerName = "seedcontroller_lifecycle_manager"
// We must only enqueue this one key
queueKey = ControllerName
)
var (
seedKubeconfigRetrievalSuccessMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "kubermatic",
Subsystem: "master_controller_manager",
Name: "seed_kubeconfig_retrieval_success",
Help: "Indicates if retrieving the kubeconfig for the given seed was successful",
}, []string{"seed"})
)
func init() {
prometheus.MustRegister(seedKubeconfigRetrievalSuccessMetric)
}
type Reconciler struct {
ctx context.Context
masterKubeCfg *rest.Config
masterClient ctrlruntimeclient.Client
masterCache cache.Cache
log *zap.SugaredLogger
seedsGetter provider.SeedsGetter
seedKubeconfigGetter provider.SeedKubeconfigGetter
controllerFactories []ControllerFactory
enqueue func()
activeManager *managerInstance
}
// managerInstance represents an instance of controllerManager
type managerInstance struct {
config map[string]rest.Config
mgr manager.Manager
running bool
stopChan chan struct{}
cancelCtx context.CancelFunc
}
// ControllerFactory is a function to create a new controller instance
type ControllerFactory func(context.Context, manager.Manager, map[string]manager.Manager) (controllerName string, err error)
func Add(
ctx context.Context,
log *zap.SugaredLogger,
mgr manager.Manager,
namespace string,
seedsGetter provider.SeedsGetter,
seedKubeconfigGetter provider.SeedKubeconfigGetter,
controllerFactories ...ControllerFactory,
) error {
// prepare a shared cache across all future master managers; this cache
// will be wrapped so that ctrlruntime cannot start and stop it, which
// would cause it to close the same channels multiple times and panic
cache := mgr.GetCache()
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")
}
reconciler := &Reconciler{
ctx: ctx,
masterKubeCfg: mgr.GetConfig(),
masterClient: mgr.GetClient(),
masterCache: &unstartableCache{cache},
log: log.Named(ControllerName),
seedsGetter: seedsGetter,
seedKubeconfigGetter: seedKubeconfigGetter,
controllerFactories: controllerFactories,
}
c, err := controller.New(ControllerName, mgr,
controller.Options{Reconciler: reconciler, MaxConcurrentReconciles: 1})
if err != nil {
return fmt.Errorf("failed to construct controller: %v", err)
}
for _, t := range []runtime.Object{&kubermaticv1.Seed{}, &corev1.Secret{}} {
if err := c.Watch(
&source.Kind{Type: t},
controllerutil.EnqueueConst(queueKey),
predicateutil.ByNamespace(namespace),
); err != nil {
return fmt.Errorf("failed to create watch for type %T: %v", t, err)
}
}
sourceChannel := make(chan event.GenericEvent)
reconciler.enqueue = func() {
sourceChannel <- event.GenericEvent{
// TODO: Is it needed to fill this?
Object: &kubermaticv1.Seed{},
}
}
if err := c.Watch(&source.Channel{Source: sourceChannel}, controllerutil.EnqueueConst(queueKey)); err != nil {
return fmt.Errorf("failed to create watch for channelSource: %v", err)
}
return nil
}
func (r *Reconciler) Reconcile(_ reconcile.Request) (reconcile.Result, error) {
err := r.reconcile()
if err != nil {
r.log.Errorw("reconiliation failed", zap.Error(err))
}
return reconcile.Result{}, err
}
func (r *Reconciler) reconcile() error {
seeds, err := r.seedsGetter()
if err != nil {
return fmt.Errorf("failed to get seeds: %v", err)
}
seedKubeconfigMap := map[string]rest.Config{}
for _, seed := range seeds {
cfg, err := r.seedKubeconfigGetter(seed)
if err != nil {
// Don't let a single broken kubeconfig break everything.
r.log.Errorw("failed to get kubeconfig", "seed", seed.Name, zap.Error(err))
seedKubeconfigRetrievalSuccessMetric.WithLabelValues(seed.Name).Set(0)
continue
}
seedKubeconfigMap[seed.Name] = *cfg
seedKubeconfigRetrievalSuccessMetric.WithLabelValues(seed.Name).Set(1)
}
// delete label combinations for non-existing seeds
if r.activeManager != nil {
removedSeeds := sets.StringKeySet(r.activeManager.config).Difference(sets.StringKeySet(seedKubeconfigMap))
for _, seedName := range removedSeeds.List() {
seedKubeconfigRetrievalSuccessMetric.DeleteLabelValues(seedName)
}
}
if r.activeManager != nil && r.activeManager.running && reflect.DeepEqual(r.activeManager.config, seedKubeconfigMap) {
r.log.Debug("All controllers running and config up-to-date, nothing to do.")
return nil
}
// We let a master controller manager run the controllers for us.
mgr, err := manager.New(r.masterKubeCfg, manager.Options{
LeaderElection: false,
MetricsBindAddress: "0",
// Avoid duplicating caches or client for master cluster, as it's static.
NewCache: func(_ *rest.Config, _ cache.Options) (cache.Cache, error) {
return r.masterCache, nil
},
NewClient: func(_ cache.Cache, _ *rest.Config, _ ctrlruntimeclient.Options) (ctrlruntimeclient.Client, error) {
return r.masterClient, nil
},
})
if err != nil {
return fmt.Errorf("failed to create master controller manager: %v", err)
}
// create one manager per seed, so that all controllers share the same caches
seedManagers, err := r.createSeedManagers(mgr, seeds, seedKubeconfigMap)
if err != nil {
return fmt.Errorf("failed to create managers for all seeds: %v", err)
}
ctrlCtx, cancelCtrlCtx := context.WithCancel(r.ctx)
for _, factory := range r.controllerFactories {
controllerName, err := factory(ctrlCtx, mgr, seedManagers)
if err != nil {
cancelCtrlCtx()
return fmt.Errorf("failed to construct controller %s: %v", controllerName, err)
}
}
if r.activeManager != nil {
r.log.Info("Stopping old instance of controller manager")
r.activeManager.cancelCtx() // Just in case any controller ever actually makes use of their context
close(r.activeManager.stopChan)
}
mi := &managerInstance{
config: seedKubeconfigMap,
mgr: mgr,
stopChan: make(chan struct{}),
cancelCtx: cancelCtrlCtx,
}
go func() {
r.log.Info("Starting controller manager")
mi.running = true
if err := mi.mgr.Start(mi.stopChan); err != nil {
r.log.Errorw("Controller manager stopped with error", zap.Error(err))
}
mi.running = false
r.log.Info("Controller manager stopped")
// Make sure we check on this
r.enqueue()
}()
r.activeManager = mi
return nil
}
func (r *Reconciler) createSeedManagers(masterMgr manager.Manager, seeds map[string]*kubermaticv1.Seed, kubeconfigs map[string]rest.Config) (map[string]manager.Manager, error) {
seedManagers := make(map[string]manager.Manager, len(seeds))
for seedName, seed := range seeds {
kubeconfig, exists := kubeconfigs[seedName]
if !exists {
continue // we already warned earlier about the inability to retrieve the kubeconfig
}
log := r.log.With("seed", seed.Name)
seedMgr, err := manager.New(&kubeconfig, manager.Options{MetricsBindAddress: "0"})
if err != nil {
log.Errorw("Failed to construct manager for seed", zap.Error(err))
continue
}
seedManagers[seedName] = seedMgr
if err := masterMgr.Add(seedMgr); err != nil {
return nil, fmt.Errorf("failed to add controller manager for seed %q to master manager: %v", seedName, err)
}
}
return seedManagers, nil
}
// unstartableCache is used to prevent the ctrlruntime manager from starting the
// cache *again*, just after we started and initialized it.
type unstartableCache struct {
cache.Cache
}
func (m *unstartableCache) Start(_ <-chan struct{}) error {
return nil
}
func (m *unstartableCache) WaitForCacheSync(_ <-chan struct{}) bool {
return true
}