Skip to content

Commit

Permalink
Add Watch of Velero Backup and remove Backup controller (#12)
Browse files Browse the repository at this point in the history
Signed-off-by: Michal Pryc <[email protected]>
Co-authored-by: Tiger Kaovilai <[email protected]>
  • Loading branch information
mpryc and kaovilai authored Mar 6, 2024
1 parent 065eed6 commit 5bdd750
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 149 deletions.
5 changes: 0 additions & 5 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,4 @@ resources:
kind: NonAdminBackup
path: github.com/migtools/oadp-non-admin/api/v1alpha1
version: v1alpha1
- controller: true
domain: oadp.openshift.io
group: nac.oadp.openshift.io
kind: VeleroBackup
version: v1alpha1
version: "3"
8 changes: 0 additions & 8 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,6 @@ func main() {
}
//+kubebuilder:scaffold:builder

if err = (&controller.VeleroBackupReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "VeleroBackup")
os.Exit(1)
}

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
Expand Down
1 change: 0 additions & 1 deletion config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ rules:
- backups
verbs:
- create
- delete
- get
- list
- patch
Expand Down
27 changes: 27 additions & 0 deletions internal/controller/common_nab.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package controller

import (
"context"
"crypto/sha1"
"encoding/hex"
"fmt"
"reflect"

"github.com/go-logr/logr"
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

nacv1alpha1 "github.com/migtools/oadp-non-admin/api/v1alpha1"
)
Expand Down Expand Up @@ -48,3 +52,26 @@ func GenerateVeleroBackupName(namespace, nabName string) string {

return veleroBackupName
}

func UpdateNonAdminBackupFromVeleroBackup(ctx context.Context, r client.Client, log logr.Logger, nab *nacv1alpha1.NonAdminBackup, veleroBackup *velerov1api.Backup) error {
// Make a copy of the current status for comparison
oldStatus := nab.Spec.BackupStatus.DeepCopy()
oldSpec := nab.Spec.BackupSpec.DeepCopy()

// Update the status & spec
nab.Spec.BackupStatus = &veleroBackup.Status
nab.Spec.BackupSpec = &veleroBackup.Spec

if reflect.DeepEqual(oldStatus, nab.Spec.BackupStatus) && reflect.DeepEqual(oldSpec, nab.Spec.BackupSpec) {
// No change, no need to update
log.V(1).Info("NonAdminBackup status and spec is already up to date")
return nil
}

if err := r.Update(ctx, nab); err != nil {
log.Error(err, "Failed to update NonAdminBackup")
return err
}

return nil
}
51 changes: 51 additions & 0 deletions internal/controller/composite_predicate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// composite_predicate.go

package controller

import (
"context"

"sigs.k8s.io/controller-runtime/pkg/event"
)

type CompositePredicate struct {
NonAdminBackupPredicate NonAdminBackupPredicate
VeleroBackupPredicate VeleroBackupPredicate
Context context.Context
}

func (p CompositePredicate) Create(evt event.CreateEvent) bool {
// If NonAdminBackupPredicate returns true, ignore VeleroBackupPredicate
if p.NonAdminBackupPredicate.Create(p.Context, evt) {
return true
}
// Otherwise, apply VeleroBackupPredicate
return p.VeleroBackupPredicate.Create(p.Context, evt)
}

func (p CompositePredicate) Update(evt event.UpdateEvent) bool {
// If NonAdminBackupPredicate returns true, ignore VeleroBackupPredicate
if p.NonAdminBackupPredicate.Update(p.Context, evt) {
return true
}
// Otherwise, apply VeleroBackupPredicate
return p.VeleroBackupPredicate.Update(p.Context, evt)
}

func (p CompositePredicate) Delete(evt event.DeleteEvent) bool {
// If NonAdminBackupPredicate returns true, ignore VeleroBackupPredicate
if p.NonAdminBackupPredicate.Delete(p.Context, evt) {
return true
}
// Otherwise, apply VeleroBackupPredicate
return p.VeleroBackupPredicate.Delete(p.Context, evt)
}

func (p CompositePredicate) Generic(evt event.GenericEvent) bool {
// If NonAdminBackupPredicate returns true, ignore VeleroBackupPredicate
if p.NonAdminBackupPredicate.Generic(p.Context, evt) {
return true
}
// Otherwise, apply VeleroBackupPredicate
return p.VeleroBackupPredicate.Generic(p.Context, evt)
}
20 changes: 17 additions & 3 deletions internal/controller/nonadminbackup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type NonAdminBackupReconciler struct {
//+kubebuilder:rbac:groups=nac.oadp.openshift.io,resources=nonadminbackups/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=nac.oadp.openshift.io,resources=nonadminbackups/finalizers,verbs=update

//+kubebuilder:rbac:groups=velero.io,resources=backups,verbs=get;list;watch;create;update;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.
// TODO(user): Modify the Reconcile function to compare the state specified by
Expand Down Expand Up @@ -79,7 +81,7 @@ func (r *NonAdminBackupReconciler) Reconcile(ctx context.Context, req ctrl.Reque
veleroBackupSpec, err := GetVeleroBackupSpecFromNonAdminBackup(&nab)

if veleroBackupSpec == nil {
log.Error(err, "unable to fetch VeleroBackupSpec from NonAdminBackup")
log.Error(err, "NonAdminBackup CR does not contain valid VeleroBackupSpec")
return ctrl.Result{}, nil
}

Expand All @@ -106,10 +108,14 @@ func (r *NonAdminBackupReconciler) Reconcile(ctx context.Context, req ctrl.Reque
Spec: *veleroBackupSpec,
}
} else if err != nil && !errors.IsNotFound(err) {
log.Error(err, "unable to fetch VeleroBackup")
log.Error(err, "Unable to fetch VeleroBackup")
return ctrl.Result{}, err
} else {
log.Info("Backup already exists", "Name", veleroBackupName)
log.Info("Backup already exists, updating NonAdminBackup status", "Name", veleroBackupName)
err := UpdateNonAdminBackupFromVeleroBackup(ctx, r.Client, log, &nab, &veleroBackup)
if err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -142,5 +148,13 @@ func (r *NonAdminBackupReconciler) Reconcile(ctx context.Context, req ctrl.Reque
func (r *NonAdminBackupReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&nacv1alpha1.NonAdminBackup{}).
Watches(&velerov1api.Backup{}, &VeleroBackupHandler{}).
WithEventFilter(CompositePredicate{
NonAdminBackupPredicate: NonAdminBackupPredicate{},
VeleroBackupPredicate: VeleroBackupPredicate{
OadpVeleroNamespace: "openshift-adp",
},
Context: r.Context,
}).
Complete(r)
}
67 changes: 67 additions & 0 deletions internal/controller/nonadminbackup_predicate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// velerobackup_predicate.go

package controller

import (
"context"

"github.com/go-logr/logr"
nacv1alpha1 "github.com/migtools/oadp-non-admin/api/v1alpha1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
)

type NonAdminBackupPredicate struct {
Logger logr.Logger
}

func getNonAdminBackupPredicateLogger(ctx context.Context, name, namespace string) logr.Logger {
return log.FromContext(ctx).WithValues("NonAdminBackupPredicate", types.NamespacedName{Name: name, Namespace: namespace})
}

func (predicate NonAdminBackupPredicate) Create(ctx context.Context, evt event.CreateEvent) bool {

if _, ok := evt.Object.(*nacv1alpha1.NonAdminBackup); ok {
nameSpace := evt.Object.GetNamespace()
name := evt.Object.GetName()
log := getNonAdminBackupPredicateLogger(ctx, name, nameSpace)
log.V(1).Info("Received Create NonAdminBackupPredicate")
return true
}

return false
}

func (predicate NonAdminBackupPredicate) Update(ctx context.Context, evt event.UpdateEvent) bool {
if _, ok := evt.ObjectNew.(*nacv1alpha1.NonAdminBackup); ok {
nameSpace := evt.ObjectNew.GetNamespace()
name := evt.ObjectNew.GetName()
log := getNonAdminBackupPredicateLogger(ctx, name, nameSpace)
log.V(1).Info("Received Update NonAdminBackupPredicate")
return true
}
return false
}

func (predicate NonAdminBackupPredicate) Delete(ctx context.Context, evt event.DeleteEvent) bool {
if _, ok := evt.Object.(*nacv1alpha1.NonAdminBackup); ok {
nameSpace := evt.Object.GetNamespace()
name := evt.Object.GetName()
log := getNonAdminBackupPredicateLogger(ctx, name, nameSpace)
log.V(1).Info("Received Delete NonAdminBackupPredicate")
return true
}
return false
}

func (predicate NonAdminBackupPredicate) Generic(ctx context.Context, evt event.GenericEvent) bool {
if _, ok := evt.Object.(*nacv1alpha1.NonAdminBackup); ok {
nameSpace := evt.Object.GetNamespace()
name := evt.Object.GetName()
log := getNonAdminBackupPredicateLogger(ctx, name, nameSpace)
log.V(1).Info("Received Generic NonAdminBackupPredicate")
return true
}
return false
}
100 changes: 0 additions & 100 deletions internal/controller/velerobackup_controller.go

This file was deleted.

32 changes: 0 additions & 32 deletions internal/controller/velerobackup_controller_test.go

This file was deleted.

Loading

0 comments on commit 5bdd750

Please sign in to comment.