/
controller.go
226 lines (189 loc) · 7.04 KB
/
controller.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
/*
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 seed
import (
"context"
"fmt"
"go.uber.org/zap"
"github.com/kubermatic/kubermatic/api/pkg/controller/operator/common"
"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"
operatorv1alpha1 "github.com/kubermatic/kubermatic/api/pkg/crd/operator/v1alpha1"
"github.com/kubermatic/kubermatic/api/pkg/provider"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/record"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const (
// ControllerName is the name of this very controller.
ControllerName = "kubermatic-seed-operator"
)
func Add(
ctx context.Context,
log *zap.SugaredLogger,
namespace string,
masterManager manager.Manager,
seedManagers map[string]manager.Manager,
seedsGetter provider.SeedsGetter,
numWorkers int,
workerName string,
) error {
namespacePredicate := predicateutil.ByNamespace(namespace)
reconciler := &Reconciler{
ctx: ctx,
log: log.Named(ControllerName),
scheme: masterManager.GetScheme(),
namespace: namespace,
masterClient: masterManager.GetClient(),
masterRecorder: masterManager.GetEventRecorderFor(ControllerName),
seedClients: map[string]ctrlruntimeclient.Client{},
seedRecorders: map[string]record.EventRecorder{},
seedsGetter: seedsGetter,
workerName: workerName,
versions: common.NewDefaultVersions(),
}
ctrlOpts := controller.Options{
Reconciler: reconciler,
MaxConcurrentReconciles: numWorkers,
}
c, err := controller.New(ControllerName, masterManager, ctrlOpts)
if err != nil {
return fmt.Errorf("failed to construct controller: %v", err)
}
// watch for changes to KubermaticConfigurations in the master cluster and reconcile all seeds
configEventHandler := newEventHandler(func(_ handler.MapObject) []reconcile.Request {
seeds, err := seedsGetter()
if err != nil {
log.Errorw("Failed to handle request", zap.Error(err))
utilruntime.HandleError(err)
return nil
}
requests := []reconcile.Request{}
for _, seed := range seeds {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: seed.Name,
},
})
}
return requests
})
config := &operatorv1alpha1.KubermaticConfiguration{}
if err := c.Watch(&source.Kind{Type: config}, configEventHandler, namespacePredicate); err != nil {
return fmt.Errorf("failed to create watcher for %T: %v", config, err)
}
// watch for changes to Seed CRs inside the master cluster and reconcile the seed itself only
seed := &kubermaticv1.Seed{}
if err := c.Watch(&source.Kind{Type: seed}, &handler.EnqueueRequestForObject{}, namespacePredicate); err != nil {
return fmt.Errorf("failed to create watcher for %T: %v", seed, err)
}
// watch all resources we manage inside all configured seeds
for key, manager := range seedManagers {
reconciler.seedClients[key] = manager.GetClient()
reconciler.seedRecorders[key] = manager.GetEventRecorderFor(ControllerName)
if err := createSeedWatches(c, key, manager, namespace); err != nil {
return fmt.Errorf("failed to setup watches for seed %s: %v", key, err)
}
}
return nil
}
func createSeedWatches(controller controller.Controller, seedName string, seedManager manager.Manager, namespace string) error {
cache := seedManager.GetCache()
eventHandler := util.EnqueueConst(seedName)
watch := func(t runtime.Object, preds ...predicate.Predicate) error {
seedTypeWatch := &source.Kind{Type: t}
if err := seedTypeWatch.InjectCache(cache); err != nil {
return fmt.Errorf("failed to inject cache into watch for %T: %v", t, err)
}
if err := controller.Watch(seedTypeWatch, eventHandler, preds...); err != nil {
return fmt.Errorf("failed to watch %T: %v", t, err)
}
return nil
}
namespacedTypesToWatch := []runtime.Object{
&appsv1.Deployment{},
&corev1.ConfigMap{},
&corev1.Secret{},
&corev1.Service{},
&corev1.ServiceAccount{},
&policyv1beta1.PodDisruptionBudget{},
}
for _, t := range namespacedTypesToWatch {
if err := watch(t, predicateutil.ByNamespace(namespace), common.ManagedByOperatorPredicate); err != nil {
return err
}
}
globalTypesToWatch := []runtime.Object{
&rbacv1.ClusterRoleBinding{},
&admissionregistrationv1beta1.ValidatingWebhookConfiguration{},
}
for _, t := range globalTypesToWatch {
if err := watch(t, common.ManagedByOperatorPredicate); err != nil {
return err
}
}
// Seeds are not managed by the operator, but we still need to be notified when
// they are marked for deletion inside seed clusters
if err := watch(&kubermaticv1.Seed{}, predicateutil.ByNamespace(namespace)); err != nil {
return err
}
// namespaces are not managed by the operator and so can use neither namespacePredicate
// nor ManagedByPredicate, but still need to get their labels reconciled
if err := watch(&corev1.Namespace{}, predicateutil.ByName(namespace)); err != nil {
return err
}
// The VPA gets resources deployed into the kube-system namespace.
namespacedVPATypes := []runtime.Object{
&appsv1.Deployment{},
&corev1.Secret{},
&corev1.Service{},
&corev1.ServiceAccount{},
}
for _, t := range namespacedVPATypes {
if err := watch(t, predicateutil.ByNamespace(metav1.NamespaceSystem), common.ManagedByOperatorPredicate); err != nil {
return err
}
}
globalVPATypes := []runtime.Object{
&rbacv1.ClusterRole{},
&rbacv1.ClusterRoleBinding{},
}
for _, t := range globalVPATypes {
if err := watch(t, common.ManagedByOperatorPredicate); err != nil {
return err
}
}
return nil
}
// newEventHandler takes a obj->request mapper function and wraps it into an
// handler.EnqueueRequestsFromMapFunc.
func newEventHandler(rf handler.ToRequestsFunc) *handler.EnqueueRequestsFromMapFunc {
return &handler.EnqueueRequestsFromMapFunc{
ToRequests: rf,
}
}