github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/dataprotection/actionset_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  // ActionSetReconciler reconciles a ActionSet object
    37  type ActionSetReconciler struct {
    38  	client.Client
    39  	Scheme   *runtime.Scheme
    40  	Recorder record.EventRecorder
    41  }
    42  
    43  // +kubebuilder:rbac:groups=dataprotection.kubeblocks.io,resources=actionsets,verbs=get;list;watch;create;update;patch;delete
    44  // +kubebuilder:rbac:groups=dataprotection.kubeblocks.io,resources=actionsets/status,verbs=get;update;patch
    45  // +kubebuilder:rbac:groups=dataprotection.kubeblocks.io,resources=actionsets/finalizers,verbs=update
    46  
    47  // Reconcile is part of the main kubernetes reconciliation loop which aims to
    48  // move the current state of the actionset closer to the desired state.
    49  func (r *ActionSetReconciler) 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("actionSet", req.Name),
    54  		Recorder: r.Recorder,
    55  	}
    56  
    57  	actionSet := &dpv1alpha1.ActionSet{}
    58  	if err := r.Client.Get(reqCtx.Ctx, reqCtx.Req.NamespacedName, actionSet); err != nil {
    59  		return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "")
    60  	}
    61  
    62  	// handle finalizer
    63  	res, err := intctrlutil.HandleCRDeletion(reqCtx, r, actionSet, dptypes.DataProtectionFinalizerName,
    64  		func() (*ctrl.Result, error) {
    65  			return nil, r.deleteExternalResources(reqCtx, actionSet)
    66  		})
    67  	if res != nil {
    68  		return *res, err
    69  	}
    70  
    71  	if actionSet.Status.ObservedGeneration == actionSet.Generation &&
    72  		actionSet.Status.Phase.IsAvailable() {
    73  		return ctrl.Result{}, nil
    74  	}
    75  
    76  	patchStatus := func(phase dpv1alpha1.Phase, message string) error {
    77  		patch := client.MergeFrom(actionSet.DeepCopy())
    78  		actionSet.Status.Phase = phase
    79  		actionSet.Status.Message = message
    80  		actionSet.Status.ObservedGeneration = actionSet.Generation
    81  		return r.Client.Status().Patch(reqCtx.Ctx, actionSet, patch)
    82  	}
    83  
    84  	// TODO(ldm): validate actionSet
    85  
    86  	if err = patchStatus(dpv1alpha1.AvailablePhase, ""); err != nil {
    87  		return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "")
    88  	}
    89  	intctrlutil.RecordCreatedEvent(r.Recorder, actionSet)
    90  	return ctrl.Result{}, nil
    91  }
    92  
    93  // SetupWithManager sets up the controller with the Manager.
    94  func (r *ActionSetReconciler) SetupWithManager(mgr ctrl.Manager) error {
    95  	return ctrl.NewControllerManagedBy(mgr).
    96  		For(&dpv1alpha1.ActionSet{}).Complete(r)
    97  }
    98  
    99  func (r *ActionSetReconciler) deleteExternalResources(
   100  	_ intctrlutil.RequestCtx,
   101  	_ *dpv1alpha1.ActionSet) error {
   102  	return nil
   103  }