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 7, 2024
1 parent 5bdd750 commit 068dbc6
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 42 deletions.
28 changes: 25 additions & 3 deletions api/v1alpha1/nonadminbackup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +kubebuilder:validation:Enum=Error;FailedValidation;New;Completed
type NonAdminBackupPhase string

const (
NonAdminBackupPhaseError NonAdminBackupPhase = "Error"

NonAdminBackupPhaseFailedValidation NonAdminBackupPhase = "FailedValidation"

// Used as a New object, that needs to be processed
// By NonAdminController
NonAdminBackupPhaseNew NonAdminBackupPhase = "New"

NonAdminBackupPhaseCompleted NonAdminBackupPhase = "Completed"
)

// NonAdminBackupSpec defines the desired state of NonAdminBackup
type NonAdminBackupSpec struct {
// NonAdminBackup log level (use debug for the most logging, leave unset for default)
Expand All @@ -32,14 +47,21 @@ 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"`

// +optional
Phase NonAdminBackupPhase `json:"phase,omitempty"`

// +optional
FailureReason string `json:"failureReason,omitempty"`

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

//+kubebuilder:object:root=true
Expand Down
16 changes: 4 additions & 12 deletions api/v1alpha1/zz_generated.deepcopy.go

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

41 changes: 25 additions & 16 deletions config/crd/bases/nac.oadp.openshift.io_nonadminbackups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,22 @@ spec:
type: string
type: array
type: object
logLevel:
description: NonAdminBackup log level (use debug for the most logging,
leave unset for default)
enum:
- trace
- debug
- info
- warning
- error
- fatal
- panic
type: string
type: object
status:
description: NonAdminBackupStatus defines the observed state of NonAdminBackup
properties:
backupStatus:
description: BackupStatus captures the current status of a Velero
backup.
Expand Down Expand Up @@ -634,22 +650,6 @@ spec:
file in object storage.
type: integer
type: object
logLevel:
description: NonAdminBackup log level (use debug for the most logging,
leave unset for default)
enum:
- trace
- debug
- info
- warning
- error
- fatal
- panic
type: string
type: object
status:
description: NonAdminBackupStatus defines the observed state of NonAdminBackup
properties:
conditions:
items:
description: "Condition contains details for one aspect of the current
Expand Down Expand Up @@ -719,6 +719,15 @@ spec:
- type
type: object
type: array
failureReason:
type: string
phase:
enum:
- Error
- FailedValidation
- New
- Completed
type: string
type: object
type: object
served: true
Expand Down
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.41
33 changes: 23 additions & 10 deletions internal/controller/common_nab.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,35 @@ 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.BackupStatus = veleroBackup.Status.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
nab.Spec.BackupSpec = veleroBackup.Spec.DeepCopy()

// 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
8 changes: 8 additions & 0 deletions internal/controller/nonadminbackup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func (r *NonAdminBackupReconciler) Reconcile(ctx context.Context, req ctrl.Reque
r.NamespacedName = req.NamespacedName

nab := nacv1alpha1.NonAdminBackup{}
nab.Status.Phase = nacv1alpha1.NonAdminBackupPhaseNew

err := r.Get(ctx, req.NamespacedName, &nab)

if err != nil && errors.IsNotFound(err) {
Expand All @@ -82,6 +84,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 = nacv1alpha1.NonAdminBackupPhaseFailedValidation
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

0 comments on commit 068dbc6

Please sign in to comment.