-
Notifications
You must be signed in to change notification settings - Fork 1
/
delete_env.go
137 lines (123 loc) · 3.38 KB
/
delete_env.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package cmd
import (
"fmt"
"io"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
delete_env_long = templates.LongDesc(`
Deletes one or more environments.
`)
delete_env_example = templates.Examples(`
# Deletes an environment
jx delete env staging
`)
)
// DeleteEnvOptions the options for the create spring command
type DeleteEnvOptions struct {
CommonOptions
DeleteNamespace bool
}
// NewCmdDeleteEnv creates a command object for the "delete repo" command
func NewCmdDeleteEnv(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &DeleteEnvOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "environment",
Short: "Deletes one or more Environments",
Aliases: []string{"env"},
Long: delete_env_long,
Example: delete_env_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
//addDeleteFlags(cmd, &options.CreateOptions)
cmd.Flags().BoolVarP(&options.DeleteNamespace, "namespace", "n", false, "Delete the namespace for the Environment too?")
return cmd
}
// Run implements the command
func (o *DeleteEnvOptions) Run() error {
jxClient, currentNs, err := o.JXClient()
if err != nil {
return err
}
kubeClient, _, err := o.KubeClient()
if err != nil {
return err
}
apisClient, err := o.CreateApiExtensionsClient()
if err != nil {
return err
}
kube.RegisterEnvironmentCRD(apisClient)
ns, currentEnv, err := kube.GetDevNamespace(kubeClient, currentNs)
if err != nil {
return err
}
envMap, envNames, err := kube.GetEnvironments(jxClient, ns)
if err != nil {
return err
}
name := ""
args := o.Args
if len(args) > 0 {
for _, arg := range args {
if util.StringArrayIndex(envNames, arg) < 0 {
return util.InvalidArg(arg, envNames)
}
}
for _, arg := range args {
err = o.deleteEnviroment(jxClient, ns, arg, envMap)
if err != nil {
return err
}
}
} else {
name, err = kube.PickEnvironment(envNames, currentEnv, o.In, o.Out, o.Err)
if err != nil {
return err
}
err = o.deleteEnviroment(jxClient, ns, name, envMap)
if err != nil {
return err
}
}
return nil
}
func (o *DeleteEnvOptions) deleteEnviroment(jxClient versioned.Interface, ns string, name string, envMap map[string]*v1.Environment) error {
err := jxClient.JenkinsV1().Environments(ns).Delete(name, &metav1.DeleteOptions{})
if err != nil {
return err
}
log.Infof("Deleted environment %s\n", util.ColorInfo(name))
env := envMap[name]
envNs := env.Spec.Namespace
if envNs == "" {
return fmt.Errorf("No namespace for environment %s", name)
}
kind := env.Spec.Kind
if o.DeleteNamespace || !kind.IsPermanent() {
return o.KubeClientCached.CoreV1().Namespaces().Delete(envNs, &metav1.DeleteOptions{})
}
log.Infof("To delete the associated namespace %s for environment %s then please run this command\n", name, envNs)
log.Infof(util.ColorInfo(" kubectl delete namespace %s\n"), envNs)
return nil
}