-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.go
104 lines (85 loc) · 3.19 KB
/
cmd.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
package main
import (
"fmt"
"os"
"github.com/abema/kubetool/kube"
"github.com/alecthomas/kingpin"
"github.com/fatih/color"
)
var (
red = color.New(color.FgRed).SprintfFunc()
app = kingpin.New("kubetool", "kubernetes bulk task executor.")
verbose = app.Flag("verbose", "Enable verbose log.").Short('v').Bool()
namespace = app.Flag("namespace", "Target namespace. default is all namespaces").String()
yes = app.Flag("yes", "Skip confirmation.").Short('y').Bool()
force = app.Flag("force", "Force reload pods. Ignores pod status while reloading.").Short('f').Bool()
interval = app.Flag("interval", "Reloading interval on restarting each pod.").Default("0").Int()
minStable = app.Flag("min-stable", "Minimum value of available pod percentage to detect RC is stable or not. (0.0-1.0). Defalt is 0.8").Default("0.8").Float64()
// command info
info = app.Command("info", "Print cluster & version info about cluster.").Alias("i")
// command rc
rc = app.Command("rc", "Print all rc.")
// command pod
pod = app.Command("pod", "Print all pods").Alias("pods").Alias("po")
podRC = pod.Flag("rc", "rc name for pod target").String()
// command reload
reload = app.Command("reload", "Reload all pods in rc.")
reloadName = reload.Arg("rc-name", "Name of target RC.").Required().String()
reloadOne = reload.Flag("1", "Reload only 1 pod").Bool()
// command set version
update = app.Command("update", "Update image version of rc")
updateName = update.Arg("rc-name", "Name of target RC.").Required().String()
updateVersion = update.Arg("version", "Version tag of image.").String()
updateReload = update.Flag("reload", "Reload pods after update.").Bool()
updateReloadOne = update.Flag("1", "Reload only 1 pod after update.").Short('1').Bool()
updateContainer = update.Flag("container", "Target container name. Default is first container in defs.").Short('c').String()
fixVersion = app.Command("fix-version", "Fix all pods to destroy all that has different version of RC ones.")
fixVersionName = fixVersion.Arg("rc-name", "Name of target RC.").Required().String()
)
func init() {
app.Version("0.1.0")
}
func main() {
cmd := kingpin.MustParse(app.Parse(os.Args[1:]))
ktool := kube.Tool{}
ktool.SetYes(*yes)
ktool.SetForce(*force)
ktool.SetInterval(*interval)
if namespace != nil {
ktool.SetNamespace(*namespace)
}
if *minStable < 0 || *minStable > 1 {
fmt.Fprintln(os.Stderr, "minimum stable rate must be in range of 0.0-1.0")
os.Exit(1)
}
var err error
switch cmd {
case info.FullCommand():
err = ktool.PrintInfo()
case rc.FullCommand():
err = ktool.PrintRCList()
case pod.FullCommand():
rcname := ""
if podRC != nil {
rcname = *podRC
}
err = ktool.PrintPodList(rcname)
case reload.FullCommand():
err = ktool.Reload(*reloadName, *reloadOne)
case update.FullCommand():
container := ""
if updateContainer != nil {
container = *updateContainer
}
err = ktool.Update(*updateName, container, *updateVersion)
if *updateReload && err == nil {
err = ktool.Reload(*updateName, *updateReloadOne)
}
case fixVersion.FullCommand():
err = ktool.FixVersion(*fixVersionName)
}
if err != nil {
fmt.Println(red(err.Error()))
os.Exit(1)
}
}