...
1 package kubedev
2
3 import (
4 "context"
5 "fmt"
6 "io"
7 "strings"
8
9 kerrors "k8s.io/apimachinery/pkg/api/errors"
10
11 "github.com/redhat-developer/odo/pkg/labels"
12 odocontext "github.com/redhat-developer/odo/pkg/odo/context"
13 )
14
15 func (o *DevClient) CleanupResources(ctx context.Context, out io.Writer) error {
16 var (
17 componentName = odocontext.GetComponentName(ctx)
18 devfileObj = odocontext.GetEffectiveDevfileObj(ctx)
19 )
20 fmt.Fprintln(out, "Cleaning resources, please wait")
21 appname := odocontext.GetApplication(ctx)
22 isInnerLoopDeployed, resources, err := o.deleteClient.ListClusterResourcesToDeleteFromDevfile(*devfileObj, appname, componentName, labels.ComponentDevMode)
23 if err != nil {
24 if kerrors.IsUnauthorized(err) || kerrors.IsForbidden(err) {
25 fmt.Fprintf(out, "Error connecting to the cluster, the resources were not cleaned up.\nPlease log in again and cleanup the resource with `odo delete component`\n\n")
26 } else {
27 fmt.Fprintf(out, "Failed to delete inner loop resources: %v\n", err)
28 }
29 return err
30 }
31
32 if isInnerLoopDeployed {
33 err = o.deleteClient.ExecutePreStopEvents(ctx, *devfileObj, appname, componentName)
34 if err != nil {
35 fmt.Fprint(out, "Failed to execute preStop events")
36 }
37 }
38
39 failed := o.deleteClient.DeleteResources(resources, true)
40 if len(failed) == 0 {
41 return nil
42 }
43 var list []string
44 for _, fail := range failed {
45 list = append(list, fmt.Sprintf("- %s/%s", fail.GetKind(), fail.GetName()))
46 }
47
48 return fmt.Errorf("could not delete the following resource(s): \n%v", strings.Join(list, "\n"))
49 }
50
View as plain text