Skip to content

Commit

Permalink
fix: add a flag to keep namespaces when uninstalling jx
Browse files Browse the repository at this point in the history
Signed-off-by: ankitm123 <[email protected]>
  • Loading branch information
ankitm123 authored and jenkins-x-bot committed Sep 6, 2020
1 parent 7db1f96 commit c65b5e2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 9 additions & 4 deletions pkg/cmd/uninstall/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type UninstallOptions struct {
Context string
Force bool // Force uninstallation - programmatic use only - do not expose to the user
KeepEnvironments bool
KeepNamespaces bool
}

var (
Expand Down Expand Up @@ -61,6 +62,7 @@ func NewCmdUninstall(commonOpts *opts.CommonOptions) *cobra.Command {
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "The team namespace to uninstall. Defaults to the current namespace.")
cmd.Flags().StringVarP(&options.Context, "context", "", "", "The kube context to uninstall JX from. This will be compared with the current context to prevent accidental uninstallation from the wrong cluster")
cmd.Flags().BoolVarP(&options.KeepEnvironments, "keep-environments", "", false, "Don't delete environments. Uninstall Jenkins X only.")
cmd.Flags().BoolVarP(&options.KeepNamespaces, "keep-namespaces", "", false, "Don't delete namespaces.")
return cmd
}

Expand Down Expand Up @@ -88,7 +90,8 @@ func (o *UninstallOptions) Run() error {
help += "\n " + envNamespace
}

if answer, err := util.Confirm("Uninstall JX - this command will remove all JX components and delete all namespaces created by Jenkins X. Do you wish to continue?", false,
if answer, err := util.Confirm("Uninstall JX - this command will remove all JX components and "+
"delete all namespaces (if keep-namespaces is set to false) created by Jenkins X. Do you wish to continue?", false,
help, o.GetIOFileHandles()); !answer {
return err
}
Expand Down Expand Up @@ -144,9 +147,11 @@ func (o *UninstallOptions) Run() error {
if err != nil {
errs = append(errs, fmt.Errorf("failed to delete the environments in namespace %s: %s", namespace, err))
}
err = o.cleanupNamespaces(namespace, envNames, envMap)
if err != nil {
errs = append(errs, fmt.Errorf("failed to cleanup namespaces in namespace %s: %s", namespace, err))
if !o.KeepNamespaces {
err = o.cleanupNamespaces(namespace, envNames, envMap)
if err != nil {
errs = append(errs, fmt.Errorf("failed to cleanup namespaces in namespace %s: %s", namespace, err))
}
}
if len(errs) > 0 {
return errorutil.CombineErrors(errs...)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/uninstall/uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestUninstallOptions_Run_ContextSpecifiedViaCli_FailsWhenContextNamesDoNotM
donec := make(chan struct{})
go func() {
defer close(donec)
console.ExpectString("Uninstall JX - this command will remove all JX components and delete all namespaces created by Jenkins X. Do you wish to continue?")
console.ExpectString("Uninstall JX - this command will remove all JX components and delete all namespaces (if keep-namespaces is set to false) created by Jenkins X. Do you wish to continue?")
console.SendLine("Y")
console.ExpectString("This action will permanently delete Jenkins X from the Kubernetes context current-context. Please type in the name of the context to confirm:")
console.SendLine("target-context")
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestUninstallOptions_Run_ContextSpecifiedViaCli_PassWhenContextNamesMatch(t
//noinspection GoUnhandledErrorResult
go func() {
defer close(donec)
console.ExpectString("Uninstall JX - this command will remove all JX components and delete all namespaces created by Jenkins X. Do you wish to continue?")
console.ExpectString("Uninstall JX - this command will remove all JX components and delete all namespaces (if keep-namespaces is set to false) created by Jenkins X. Do you wish to continue?")
console.SendLine("Y")
console.ExpectString("This action will permanently delete Jenkins X from the Kubernetes context correct-context-to-delete. Please type in the name of the context to confirm:")
console.SendLine("correct-context-to-delete")
Expand Down

0 comments on commit c65b5e2

Please sign in to comment.