-
Notifications
You must be signed in to change notification settings - Fork 480
/
reconciler.go
277 lines (229 loc) · 11.3 KB
/
reconciler.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
// Copyright (c) 2019 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 backupentry
import (
"context"
"fmt"
"github.com/go-logr/logr"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
extensionscontroller "github.com/gardener/gardener/extensions/pkg/controller"
"github.com/gardener/gardener/extensions/pkg/controller/common"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
gardencorev1beta1helper "github.com/gardener/gardener/pkg/apis/core/v1beta1/helper"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/gardener/gardener/pkg/controllerutils"
reconcilerutils "github.com/gardener/gardener/pkg/controllerutils/reconciler"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
)
type reconciler struct {
logger logr.Logger
actuator Actuator
client client.Client
reader client.Reader
statusUpdater extensionscontroller.StatusUpdater
}
// NewReconciler creates a new reconcile.Reconciler that reconciles
// backupentry resources of Gardener's `extensions.gardener.cloud` API group.
func NewReconciler(actuator Actuator) reconcile.Reconciler {
logger := log.Log.WithName(ControllerName)
return reconcilerutils.OperationAnnotationWrapper(
func() client.Object { return &extensionsv1alpha1.BackupEntry{} },
&reconciler{
logger: logger,
actuator: actuator,
statusUpdater: extensionscontroller.NewStatusUpdater(logger),
},
)
}
func (r *reconciler) InjectFunc(f inject.Func) error {
return f(r.actuator)
}
func (r *reconciler) InjectClient(client client.Client) error {
r.client = client
r.statusUpdater.InjectClient(client)
return nil
}
func (r *reconciler) InjectAPIReader(reader client.Reader) error {
r.reader = reader
return nil
}
func (r *reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
be := &extensionsv1alpha1.BackupEntry{}
if err := r.client.Get(ctx, request.NamespacedName, be); err != nil {
if apierrors.IsNotFound(err) {
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}
shootTechnicalID, _ := ExtractShootDetailsFromBackupEntryName(be.Name)
cluster, err := extensionscontroller.GetCluster(ctx, r.client, shootTechnicalID)
// As BackupEntry continues to exist post deletion of a Shoot,
// we do not want to block its deletion when the Cluster is not found.
if client.IgnoreNotFound(err) != nil {
return reconcile.Result{}, err
}
logger := r.logger.WithValues("backupentry", be.Name)
if extensionscontroller.IsFailed(cluster) {
logger.Info("Skipping the reconciliation of backupentry of failed shoot")
return reconcile.Result{}, nil
}
operationType := gardencorev1beta1helper.ComputeOperationType(be.ObjectMeta, be.Status.LastOperation)
if cluster != nil && cluster.Shoot != nil && operationType != gardencorev1beta1.LastOperationTypeMigrate {
key := "backupentry:" + be.Name
ok, watchdogCtx, cleanup, err := common.GetOwnerCheckResultAndContext(ctx, r.client, shootTechnicalID, cluster.Shoot.Name, key)
if err != nil {
return reconcile.Result{}, err
} else if !ok {
return reconcile.Result{}, fmt.Errorf("this seed is not the owner of shoot %s", kutil.ObjectName(cluster.Shoot))
}
ctx = watchdogCtx
if cleanup != nil {
defer cleanup()
}
}
switch {
case extensionscontroller.ShouldSkipOperation(operationType, be):
return reconcile.Result{}, nil
case operationType == gardencorev1beta1.LastOperationTypeMigrate:
return r.migrate(ctx, be)
case be.DeletionTimestamp != nil:
return r.delete(ctx, be)
case operationType == gardencorev1beta1.LastOperationTypeRestore:
return r.restore(ctx, be)
default:
return r.reconcile(ctx, be, operationType)
}
}
func (r *reconciler) reconcile(ctx context.Context, be *extensionsv1alpha1.BackupEntry, operationType gardencorev1beta1.LastOperationType) (reconcile.Result, error) {
if err := controllerutils.EnsureFinalizer(ctx, r.reader, r.client, be, FinalizerName); err != nil {
return reconcile.Result{}, err
}
if err := r.statusUpdater.Processing(ctx, be, operationType, "Reconciling the backupentry"); err != nil {
return reconcile.Result{}, err
}
secret, err := kutil.GetSecretByReference(ctx, r.client, &be.Spec.SecretRef)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to get backup entry secret: %+v", err)
}
if err := controllerutils.EnsureFinalizer(ctx, r.reader, r.client, secret, FinalizerName); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to ensure finalizer on backup entry secret: %+v", err)
}
r.logger.Info("Starting the reconciliation of backupentry", "backupentry", kutil.ObjectName(be))
if err := r.actuator.Reconcile(ctx, be); err != nil {
_ = r.statusUpdater.Error(ctx, be, reconcilerutils.ReconcileErrCauseOrErr(err), operationType, "Error reconciling backupentry")
return reconcilerutils.ReconcileErr(err)
}
if err := r.statusUpdater.Success(ctx, be, operationType, "Successfully reconciled backupentry"); err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
}
func (r *reconciler) restore(ctx context.Context, be *extensionsv1alpha1.BackupEntry) (reconcile.Result, error) {
if err := controllerutils.EnsureFinalizer(ctx, r.reader, r.client, be, FinalizerName); err != nil {
return reconcile.Result{}, err
}
if err := r.statusUpdater.Processing(ctx, be, gardencorev1beta1.LastOperationTypeRestore, "Restoring the backupentry"); err != nil {
return reconcile.Result{}, err
}
secret, err := kutil.GetSecretByReference(ctx, r.client, &be.Spec.SecretRef)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to get backup entry secret: %+v", err)
}
if err := controllerutils.EnsureFinalizer(ctx, r.reader, r.client, secret, FinalizerName); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to ensure finalizer on backup entry secret: %+v", err)
}
r.logger.Info("Starting the restoration of backupentry", "backupentry", kutil.ObjectName(be))
if err := r.actuator.Restore(ctx, be); err != nil {
_ = r.statusUpdater.Error(ctx, be, reconcilerutils.ReconcileErrCauseOrErr(err), gardencorev1beta1.LastOperationTypeRestore, "Error restoring backupentry")
return reconcilerutils.ReconcileErr(err)
}
if err := r.statusUpdater.Success(ctx, be, gardencorev1beta1.LastOperationTypeRestore, "Successfully restored backupentry"); err != nil {
return reconcile.Result{}, err
}
if err := extensionscontroller.RemoveAnnotation(ctx, r.client, be, v1beta1constants.GardenerOperation); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to remove the annotation from backupentry: %+v", err)
}
return reconcile.Result{}, nil
}
func (r *reconciler) delete(ctx context.Context, be *extensionsv1alpha1.BackupEntry) (reconcile.Result, error) {
if !controllerutil.ContainsFinalizer(be, FinalizerName) {
r.logger.Info("Deleting backupentry causes a no-op as there is no finalizer", "backupentry", kutil.ObjectName(be))
return reconcile.Result{}, nil
}
operationType := gardencorev1beta1helper.ComputeOperationType(be.ObjectMeta, be.Status.LastOperation)
if err := r.statusUpdater.Processing(ctx, be, operationType, "Deleting the backupentry"); err != nil {
return reconcile.Result{}, err
}
r.logger.Info("Starting the deletion of backupentry", "backupentry", kutil.ObjectName(be))
secret, err := kutil.GetSecretByReference(ctx, r.client, &be.Spec.SecretRef)
if err != nil {
if !apierrors.IsNotFound(err) {
return reconcile.Result{}, fmt.Errorf("failed to get backup entry secret: %+v", err)
}
r.logger.Info("Skipping deletion as referred secret does not exist any more - removing finalizer", "backupentry", kutil.ObjectName(be))
if err := controllerutils.RemoveFinalizer(ctx, r.reader, r.client, be, FinalizerName); err != nil {
return reconcile.Result{}, fmt.Errorf("error removing finalizer from backupentry: %+v", err)
}
return reconcile.Result{}, nil
}
if err := r.actuator.Delete(ctx, be); err != nil {
_ = r.statusUpdater.Error(ctx, be, reconcilerutils.ReconcileErrCauseOrErr(err), operationType, "Error deleting backupentry")
return reconcilerutils.ReconcileErr(err)
}
if err := r.statusUpdater.Success(ctx, be, operationType, "Successfully deleted backupentry"); err != nil {
return reconcile.Result{}, err
}
if err := controllerutils.RemoveFinalizer(ctx, r.reader, r.client, secret, FinalizerName); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to remove finalizer on backup entry secret: %+v", err)
}
r.logger.Info("Removing finalizer", "backupentry", be.Name)
if err := controllerutils.RemoveFinalizer(ctx, r.reader, r.client, be, FinalizerName); err != nil {
return reconcile.Result{}, fmt.Errorf("error removing finalizer from backupentry: %+v", err)
}
return reconcile.Result{}, nil
}
func (r *reconciler) migrate(ctx context.Context, be *extensionsv1alpha1.BackupEntry) (reconcile.Result, error) {
if err := r.statusUpdater.Processing(ctx, be, gardencorev1beta1.LastOperationTypeMigrate, "Migrating the backupentry"); err != nil {
return reconcile.Result{}, err
}
r.logger.Info("Starting the migration of backupentry", "backupentry", kutil.ObjectName(be))
if err := r.actuator.Migrate(ctx, be); err != nil {
_ = r.statusUpdater.Error(ctx, be, reconcilerutils.ReconcileErrCauseOrErr(err), gardencorev1beta1.LastOperationTypeMigrate, "Error migrating backupentry")
return reconcilerutils.ReconcileErr(err)
}
if err := r.statusUpdater.Success(ctx, be, gardencorev1beta1.LastOperationTypeMigrate, "Successfully migrated backupentry"); err != nil {
return reconcile.Result{}, err
}
secret, err := kutil.GetSecretByReference(ctx, r.client, &be.Spec.SecretRef)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to get backup entry secret: %+v", err)
}
if err := controllerutils.RemoveFinalizer(ctx, r.reader, r.client, secret, FinalizerName); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to remove finalizer on backup entry secret: %+v", err)
}
r.logger.Info("Removing all finalizers", "backupentry", kutil.ObjectName(be))
if err := controllerutils.RemoveAllFinalizers(ctx, r.client, r.client, be); err != nil {
return reconcile.Result{}, fmt.Errorf("error removing all finalizers from backupentry: %+v", err)
}
if err := extensionscontroller.RemoveAnnotation(ctx, r.client, be, v1beta1constants.GardenerOperation); err != nil {
return reconcile.Result{}, fmt.Errorf("error removing annotation from backupentry: %+v", err)
}
return reconcile.Result{}, nil
}