diff --git a/pkg/deploy/deploy.go b/pkg/deploy/deploy.go index ace06e61bc4..3d8ed997f29 100644 --- a/pkg/deploy/deploy.go +++ b/pkg/deploy/deploy.go @@ -407,34 +407,44 @@ func ApplyParams(componentPath string, imageParamsMap map[string]string, isUpdat return nil } -// SubscriptionExists checks if a Subscription for the operator exists in the given namespace. -// if exsit, return object; if not exsit, return nil. -func SubscriptionExists(cli client.Client, namespace string, name string) (*ofapiv1alpha1.Subscription, error) { +// GetSubscription checks if a Subscription for the operator exists in the given namespace. +// if exist, return object; otherwise, return error. +func GetSubscription(cli client.Client, namespace string, name string) (*ofapiv1alpha1.Subscription, error) { sub := &ofapiv1alpha1.Subscription{} if err := cli.Get(context.TODO(), client.ObjectKey{Namespace: namespace, Name: name}, sub); err != nil { - if apierrs.IsNotFound(err) { - return nil, nil - } + // real error or 'not found' both return here return nil, err } - return sub, nil } +// Delete given Subscription if it exists +// Do not error if the Subscription does not exist. +func DeleteExistingSubscription(cli client.Client, operatorNs string, subsName string) error { + sub, err := GetSubscription(cli, operatorNs, subsName) + if err != nil { + return client.IgnoreNotFound(err) + } + + if err := cli.Delete(context.TODO(), sub); client.IgnoreNotFound(err) != nil { + return fmt.Errorf("error deleting subscription %s: %w", sub.Name, err) + } + + return nil +} + // OperatorExists checks if an Operator with 'operatorPrefix' is installed. // Return true if found it, false if not. // if we need to check exact version of the operator installed, can append vX.Y.Z later. func OperatorExists(cli client.Client, operatorPrefix string) (bool, error) { opConditionList := &ofapiv2.OperatorConditionList{} - if err := cli.List(context.TODO(), opConditionList); err != nil { - if !apierrs.IsNotFound(err) { // real error to run List() - return false, err - } - } else { - for _, opCondition := range opConditionList.Items { - if strings.HasPrefix(opCondition.Name, operatorPrefix) { - return true, nil - } + err := cli.List(context.TODO(), opConditionList) + if err != nil { + return false, err + } + for _, opCondition := range opConditionList.Items { + if strings.HasPrefix(opCondition.Name, operatorPrefix) { + return true, nil } } diff --git a/pkg/deploy/setup.go b/pkg/deploy/setup.go index 206ef9eab49..bcabd72247b 100644 --- a/pkg/deploy/setup.go +++ b/pkg/deploy/setup.go @@ -62,9 +62,6 @@ func isManagedRHODS(cli client.Client) (Platform, error) { expectedCatlogSource := &ofapi.CatalogSourceList{} err = cli.List(context.TODO(), expectedCatlogSource) if err != nil { - if apierrs.IsNotFound(err) { - return Unknown, nil - } return Unknown, err } if len(expectedCatlogSource.Items) > 0 { diff --git a/pkg/upgrade/upgrade.go b/pkg/upgrade/upgrade.go index 5463244e4c4..25dc5d4d31b 100644 --- a/pkg/upgrade/upgrade.go +++ b/pkg/upgrade/upgrade.go @@ -108,18 +108,11 @@ func OperatorUninstall(cli client.Client, cfg *rest.Config) error { } else if platform == deploy.ManagedRhods { subsName = "addon-managed-odh" } - sub, _ := deploy.SubscriptionExists(cli, operatorNs, subsName) - if sub == nil { - fmt.Printf("Could not find subscription %s in namespace %s. Maybe you have a different one", subsName, operatorNs) - } else { - if err := cli.Delete(context.TODO(), sub); err != nil { - return fmt.Errorf("error deleting subscription %s: %w", sub.Name, err) - } + if err := deploy.DeleteExistingSubscription(cli, operatorNs, subsName); err != nil { + return err } fmt.Printf("Removing the operator CSV in turn remove operator deployment\n") - time.Sleep(5 * time.Second) - err = removeCSV(cli, cfg) fmt.Printf("All resources deleted as part of uninstall.") @@ -325,10 +318,6 @@ func UpdateFromLegacyVersion(cli client.Client, platform deploy.Platform, appNS kfDefList := &kfdefv1.KfDefList{} err := cli.List(context.TODO(), kfDefList) if err != nil { - if apierrs.IsNotFound(err) { - // If no KfDefs, do nothing and return - return nil - } return fmt.Errorf("error getting kfdef instances: : %w", err) } if len(kfDefList.Items) > 0 { @@ -421,10 +410,6 @@ func RemoveKfDefInstances(cli client.Client, _ deploy.Platform) error { expectedKfDefList := &kfdefv1.KfDefList{} err = cli.List(context.TODO(), expectedKfDefList) if err != nil { - if apierrs.IsNotFound(err) { - // If no KfDefs, do nothing and return - return nil - } return fmt.Errorf("error getting list of kfdefs: %w", err) } // Delete kfdefs diff --git a/tests/e2e/dsc_cfmap_deletion_test.go b/tests/e2e/dsc_cfmap_deletion_test.go index ae95fabcce7..ba887219af8 100644 --- a/tests/e2e/dsc_cfmap_deletion_test.go +++ b/tests/e2e/dsc_cfmap_deletion_test.go @@ -46,11 +46,6 @@ func cfgMapDeletionTestSuite(t *testing.T) { require.NoError(t, err, "Error while deleting owned namespaces") }) - // t.Run("DS Projects should be deleted", func(t *testing.T) { - // err = testCtx.testDSProjectDeletion() - // require.NoError(t, err, "Error while deleting DS Projectss") - // }) - t.Run("dsci should be deleted", func(t *testing.T) { err = testCtx.testDSCIDeletion() require.NoError(t, err, "failed deleting DSCI")