github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/dataprotection/backuppolicy_controller.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package dataprotection 21 22 import ( 23 "context" 24 25 "k8s.io/apimachinery/pkg/runtime" 26 "k8s.io/client-go/tools/record" 27 ctrl "sigs.k8s.io/controller-runtime" 28 "sigs.k8s.io/controller-runtime/pkg/client" 29 "sigs.k8s.io/controller-runtime/pkg/log" 30 31 dpv1alpha1 "github.com/1aal/kubeblocks/apis/dataprotection/v1alpha1" 32 intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil" 33 dptypes "github.com/1aal/kubeblocks/pkg/dataprotection/types" 34 ) 35 36 // BackupPolicyReconciler reconciles a BackupPolicy object 37 type BackupPolicyReconciler struct { 38 client.Client 39 Scheme *runtime.Scheme 40 Recorder record.EventRecorder 41 } 42 43 // +kubebuilder:rbac:groups=dataprotection.kubeblocks.io,resources=backuppolicies,verbs=get;list;watch;create;update;patch;delete 44 // +kubebuilder:rbac:groups=dataprotection.kubeblocks.io,resources=backuppolicies/status,verbs=get;update;patch 45 // +kubebuilder:rbac:groups=dataprotection.kubeblocks.io,resources=backuppolicies/finalizers,verbs=update 46 47 // Reconcile is part of the main kubernetes reconciliation loop which aims to 48 // move the current state of the backuppolicy closer to the desired state. 49 func (r *BackupPolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { 50 reqCtx := intctrlutil.RequestCtx{ 51 Ctx: ctx, 52 Req: req, 53 Log: log.FromContext(ctx).WithValues("backupPolicy", req.NamespacedName), 54 Recorder: r.Recorder, 55 } 56 57 backupPolicy := &dpv1alpha1.BackupPolicy{} 58 if err := r.Client.Get(reqCtx.Ctx, reqCtx.Req.NamespacedName, backupPolicy); err != nil { 59 return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "") 60 } 61 62 // handle finalizer 63 res, err := intctrlutil.HandleCRDeletion(reqCtx, r, backupPolicy, dptypes.DataProtectionFinalizerName, 64 func() (*ctrl.Result, error) { 65 return nil, r.deleteExternalResources(reqCtx, backupPolicy) 66 }) 67 if res != nil { 68 return *res, err 69 } 70 71 if backupPolicy.Status.ObservedGeneration == backupPolicy.Generation && 72 backupPolicy.Status.Phase.IsAvailable() { 73 return ctrl.Result{}, nil 74 } 75 76 patchStatus := func(phase dpv1alpha1.Phase, message string) error { 77 patch := client.MergeFrom(backupPolicy.DeepCopy()) 78 backupPolicy.Status.Phase = phase 79 backupPolicy.Status.Message = message 80 backupPolicy.Status.ObservedGeneration = backupPolicy.Generation 81 return r.Status().Patch(ctx, backupPolicy, patch) 82 } 83 84 // TODO(ldm): validate backup policy 85 86 if err = patchStatus(dpv1alpha1.AvailablePhase, ""); err != nil { 87 return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "") 88 } 89 intctrlutil.RecordCreatedEvent(r.Recorder, backupPolicy) 90 return ctrl.Result{}, nil 91 } 92 93 // SetupWithManager sets up the controller with the Manager. 94 func (r *BackupPolicyReconciler) SetupWithManager(mgr ctrl.Manager) error { 95 return ctrl.NewControllerManagedBy(mgr). 96 For(&dpv1alpha1.BackupPolicy{}). 97 Complete(r) 98 } 99 100 func (r *BackupPolicyReconciler) deleteExternalResources( 101 _ intctrlutil.RequestCtx, 102 _ *dpv1alpha1.BackupPolicy) error { 103 return nil 104 }