github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/deletecmd/delete_namespace.go (about) 1 package deletecmd 2 3 import ( 4 "fmt" 5 "sort" 6 "strings" 7 8 "github.com/olli-ai/jx/v2/pkg/cmd/helper" 9 survey "gopkg.in/AlecAivazis/survey.v1" 10 11 "github.com/jenkins-x/jx-logging/pkg/log" 12 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 13 "github.com/olli-ai/jx/v2/pkg/cmd/templates" 14 "github.com/olli-ai/jx/v2/pkg/util" 15 "github.com/spf13/cobra" 16 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 17 ) 18 19 // DeleteNamespaceOptions are the flags for delete commands 20 type DeleteNamespaceOptions struct { 21 *opts.CommonOptions 22 23 SelectAll bool 24 SelectFilter string 25 Confirm bool 26 } 27 28 var ( 29 deleteNamespaceLong = templates.LongDesc(` 30 Deletes one or more namespaces 31 `) 32 33 deleteNamespaceExample = templates.Examples(` 34 # Delete the named namespace 35 jx delete namespace cheese 36 37 # Delete the namespaces matching the given filter 38 jx delete namespace -f foo -a 39 `) 40 ) 41 42 // NewCmdDeleteNamespace creates a command object 43 // retrieves one or more resources from a server. 44 func NewCmdDeleteNamespace(commonOpts *opts.CommonOptions) *cobra.Command { 45 options := &DeleteNamespaceOptions{ 46 CommonOptions: commonOpts, 47 } 48 49 cmd := &cobra.Command{ 50 Use: "namespace", 51 Short: "Deletes one or more namespaces and their associated resources (Environments, Jenkins etc)", 52 Long: deleteNamespaceLong, 53 Example: deleteNamespaceExample, 54 Aliases: []string{"namespaces", "ns"}, 55 Run: func(cmd *cobra.Command, args []string) { 56 options.Cmd = cmd 57 options.Args = args 58 err := options.Run() 59 helper.CheckErr(err) 60 }, 61 } 62 63 cmd.Flags().BoolVarP(&options.SelectAll, "all", "a", false, "Should we default to selecting all the matched namespaces for deletion") 64 cmd.Flags().StringVarP(&options.SelectFilter, "filter", "f", "", "Filters the list of namespaces you can pick from") 65 cmd.Flags().BoolVarP(&options.Confirm, "yes", "y", false, "Confirms we should uninstall this installation") 66 return cmd 67 } 68 69 // Run implements this command 70 func (o *DeleteNamespaceOptions) Run() error { 71 surveyOpts := survey.WithStdio(o.In, o.Out, o.Err) 72 kubeClient, err := o.KubeClient() 73 if err != nil { 74 return err 75 } 76 namespaceInterface := kubeClient.CoreV1().Namespaces() 77 nsList, err := namespaceInterface.List(metav1.ListOptions{}) 78 if err != nil { 79 return err 80 } 81 namespaceNames := []string{} 82 for _, namespace := range nsList.Items { 83 namespaceNames = append(namespaceNames, namespace.Name) 84 } 85 sort.Strings(namespaceNames) 86 87 names := o.Args 88 if len(names) == 0 { 89 if o.BatchMode { 90 return fmt.Errorf("Missing namespace name argument") 91 } 92 names, err = util.SelectNamesWithFilter(namespaceNames, "Which namespaces do you want to delete: ", o.SelectAll, o.SelectFilter, "", o.GetIOFileHandles()) 93 if err != nil { 94 return err 95 } 96 } 97 98 if o.BatchMode { 99 if !o.Confirm { 100 return fmt.Errorf("In batch mode you must specify the '-y' flag to confirm") 101 } 102 } else { 103 log.Logger().Warnf("You are about to delete the following namespaces '%s'. This operation CANNOT be undone!", 104 strings.Join(names, ",")) 105 106 flag := false 107 prompt := &survey.Confirm{ 108 Message: "Are you sure you want to delete all these namespaces?", 109 Default: false, 110 } 111 err = survey.AskOne(prompt, &flag, nil, surveyOpts) 112 if err != nil { 113 return err 114 } 115 if !flag { 116 return nil 117 } 118 } 119 120 for _, name := range names { 121 log.Logger().Infof("Deleting namespace: %s", util.ColorInfo(name)) 122 err = namespaceInterface.Delete(name, nil) 123 if err != nil { 124 log.Logger().Warnf("Failed to delete namespace %s: %s", name, err) 125 } 126 } 127 return nil 128 }