forked from kubesphere-sigs/ks
/
kk.go
104 lines (91 loc) · 2.77 KB
/
kk.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 install
import (
"fmt"
"github.com/linuxsuren/http-downloader/pkg/installer"
"github.com/linuxsuren/ks/kubectl-plugin/common"
"github.com/spf13/cobra"
"strings"
)
const (
// DefaultKubeSphereVersion is the default version of KubeSphere
DefaultKubeSphereVersion = "v3.1.0"
)
func newInstallWithKKCmd() (cmd *cobra.Command) {
opt := &kkOption{}
cmd = &cobra.Command{
Use: "kk",
Aliases: []string{"kubekey"},
Short: "Install KubeSphere with kubekey (aka kk)",
Long: `Install KubeSphere with kubekey (aka kk)
Get more details about kubekey from https://github.com/kubesphere/kubekey`,
Example: `ks install kk --components devops
ks install kk --version nightly --components devops`,
ValidArgsFunction: common.PluginAbleComponentsCompletion(),
PreRunE: opt.preRunE,
RunE: opt.runE,
}
flags := cmd.Flags()
flags.StringVarP(&opt.version, "version", "v", DefaultKubeSphereVersion,
fmt.Sprintf("The version of KubeSphere. Support value could be %s, nightly, nightly-20210309. nightly equals to nightly-latest", DefaultKubeSphereVersion))
flags.StringArrayVarP(&opt.components, "components", "", []string{},
"The components which you want to enable after the installation")
flags.StringVarP(&opt.zone, "zone", "", "cn",
"Set environment variables, for example export KKZONE=cn")
return
}
type kkOption struct {
version string
kubernetesVersion string
components []string
zone string
}
func (o *kkOption) versionCheck() (err error) {
if strings.HasPrefix(o.version, "nightly") {
ver := strings.ReplaceAll(o.version, "nightly-", "")
ver = strings.ReplaceAll(ver, "nightly", "")
if ver == "" {
ver = "latest"
}
if _, ver = common.GetNightlyTag(ver); ver == "" {
err = fmt.Errorf("not support version: %s", o.version)
} else {
o.version = ver
}
} else if o.version != DefaultKubeSphereVersion {
switch o.version {
case DefaultKubeSphereVersion, "v3.0.0":
default:
err = fmt.Errorf("not support version: %s", o.version)
}
}
return
}
func (o *kkOption) preRunE(cmd *cobra.Command, args []string) (err error) {
if err = o.versionCheck(); err != nil {
return
}
is := installer.Installer{
Provider: "github",
}
err = is.CheckDepAndInstall(map[string]string{
"kk": "kubesphere/kubekey",
})
return
}
func (o *kkOption) runE(cmd *cobra.Command, args []string) (err error) {
report := installReport{}
report.init()
commander := Commander{
Env: []string{fmt.Sprintf("KKZONE=%s", o.zone)},
}
if err = commander.execCommand("kk", "create", "cluster", "--with-kubesphere", o.version, "--yes"); err != nil {
return
}
for _, component := range o.components {
if err = commander.execCommand("ks", "com", "enable", component); err != nil {
return
}
}
report.end()
return
}