forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_env.go
138 lines (124 loc) · 3.34 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
138
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"
cmdutil "github.com/jenkins-x/jx/pkg/jx/cmd/util"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
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 {
CreateOptions
DeleteNamespace bool
}
// NewCmdDeleteEnv creates a command object for the "delete repo" command
func NewCmdDeleteEnv(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &DeleteEnvOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
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()
cmdutil.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 {
f := o.Factory
jxClient, currentNs, err := f.CreateJXClient()
if err != nil {
return err
}
kubeClient, _, err := f.CreateClient()
if err != nil {
return err
}
apisClient, err := f.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)
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.Clientset, ns string, name string, envMap map[string]*v1.Environment) error {
err := jxClient.JenkinsV1().Environments(ns).Delete(name, &metav1.DeleteOptions{})
if err != nil {
return err
}
o.Printf("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.runCommand("kubectl", "delete", "namespace", envNs)
}
o.Printf("To delete the associated namespace %s for environment %s then please run this command\n", name, envNs)
o.Printf(util.ColorInfo(" kubectl delete namespace %s\n"), envNs)
return nil
}