-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathcluster_restore_controller.go
More file actions
198 lines (175 loc) · 7.82 KB
/
Copy pathcluster_restore_controller.go
File metadata and controls
198 lines (175 loc) · 7.82 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
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
/*
Copyright (C) 2022-2026 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package dataprotection
import (
"context"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
dpv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
dprestore "github.com/apecloud/kubeblocks/pkg/dataprotection/restore"
dptypes "github.com/apecloud/kubeblocks/pkg/dataprotection/types"
)
// ClusterRestoreReconciler coordinates the Cluster-level restore lifecycle.
type ClusterRestoreReconciler struct {
client.Client
Recorder record.EventRecorder
}
// +kubebuilder:rbac:groups=apps.kubeblocks.io,resources=clusters,verbs=get;list;watch;patch;update
// +kubebuilder:rbac:groups=apps.kubeblocks.io,resources=clusters/finalizers,verbs=update;patch
// +kubebuilder:rbac:groups=dataprotection.kubeblocks.io,resources=restores,verbs=get;list;watch
// +kubebuilder:rbac:groups=core,resources=persistentvolumeclaims,verbs=get;list;watch
func (r *ClusterRestoreReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
reqCtx := intctrlutil.RequestCtx{
Ctx: ctx, Req: req,
Log: log.FromContext(ctx).WithValues("cluster-restore", req.NamespacedName),
Recorder: r.Recorder,
}
cluster := &appsv1.Cluster{}
if err := r.Client.Get(ctx, req.NamespacedName, cluster); err != nil {
return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "")
}
restoring, err := r.isClusterRestoring(ctx, cluster)
if err != nil {
return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "failed to determine Cluster restore state")
}
if !restoring {
return r.releaseClusterRestoreProtection(reqCtx, cluster)
}
if !isClusterRestoreProtected(cluster) {
// If deletion began without our finalizer, this controller cannot acquire
// protection and must not delay Cluster deletion.
if !cluster.DeletionTimestamp.IsZero() {
return intctrlutil.Reconciled()
}
return r.protectClusterRestore(reqCtx, cluster)
}
if !cluster.DeletionTimestamp.IsZero() {
return intctrlutil.RequeueAfter(reconcileInterval, reqCtx.Log,
"waiting for restore owners to finish Cluster termination")
}
return intctrlutil.Reconciled()
}
func (r *ClusterRestoreReconciler) SetupWithManager(mgr ctrl.Manager) error {
return intctrlutil.NewControllerManagedBy(mgr).
For(&appsv1.Cluster{}).
Watches(&dpv1alpha1.Restore{}, handler.EnqueueRequestsFromMapFunc(r.mapObjectToCluster)).
Watches(&corev1.PersistentVolumeClaim{}, handler.EnqueueRequestsFromMapFunc(r.mapObjectToCluster)).
Complete(r)
}
func (r *ClusterRestoreReconciler) mapObjectToCluster(_ context.Context, obj client.Object) []reconcile.Request {
clusterName := obj.GetLabels()[constant.AppInstanceLabelKey]
if clusterName == "" {
return nil
}
return []reconcile.Request{{NamespacedName: client.ObjectKey{Namespace: obj.GetNamespace(), Name: clusterName}}}
}
func (r *ClusterRestoreReconciler) isClusterRestoring(ctx context.Context,
cluster *appsv1.Cluster) (bool, error) {
if cluster.DeletionTimestamp.IsZero() && clusterAllowsRestoreProgress(cluster) {
return true, nil
}
// Inspect PVCs before Restores. VP releases temporary target protection only
// after observing postReady Restore, so this order closes the handoff window.
pvcs := &corev1.PersistentVolumeClaimList{}
if err := r.Client.List(ctx, pvcs, client.InNamespace(cluster.Namespace), client.MatchingLabels{
constant.AppInstanceLabelKey: cluster.Name,
}); err != nil {
return false, err
}
for i := range pvcs.Items {
pvc := &pvcs.Items[i]
if pvc.Labels[dptypes.ClusterUIDLabelKey] != string(cluster.UID) {
continue
}
if isClusterRestoreHelperPVC(pvc) ||
(isClusterRestoreTargetPVC(pvc) && controllerutil.ContainsFinalizer(pvc, dptypes.DataProtectionFinalizerName)) {
return true, nil
}
}
restores := &dpv1alpha1.RestoreList{}
if err := r.Client.List(ctx, restores, client.InNamespace(cluster.Namespace), client.MatchingLabels{
constant.AppInstanceLabelKey: cluster.Name,
}); err != nil {
return false, err
}
for i := range restores.Items {
restore := &restores.Items[i]
if restore.Labels[dprestore.DataProtectionRestoreLabelKey] != restore.Name {
continue
}
owned := restore.Labels[dptypes.ClusterUIDLabelKey] == string(cluster.UID)
terminal := restore.Status.Phase == dpv1alpha1.RestorePhaseCompleted ||
restore.Status.Phase == dpv1alpha1.RestorePhaseFailed
if owned && (!cluster.DeletionTimestamp.IsZero() || !terminal || !restore.DeletionTimestamp.IsZero()) {
return true, nil
}
}
return false, nil
}
func clusterAllowsRestoreProgress(cluster *appsv1.Cluster) bool {
if cluster.Spec.Restore == nil {
return false
}
condition := meta.FindStatusCondition(cluster.Status.Conditions, appsv1.ConditionTypeRestore)
// Restore=False is terminal for status aggregation, but the Cluster still
// has initial-restore intent. Keep the lifecycle active until deletion so
// PVC restores cannot lose protection while converging on the failure.
return condition == nil || condition.Status != metav1.ConditionTrue
}
func isClusterRestoreProtected(cluster *appsv1.Cluster) bool {
return controllerutil.ContainsFinalizer(cluster, dptypes.RestoreProtectionFinalizerName)
}
func (r *ClusterRestoreReconciler) protectClusterRestore(reqCtx intctrlutil.RequestCtx,
cluster *appsv1.Cluster) (ctrl.Result, error) {
if isClusterRestoreProtected(cluster) {
return intctrlutil.Reconciled()
}
patch := client.MergeFromWithOptions(cluster.DeepCopy(), client.MergeFromWithOptimisticLock{})
controllerutil.AddFinalizer(cluster, dptypes.RestoreProtectionFinalizerName)
if err := r.Client.Patch(reqCtx.Ctx, cluster, patch); err != nil {
return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "failed to add Cluster restore-protection finalizer")
}
return intctrlutil.Reconciled()
}
func (r *ClusterRestoreReconciler) releaseClusterRestoreProtection(reqCtx intctrlutil.RequestCtx,
cluster *appsv1.Cluster) (ctrl.Result, error) {
if !isClusterRestoreProtected(cluster) {
return intctrlutil.Reconciled()
}
patch := client.MergeFromWithOptions(cluster.DeepCopy(), client.MergeFromWithOptimisticLock{})
controllerutil.RemoveFinalizer(cluster, dptypes.RestoreProtectionFinalizerName)
if err := r.Client.Patch(reqCtx.Ctx, cluster, patch); err != nil {
return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "failed to remove Cluster restore-protection finalizer")
}
return intctrlutil.Reconciled()
}
func isClusterRestoreHelperPVC(pvc *corev1.PersistentVolumeClaim) bool {
return pvc.Labels[dprestore.DataProtectionPopulatePVCLabelKey] != ""
}
func isClusterRestoreTargetPVC(pvc *corev1.PersistentVolumeClaim) bool {
return pvc.Spec.DataSourceRef != nil && pvc.Spec.DataSourceRef.APIGroup != nil &&
*pvc.Spec.DataSourceRef.APIGroup == dptypes.DataprotectionAPIGroup
}