/
delete.go
87 lines (75 loc) · 2.06 KB
/
delete.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
package cmd
import (
"os"
"github.com/baetyl/baetyl-go/v2/context"
"github.com/baetyl/baetyl-go/v2/errors"
"github.com/baetyl/baetyl-go/v2/log"
specv1 "github.com/baetyl/baetyl-go/v2/spec/v1"
"github.com/baetyl/baetyl-go/v2/utils"
"github.com/spf13/cobra"
"github.com/baetyl/baetyl/ami"
"github.com/baetyl/baetyl/config"
)
var (
ns string
)
func init() {
rootCmd.AddCommand(deleteCmd)
deleteCmd.Flags().StringVarP(&ns, "namespace", "n", "baetyl-edge-system", "The namespace of applications which will be deleted.")
deleteCmd.Flags().StringVarP(&mode, "mode", "m", "native", "The running mode of applications, supports 'kube' and 'native'.")
}
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete baetyl applications.",
Long: "Delete baetyl applications by namespace.",
Run: func(_ *cobra.Command, _ []string) {
delete()
},
}
func delete() {
var err error
var l *log.Logger
l, err = log.Init(log.Config{Level: "debug", Encoding: "console"})
if err != nil {
log.L().Error("failed to init logger", log.Error(err))
return
}
defer func() {
if err != nil {
l.Error(err.Error())
}
}()
// prepare env
if _, ok := modes[mode]; !ok {
err = errors.New("The parameter 'mode' is invalid")
return
}
err = os.Setenv(context.KeyRunMode, mode)
if err != nil {
return
}
// stats app, then delete them
amiConfig := config.AmiConfig{}
err = utils.SetDefaults(&amiConfig)
if err != nil {
return
}
// TODO: create client like kubectl without confpath
amiConfig.Kube.OutCluster = true
amiConfig.Kube.ConfPath = ".kube/config"
am, err := ami.NewAMI(mode, amiConfig)
if err != nil {
return
}
var allstats []specv1.AppStats
allstats, err = am.StatsApps(ns)
log.L().Info("stats apps", log.Any("all", allstats), log.Error(err))
for _, appstats := range allstats {
err = am.DeleteApp(ns, appstats.Name)
if err != nil {
log.L().Error("failed to delete app", log.Any("name", appstats.Name), log.Any("namespace", ns), log.Error(err))
} else {
log.L().Info("delete app", log.Any("name", appstats.Name), log.Any("namespace", ns))
}
}
}