Skip to content

Commit

Permalink
Adjust the Status of NonAdminBackup
Browse files Browse the repository at this point in the history
Moves Status outside of Spec and adjusts this to reflect
Velero Backup status as well additional Status when the
Spec within NonAdminBackup is not defined.

Signed-off-by: Michal Pryc <[email protected]>
  • Loading branch information
mpryc committed Mar 6, 2024
1 parent 5bdd750 commit 70434b7
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 230 deletions.
6 changes: 2 additions & 4 deletions api/v1alpha1/nonadminbackup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ type NonAdminBackupSpec struct {

// BackupSpec defines the specification for a Velero backup.
BackupSpec *velerov1api.BackupSpec `json:"backupSpec,omitempty"`

// BackupStatus captures the current status of a Velero backup.
BackupStatus *velerov1api.BackupStatus `json:"backupStatus,omitempty"`
}

// NonAdminBackupStatus defines the observed state of NonAdminBackup
type NonAdminBackupStatus struct {
Conditions []metav1.Condition `json:"conditions,omitempty"`
// BackupStatus captures the current status of a Velero backup.
velerov1api.BackupStatus `json:",inline"`
}

//+kubebuilder:object:root=true
Expand Down
14 changes: 1 addition & 13 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

322 changes: 124 additions & 198 deletions config/crd/bases/nac.oadp.openshift.io_nonadminbackups.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ kind: Kustomization
images:
- name: controller
newName: quay.io/migi/oadp-nac-operator
newTag: v0.0.37
newTag: v0.0.39
35 changes: 25 additions & 10 deletions internal/controller/common_nab.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,37 @@ func GenerateVeleroBackupName(namespace, nabName string) string {

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()
oldStatus := nab.Status.BackupStatus.DeepCopy()
oldSpec := nab.Spec.BackupSpec.DeepCopy()

// Update the status & spec
nab.Spec.BackupStatus = &veleroBackup.Status
nab.Spec.BackupSpec = &veleroBackup.Spec
// Copy the status from veleroBackup.Status to nab.Status
nab.Status = nacv1alpha1.NonAdminBackupStatus{
BackupStatus: veleroBackup.Status,
}

nab.Spec.BackupSpec = veleroBackup.Spec.DeepCopy()

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
// Check if the spec has been updated
if !reflect.DeepEqual(oldSpec, nab.Spec.BackupSpec) {
if err := r.Update(ctx, nab); err != nil {
log.Error(err, "Failed to update NonAdminBackup Spec")
return err
}
log.V(1).Info("NonAdminBackup spec was updated")
} else {
log.V(1).Info("NonAdminBackup spec is already up to date")
}

if err := r.Update(ctx, nab); err != nil {
log.Error(err, "Failed to update NonAdminBackup")
return err
// Check if the status has been updated
if !reflect.DeepEqual(oldStatus, nab.Status.BackupStatus) {
if err := r.Status().Update(ctx, nab); err != nil {
log.Error(err, "Failed to update NonAdminBackup Status")
return err
}
log.V(1).Info("NonAdminBackup status was updated")
} else {
log.V(1).Info("NonAdminBackup status is already up to date")
}

return nil
Expand Down
6 changes: 6 additions & 0 deletions internal/controller/nonadminbackup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ func (r *NonAdminBackupReconciler) Reconcile(ctx context.Context, req ctrl.Reque

if veleroBackupSpec == nil {
log.Error(err, "NonAdminBackup CR does not contain valid VeleroBackupSpec")
nab.Status.Phase = velerov1api.BackupPhaseFailedValidation
nab.Status.FailureReason = "NonAdminBackup CR does not contain valid VeleroBackupSpec"
if err := r.Status().Update(ctx, &nab); err != nil {
log.Error(err, "Failed to update NonAdminBackup Status")
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}

Expand Down
8 changes: 4 additions & 4 deletions internal/controller/velerobackup_predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func getBackupPredicateLogger(ctx context.Context, name, namespace string) logr.

func (veleroBackupPredicate VeleroBackupPredicate) Create(ctx context.Context, evt event.CreateEvent) bool {
nameSpace := evt.Object.GetNamespace()
if nameSpace != veleroBackupPredicate.OadpVeleroNamespace {
return false
}

name := evt.Object.GetName()
log := getBackupPredicateLogger(ctx, name, nameSpace)
log.V(1).Info("Received Create VeleroBackupPredicate")

if nameSpace != veleroBackupPredicate.OadpVeleroNamespace {
return false
}

backup, ok := evt.Object.(*velerov1api.Backup)
if !ok {
// The event object is not a Backup, ignore it
Expand Down

0 comments on commit 70434b7

Please sign in to comment.