forked from kubesphere-sigs/ks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k3d.go
87 lines (76 loc) · 2.29 KB
/
k3d.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 install
import (
"fmt"
"github.com/linuxsuren/http-downloader/pkg/installer"
"github.com/linuxsuren/ks/kubectl-plugin/common"
"github.com/spf13/cobra"
)
func newInstallK3DCmd() (cmd *cobra.Command) {
opt := &k3dOption{}
cmd = &cobra.Command{
Use: "k3d",
Short: "Install KubeSphere with k3d",
Long: `Install KubeSphere with k3d
You can get more details from https://github.com/rancher/k3d/`,
PreRunE: opt.preRunE,
RunE: opt.runE,
PostRunE: opt.postRunE,
}
flags := cmd.Flags()
flags.StringVarP(&opt.name, "name", "n", "",
"The name of k3d cluster")
flags.IntVarP(&opt.agents, "agents", "", 1,
"Specify how many agents you want to create")
flags.IntVarP(&opt.servers, "servers", "", 1,
"Specify how many servers you want to create")
// TODO find a better way to reuse the flags from another command
flags.StringVarP(&opt.version, "version", "", "v3.0.0",
"The version of KubeSphere which you want to install")
flags.StringVarP(&opt.nightly, "nightly", "", "",
"The nightly version you want to install")
flags.StringArrayVarP(&opt.components, "components", "", []string{},
"The components that you want to Enabled with KubeSphere")
return
}
type k3dOption struct {
installerOption
name string
agents int
servers int
}
func (o *k3dOption) preRunE(cmd *cobra.Command, args []string) (err error) {
if o.name == "" && len(args) > 0 {
o.name = args[0]
}
is := installer.Installer{
Provider: "github",
}
err = is.CheckDepAndInstall(map[string]string{
"k3d": "rancher/k3d",
})
fmt.Println(err, "check error")
return
}
func (o *k3dOption) runE(cmd *cobra.Command, args []string) (err error) {
freePort := &common.FreePort{}
var ports []int
if ports, err = freePort.FindFreePortsOfKubeSphere(); err != nil {
return
}
k3dArgs := []string{"cluster", "create",
"-p", fmt.Sprintf(`%d:30880@agent[0]`, ports[0]),
"-p", fmt.Sprintf(`%d:30180@agent[0]`, ports[1]),
"--agents", fmt.Sprintf("%d", o.agents),
"--servers", fmt.Sprintf("%d", o.servers)}
if o.name != "" {
k3dArgs = append(k3dArgs, o.name)
}
err = common.ExecCommand("k3d", k3dArgs...)
return
}
func (o *k3dOption) postRunE(cmd *cobra.Command, args []string) (err error) {
if err = o.installerOption.preRunE(cmd, args); err == nil {
err = o.installerOption.runE(cmd, args)
}
return
}