forked from kubesphere-sigs/ks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reset.go
109 lines (97 loc) · 3.16 KB
/
reset.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
package component
import (
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/linuxsuren/ks/kubectl-plugin/common"
kstypes "github.com/linuxsuren/ks/kubectl-plugin/types"
"github.com/spf13/cobra"
"k8s.io/client-go/dynamic"
)
// NewComponentResetCmd returns a command to enable (or disable) a component by name
func NewComponentResetCmd(client dynamic.Interface) (cmd *cobra.Command) {
opt := &ResetOption{
Option: Option{
Client: client,
},
}
cmd = &cobra.Command{
Use: "reset",
Short: "Reset the component by name",
Example: `'ks com reset -r=false --nightly latest console' will reset ks-console to the latest release
'ks com reset -r=false -a' will reset ks-apiserver, ks-controller-manager, ks-console to the latest
'ks com reset -a --nightly latest' will reset the images to the latest nightly which comes from yesterday
'ks com reset -a --nightly latest-1' will reset the images to the nightly which comes from the day before yesterday`,
PreRunE: opt.preRunE,
RunE: opt.resetRunE,
}
flags := cmd.Flags()
flags.BoolVarP(&opt.Release, "release", "r", true,
"Indicate if you want to update KubeSphere deploy image to release. Released images come from KubeSphere/xxx. Otherwise images come from kubespheredev/xxx")
flags.StringVarP(&opt.Tag, "tag", "t", kstypes.KsVersion,
"The tag of KubeSphere deploys")
flags.BoolVarP(&opt.ResetAll, "all", "a", false,
"Indicate if you want to all supported components")
flags.StringVarP(&opt.Nightly, "nightly", "", "",
"Indicate if you want to update component to nightly build. It should be date, e.g. 2021-01-01. Or you can just use latest represents the last day")
flags.StringVarP(&opt.Name, "name", "n", "",
"The name of target component which you want to reset. This does not work if you provide flag --all")
return
}
func (o *ResetOption) preRunE(cmd *cobra.Command, args []string) (err error) {
if o.Name == "" && len(args) > 0 {
o.Name = args[0]
}
return
}
func (o *ResetOption) resetRunE(cmd *cobra.Command, args []string) (err error) {
if o.Tag == "" {
// let users choose it if the tag option is empty
dc := kstypes.DockerClient{
Image: "kubesphere/ks-apiserver",
}
var tags *kstypes.DockerTags
if tags, err = dc.GetTags(); err != nil {
err = fmt.Errorf("cannot get the tags, %#v", err)
return
}
prompt := &survey.Select{
Message: "Please select the tag which you want to check:",
Options: tags.Tags,
}
if err = survey.AskOne(prompt, &o.Tag); err != nil {
return
}
}
imageOrg := "kubespheredev"
if o.Release && o.Nightly == "" {
imageOrg = "kubesphere"
} else {
// try to parse the nightly date
_, o.Tag = common.GetNightlyTag(o.Nightly)
}
if o.ResetAll {
o.Name = "apiserver"
if err = o.updateBy(imageOrg, o.Tag); err != nil {
return
}
o.Name = "controller"
if err = o.updateBy(imageOrg, o.Tag); err != nil {
return
}
o.Name = "console"
if err = o.updateBy(imageOrg, o.Tag); err != nil {
return
}
o.Name = "installer"
if err = o.updateBy(imageOrg, o.Tag); err != nil {
return
}
} else {
if o.Name == "" {
err = fmt.Errorf("please provide a component name")
} else {
err = o.updateBy(imageOrg, o.Tag)
}
}
return
}