github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/backuppolicytemplate_controller.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package apps 18 19 import ( 20 "context" 21 22 k8sruntime "k8s.io/apimachinery/pkg/runtime" 23 "k8s.io/client-go/tools/record" 24 ctrl "sigs.k8s.io/controller-runtime" 25 "sigs.k8s.io/controller-runtime/pkg/client" 26 "sigs.k8s.io/controller-runtime/pkg/log" 27 "sigs.k8s.io/controller-runtime/pkg/reconcile" 28 29 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 30 "github.com/1aal/kubeblocks/pkg/constant" 31 intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil" 32 ) 33 34 // +kubebuilder:rbac:groups=apps.kubeblocks.io,resources=backuppolicytemplates,verbs=get;list;watch;create;update;patch;delete 35 // +kubebuilder:rbac:groups=apps.kubeblocks.io,resources=backuppolicytemplates/status,verbs=get;update;patch 36 // +kubebuilder:rbac:groups=apps.kubeblocks.io,resources=backuppolicytemplates/finalizers,verbs=update 37 38 type BackupPolicyTemplateReconciler struct { 39 client.Client 40 Scheme *k8sruntime.Scheme 41 Recorder record.EventRecorder 42 } 43 44 func (r *BackupPolicyTemplateReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { 45 reqCtx := intctrlutil.RequestCtx{ 46 Ctx: ctx, 47 Req: req, 48 Log: log.FromContext(ctx).WithValues("backupPolicyTemplate", req.NamespacedName), 49 Recorder: r.Recorder, 50 } 51 52 backupPolicyTemplate := &appsv1alpha1.BackupPolicyTemplate{} 53 if err := r.Client.Get(reqCtx.Ctx, reqCtx.Req.NamespacedName, backupPolicyTemplate); err != nil { 54 return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "") 55 } 56 57 // infer clusterDefRef from spec.clusterDefRef 58 backupPolicyTemplate.Labels[constant.ClusterDefLabelKey] = backupPolicyTemplate.Spec.ClusterDefRef 59 60 if err := r.Client.Update(reqCtx.Ctx, backupPolicyTemplate); err != nil { 61 return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "") 62 } 63 64 return intctrlutil.Reconciled() 65 } 66 67 // SetupWithManager sets up the controller with the Manager. 68 func (r *BackupPolicyTemplateReconciler) SetupWithManager(mgr ctrl.Manager) error { 69 return ctrl.NewControllerManagedBy(mgr). 70 For(&appsv1alpha1.BackupPolicyTemplate{}). 71 Complete(r) 72 }