forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreconciler.go
More file actions
94 lines (78 loc) · 3.74 KB
/
reconciler.go
File metadata and controls
94 lines (78 loc) · 3.74 KB
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
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package shootstate
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
controllermanagerconfigv1alpha1 "github.com/gardener/gardener/pkg/controllermanager/apis/config/v1alpha1"
"github.com/gardener/gardener/pkg/controllerutils"
)
// FinalizerName is the finalizer used by the controller.
const FinalizerName = "core.gardener.cloud/shootstate"
// Reconciler reconciles ShootState objects and ensures the finalizer
// exists during "Migrate" and "Restore" phases of Shoot migration to
// another Seed.
type Reconciler struct {
// Client is the API Server client used by the Reconciler.
Client client.Client
// Config is the configuration used by the Reconciler.
Config controllermanagerconfigv1alpha1.ShootStateControllerConfiguration
}
// Reconcile reconciles ShootStates.
func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
log := logf.FromContext(ctx)
shootState := &gardencorev1beta1.ShootState{}
if err := r.Client.Get(ctx, request.NamespacedName, shootState); err != nil {
if apierrors.IsNotFound(err) {
log.V(1).Info("Object is gone, stop reconciling")
// Since reconciliation runs on Shoot update, we should not
// flood the logs with errors when a migration is not initiated
// and the `ShootState` is not supposed to exist.
return reconcile.Result{}, nil
}
return reconcile.Result{}, fmt.Errorf("error retrieving object from store: %w", err)
}
shoot := &gardencorev1beta1.Shoot{}
if err := r.Client.Get(ctx, request.NamespacedName, shoot); err != nil {
return reconcile.Result{}, fmt.Errorf("error retrieving Shoot from store: %w", err)
}
if shoot.Status.LastOperation == nil {
return reconcile.Result{}, fmt.Errorf("last operation of Shoot '%s' is not set", request.NamespacedName)
}
var (
shootLastOperationType = shoot.Status.LastOperation.Type
shootLastOperationState = shoot.Status.LastOperation.State
isShootOpMigrate = shootLastOperationType == gardencorev1beta1.LastOperationTypeMigrate
isShootOpRestore = shootLastOperationType == gardencorev1beta1.LastOperationTypeRestore
isShootOpReconcile = shootLastOperationType == gardencorev1beta1.LastOperationTypeReconcile
isShootOpSucceeded = shootLastOperationState == gardencorev1beta1.LastOperationStateSucceeded
shootStateHasFinalizer = controllerutil.ContainsFinalizer(shootState, FinalizerName)
)
log = log.
WithValues("shootLastOperationType", shootLastOperationType).
WithValues("shootLastOperationState", shootLastOperationState)
isShootRestoreNotSucceeded := isShootOpRestore && !isShootOpSucceeded
if (isShootOpMigrate || isShootRestoreNotSucceeded) && !shootStateHasFinalizer {
log.Info("Adding finalizer")
if err := controllerutils.AddFinalizers(ctx, r.Client, shootState, FinalizerName); err != nil {
return reconcile.Result{}, fmt.Errorf("error adding finalizer to ShootState: %w", err)
}
return reconcile.Result{}, nil
}
isShootRestoreSucceeded := isShootOpRestore && isShootOpSucceeded
if (isShootRestoreSucceeded || isShootOpReconcile) && shootStateHasFinalizer {
log.Info("Removing finalizer")
if err := controllerutils.RemoveFinalizers(ctx, r.Client, shootState, FinalizerName); err != nil {
return reconcile.Result{}, fmt.Errorf("error removing finalizer from ShootState: %w", err)
}
return reconcile.Result{}, nil
}
return reconcile.Result{}, nil
}