generated from LinuxSuRen/.github
/
scale.go
70 lines (60 loc) · 1.62 KB
/
scale.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
package component
import (
"fmt"
"github.com/kubesphere-sigs/ks/kubectl-plugin/common"
"github.com/spf13/cobra"
)
func newScaleCmd() (cmd *cobra.Command) {
opt := &scaleOption{}
cmd = &cobra.Command{
Use: "scale",
Short: "Set a new size for a Deployment, ReplicaSet, Replication Controller, or StatefulSet of the KubeSphere components",
PreRunE: opt.preRunE,
RunE: opt.runE,
}
flags := cmd.Flags()
flags.IntVarP(&opt.replicas, "replicas", "r", -1, "The new desired number of replicas")
flags.StringVarP(&opt.name, "name", "n", "", "The name of KubeSphere component")
return
}
type scaleOption struct {
name string
namespace string
replicas int
}
func (o *scaleOption) preRunE(_ *cobra.Command, args []string) (err error) {
if len(args) > 0 {
o.name = args[0]
}
if o.name == "" {
err = fmt.Errorf("provide the name of component")
return
}
if o.replicas < 0 {
err = fmt.Errorf("the value of replicas must be >= 0")
return
}
switch o.name {
case "apiserver":
o.name = "ks-apiserver"
o.namespace = "kubesphere-system"
case "controller":
o.name = "ks-controller-manager"
o.namespace = "kubesphere-system"
case "console":
o.name = "ks-console"
o.namespace = "kubesphere-system"
case "jenkins":
o.name = "ks-jenkins"
o.namespace = "kubesphere-devops-system"
case "installer":
o.name = "ks-installer"
o.namespace = "kubesphere-system"
}
return
}
func (o *scaleOption) runE(_ *cobra.Command, _ []string) (err error) {
err = common.ExecCommand("kubectl", "scale", "-n", o.namespace, "--replicas",
fmt.Sprintf("%d", o.replicas), fmt.Sprintf("deploy/%s", o.name))
return
}