forked from kubesphere-sigs/ks
/
installer.go
157 lines (138 loc) · 4.05 KB
/
installer.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package install
import (
"fmt"
"github.com/linuxsuren/ks/kubectl-plugin/common"
"github.com/linuxsuren/ks/kubectl-plugin/install/storage"
"github.com/spf13/cobra"
"html/template"
"k8s.io/client-go/dynamic"
"os"
"path"
)
func newInstallerCmd() (cmd *cobra.Command) {
opt := &installerOption{}
cmd = &cobra.Command{
Use: "installer",
Short: "Install KubeSphere in an exist Kubernetes with ks-installer",
Long: `Install KubeSphere in an exist Kubernetes with ks-installer
You can get more details about the ks-installer from https://github.com/kubesphere/ks-installer`,
Example: "ks install installer --nightly latest --components DevOps,Logging",
PreRunE: opt.preRunE,
RunE: opt.runE,
}
flags := cmd.Flags()
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 installerOption struct {
version string
nightly string
components []string
// inner fields
client dynamic.Interface
ksInstaller common.KSInstallerSpec
}
func (o *installerOption) preRunE(_ *cobra.Command, args []string) (err error) {
if o.client, _, err = common.GetClient(); err != nil {
err = fmt.Errorf("unable to init the k8s client, error: %v", err)
}
// check if ks was exists
_, o.nightly = common.GetNightlyTag(o.nightly)
// parse the ks-installer
o.ksInstaller = common.KSInstallerSpec{
Version: o.version,
ImageNamespace: "kubesphere",
}
if o.nightly != "" {
o.ksInstaller.ImageNamespace = "kubespheredev"
o.ksInstaller.Version = o.nightly
}
for _, item := range o.components {
switch item {
case "servicemesh":
o.ksInstaller.Servicemesh.Enabled = true
case "openpitrix":
o.ksInstaller.Openpitrix.Enabled = true
case "notification":
o.ksInstaller.Notification.Enabled = true
case "networkPolicy":
o.ksInstaller.NetworkPolicy.Enabled = true
case "metricsServer":
o.ksInstaller.MetricsServer.Enabled = true
case "logging":
o.ksInstaller.Logging.Enabled = true
case "events":
o.ksInstaller.Events.Enabled = true
case "devops":
o.ksInstaller.DevOps.Enabled = true
case "auditing":
o.ksInstaller.Auditing.Enabled = true
case "alerting":
o.ksInstaller.Alerting.Enabled = true
}
}
if !storage.HasDefaultStorageClass(o.client) {
err = storage.CreateEBSAsDefault()
}
return
}
func (o *installerOption) getCrdAndCC() (crd, cc string, err error) {
var crdTmp *template.Template
if crdTmp, err = template.New("crd").Parse(ks3_0CRD); err != nil {
err = fmt.Errorf("failed to parse the crd template, error: %v", err)
return
}
var ccTmp *template.Template
if ccTmp, err = template.New("cc").Parse(clusterConfiguration); err != nil {
err = fmt.Errorf("failed to parse the clusterConfigration template, error: %v", err)
return
}
crd = path.Join(os.TempDir(), "crd.yaml")
cc = path.Join(os.TempDir(), "cc.yaml")
var crdOut, ccOut *os.File
if crdOut, err = os.Create(crd); err != nil {
return
}
if ccOut, err = os.Create(cc); err != nil {
return
}
if err = crdTmp.Execute(crdOut, o.ksInstaller); err != nil {
return
}
err = ccTmp.Execute(ccOut, o.ksInstaller)
return
}
func (o *installerOption) runE(_ *cobra.Command, args []string) (err error) {
var crdPath, ccPath string
if crdPath, ccPath, err = o.getCrdAndCC(); err != nil {
return
}
defer func() {
// clean the temporary files
_ = os.RemoveAll(crdPath)
_ = os.RemoveAll(ccPath)
}()
commander := Commander{}
if err = commander.execCommand("kubectl", "apply", "-f", crdPath); err == nil {
err = commander.execCommand("kubectl", "apply", "-f", ccPath)
}
return
}
var localStorageClass = `
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: kubernetes.io/storageos
parameters:
pool: default
description: Kubernetes volume
fsType: ext4
adminSecretNamespace: default
adminSecretName: storageos-secret
`