-
Notifications
You must be signed in to change notification settings - Fork 472
/
add.go
297 lines (256 loc) · 10.6 KB
/
add.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
296
297
// Copyright 2022 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 managedseed
import (
"context"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
"k8s.io/utils/clock"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"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"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
seedmanagementv1alpha1 "github.com/gardener/gardener/pkg/apis/seedmanagement/v1alpha1"
"github.com/gardener/gardener/pkg/controllerutils/mapper"
"github.com/gardener/gardener/pkg/utils"
gardenerutils "github.com/gardener/gardener/pkg/utils/gardener"
kubernetesutils "github.com/gardener/gardener/pkg/utils/kubernetes"
)
const (
// ControllerName is the name of this controller.
ControllerName = "managedseed"
// GardenletDefaultKubeconfigSecretName is the default name for the field in the Gardenlet component configuration
// .gardenClientConnection.KubeconfigSecret.Name
GardenletDefaultKubeconfigSecretName = "gardenlet-kubeconfig"
// GardenletDefaultKubeconfigBootstrapSecretName is the default name for the field in the Gardenlet component configuration
// .gardenClientConnection.BootstrapKubeconfig.Name
GardenletDefaultKubeconfigBootstrapSecretName = "gardenlet-kubeconfig-bootstrap"
)
// AddToManager adds Reconciler to the given manager.
func (r *Reconciler) AddToManager(
ctx context.Context,
mgr manager.Manager,
gardenCluster cluster.Cluster,
seedCluster cluster.Cluster,
) error {
if r.GardenClient == nil {
r.GardenClient = gardenCluster.GetClient()
}
if r.Clock == nil {
r.Clock = clock.RealClock{}
}
if r.GardenNamespaceGarden == "" {
r.GardenNamespaceGarden = v1beta1constants.GardenNamespace
}
if r.GardenNamespaceShoot == "" {
r.GardenNamespaceShoot = v1beta1constants.GardenNamespace
}
if r.Actuator == nil {
r.Actuator = newActuator(
gardenCluster.GetConfig(),
gardenCluster.GetAPIReader(),
gardenCluster.GetClient(),
seedCluster.GetClient(),
r.ShootClientMap,
r.Clock,
NewValuesHelper(&r.Config),
gardenCluster.GetEventRecorderFor(ControllerName+"-controller"),
r.GardenNamespaceShoot,
)
}
c, err := builder.
ControllerManagedBy(mgr).
Named(ControllerName).
WithOptions(controller.Options{
MaxConcurrentReconciles: pointer.IntDeref(r.Config.Controllers.ManagedSeed.ConcurrentSyncs, 0),
}).
Watches(
source.NewKindWithCache(&seedmanagementv1alpha1.ManagedSeed{}, gardenCluster.GetCache()),
r.EnqueueWithJitterDelay(),
builder.WithPredicates(
r.ManagedSeedPredicate(ctx, r.Config.SeedConfig.SeedTemplate.Name),
&predicate.GenerationChangedPredicate{},
),
).
Build(r)
if err != nil {
return err
}
return c.Watch(
source.NewKindWithCache(&gardencorev1beta1.Seed{}, gardenCluster.GetCache()),
mapper.EnqueueRequestsFrom(ctx, mgr.GetCache(), mapper.MapFunc(r.MapSeedToManagedSeed), mapper.UpdateWithNew, c.GetLogger()),
r.SeedOfManagedSeedPredicate(ctx, r.Config.SeedConfig.SeedTemplate.Name),
)
}
// ManagedSeedPredicate returns the predicate for ManagedSeed events.
func (r *Reconciler) ManagedSeedPredicate(ctx context.Context, seedName string) predicate.Predicate {
return &managedSeedPredicate{
ctx: ctx,
reader: r.GardenClient,
seedName: seedName,
}
}
type managedSeedPredicate struct {
ctx context.Context
reader client.Reader
seedName string
}
func (p *managedSeedPredicate) Create(e event.CreateEvent) bool {
return p.filterManagedSeed(e.Object)
}
func (p *managedSeedPredicate) Update(e event.UpdateEvent) bool {
return p.filterManagedSeed(e.ObjectNew)
}
func (p *managedSeedPredicate) Delete(e event.DeleteEvent) bool {
return p.filterManagedSeed(e.Object)
}
func (p *managedSeedPredicate) Generic(_ event.GenericEvent) bool { return false }
// filterManagedSeed is filtering func for ManagedSeeds that checks if the ManagedSeed references a Shoot scheduled on a Seed,
// for which the gardenlet is responsible.
func (p *managedSeedPredicate) filterManagedSeed(obj client.Object) bool {
managedSeed, ok := obj.(*seedmanagementv1alpha1.ManagedSeed)
if !ok {
return false
}
return filterManagedSeed(p.ctx, p.reader, managedSeed, p.seedName)
}
// SeedOfManagedSeedPredicate returns the predicate for Seed events.
func (r *Reconciler) SeedOfManagedSeedPredicate(ctx context.Context, seedName string) predicate.Predicate {
return &seedOfManagedSeedPredicate{
ctx: ctx,
reader: r.GardenClient,
gardenNamespace: r.GardenNamespaceGarden,
seedName: seedName,
}
}
type seedOfManagedSeedPredicate struct {
ctx context.Context
reader client.Reader
gardenNamespace string
seedName string
}
func (p *seedOfManagedSeedPredicate) Create(e event.CreateEvent) bool {
return p.filterSeedOfManagedSeed(e.Object)
}
func (p *seedOfManagedSeedPredicate) Update(e event.UpdateEvent) bool {
return p.filterSeedOfManagedSeed(e.ObjectNew)
}
func (p *seedOfManagedSeedPredicate) Delete(e event.DeleteEvent) bool {
return p.filterSeedOfManagedSeed(e.Object)
}
func (p *seedOfManagedSeedPredicate) Generic(_ event.GenericEvent) bool { return false }
// filterSeedOfManagedSeed is filtering func for Seeds that checks if the Seed is owned by a ManagedSeed that references a Shoot
// scheduled on a Seed, for which the gardenlet is responsible.
func (p *seedOfManagedSeedPredicate) filterSeedOfManagedSeed(obj client.Object) bool {
seed, ok := obj.(*gardencorev1beta1.Seed)
if !ok {
return false
}
managedSeed := &seedmanagementv1alpha1.ManagedSeed{}
if err := p.reader.Get(p.ctx, kubernetesutils.Key(p.gardenNamespace, seed.Name), managedSeed); err != nil {
return false
}
return filterManagedSeed(p.ctx, p.reader, managedSeed, p.seedName)
}
func filterManagedSeed(ctx context.Context, reader client.Reader, managedSeed *seedmanagementv1alpha1.ManagedSeed, seedName string) bool {
if managedSeed.Spec.Shoot == nil || managedSeed.Spec.Shoot.Name == "" {
return false
}
shoot := &gardencorev1beta1.Shoot{}
if err := reader.Get(ctx, kubernetesutils.Key(managedSeed.Namespace, managedSeed.Spec.Shoot.Name), shoot); err != nil {
return false
}
specSeedName, statusSeedName := gardenerutils.GetShootSeedNames(shoot)
return gardenerutils.GetResponsibleSeedName(specSeedName, statusSeedName) == seedName
}
// MapSeedToManagedSeed is a mapper.MapFunc for mapping a Seed to the owning ManagedSeed.
func (r *Reconciler) MapSeedToManagedSeed(_ context.Context, _ logr.Logger, _ client.Reader, obj client.Object) []reconcile.Request {
return []reconcile.Request{{NamespacedName: types.NamespacedName{Namespace: r.GardenNamespaceGarden, Name: obj.GetName()}}}
}
func reconcileRequest(obj client.Object) reconcile.Request {
return reconcile.Request{NamespacedName: types.NamespacedName{
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
}}
}
// RandomDurationWithMetaDuration is an alias for `utils.RandomDurationWithMetaDuration`. Exposed for unit tests.
var RandomDurationWithMetaDuration = utils.RandomDurationWithMetaDuration
// EnqueueWithJitterDelay returns handler.Funcs which enqueues the object with a random Jitter duration when the JitterUpdate
// is enabled in ManagedSeed controller configuration.
// All other events are normally enqueued.
func (r *Reconciler) EnqueueWithJitterDelay() handler.EventHandler {
return &handler.Funcs{
CreateFunc: func(evt event.CreateEvent, q workqueue.RateLimitingInterface) {
managedSeed, ok := evt.Object.(*seedmanagementv1alpha1.ManagedSeed)
if !ok {
return
}
generationChanged := managedSeed.Generation != managedSeed.Status.ObservedGeneration
// Managed seed with deletion timestamp and newly created managed seed will be enqueued immediately.
// Generation is 1 for newly created objects.
if managedSeed.DeletionTimestamp != nil || managedSeed.Generation == 1 {
q.Add(reconcileRequest(evt.Object))
return
}
if generationChanged {
if *r.Config.Controllers.ManagedSeed.JitterUpdates {
q.AddAfter(reconcileRequest(evt.Object), RandomDurationWithMetaDuration(r.Config.Controllers.ManagedSeed.SyncJitterPeriod))
} else {
q.Add(reconcileRequest(evt.Object))
}
return
}
// Spread reconciliation of managed seeds (including gardenlet updates/rollouts) across the configured sync jitter
// period to avoid overloading the gardener-apiserver if all gardenlets in all managed seeds are (re)starting
// roughly at the same time.
q.AddAfter(reconcileRequest(evt.Object), RandomDurationWithMetaDuration(r.Config.Controllers.ManagedSeed.SyncJitterPeriod))
},
UpdateFunc: func(evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
managedSeed, ok := evt.ObjectNew.(*seedmanagementv1alpha1.ManagedSeed)
if !ok {
return
}
if managedSeed.Generation == managedSeed.Status.ObservedGeneration {
return
}
// Managed seed with deletion timestamp and newly created managed seed will be enqueued immediately.
// Generation is 1 for newly created objects.
if managedSeed.DeletionTimestamp != nil || managedSeed.Generation == 1 {
q.Add(reconcileRequest(evt.ObjectNew))
return
}
if pointer.BoolDeref(r.Config.Controllers.ManagedSeed.JitterUpdates, false) {
q.AddAfter(reconcileRequest(evt.ObjectNew), RandomDurationWithMetaDuration(r.Config.Controllers.ManagedSeed.SyncJitterPeriod))
} else {
q.Add(reconcileRequest(evt.ObjectNew))
}
},
DeleteFunc: func(evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
if evt.Object == nil {
return
}
q.Add(reconcileRequest(evt.Object))
},
}
}