From 1fb3197ef88515f3ecd5c1b5ee088e0fe476e0c6 Mon Sep 17 00:00:00 2001 From: Kam Kasravi Date: Thu, 28 Mar 2019 11:16:00 -0700 Subject: [PATCH] fixes 'kfctl delete fails when no platform or minikube is specified' (#2810) * call package manager, then platform in delete. Allow ksonnet to delete k8s * fix logic around deletion of ALL and PLATFORM * compilation error * convert to KfError on error returns * update on code comments, make sure all errors are returned as KfError --- .../pkg/kfapp/coordinator/coordinator.go | 32 ++++++++++++-- bootstrap/pkg/kfapp/ksonnet/ksonnet.go | 42 +++++++++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/bootstrap/pkg/kfapp/coordinator/coordinator.go b/bootstrap/pkg/kfapp/coordinator/coordinator.go index 94cd0588d4b..a0b2e4c3c40 100644 --- a/bootstrap/pkg/kfapp/coordinator/coordinator.go +++ b/bootstrap/pkg/kfapp/coordinator/coordinator.go @@ -20,6 +20,7 @@ import ( "fmt" "github.com/ghodss/yaml" gogetter "github.com/hashicorp/go-getter" + kfapis "github.com/kubeflow/kubeflow/bootstrap/pkg/apis" kftypes "github.com/kubeflow/kubeflow/bootstrap/pkg/apis/apps" kfdefs "github.com/kubeflow/kubeflow/bootstrap/pkg/apis/apps/kfdef/v1alpha1" "github.com/kubeflow/kubeflow/bootstrap/pkg/kfapp/gcp" @@ -453,14 +454,37 @@ func (kfapp *coordinator) Delete(resources kftypes.ResourceEnum) error { switch resources { case kftypes.ALL: + // if we're deleting ALL, any problems with deleting k8s will abort and not delete the platform + if err := k8s(); err != nil { + return &kfapis.KfError{ + Code: int(kfapis.INTERNAL_ERROR), + Message: fmt.Sprintf("error while deleting k8 resources, aborting deleting the platform. Error %v", err), + } + } if err := platform(); err != nil { - return err + return &kfapis.KfError{ + Code: int(kfapis.INTERNAL_ERROR), + Message: fmt.Sprintf("error while deleting platform resources. Error %v", err), + } } - return k8s() case kftypes.PLATFORM: - return platform() + // deleting the PLATFORM means deleting the cluster. We remove k8s first in order free up any cloud vendor + // resources. Deleting k8 resources is a best effort and partial delete or failure should not + // prevent PLATFORM (cluster) deletion + _ = k8s() + if err := platform(); err != nil { + return &kfapis.KfError{ + Code: int(kfapis.INTERNAL_ERROR), + Message: fmt.Sprintf("error while deleting platform resources. Error %v", err), + } + } case kftypes.K8S: - return k8s() + if err := k8s(); err != nil { + return &kfapis.KfError{ + Code: int(kfapis.INTERNAL_ERROR), + Message: fmt.Sprintf("error while deleting k8 resources, aborting deleting the platform. Error %v", err), + } + } } return nil } diff --git a/bootstrap/pkg/kfapp/ksonnet/ksonnet.go b/bootstrap/pkg/kfapp/ksonnet/ksonnet.go index a63b8cbfc18..216fcb7340a 100644 --- a/bootstrap/pkg/kfapp/ksonnet/ksonnet.go +++ b/bootstrap/pkg/kfapp/ksonnet/ksonnet.go @@ -288,6 +288,48 @@ func (ksApp *ksApp) deleteGlobalResources(config *rest.Config) error { } func (ksApp *ksApp) Delete(resources kftypes.ResourceEnum) error { + config := kftypes.GetConfig() + err := ksApp.deleteGlobalResources(config) + if err != nil { + log.Errorf("there was a problem deleting global resources: %v", err) + } + envSetErr := ksApp.envSet(ksApp.KsEnvName, config.Host) + if envSetErr != nil { + return fmt.Errorf("couldn't create ksonnet env %v Error: %v", ksApp.KsEnvName, envSetErr) + } + clientConfig := kftypes.GetKubeConfig() + components := []string{"application", "metacontroller"} + err = actions.RunDelete(map[string]interface{}{ + actions.OptionApp: ksApp.KApp, + actions.OptionClientConfig: &client.Config{ + Overrides: &clientcmd.ConfigOverrides{}, + Config: clientcmd.NewDefaultClientConfig(*clientConfig, &clientcmd.ConfigOverrides{}), + }, + actions.OptionEnvName: ksApp.KsEnvName, + actions.OptionComponentNames: components, + actions.OptionGracePeriod: int64(10), + }) + if err != nil { + log.Infof("there was a problem deleting %v: %v", components, err) + } + namespace := ksApp.ObjectMeta.Namespace + log.Infof("deleting namespace: %v", namespace) + clientset := kftypes.GetClientset(config) + ns, nsMissingErr := clientset.CoreV1().Namespaces().Get(namespace, metav1.GetOptions{}) + if nsMissingErr == nil { + nsErr := clientset.CoreV1().Namespaces().Delete(ns.Name, metav1.NewDeleteOptions(int64(100))) + if nsErr != nil { + return fmt.Errorf("couldn't delete namespace %v Error: %v", namespace, nsErr) + } + } + name := "meta-controller-cluster-role-binding" + crb, crbErr := clientset.RbacV1().ClusterRoleBindings().Get(name, metav1.GetOptions{}) + if crbErr == nil { + crbDeleteErr := clientset.RbacV1().ClusterRoleBindings().Delete(crb.Name, metav1.NewDeleteOptions(int64(5))) + if crbDeleteErr != nil { + return fmt.Errorf("couldn't delete clusterrolebinding %v Error: %v", name, crbDeleteErr) + } + } return nil }