Skip to content

Commit

Permalink
Add more verbosity to undeploy (#5340)
Browse files Browse the repository at this point in the history
* Add more verbosity to undeploy

* Fix stdout messages for odo delete

* Fix styling for odo delete

* Update pkg/devfile/adapters/kubernetes/component/adapter.go

Co-authored-by: Dharmit Shah <[email protected]>

* Fix typo

Co-authored-by: Dharmit Shah <[email protected]>
  • Loading branch information
valaparthvi and dharmit authored Jan 19, 2022
1 parent b015b81 commit ce9506d
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 26 deletions.
6 changes: 3 additions & 3 deletions pkg/devfile/adapters/kubernetes/component/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ func (a Adapter) Delete(labels map[string]string, show bool, wait bool) error {
if labels == nil {
return fmt.Errorf("cannot delete with labels being nil")
}
log.Infof("\nGathering information for component %s", a.ComponentName)
log.Printf("Gathering information for component: %q", a.ComponentName)
podSpinner := log.Spinner("Checking status for component")
defer podSpinner.End(false)

Expand Down Expand Up @@ -853,10 +853,10 @@ func (a Adapter) getDeployCommand() (devfilev1.Command, error) {
return devfilev1.Command{}, err
}
if len(deployGroupCmd) == 0 {
return devfilev1.Command{}, errors.New("error deploying, no default deploy command found in devfile")
return devfilev1.Command{}, &NoDefaultDeployCommandFoundError{}
}
if len(deployGroupCmd) > 1 {
return devfilev1.Command{}, errors.New("more than one default deploy command found in devfile, should not happen")
return devfilev1.Command{}, &MoreThanOneDefaultDeployCommandFoundError{}
}
return deployGroupCmd[0], nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (o componentKubernetes) UnApply(devfilePath string) error {
if err != nil {
return err
}
log.Infof("Un-deploying the Kubernetes %s: %s", u.GetKind(), u.GetName())
log.Printf("Un-deploying the Kubernetes %s: %s", u.GetKind(), u.GetName())
// Un-deploy the K8s manifest
return o.client.DeleteDynamicResource(u.GetName(), gvr.Resource.Group, gvr.Resource.Version, gvr.Resource.Resource)
}
13 changes: 13 additions & 0 deletions pkg/devfile/adapters/kubernetes/component/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package component

type NoDefaultDeployCommandFoundError struct{}

func (e NoDefaultDeployCommandFoundError) Error() string {
return "error deploying, no default deploy command found in devfile"
}

type MoreThanOneDefaultDeployCommandFoundError struct{}

func (e MoreThanOneDefaultDeployCommandFoundError) Error() string {
return "more than one default deploy command found in devfile, should not happen"
}
8 changes: 8 additions & 0 deletions pkg/log/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ func Progressf(format string, a ...interface{}) {
}
}

// Printf will output in an appropriate "information" manner; for e.g.
// • <message>
func Printf(format string, a ...interface{}) {
if !IsJSON() {
fmt.Fprintf(GetStdout(), "%s%s%s%s\n", prefixSpacing, getSpacingString(), suffixSpacing, fmt.Sprintf(format, a...))
}
}

// Success will output in an appropriate "success" manner
func Success(a ...interface{}) {
if !IsJSON() {
Expand Down
53 changes: 36 additions & 17 deletions pkg/odo/cli/component/delete.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package component

import (
"errors"
"fmt"
"github.com/redhat-developer/odo/pkg/devfile/adapters/kubernetes/component"
"github.com/spf13/cobra"
"os"
"path/filepath"

"github.com/spf13/cobra"

"github.com/redhat-developer/odo/pkg/devfile"
"github.com/redhat-developer/odo/pkg/devfile/adapters/common"
"github.com/redhat-developer/odo/pkg/devfile/consts"
Expand Down Expand Up @@ -82,25 +83,37 @@ func (do *DeleteOptions) Validate() error {
func (do *DeleteOptions) Run() (err error) {
klog.V(4).Infof("component delete called")
klog.V(4).Infof("args: %#v", do)
devfileExists := util.CheckPathExists(do.GetDevfilePath())

// odo delete --deploy || odo delete --all
if do.deployFlag || do.allFlag {
if do.forceFlag || ui.Proceed("Are you sure you want to undeploy?") {
log.Info("Un-deploying the Kubernetes Component")
if do.forceFlag || ui.Proceed("Are you sure you want to un-deploy?") {
err = do.DevfileUnDeploy()
if err != nil {
log.Errorf("error occurred while undeploying, cause: %v", err)
// if there is no component in the devfile to undeploy or if the devfile is non-existent, then skip the error log
if errors.Is(err, &component.NoDefaultDeployCommandFoundError{}) || !devfileExists {
log.Printf("no kubernetes component to un-deploy")
} else {
log.Errorf("error occurred while un-deploying, cause: %v", err)
}
}
} else {
log.Error("Aborting the un-deployment")
log.Error("Aborting un-deployment of the component")
}
}

// odo delete || odo delete --all
if !do.deployFlag || do.allFlag {
log.Info("Deleting the devfile component")
if do.forceFlag || ui.Proceed(fmt.Sprintf("Are you sure you want to delete the devfile component: %s?", do.EnvSpecificInfo.GetName())) {
err = do.DevfileComponentDelete()
if err != nil {
log.Errorf("error occurred while deleting component, cause: %v", err)
if !devfileExists {
log.Printf("no devfile component to delete")
} else {
log.Errorf("error occurred while deleting component, cause: %v", err)
}
}
} else {
log.Error("Aborting deletion of component")
Expand All @@ -109,11 +122,12 @@ func (do *DeleteOptions) Run() (err error) {

// Delete the configuration files
if do.allFlag {
log.Info("\nDeleting local config")
log.Info("Deleting local config")
// Prompt and delete env folder
if do.forceFlag || ui.Proceed("Are you sure you want to delete env folder?") {
if !do.EnvSpecificInfo.Exists() {
return fmt.Errorf("env folder doesn't exist for the component")
log.Printf("env folder doesn't exist for the component")
return nil
}
if err = util.DeleteIndexFile(filepath.Dir(do.GetDevfilePath())); err != nil {
return err
Expand All @@ -136,9 +150,13 @@ func (do *DeleteOptions) Run() (err error) {
log.Error("Aborting deletion of env folder")
}

// Prompt and delete the devfile.yaml
successMessage := "Successfully deleted devfile.yaml file"
devfileNotExistsMessage := "devfile.yaml does not exist in the current directory"
if do.forceFlag {
if !util.CheckPathExists(do.GetDevfilePath()) {
return fmt.Errorf("devfile.yaml does not exist in the current directory")
if !devfileExists {
log.Printf(devfileNotExistsMessage)
return nil
}
if !do.EnvSpecificInfo.IsUserCreatedDevfile() {

Expand Down Expand Up @@ -170,15 +188,16 @@ func (do *DeleteOptions) Run() (err error) {
return err
}

log.Successf("Successfully deleted devfile.yaml file")
log.Successf(successMessage)

} else {
log.Info("Didn't delete the devfile as it was user provided")
}

} else if ui.Proceed("Are you sure you want to delete devfile.yaml?") {
if !util.CheckPathExists(do.GetDevfilePath()) {
return fmt.Errorf("devfile.yaml does not exist in the current directory")
if !devfileExists {
log.Printf(devfileNotExistsMessage)
return nil
}

// first remove the uri based files mentioned in the devfile
Expand Down Expand Up @@ -209,7 +228,7 @@ func (do *DeleteOptions) Run() (err error) {
return err
}

log.Successf("Successfully deleted devfile.yaml file")
log.Successf(successMessage)
} else {
log.Error("Aborting deletion of devfile.yaml file")
}
Expand Down Expand Up @@ -243,12 +262,12 @@ func NewCmdDelete(name, fullName string) *cobra.Command {

componentDeleteCmd.SetUsageTemplate(odoutil.CmdUsageTemplate)
completion.RegisterCommandHandler(componentDeleteCmd, completion.ComponentNameCompletionHandler)
//Adding `--context` flag
// Adding `--context` flag
odoutil.AddContextFlag(componentDeleteCmd, &do.contextFlag)

//Adding `--project` flag
// Adding `--project` flag
projectCmd.AddProjectFlag(componentDeleteCmd)
//Adding `--application` flag
// Adding `--application` flag
appCmd.AddApplicationFlag(componentDeleteCmd)

return componentDeleteCmd
Expand Down
2 changes: 1 addition & 1 deletion pkg/odo/cli/service/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (o *CreateOptions) Complete(cmdline cmdline.Cmdline, args []string) (err er
if err != nil {
return err
}
//if no args are provided and if request is not from file, user wants interactive mode
// if no args are provided and if request is not from file, user wants interactive mode
if o.fromFileFlag == "" && len(args) == 0 {
return fmt.Errorf("odo doesn't support interactive mode for creating Operator backed service")
}
Expand Down
19 changes: 15 additions & 4 deletions tests/integration/devfile/cmd_devfile_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,33 @@ var _ = Describe("odo devfile delete command tests", func() {
It("should delete the component", func() {
helper.Cmd("odo", "delete", "-f").ShouldPass()
})
It("should delete the component, env, odo folders and odo-index-file.json with --all flag", func() {
stdOut := helper.Cmd("odo", "delete", "-f", "--all").ShouldPass().Out()

files := helper.ListFilesInDir(commonVar.Context)
Expect(files).To(Not(ContainElement(".odo")))
Expect(files).To(Not(ContainElement("devfile.yaml")))
Expect(stdOut).To(ContainSubstring("no kubernetes component to un-deploy"))
})

When("odo deploy is run", func() {
undeploymentMessage := "Un-deploying the Kubernetes Component"
BeforeEach(func() {
// requires a different devfile
helper.CopyExampleDevFile(filepath.Join("source", "devfiles", "nodejs", "devfile-deploy.yaml"), path.Join(commonVar.Context, "devfile.yaml"))
helper.Cmd("odo", "deploy").AddEnv("PODMAN_CMD=echo").ShouldPass()
})
It("should successfully delete the deploy resources with --deploy flag", func() {
stdOut := helper.Cmd("odo", "delete", "--deploy", "-f").ShouldPass().Out()
Expect(stdOut).To(ContainSubstring("Un-deploying the Kubernetes Deployment"))
Expect(stdOut).To(ContainSubstring(undeploymentMessage))
})
It("should successfully delete the deploy resources", func() {
stdOut := helper.Cmd("odo", "delete", "-a", "-f").ShouldPass().Out()
Expect(stdOut).To(ContainSubstring("Un-deploying the Kubernetes Deployment"))
Expect(stdOut).To(ContainSubstring(undeploymentMessage))
})
It("should successfully delete the component, but the deployed resources should remain intact", func() {
stdOut := helper.Cmd("odo", "delete", "-f").ShouldPass().Out()
Expect(stdOut).ToNot(ContainSubstring("Un-deploying the Kubernetes"))
Expect(stdOut).ToNot(ContainSubstring(undeploymentMessage))
})
When("outside the context directory", func() {
BeforeEach(func() {
Expand All @@ -83,11 +93,12 @@ var _ = Describe("odo devfile delete command tests", func() {
helper.Cmd("odo", "push", "--project", commonVar.Project).ShouldPass()
})
It("should delete the component, env, odo folders and odo-index-file.json with --all flag", func() {
helper.Cmd("odo", "delete", "-f", "--all").ShouldPass()
stdOut := helper.Cmd("odo", "delete", "-f", "--all").ShouldPass().Out()

files := helper.ListFilesInDir(commonVar.Context)
Expect(files).To(Not(ContainElement(".odo")))
Expect(files).To(Not(ContainElement("devfile.yaml")))
Expect(stdOut).To(ContainSubstring("no kubernetes component to un-deploy"))
})

Describe("deleting a component from other component directory", func() {
Expand Down

0 comments on commit ce9506d

Please sign in to comment.