forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_devpod.go
118 lines (100 loc) · 2.68 KB
/
delete_devpod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package cmd
import (
"fmt"
"io"
"os/user"
"strings"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/spf13/cobra"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
deleteDevPodLong = templates.LongDesc(`
Deletes one or more DevPods
For more documentation see: [https://jenkins-x.io/developing/devpods/](https://jenkins-x.io/developing/devpods/)
`)
deleteDevPodExample = templates.Examples(`
# deletes a DevPod by picking one from the list and confirming to it
jx delete devpod
# delete a specific DevPod
jx delete devpod myuser-maven2
`)
)
// DeleteDevPodOptions are the flags for delete commands
type DeleteDevPodOptions struct {
CommonOptions
}
// NewCmdDeleteDevPod creates a command object for the generic "get" action, which
// retrieves one or more resources from a server.
func NewCmdDeleteDevPod(f Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &DeleteDevPodOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "devpod",
Short: "Deletes one or many DevPods",
Long: deleteDevPodLong,
Example: deleteDevPodExample,
Aliases: []string{"buildpod", "buildpods", "devpods"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
return cmd
}
// Run implements this command
func (o *DeleteDevPodOptions) Run() error {
args := o.Args
client, curNs, err := o.KubeClient()
if err != nil {
return err
}
ns, _, err := kube.GetDevNamespace(client, curNs)
if err != nil {
return err
}
u, err := user.Current()
if err != nil {
return err
}
username := u.Username
names, err := kube.GetPodNames(client, ns, username)
if err != nil {
return err
}
info := util.ColorInfo
if len(names) == 0 {
return fmt.Errorf("There are no DevPods for user %s in namespace %s. You can create one via: %s\n", info(username), info(ns), info("jx create devpod"))
}
if len(args) == 0 {
args, err = util.PickNames(names, "Pick DevPod:")
if err != nil {
return err
}
}
deletePods := strings.Join(args, ", ")
if !util.Confirm("You are about to delete the DevPods: "+deletePods, false, "The list of DevPods names to be deleted") {
return nil
}
for _, name := range args {
if util.StringArrayIndex(names, name) < 0 {
return util.InvalidOption(optionLabel, name, names)
}
err = client.CoreV1().Pods(ns).Delete(name, &metav1.DeleteOptions{})
if err != nil {
return err
}
}
log.Infof("Deleted DevPods %s\n", util.ColorInfo(deletePods))
return nil
}