Skip to content

Commit

Permalink
Merge pull request #242 from VaishnaviHire/sync_upstream_4_10
Browse files Browse the repository at this point in the history
pkg/upgrade: remove watson-studio dashboard application (#959)
  • Loading branch information
VaishnaviHire authored Apr 10, 2024
2 parents b9bbecd + 7ed841e commit bde70ab
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/gvk/gvk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gvk

import "k8s.io/apimachinery/pkg/runtime/schema"

var (
OdhApplication = schema.GroupVersionKind{Group: "dashboard.opendatahub.io", Version: "v1", Kind: "OdhApplication"}
OdhDocument = schema.GroupVersionKind{Group: "dashboard.opendatahub.io", Version: "v1", Kind: "OdhDocument"}
OdhQuickStart = schema.GroupVersionKind{Group: "console.openshift.io", Version: "v1", Kind: "OdhQuickStart"}
)
93 changes: 93 additions & 0 deletions pkg/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package upgrade

import (
"context"
"errors"
"fmt"
"reflect"
"strings"
Expand All @@ -16,6 +17,7 @@ import (
authv1 "k8s.io/api/rbac/v1"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -37,9 +39,19 @@ import (
"github.com/opendatahub-io/opendatahub-operator/v2/components/workbenches"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/gvk"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
)

type ResourceSpec struct {
Gvk schema.GroupVersionKind
Namespace string
// path to the field, like "metadata", "name"
Path []string
// set of values for the field to match object, any one matches
Values []string
}

// CreateDefaultDSC creates a default instance of DSC.
// Note: When the platform is not Managed, and a DSC instance already exists, the function doesn't re-create/update the resource.
func CreateDefaultDSC(ctx context.Context, cli client.Client) error {
Expand Down Expand Up @@ -240,6 +252,33 @@ func UpdateFromLegacyVersion(cli client.Client, platform deploy.Platform, appNS
return nil
}

func getDashboardWatsonResources(ns string) []ResourceSpec {
metadataName := []string{"metadata", "name"}
specAppName := []string{"spec", "appName"}
appName := []string{"watson-studio"}

return []ResourceSpec{
{
Gvk: gvk.OdhQuickStart,
Namespace: ns,
Path: specAppName,
Values: appName,
},
{
Gvk: gvk.OdhDocument,
Namespace: ns,
Path: specAppName,
Values: appName,
},
{
Gvk: gvk.OdhApplication,
Namespace: ns,
Path: metadataName,
Values: appName,
},
}
}

// TODO: remove function once we have a generic solution across all components.
func CleanupExistingResource(ctx context.Context, cli client.Client, platform deploy.Platform, dscApplicationsNamespace, dscMonitoringNamespace string) error {
var multiErr *multierror.Error
Expand Down Expand Up @@ -283,9 +322,63 @@ func CleanupExistingResource(ctx context.Context, cli client.Client, platform de
Kind: "OdhApplication",
}
multiErr = multierror.Append(multiErr, removOdhApplicationsCR(ctx, cli, JupyterhubApp, "jupyterhub", dscApplicationsNamespace))

// to take a reference
toDelete := getDashboardWatsonResources(dscApplicationsNamespace)
multiErr = multierror.Append(multiErr, deleteResources(ctx, cli, &toDelete))

return multiErr.ErrorOrNil()
}

func deleteResources(ctx context.Context, c client.Client, resources *[]ResourceSpec) error {
var errors *multierror.Error

for _, res := range *resources {
err := deleteOneResource(ctx, c, res)
errors = multierror.Append(errors, err)
}

return errors.ErrorOrNil()
}

func deleteOneResource(ctx context.Context, c client.Client, res ResourceSpec) error {
list := &unstructured.UnstructuredList{}
list.SetGroupVersionKind(res.Gvk)

err := c.List(ctx, list, client.InNamespace(res.Namespace))
if err != nil {
if errors.Is(err, &meta.NoKindMatchError{}) {
fmt.Printf("Could not delete %v: CRD not found\n", res.Gvk)
return nil
}
return fmt.Errorf("failed to list %s: %w", res.Gvk.Kind, err)
}

for _, item := range list.Items {
item := item
v, ok, err := unstructured.NestedString(item.Object, res.Path...)
if err != nil {
return fmt.Errorf("failed to get field %v for %s %s/%s: %w", res.Path, res.Gvk.Kind, res.Namespace, item.GetName(), err)
}

if !ok {
return fmt.Errorf("unexisting path to delete: %v", res.Path)
}

for _, toDelete := range res.Values {
if v == toDelete {
err = c.Delete(ctx, &item)
if err != nil {
return fmt.Errorf("failed to delete %s %s/%s: %w", res.Gvk.Kind, res.Namespace, item.GetName(), err)
}
fmt.Println("Deleted object", item.GetName(), res.Gvk, "in namespace", res.Namespace)
}
}
}

return nil
}

func RemoveKfDefInstances(ctx context.Context, cli client.Client) error {
// Check if kfdef are deployed
kfdefCrd := &apiextv1.CustomResourceDefinition{}
Expand Down

0 comments on commit bde70ab

Please sign in to comment.