mirror of
https://github.com/mariadb-operator/mariadb-operator.git
synced 2025-08-08 06:35:58 +00:00
190 lines
6.7 KiB
Go
190 lines
6.7 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
mariadbv1alpha1 "github.com/mariadb-operator/mariadb-operator/v25/api/v1alpha1"
|
|
"github.com/mariadb-operator/mariadb-operator/v25/pkg/builder"
|
|
condition "github.com/mariadb-operator/mariadb-operator/v25/pkg/condition"
|
|
"github.com/mariadb-operator/mariadb-operator/v25/pkg/controller/batch"
|
|
"github.com/mariadb-operator/mariadb-operator/v25/pkg/controller/rbac"
|
|
"github.com/mariadb-operator/mariadb-operator/v25/pkg/refresolver"
|
|
batchv1 "k8s.io/api/batch/v1"
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
)
|
|
|
|
// RestoreReconciler reconciles a restore object
|
|
type RestoreReconciler struct {
|
|
client.Client
|
|
Scheme *runtime.Scheme
|
|
Builder *builder.Builder
|
|
RefResolver *refresolver.RefResolver
|
|
ConditionComplete *condition.Complete
|
|
RBACReconciler *rbac.RBACReconciler
|
|
BatchReconciler *batch.BatchReconciler
|
|
}
|
|
|
|
//+kubebuilder:rbac:groups=k8s.mariadb.com,resources=restores,verbs=get;list;watch;create;update;patch;delete
|
|
//+kubebuilder:rbac:groups=k8s.mariadb.com,resources=restores/status,verbs=get;update;patch
|
|
//+kubebuilder:rbac:groups=k8s.mariadb.com,resources=restores/finalizers,verbs=update
|
|
//+kubebuilder:rbac:groups=batch,resources=jobs,verbs=list;watch;create;patch
|
|
//+kubebuilder:rbac:groups="",resources=serviceaccounts,verbs=list;watch;create;patch
|
|
//+kubebuilder:rbac:groups="",resources=persistentvolumeclaims,verbs=list;watch;create;patch
|
|
|
|
// Reconcile is part of the main kubernetes reconciliation loop which aims to
|
|
// move the current state of the cluster closer to the desired state.
|
|
func (r *RestoreReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
|
var restore mariadbv1alpha1.Restore
|
|
if err := r.Get(ctx, req.NamespacedName, &restore); err != nil {
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
}
|
|
|
|
mariadb, err := r.RefResolver.MariaDB(ctx, &restore.Spec.MariaDBRef, restore.Namespace)
|
|
if err != nil {
|
|
var mariaDbErr *multierror.Error
|
|
mariaDbErr = multierror.Append(mariaDbErr, err)
|
|
|
|
err = r.patchStatus(ctx, &restore, r.ConditionComplete.PatcherRefResolver(err, mariadb))
|
|
mariaDbErr = multierror.Append(mariaDbErr, err)
|
|
|
|
return ctrl.Result{}, fmt.Errorf("error getting MariaDB: %v", mariaDbErr)
|
|
}
|
|
|
|
// We cannot check if mariaDb.IsReady() here and update the status accordingly
|
|
// because we would be creating a deadlock when bootstrapping from backup
|
|
// TODO: add a IsBootstrapping() method to MariaDB?
|
|
|
|
if err := r.setDefaults(ctx, &restore, mariadb); err != nil {
|
|
var sourceErr *multierror.Error
|
|
sourceErr = multierror.Append(sourceErr, err)
|
|
|
|
patchErr := r.patchStatus(
|
|
ctx,
|
|
&restore,
|
|
r.ConditionComplete.PatcherFailed(fmt.Sprintf("error initializing source: %v", err)),
|
|
)
|
|
sourceErr = multierror.Append(sourceErr, patchErr)
|
|
|
|
return ctrl.Result{}, fmt.Errorf("error initializing source: %v", sourceErr)
|
|
}
|
|
|
|
if err := r.reconcileServiceAccount(ctx, &restore); err != nil {
|
|
return ctrl.Result{}, fmt.Errorf("error reconciling ServiceAccount: %v", err)
|
|
}
|
|
|
|
if err := r.BatchReconciler.Reconcile(ctx, &restore, mariadb); err != nil {
|
|
if apierrors.IsNotFound(err) {
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
}
|
|
return ctrl.Result{}, fmt.Errorf("error reconciling batch: %v", err)
|
|
}
|
|
|
|
patcher, err := r.ConditionComplete.PatcherWithJob(ctx, err, req.NamespacedName)
|
|
if err != nil {
|
|
if apierrors.IsNotFound(err) {
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
}
|
|
return ctrl.Result{}, fmt.Errorf("error getting patcher for restore: %v", err)
|
|
}
|
|
|
|
if err = r.patchStatus(ctx, &restore, patcher); err != nil {
|
|
if apierrors.IsNotFound(err) {
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
}
|
|
return ctrl.Result{}, fmt.Errorf("error patching restore status: %v", err)
|
|
}
|
|
return ctrl.Result{}, nil
|
|
}
|
|
|
|
func (r *RestoreReconciler) setDefaults(ctx context.Context, restore *mariadbv1alpha1.Restore,
|
|
mariadb *mariadbv1alpha1.MariaDB) error {
|
|
if err := r.patch(ctx, restore, func(r *mariadbv1alpha1.Restore) error {
|
|
restore.SetDefaults(mariadb)
|
|
r.Spec.RestoreSource.SetDefaults(restore)
|
|
return nil
|
|
}); err != nil {
|
|
return fmt.Errorf("error patching restore: %v", err)
|
|
}
|
|
|
|
if restore.Spec.IsDefaulted() {
|
|
return nil
|
|
}
|
|
if restore.Spec.BackupRef == nil {
|
|
var restoreErr *multierror.Error
|
|
restoreErr = multierror.Append(restoreErr, errors.New("unable to determine restore source, 'backupRef' is nil"))
|
|
|
|
err := r.patchStatus(ctx, restore, r.ConditionComplete.PatcherFailed("Unable to determine restore source"))
|
|
restoreErr = multierror.Append(restoreErr, err)
|
|
|
|
return restoreErr
|
|
}
|
|
|
|
backup, err := r.RefResolver.Backup(ctx, restore.Spec.BackupRef, restore.Namespace)
|
|
if err != nil {
|
|
var backupErr *multierror.Error
|
|
backupErr = multierror.Append(backupErr, err)
|
|
|
|
err = r.patchStatus(ctx, restore, r.ConditionComplete.PatcherRefResolver(err, backup))
|
|
backupErr = multierror.Append(backupErr, err)
|
|
|
|
return fmt.Errorf("error getting Backup: %v", backupErr)
|
|
}
|
|
|
|
if !backup.IsComplete() {
|
|
var errBundle *multierror.Error
|
|
errBundle = multierror.Append(errBundle, errors.New("Backup not complete")) //nolint:staticcheck
|
|
|
|
err := r.patchStatus(ctx, restore, r.ConditionComplete.PatcherFailed("Backup not complete"))
|
|
errBundle = multierror.Append(errBundle, err)
|
|
|
|
return errBundle
|
|
}
|
|
|
|
if err := r.patch(ctx, restore, func(r *mariadbv1alpha1.Restore) error {
|
|
return r.Spec.SetDefaultsWithBackup(backup)
|
|
}); err != nil {
|
|
return fmt.Errorf("error patching restore: %v", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *RestoreReconciler) reconcileServiceAccount(ctx context.Context, restore *mariadbv1alpha1.Restore) error {
|
|
key := restore.Spec.ServiceAccountKey(restore.ObjectMeta)
|
|
_, err := r.RBACReconciler.ReconcileServiceAccount(ctx, key, restore, restore.Spec.InheritMetadata)
|
|
return err
|
|
}
|
|
|
|
func (r *RestoreReconciler) patchStatus(ctx context.Context, restore *mariadbv1alpha1.Restore,
|
|
patcher condition.Patcher) error {
|
|
patch := client.MergeFrom(restore.DeepCopy())
|
|
patcher(&restore.Status)
|
|
|
|
if err := r.Client.Status().Patch(ctx, restore, patch); err != nil {
|
|
return fmt.Errorf("error patching restore status: %v", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *RestoreReconciler) patch(ctx context.Context, restore *mariadbv1alpha1.Restore,
|
|
patcher func(*mariadbv1alpha1.Restore) error) error {
|
|
patch := client.MergeFrom(restore.DeepCopy())
|
|
if err := patcher(restore); err != nil {
|
|
return err
|
|
}
|
|
return r.Patch(ctx, restore, patch)
|
|
}
|
|
|
|
// SetupWithManager sets up the controller with the Manager.
|
|
func (r *RestoreReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
|
return ctrl.NewControllerManagedBy(mgr).
|
|
For(&mariadbv1alpha1.Restore{}).
|
|
Owns(&batchv1.Job{}).
|
|
Complete(r)
|
|
}
|