-
Notifications
You must be signed in to change notification settings - Fork 450
/
customresources.go
440 lines (389 loc) · 15.5 KB
/
customresources.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Copyright (c) 2020 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 extensions
import (
"context"
"errors"
"fmt"
"time"
gardencorev1alpha1 "github.com/gardener/gardener/pkg/apis/core/v1alpha1"
gardencorev1alpha1helper "github.com/gardener/gardener/pkg/apis/core/v1alpha1/helper"
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/utils/flow"
gutil "github.com/gardener/gardener/pkg/utils/gardener"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
"github.com/gardener/gardener/pkg/utils/kubernetes/health"
unstructuredutils "github.com/gardener/gardener/pkg/utils/kubernetes/unstructured"
"github.com/gardener/gardener/pkg/utils/retry"
"github.com/go-logr/logr"
autoscalingv1 "k8s.io/api/autoscaling/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// TimeNow returns the current time. Exposed for testing.
var TimeNow = time.Now
// WaitUntilExtensionObjectReady waits until the given extension object has become ready.
// Passed objects are expected to be filled with the latest state the controller/component
// applied/observed/retrieved, but at least namespace and name.
func WaitUntilExtensionObjectReady(
ctx context.Context,
c client.Client,
log logr.Logger,
obj extensionsv1alpha1.Object,
kind string,
interval time.Duration,
severeThreshold time.Duration,
timeout time.Duration,
postReadyFunc func() error,
) error {
return WaitUntilObjectReadyWithHealthFunction(ctx, c, log, health.CheckExtensionObject, obj, kind, interval, severeThreshold, timeout, postReadyFunc)
}
// WaitUntilObjectReadyWithHealthFunction waits until the given object has become ready. It takes the health check
// function that should be executed.
// Passed objects are expected to be filled with the latest state the controller/component
// observed/retrieved, but at least namespace and name.
func WaitUntilObjectReadyWithHealthFunction(
ctx context.Context,
c client.Client,
log logr.Logger,
healthFunc health.Func,
obj client.Object,
kind string,
interval time.Duration,
severeThreshold time.Duration,
timeout time.Duration,
postReadyFunc func() error,
) error {
var (
lastObservedError error
retryCountUntilSevere int
name = obj.GetName()
namespace = obj.GetNamespace()
)
// If the object has been reconciled successfully before we triggered a new reconciliation and our cache
// is not updated fast enough with our reconciliation trigger (i.e. adding the reconcile annotation), we might
// falsy return early from waiting for the object to be ready (as the last state already was "ready").
// Use the timestamp annotation on the object as an ensurance, that once we see it in our cache, we are observing
// a version of the object that is fresh enough.
if expectedTimestamp, ok := obj.GetAnnotations()[v1beta1constants.GardenerTimestamp]; ok {
healthFunc = health.And(health.ObjectHasAnnotationWithValue(v1beta1constants.GardenerTimestamp, expectedTimestamp), healthFunc)
}
if err := retry.UntilTimeout(ctx, interval, timeout, func(ctx context.Context) (bool, error) {
retryCountUntilSevere++
if err := c.Get(ctx, client.ObjectKeyFromObject(obj), obj); err != nil {
if apierrors.IsNotFound(err) {
return retry.MinorError(err)
}
return retry.SevereError(err)
}
if err := healthFunc(obj); err != nil {
lastObservedError = err
log.Error(err, "Object did not get ready yet")
if retry.IsRetriable(err) {
return retry.MinorOrSevereError(retryCountUntilSevere, int(severeThreshold.Nanoseconds()/interval.Nanoseconds()), err)
}
return retry.MinorError(err)
}
if postReadyFunc != nil {
if err := postReadyFunc(); err != nil {
lastObservedError = err
return retry.SevereError(err)
}
}
return retry.Ok()
}); err != nil {
message := fmt.Sprintf("Error while waiting for %s to become ready", extensionKey(kind, namespace, name))
if lastObservedError != nil {
return fmt.Errorf("%s: %w", message, lastObservedError)
}
return fmt.Errorf("%s: %w", message, err)
}
return nil
}
// DeleteExtensionObject deletes a given extension object.
// Passed objects are expected to be filled with the latest state the controller/component
// observed/retrieved, but at least namespace and name.
func DeleteExtensionObject(
ctx context.Context,
c client.Writer,
obj extensionsv1alpha1.Object,
deleteOpts ...client.DeleteOption,
) error {
if err := gutil.ConfirmDeletion(ctx, c, obj); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
return client.IgnoreNotFound(c.Delete(ctx, obj, deleteOpts...))
}
// DeleteExtensionObjects lists all extension objects and loops over them. It executes the given predicateFunc for
// each of them, and if it evaluates to true then the object will be deleted.
func DeleteExtensionObjects(
ctx context.Context,
c client.Client,
listObj client.ObjectList,
namespace string,
predicateFunc func(obj extensionsv1alpha1.Object) bool,
deleteOpts ...client.DeleteOption,
) error {
fns, err := applyFuncToExtensionObjects(ctx, c, listObj, namespace, predicateFunc, func(ctx context.Context, obj extensionsv1alpha1.Object) error {
return DeleteExtensionObject(ctx, c, obj, deleteOpts...)
})
if err != nil {
return err
}
return flow.Parallel(fns...)(ctx)
}
// WaitUntilExtensionObjectsDeleted lists all extension objects and loops over them. It executes the given
// predicateFunc for each of them, and if it evaluates to true, then it waits for the object to be deleted.
// If the component needs to wait for a given subset of all extension objects to be deleted (e.g. after deleting
// unwanted objects), it should pass a predicateFunc that filters objects to wait for by name.
func WaitUntilExtensionObjectsDeleted(
ctx context.Context,
c client.Client,
log logr.Logger,
listObj client.ObjectList,
kind string,
namespace string,
interval time.Duration,
timeout time.Duration,
predicateFunc func(obj extensionsv1alpha1.Object) bool,
) error {
fns, err := applyFuncToExtensionObjects(ctx, c, listObj, namespace, predicateFunc, func(ctx context.Context, obj extensionsv1alpha1.Object) error {
return WaitUntilExtensionObjectDeleted(ctx, c, log, obj, kind, interval, timeout)
})
if err != nil {
return err
}
return flow.Parallel(fns...)(ctx)
}
// WaitUntilExtensionObjectDeleted waits until an extension oject is deleted from the system.
// Passed objects are expected to be filled with the latest state the controller/component
// observed/retrieved, but at least namespace and name.
func WaitUntilExtensionObjectDeleted(
ctx context.Context,
c client.Client,
log logr.Logger,
obj extensionsv1alpha1.Object,
kind string,
interval time.Duration,
timeout time.Duration,
) error {
var (
lastObservedError error
name = obj.GetName()
namespace = obj.GetNamespace()
)
if err := retry.UntilTimeout(ctx, interval, timeout, func(ctx context.Context) (bool, error) {
if err := c.Get(ctx, client.ObjectKeyFromObject(obj), obj); err != nil {
if apierrors.IsNotFound(err) {
return retry.Ok()
}
return retry.SevereError(err)
}
if lastErr := obj.GetExtensionStatus().GetLastError(); lastErr != nil {
log.Error(fmt.Errorf(lastErr.Description), "Object did not get deleted yet")
lastObservedError = gardencorev1beta1helper.NewErrorWithCodes(errors.New(lastErr.Description), lastErr.Codes...)
}
var message = fmt.Sprintf("%s is still present", extensionKey(kind, namespace, name))
if lastObservedError != nil {
message += fmt.Sprintf(", last observed error: %s", lastObservedError.Error())
}
return retry.MinorError(fmt.Errorf(message))
}); err != nil {
message := fmt.Sprintf("Failed to delete %s", extensionKey(kind, namespace, name))
if lastObservedError != nil {
return fmt.Errorf("%s: %w", message, lastObservedError)
}
return fmt.Errorf("%s: %w", message, err)
}
return nil
}
// RestoreExtensionWithDeployFunction deploys the extension object with the passed in deployFunc and sets its operation annotation to wait-for-state.
// It then restores the state of the extension object from the ShootState, creates any required state object and sets the operation annotation to restore.
func RestoreExtensionWithDeployFunction(
ctx context.Context,
c client.Client,
shootState *gardencorev1alpha1.ShootState,
kind string,
deployFunc func(ctx context.Context, operationAnnotation string) (extensionsv1alpha1.Object, error),
) error {
extensionObj, err := deployFunc(ctx, v1beta1constants.GardenerOperationWaitForState)
if err != nil {
return err
}
if err := RestoreExtensionObjectState(ctx, c, shootState, extensionObj, kind); err != nil {
return err
}
return AnnotateObjectWithOperation(ctx, c, extensionObj, v1beta1constants.GardenerOperationRestore)
}
// RestoreExtensionObjectState restores the status.state field of the extension objects and deploys any required objects from the provided shoot state
func RestoreExtensionObjectState(
ctx context.Context,
c client.Client,
shootState *gardencorev1alpha1.ShootState,
extensionObj extensionsv1alpha1.Object,
kind string,
) error {
var resourceRefs []autoscalingv1.CrossVersionObjectReference
if shootState.Spec.Extensions != nil {
resourceName := extensionObj.GetName()
purpose := extensionObj.GetExtensionSpec().GetExtensionPurpose()
list := gardencorev1alpha1helper.ExtensionResourceStateList(shootState.Spec.Extensions)
if extensionResourceState := list.Get(kind, &resourceName, purpose); extensionResourceState != nil {
patch := client.MergeFrom(extensionObj.DeepCopyObject().(client.Object))
extensionStatus := extensionObj.GetExtensionStatus()
extensionStatus.SetState(extensionResourceState.State)
extensionStatus.SetResources(extensionResourceState.Resources)
if err := c.Status().Patch(ctx, extensionObj, patch); err != nil {
return err
}
for _, r := range extensionResourceState.Resources {
resourceRefs = append(resourceRefs, r.ResourceRef)
}
}
}
if shootState.Spec.Resources != nil {
list := gardencorev1alpha1helper.ResourceDataList(shootState.Spec.Resources)
for _, resourceRef := range resourceRefs {
resourceData := list.Get(&resourceRef)
if resourceData != nil {
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&resourceData.Data)
if err != nil {
return err
}
if err := unstructuredutils.CreateOrPatchObjectByRef(ctx, c, &resourceRef, extensionObj.GetNamespace(), obj); err != nil {
return err
}
}
}
}
return nil
}
// MigrateExtensionObject adds the migrate operation annotation to the extension object.
// Passed objects are expected to be filled with the latest state the controller/component
// observed/retrieved, but at least namespace and name.
func MigrateExtensionObject(
ctx context.Context,
c client.Writer,
obj extensionsv1alpha1.Object,
) error {
return client.IgnoreNotFound(AnnotateObjectWithOperation(ctx, c, obj, v1beta1constants.GardenerOperationMigrate))
}
// MigrateExtensionObjects lists all extension objects of a given kind and annotates them with the Migrate operation.
func MigrateExtensionObjects(
ctx context.Context,
c client.Client,
listObj client.ObjectList,
namespace string,
) error {
fns, err := applyFuncToExtensionObjects(ctx, c, listObj, namespace, nil, func(ctx context.Context, obj extensionsv1alpha1.Object) error {
return MigrateExtensionObject(ctx, c, obj)
})
if err != nil {
return err
}
return flow.Parallel(fns...)(ctx)
}
// WaitUntilExtensionObjectMigrated waits until the migrate operation for the extension object is successful.
// Passed objects are expected to be filled with the latest state the controller/component
// observed/retrieved, but at least namespace and name.
func WaitUntilExtensionObjectMigrated(
ctx context.Context,
c client.Client,
obj extensionsv1alpha1.Object,
kind string,
interval time.Duration,
timeout time.Duration,
) error {
return retry.UntilTimeout(ctx, interval, timeout, func(ctx context.Context) (done bool, err error) {
if err := c.Get(ctx, client.ObjectKeyFromObject(obj), obj); err != nil {
if client.IgnoreNotFound(err) == nil {
return retry.Ok()
}
return retry.SevereError(err)
}
if extensionObjStatus := obj.GetExtensionStatus(); extensionObjStatus != nil {
if lastOperation := extensionObjStatus.GetLastOperation(); lastOperation != nil {
if lastOperation.Type == gardencorev1beta1.LastOperationTypeMigrate && lastOperation.State == gardencorev1beta1.LastOperationStateSucceeded {
return retry.Ok()
}
}
}
return retry.MinorError(fmt.Errorf("error while waiting for %s to be successfully migrated", extensionKey(kind, obj.GetNamespace(), obj.GetName())))
})
}
// WaitUntilExtensionObjectsMigrated lists all extension objects of a given kind and waits until they are migrated.
func WaitUntilExtensionObjectsMigrated(
ctx context.Context,
c client.Client,
listObj client.ObjectList,
kind string,
namespace string,
interval time.Duration,
timeout time.Duration,
) error {
fns, err := applyFuncToExtensionObjects(ctx, c, listObj, namespace, nil, func(ctx context.Context, obj extensionsv1alpha1.Object) error {
return WaitUntilExtensionObjectMigrated(ctx, c, obj, kind, interval, timeout)
})
if err != nil {
return err
}
return flow.Parallel(fns...)(ctx)
}
// AnnotateObjectWithOperation annotates the object with the provided operation annotation value.
func AnnotateObjectWithOperation(ctx context.Context, w client.Writer, obj client.Object, operation string) error {
patch := client.MergeFrom(obj.DeepCopyObject().(client.Object))
kutil.SetMetaDataAnnotation(obj, v1beta1constants.GardenerOperation, operation)
kutil.SetMetaDataAnnotation(obj, v1beta1constants.GardenerTimestamp, TimeNow().UTC().String())
return w.Patch(ctx, obj, patch)
}
func applyFuncToExtensionObjects(
ctx context.Context,
c client.Reader,
listObj client.ObjectList,
namespace string,
predicateFunc func(obj extensionsv1alpha1.Object) bool,
applyFunc func(ctx context.Context, object extensionsv1alpha1.Object) error,
) ([]flow.TaskFn, error) {
if err := c.List(ctx, listObj, client.InNamespace(namespace)); err != nil {
return nil, err
}
fns := make([]flow.TaskFn, 0, meta.LenList(listObj))
if err := meta.EachListItem(listObj, func(obj runtime.Object) error {
o, ok := obj.(extensionsv1alpha1.Object)
if !ok {
return fmt.Errorf("expected extensionsv1alpha1.Object but got %T", obj)
}
if predicateFunc != nil && !predicateFunc(o) {
return nil
}
fns = append(fns, func(ctx context.Context) error {
return applyFunc(ctx, o)
})
return nil
}); err != nil {
return nil, err
}
return fns, nil
}
func extensionKey(kind, namespace, name string) string {
return fmt.Sprintf("%s %s/%s", kind, namespace, name)
}