forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_infrastructure_control.go
424 lines (372 loc) · 20.3 KB
/
backup_infrastructure_control.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright (c) 2018 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 backupinfrastructure
import (
"encoding/json"
"errors"
"fmt"
"time"
gardenv1beta1 "github.com/gardener/gardener/pkg/apis/garden/v1beta1"
"github.com/gardener/gardener/pkg/apis/garden/v1beta1/helper"
gardeninformers "github.com/gardener/gardener/pkg/client/garden/informers/externalversions/garden/v1beta1"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/controllermanager/apis/config"
controllerutils "github.com/gardener/gardener/pkg/controllermanager/controller/utils"
"github.com/gardener/gardener/pkg/logger"
"github.com/gardener/gardener/pkg/operation"
botanistpkg "github.com/gardener/gardener/pkg/operation/botanist"
cloudbotanistpkg "github.com/gardener/gardener/pkg/operation/cloudbotanist"
"github.com/gardener/gardener/pkg/operation/common"
"github.com/gardener/gardener/pkg/utils/flow"
"github.com/gardener/gardener/pkg/utils/imagevector"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
)
func (c *Controller) backupInfrastructureAdd(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err != nil {
logger.Logger.Errorf("Couldn't get key for object %+v: %v", obj, err)
return
}
c.backupInfrastructureQueue.Add(key)
}
func (c *Controller) backupInfrastructureUpdate(oldObj, newObj interface{}) {
var (
newBackupInfrastructure = newObj.(*gardenv1beta1.BackupInfrastructure)
backupInfrastructureLogger = logger.NewFieldLogger(logger.Logger, "backupinfrastructure", fmt.Sprintf("%s/%s", newBackupInfrastructure.Namespace, newBackupInfrastructure.Name))
)
// If the generation did not change for an update event (i.e., no changes to the .spec section have
// been made), we do not want to add the BackupInfrastructure to the queue. The periodic reconciliation is handled
// elsewhere by adding the BackupInfrastructure to the queue to dedicated times.
if newBackupInfrastructure.Generation == newBackupInfrastructure.Status.ObservedGeneration {
backupInfrastructureLogger.Debug("Do not need to do anything as the Update event occurred due to .status field changes")
return
}
c.backupInfrastructureAdd(newObj)
}
func (c *Controller) backupInfrastructureDelete(obj interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
logger.Logger.Errorf("Couldn't get key for object %+v: %v", obj, err)
return
}
c.backupInfrastructureQueue.Add(key)
}
func (c *Controller) reconcileBackupInfrastructureKey(key string) error {
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return err
}
backupInfrastructure, err := c.backupInfrastructureLister.BackupInfrastructures(namespace).Get(name)
if apierrors.IsNotFound(err) {
logger.Logger.Debugf("[BACKUPINFRASTRUCTURE RECONCILE] %s - skipping because BackupInfrastructure has been deleted", key)
return nil
}
if err != nil {
logger.Logger.Infof("[BACKUPINFRASTRUCTURE RECONCILE] %s - unable to retrieve object from store: %v", key, err)
return err
}
backupInfrastructureLogger := logger.NewFieldLogger(logger.Logger, "backupinfrastructure", fmt.Sprintf("%s/%s", backupInfrastructure.Namespace, backupInfrastructure.Name))
if backupInfrastructure.DeletionTimestamp != nil && !sets.NewString(backupInfrastructure.Finalizers...).Has(gardenv1beta1.GardenerName) {
backupInfrastructureLogger.Debug("Do not need to do anything as the BackupInfrastructure does not have my finalizer")
c.backupInfrastructureQueue.Forget(key)
return nil
}
durationToNextSync := c.config.Controllers.BackupInfrastructure.SyncPeriod.Duration
if reconcileErr := c.control.ReconcileBackupInfrastructure(backupInfrastructure, key); reconcileErr != nil {
durationToNextSync = 15 * time.Second
}
c.backupInfrastructureQueue.AddAfter(key, durationToNextSync)
backupInfrastructureLogger.Infof("Scheduled next reconciliation for BackupInfrastructure '%s' in %s", key, durationToNextSync)
return nil
}
// ControlInterface implements the control logic for updating BackupInfrastructures. It is implemented as an interface to allow
// for extensions that provide different semantics. Currently, there is only one implementation.
type ControlInterface interface {
// ReconcileBackupInfrastructure implements the control logic for BackupInfrastructure creation, update, and deletion.
// If an implementation returns a non-nil error, the invocation will be retried using a rate-limited strategy.
// Implementors should sink any errors that they do not wish to trigger a retry, and they may feel free to
// exit exceptionally at any point provided they wish the update to be re-run at a later point in time.
ReconcileBackupInfrastructure(backupInfrastructure *gardenv1beta1.BackupInfrastructure, key string) error
}
// NewDefaultControl returns a new instance of the default implementation ControlInterface that
// implements the documented semantics for BackupInfrastructures. You should use an instance returned from NewDefaultControl() for any
// scenario other than testing.
func NewDefaultControl(k8sGardenClient kubernetes.Interface, k8sGardenInformers gardeninformers.Interface, secrets map[string]*corev1.Secret, imageVector imagevector.ImageVector, identity *gardenv1beta1.Gardener, config *config.ControllerManagerConfiguration, recorder record.EventRecorder) ControlInterface {
return &defaultControl{k8sGardenClient, k8sGardenInformers, secrets, imageVector, identity, config, recorder}
}
type defaultControl struct {
k8sGardenClient kubernetes.Interface
k8sGardenInformers gardeninformers.Interface
secrets map[string]*corev1.Secret
imageVector imagevector.ImageVector
identity *gardenv1beta1.Gardener
config *config.ControllerManagerConfiguration
recorder record.EventRecorder
}
func (c *defaultControl) ReconcileBackupInfrastructure(obj *gardenv1beta1.BackupInfrastructure, key string) error {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err != nil {
return err
}
var (
backupInfrastructure = obj.DeepCopy()
backupInfrastructureLogger = logger.NewFieldLogger(logger.Logger, "backupinfrastructure", fmt.Sprintf("%s/%s", backupInfrastructure.Namespace, backupInfrastructure.Name))
lastOperation = backupInfrastructure.Status.LastOperation
operationType = controllerutils.ComputeOperationType(obj.ObjectMeta, lastOperation)
)
logger.Logger.Infof("[BACKUPINFRASTRUCTURE RECONCILE] %s", key)
// Skip further logic if the last successful reconciliation happened less than the specified syncPeriod ago
// and the object does not have an explicit reconcile instruction in its annotations.
syncPeriod := c.config.Controllers.BackupInfrastructure.SyncPeriod.Duration
if backupInfrastructure.DeletionTimestamp == nil &&
!nextReconcileScheduleReached(obj, syncPeriod) &&
!kutil.HasMetaDataAnnotation(&obj.ObjectMeta, common.BackupInfrastructureOperation, common.BackupInfrastructureReconcile) {
logger.Logger.Infof("Skip reconciliation for BackupInfrastructure %s. Last successful operation happened less than %q ago and reconcile annotation is not set.", key, syncPeriod)
return nil
}
backupInfrastructureJSON, _ := json.Marshal(backupInfrastructure)
backupInfrastructureLogger.Debugf(string(backupInfrastructureJSON))
op, err := operation.NewWithBackupInfrastructure(backupInfrastructure, backupInfrastructureLogger, c.k8sGardenClient, c.k8sGardenInformers, c.identity, c.secrets, c.imageVector)
if err != nil {
backupInfrastructureLogger.Errorf("Could not initialize a new operation: %s", err.Error())
return err
}
// The deletionTimestamp labels a BackupInfrastructure as intended to get deleted. Before deletion,
// it has to be ensured that no infrastructure resources are depending on the BackupInfrastructure anymore.
// When this happens the controller will remove the finalizer from the BackupInfrastructure so that it can be garbage collected.
if backupInfrastructure.DeletionTimestamp != nil {
gracePeriod := time.Hour * 24 * time.Duration(*c.config.Controllers.BackupInfrastructure.DeletionGracePeriodDays)
if time.Now().Sub(backupInfrastructure.DeletionTimestamp.Time) > gracePeriod {
if updateErr := c.updateBackupInfrastructureStatus(op, gardenv1beta1.ShootLastOperationStateProcessing, operationType, "Deletion of Backup Infrastructure in progress.", 1, nil); updateErr != nil {
backupInfrastructureLogger.Errorf("Could not update the BackupInfrastructure status after deletion start: %+v", updateErr)
return updateErr
}
if deleteErr := c.deleteBackupInfrastructure(op); deleteErr != nil {
c.recorder.Eventf(backupInfrastructure, corev1.EventTypeWarning, gardenv1beta1.EventDeleteError, "%s", deleteErr.Description)
if updateErr := c.updateBackupInfrastructureStatus(op, gardenv1beta1.ShootLastOperationStateError, operationType, deleteErr.Description+" Operation will be retried.", 1, deleteErr); updateErr != nil {
backupInfrastructureLogger.Errorf("Could not update the BackupInfrastructure status after deletion error: %+v", updateErr)
return updateErr
}
return errors.New(deleteErr.Description)
}
if updateErr := c.updateBackupInfrastructureStatus(op, gardenv1beta1.ShootLastOperationStateSucceeded, operationType, "Backup Infrastructure has been successfully deleted.", 100, nil); updateErr != nil {
backupInfrastructureLogger.Errorf("Could not update the BackupInfrastructure status after deletion successful: %+v", updateErr)
return updateErr
}
return c.removeFinalizer(op)
}
if updateErr := c.updateBackupInfrastructureStatus(op, gardenv1beta1.ShootLastOperationStatePending, operationType, fmt.Sprintf("Deletion of backup infrastructure is scheduled for %s", backupInfrastructure.DeletionTimestamp.Time.Add(gracePeriod)), 1, nil); updateErr != nil {
backupInfrastructureLogger.Errorf("Could not update the BackupInfrastructure status after suspending deletion: %+v", updateErr)
return updateErr
}
return nil
}
// When a BackupInfrastructure deletion timestamp is not set we need to create/reconcile the backup infrastructure.
if updateErr := c.updateBackupInfrastructureStatus(op, gardenv1beta1.ShootLastOperationStateProcessing, operationType, "Reconciliation of Backup Infrastructure state in progress.", 1, nil); updateErr != nil {
backupInfrastructureLogger.Errorf("Could not update the BackupInfrastructure status after reconciliation start: %+v", updateErr)
return updateErr
}
if reconcileErr := c.reconcileBackupInfrastructure(op); reconcileErr != nil {
c.recorder.Eventf(backupInfrastructure, corev1.EventTypeWarning, gardenv1beta1.EventReconcileError, "%s", reconcileErr.Description)
if updateErr := c.updateBackupInfrastructureStatus(op, gardenv1beta1.ShootLastOperationStateError, operationType, reconcileErr.Description+" Operation will be retried.", 1, reconcileErr); updateErr != nil {
backupInfrastructureLogger.Errorf("Could not update the BackupInfrastructure status after reconciliation error: %+v", updateErr)
return updateErr
}
return errors.New(reconcileErr.Description)
}
if updateErr := c.updateBackupInfrastructureStatus(op, gardenv1beta1.ShootLastOperationStateSucceeded, operationType, "Backup Infrastructure has been successfully reconciled.", 100, nil); updateErr != nil {
backupInfrastructureLogger.Errorf("Could not update the Shoot status after reconciliation success: %+v", updateErr)
return updateErr
}
if _, updateErr := kutil.TryUpdateBackupInfrastructureAnnotations(op.K8sGardenClient.Garden(), retry.DefaultRetry, obj.ObjectMeta,
func(backupInfrastructure *gardenv1beta1.BackupInfrastructure) (*gardenv1beta1.BackupInfrastructure, error) {
delete(backupInfrastructure.Annotations, common.BackupInfrastructureOperation)
return backupInfrastructure, nil
}); updateErr != nil {
backupInfrastructureLogger.Errorf("Could not remove %q annotation: %+v", common.BackupInfrastructureOperation, updateErr)
return updateErr
}
return nil
}
// reconcileBackupInfrastructure reconciles a BackupInfrastructure state.
func (c *defaultControl) reconcileBackupInfrastructure(o *operation.Operation) *gardenv1beta1.LastError {
// We create botanists (which will do the actual work).
botanist, err := botanistpkg.New(o)
if err != nil {
return formatError("Failed to create a Botanist", err)
}
seedCloudBotanist, err := cloudbotanistpkg.New(o, common.CloudPurposeSeed)
if err != nil {
return formatError("Failed to create a Seed CloudBotanist", err)
}
var (
defaultTimeout = 30 * time.Second
defaultInterval = 5 * time.Second
g = flow.NewGraph("Backup Infrastructure Creation")
deployBackupNamespace = g.Add(flow.Task{
Name: "Deploying backup namespace",
Fn: flow.SimpleTaskFn(botanist.DeployBackupNamespace).RetryUntilTimeout(defaultInterval, defaultTimeout),
})
_ = g.Add(flow.Task{
Name: "Deploying backup infrastructure",
Fn: flow.SimpleTaskFn(seedCloudBotanist.DeployBackupInfrastructure),
Dependencies: flow.NewTaskIDs(deployBackupNamespace),
})
f = g.Compile()
)
err = f.Run(flow.Opts{
Logger: o.Logger,
ProgressReporter: o.ReportBackupInfrastructureProgress,
})
if err != nil {
o.Logger.Errorf("Failed to reconcile backup infrastructure %q: %+v", o.BackupInfrastructure.Name, err)
return &gardenv1beta1.LastError{
Codes: helper.ExtractErrorCodes(flow.Causes(err)),
Description: helper.FormatLastErrDescription(err),
}
}
o.Logger.Infof("Successfully reconciled backup infrastructure %q", o.BackupInfrastructure.Name)
return nil
}
// deleteBackupInfrastructure deletes a BackupInfrastructure entirely.
func (c *defaultControl) deleteBackupInfrastructure(o *operation.Operation) *gardenv1beta1.LastError {
// We create botanists (which will do the actual work).
botanist, err := botanistpkg.New(o)
if err != nil {
return formatError("Failed to create a Botanist", err)
}
// We first check whether the namespace in the Seed cluster does exist - if it does not, then we assume that
// all resources have already been deleted. We can delete the BackupInfrastructure resource as a consequence.
namespace, err := botanist.K8sSeedClient.GetNamespace(common.GenerateBackupNamespaceName(o.BackupInfrastructure.Name))
if apierrors.IsNotFound(err) {
o.Logger.Infof("Did not find '%s' namespace in the Seed cluster - nothing to be done", common.GenerateBackupNamespaceName(o.BackupInfrastructure.Name))
return nil
}
if err != nil {
return formatError("Failed to retrieve the backup namespace in the Seed cluster", err)
}
seedCloudBotanist, err := cloudbotanistpkg.New(o, common.CloudPurposeSeed)
if err != nil {
return formatError("Failed to create a Seed CloudBotanist", err)
}
// We check whether the Backup namespace in the Seed cluster is already in a terminating state, i.e. whether
// we have tried to delete it in a previous run. In that case, we do not need to cleanup backup infrastructure resource because
// that would have already been done.
var (
cleanupBackupInfrastructureResources = namespace.Status.Phase != corev1.NamespaceTerminating
defaultInterval = 5 * time.Second
defaultTimeout = 30 * time.Second
g = flow.NewGraph("Backup infrastructure deletion")
destroyBackupInfrastructure = g.Add(flow.Task{
Name: "Destroying backup infrastructure",
Fn: flow.SimpleTaskFn(seedCloudBotanist.DestroyBackupInfrastructure).DoIf(cleanupBackupInfrastructureResources),
})
deleteBackupNamespace = g.Add(flow.Task{
Name: "Deleting backup namespace",
Fn: flow.SimpleTaskFn(botanist.DeleteBackupNamespace).RetryUntilTimeout(defaultInterval, defaultTimeout),
Dependencies: flow.NewTaskIDs(destroyBackupInfrastructure),
})
_ = g.Add(flow.Task{
Name: "Waiting until backup namespace is deleted",
Fn: flow.SimpleTaskFn(botanist.WaitUntilBackupNamespaceDeleted),
Dependencies: flow.NewTaskIDs(deleteBackupNamespace),
})
f = g.Compile()
)
err = f.Run(flow.Opts{
Logger: o.Logger,
ProgressReporter: o.ReportBackupInfrastructureProgress,
})
if err != nil {
o.Logger.Errorf("Failed to delete backup infrastructure %q: %+v", o.BackupInfrastructure.Name, err)
return &gardenv1beta1.LastError{
Codes: helper.ExtractErrorCodes(err),
Description: err.Error(),
}
}
o.Logger.Infof("Successfully deleted backup infrastructure %q", o.BackupInfrastructure.Name)
return nil
}
func (c *defaultControl) updateBackupInfrastructureStatus(o *operation.Operation, state gardenv1beta1.ShootLastOperationState, operationType gardenv1beta1.ShootLastOperationType, operationDescription string, progress int, lastError *gardenv1beta1.LastError) error {
if state == gardenv1beta1.ShootLastOperationStateError && o.BackupInfrastructure.Status.LastOperation != nil {
progress = o.BackupInfrastructure.Status.LastOperation.Progress
}
lastOperation := &gardenv1beta1.LastOperation{
Type: operationType,
State: state,
Progress: progress,
Description: operationDescription,
LastUpdateTime: metav1.Now(),
}
newBackupInfrastructure, err := kutil.TryUpdateBackupInfrastructureStatus(c.k8sGardenClient.Garden(), retry.DefaultRetry, o.BackupInfrastructure.ObjectMeta,
func(backupInfrastructure *gardenv1beta1.BackupInfrastructure) (*gardenv1beta1.BackupInfrastructure, error) {
backupInfrastructure.Status.LastOperation = lastOperation
backupInfrastructure.Status.LastError = lastError
backupInfrastructure.Status.ObservedGeneration = backupInfrastructure.Generation
return backupInfrastructure, nil
})
if err == nil {
o.BackupInfrastructure = newBackupInfrastructure
}
return err
}
func (c *defaultControl) removeFinalizer(op *operation.Operation) error {
backupInfrastructureFinalizers := sets.NewString(op.BackupInfrastructure.Finalizers...)
backupInfrastructureFinalizers.Delete(gardenv1beta1.GardenerName)
op.BackupInfrastructure.Finalizers = backupInfrastructureFinalizers.UnsortedList()
newBackupInfrastructure, err := c.k8sGardenClient.Garden().GardenV1beta1().BackupInfrastructures(op.BackupInfrastructure.Namespace).Update(op.BackupInfrastructure)
if err != nil {
op.Logger.Errorf("Could not remove finalizer of the BackupInfrastructure: %+v", err.Error())
return err
}
op.BackupInfrastructure = newBackupInfrastructure
// Wait until the above modifications are reflected in the cache to prevent unwanted reconcile
// operations (sometimes the cache is not synced fast enough).
return wait.PollImmediate(time.Second, 30*time.Second, func() (bool, error) {
backupInfrastructure, err := c.k8sGardenInformers.BackupInfrastructures().Lister().BackupInfrastructures(op.BackupInfrastructure.Namespace).Get(op.BackupInfrastructure.Name)
if apierrors.IsNotFound(err) {
return true, nil
}
if err != nil {
return false, err
}
if !sets.NewString(backupInfrastructure.Finalizers...).Has(gardenv1beta1.GardenerName) {
return true, nil
}
return false, nil
})
}
func formatError(message string, err error) *gardenv1beta1.LastError {
return &gardenv1beta1.LastError{
Description: fmt.Sprintf("%s (%s)", message, err.Error()),
}
}
func nextReconcileScheduleReached(obj *gardenv1beta1.BackupInfrastructure, syncPeriod time.Duration) bool {
lastOperation := obj.Status.LastOperation
if lastOperation != nil &&
lastOperation.Type == gardenv1beta1.ShootLastOperationTypeReconcile &&
lastOperation.State == gardenv1beta1.ShootLastOperationStateSucceeded {
earliestNextReconcile := lastOperation.LastUpdateTime.Add(syncPeriod)
return time.Now().After(earliestNextReconcile)
}
return true
}