Skip to content

Commit

Permalink
Merge pull request openshift-kni#542 from pixelsoccupied/oadp-check
Browse files Browse the repository at this point in the history
OCPBUGS-34520: add additional checks to validate if oadp is installed
  • Loading branch information
openshift-merge-bot[bot] authored May 29, 2024
2 parents 0bad066 + b8bbc8e commit 6dbdf7b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 11 deletions.
38 changes: 29 additions & 9 deletions controllers/idle_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,9 @@ func (r *ImageBasedUpgradeReconciler) cleanup(ctx context.Context, ibu *ibuv1.Im
handleError(err, "failed to remove extra manifest warning annotation from IBU")
}

r.Log.Info("Cleaning up DeleteBackupRequest and Backup CRs")
if err := r.BackupRestore.CleanupDeleteBackupRequests(ctx); err != nil {
handleError(err, "failed to cleanup DeleteBackupRequest CRs.")
}
if err := r.BackupRestore.CleanupBackups(ctx); err != nil {
handleError(err, "failed to cleanup backups")
}
if err := r.BackupRestore.RestorePVsReclaimPolicy(ctx); err != nil {
handleError(err, "failed to restore persistentVolumeReclaimPolicy in PVs created by LVMS")
r.Log.Info("Cleaning up OADP resources")
if err := r.cleanupOADPResources(ctx); err != nil {
handleError(err, "failed to cleanup OADP resources")
}

r.Log.Info("Cleaning up IBU files")
Expand All @@ -186,6 +180,32 @@ func (r *ImageBasedUpgradeReconciler) cleanup(ctx context.Context, ibu *ibuv1.Im
return successful, errorMessage
}

// cleanupOADPResources clean resources from backup/restore as long as OADP is present
func (r *ImageBasedUpgradeReconciler) cleanupOADPResources(ctx context.Context) error {
if !r.BackupRestore.IsOadpInstalled(ctx) {
r.Log.Info("OADP not installed, nothing to cleanup")
return nil
}

r.Log.Info("Cleaning up DeleteBackupRequest")
if err := r.BackupRestore.CleanupDeleteBackupRequests(ctx); err != nil {
return fmt.Errorf("failed to cleanup DeleteBackupRequest CRs: %w", err)
}

r.Log.Info("Cleaning up Backup")
if err := r.BackupRestore.CleanupBackups(ctx); err != nil {
return fmt.Errorf("failed to cleanup backups: %w", err)
}

r.Log.Info("Restoring PV reclaim policy")
if err := r.BackupRestore.RestorePVsReclaimPolicy(ctx); err != nil {
return fmt.Errorf("failed to restore persistentVolumeReclaimPolicy in PVs created by LVMS: %w", err)
}

r.Log.Info("Successfully cleaned all resources related to backup and restore (OADP)")
return nil
}

func (r *ImageBasedUpgradeReconciler) cleanupStateroot(ctx context.Context) error {
r.Log.Info("Cleaning up cluster stateroot resources")
if err := prep.DeleteStaterootSetupJob(ctx, r.Client, r.Log); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions internal/backuprestore/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"strings"
"time"

"k8s.io/apimachinery/pkg/api/meta"

"github.com/openshift-kni/lifecycle-agent/internal/common"
"github.com/openshift-kni/lifecycle-agent/utils"

Expand Down Expand Up @@ -439,7 +441,7 @@ func (h *BRHandler) CleanupBackups(ctx context.Context) error {
clusterIDLabel: clusterID,
}); err != nil {
var groupDiscoveryErr *discovery.ErrGroupDiscoveryFailed
if errors.As(err, &groupDiscoveryErr) {
if errors.As(err, &groupDiscoveryErr) || meta.IsNoMatchError(err) {
h.Log.Info("Backup CR is not installed, nothing to cleanup")
return nil
}
Expand Down Expand Up @@ -601,7 +603,7 @@ func (h *BRHandler) CleanupDeleteBackupRequests(ctx context.Context) error {
clusterIDLabel: clusterID,
}); err != nil {
var groupDiscoveryErr *discovery.ErrGroupDiscoveryFailed
if errors.As(err, &groupDiscoveryErr) {
if errors.As(err, &groupDiscoveryErr) || meta.IsNoMatchError(err) {
h.Log.Info("DeleteBackupRequest CR is not installed, nothing to cleanup")
return nil
}
Expand Down
28 changes: 28 additions & 0 deletions internal/backuprestore/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"path/filepath"
"strings"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

"github.com/go-logr/logr"

"github.com/openshift-kni/lifecycle-agent/internal/common"
Expand Down Expand Up @@ -97,6 +99,7 @@ type BackuperRestorer interface {
StartOrTrackBackup(ctx context.Context, backups []*velerov1.Backup) (*BackupTracker, error)
StartOrTrackRestore(ctx context.Context, restores []*velerov1.Restore) (*RestoreTracker, error)
ValidateOadpConfigmaps(ctx context.Context, content []ibuv1.ConfigMapRef) error
IsOadpInstalled(ctx context.Context) bool
}

// BRHandler handles the backup and restore
Expand Down Expand Up @@ -529,3 +532,28 @@ func (h *BRHandler) CheckOadpOperatorAvailability(ctx context.Context) error {
h.Log.Info("OADP operator is installed and DataProtectionApplication is validated")
return nil
}

// IsOadpInstalled a simple function to determine if OADP is present by checking the presence of at least one OADP defined CRD
func (h *BRHandler) IsOadpInstalled(ctx context.Context) bool {
crds := &apiextensionsv1.CustomResourceDefinitionList{}
if err := h.Client.List(ctx, crds); err != nil {
h.Log.Error(err, "could not list CRDs to verify if OADP is installed")
return false
}

/*
oadp installs more than one CRD (listed below from a dev cluster)...we are looking for at least one match
cloudstorages.oadp.openshift.io
dataprotectionapplications.oadp.openshift.io
volumesnapshotbackups.datamover.oadp.openshift.io
volumesnapshotrestores.datamover.oadp.openshift.io
*/
var oadpCrds []string
for _, crd := range crds.Items {
if strings.HasSuffix(crd.GetName(), "oadp.openshift.io") {
oadpCrds = append(oadpCrds, crd.ObjectMeta.Name)
}
}

return len(oadpCrds) > 0
}
14 changes: 14 additions & 0 deletions internal/backuprestore/mocks/mock_backuprestore.go

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

0 comments on commit 6dbdf7b

Please sign in to comment.