forked from rancher/rke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.go
286 lines (253 loc) · 8.16 KB
/
cluster.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package cmd
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/rancher/rke/cluster"
"github.com/rancher/rke/pki"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"k8s.io/client-go/util/cert"
)
func ClusterCommand() cli.Command {
clusterUpDownUpgradeFlags := []cli.Flag{
cli.StringFlag{
Name: "cluster-file",
Usage: "Specify an alternate cluster YAML file",
Value: cluster.DefaultClusterConfig,
EnvVar: "CLUSTER_FILE",
},
}
clusterForceRemove := []cli.Flag{
cli.BoolFlag{
Name: "force",
Usage: "Force removal of the cluster",
},
}
return cli.Command{
Name: "cluster",
ShortName: "cluster",
Usage: "Operations on the cluster",
Flags: clusterUpDownUpgradeFlags,
Subcommands: []cli.Command{
cli.Command{
Name: "up",
Usage: "Bring the cluster up",
Action: clusterUpFromCli,
Flags: clusterUpDownUpgradeFlags,
},
cli.Command{
Name: "down",
Usage: "Teardown the cluster and clean cluster nodes",
Action: clusterDownFromCli,
Flags: append(clusterUpDownUpgradeFlags, clusterForceRemove...),
},
cli.Command{
Name: "version",
Usage: "Show Cluster Kubernetes version",
Action: getClusterVersion,
Flags: clusterUpDownUpgradeFlags,
},
cli.Command{
Name: "upgrade",
Usage: "Upgrade Cluster Kubernetes version",
Action: clusterUpgradeFromCli,
Flags: clusterUpDownUpgradeFlags,
},
},
}
}
func ClusterUp(clusterFile string) (string, string, string, string, error) {
logrus.Infof("Building Kubernetes cluster")
var APIURL, caCrt, clientCert, clientKey string
kubeCluster, err := cluster.ParseConfig(clusterFile)
if err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
err = kubeCluster.TunnelHosts()
if err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
currentCluster, err := kubeCluster.GetClusterState()
if err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
if err := cluster.CheckEtcdHostsChanged(kubeCluster, currentCluster); err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
err = cluster.SetUpAuthentication(kubeCluster, currentCluster)
if err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
err = kubeCluster.SetUpHosts()
if err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
err = kubeCluster.DeployClusterPlanes()
if err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
err = cluster.ReconcileCluster(kubeCluster, currentCluster)
if err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
err = kubeCluster.SaveClusterState(clusterFile)
if err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
err = kubeCluster.DeployNetworkPlugin()
if err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
err = kubeCluster.DeployK8sAddOns()
if err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
err = kubeCluster.DeployUserAddOns()
if err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
APIURL = fmt.Sprintf("https://" + kubeCluster.ControlPlaneHosts[0].IP + ":6443")
caCrt = string(cert.EncodeCertPEM(kubeCluster.Certificates[pki.CACertName].Certificate))
clientCert = string(cert.EncodeCertPEM(kubeCluster.Certificates[pki.KubeAdminCommonName].Certificate))
clientKey = string(cert.EncodePrivateKeyPEM(kubeCluster.Certificates[pki.KubeAdminCommonName].Key))
logrus.Infof("Finished building Kubernetes cluster successfully")
return APIURL, caCrt, clientCert, clientKey, nil
}
func ClusterUpgrade(clusterFile string) (string, string, string, string, error) {
logrus.Infof("Upgrading Kubernetes cluster")
var APIURL, caCrt, clientCert, clientKey string
kubeCluster, err := cluster.ParseConfig(clusterFile)
if err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
logrus.Debugf("Getting current cluster")
currentCluster, err := kubeCluster.GetClusterState()
if err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
if currentCluster == nil {
return APIURL, caCrt, clientCert, clientKey, fmt.Errorf("Failed to get the current state of Kubernetes cluster")
}
// check if user try to add/remove hosts during upgrade
if err := cluster.CheckHostsChangedOnUpgrade(kubeCluster, currentCluster); err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
/*
kubeCluster is the cluster.yaml definition. It should have updated configuration
currentCluster is the current state fetched from kubernetes
we add currentCluster certs to kubeCluster, kubeCluster would have the latest configuration from cluster.yaml and the certs to connect to k8s and apply the upgrade
*/
kubeCluster.Certificates = currentCluster.Certificates
logrus.Debugf("Setting up upgrade tunnels")
err = kubeCluster.TunnelHosts()
if err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
logrus.Debugf("Starting cluster upgrade")
err = kubeCluster.ClusterUpgrade()
if err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
err = kubeCluster.SaveClusterState(clusterFile)
if err != nil {
return APIURL, caCrt, clientCert, clientKey, err
}
logrus.Infof("Cluster upgraded successfully")
APIURL = fmt.Sprintf("https://" + kubeCluster.ControlPlaneHosts[0].IP + ":6443")
caCrt = string(cert.EncodeCertPEM(kubeCluster.Certificates[pki.CACertName].Certificate))
clientCert = string(cert.EncodeCertPEM(kubeCluster.Certificates[pki.KubeAdminCommonName].Certificate))
clientKey = string(cert.EncodePrivateKeyPEM(kubeCluster.Certificates[pki.KubeAdminCommonName].Key))
return APIURL, caCrt, clientCert, clientKey, nil
}
func ClusterDown(clusterFile string) error {
logrus.Infof("Tearing down Kubernetes cluster")
kubeCluster, err := cluster.ParseConfig(clusterFile)
if err != nil {
return err
}
err = kubeCluster.TunnelHosts()
if err != nil {
return err
}
logrus.Debugf("Starting Cluster removal")
err = kubeCluster.ClusterDown()
if err != nil {
return err
}
logrus.Infof("Cluster removed successfully")
return nil
}
func clusterUpFromCli(ctx *cli.Context) error {
clusterFile, err := resolveClusterFile(ctx)
if err != nil {
return fmt.Errorf("Failed to resolve cluster file: %v", err)
}
_, _, _, _, err = ClusterUp(clusterFile)
return err
}
func clusterUpgradeFromCli(ctx *cli.Context) error {
clusterFile, err := resolveClusterFile(ctx)
if err != nil {
return fmt.Errorf("Failed to resolve cluster file: %v", err)
}
_, _, _, _, err = ClusterUpgrade(clusterFile)
return err
}
func clusterDownFromCli(ctx *cli.Context) error {
force := ctx.Bool("force")
if !force {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("Are you sure you want to remove Kubernetes cluster [y/n]: ")
input, err := reader.ReadString('\n')
input = strings.TrimSpace(input)
if err != nil {
return err
}
if input != "y" && input != "Y" {
return nil
}
}
clusterFile, err := resolveClusterFile(ctx)
if err != nil {
return fmt.Errorf("Failed to resolve cluster file: %v", err)
}
return ClusterDown(clusterFile)
}
func resolveClusterFile(ctx *cli.Context) (string, error) {
clusterFile := ctx.String("cluster-file")
fp, err := filepath.Abs(clusterFile)
if err != nil {
return "", fmt.Errorf("failed to lookup current directory name: %v", err)
}
file, err := os.Open(fp)
if err != nil {
return "", fmt.Errorf("Can not find cluster configuration file: %v", err)
}
defer file.Close()
buf, err := ioutil.ReadAll(file)
if err != nil {
return "", fmt.Errorf("failed to read file: %v", err)
}
clusterFileBuff := string(buf)
/*
This is a hacky way to add config path to cluster object without messing with
ClusterUp function and to avoid conflict with calls from kontainer-engine, basically
i add config path (cluster.yml by default) to a field into the config buffer
to be parsed later and added as ConfigPath field into cluster object.
*/
clusterFileBuff = fmt.Sprintf("%s\nconfig_path: %s\n", clusterFileBuff, clusterFile)
return clusterFileBuff, nil
}
func getClusterVersion(ctx *cli.Context) error {
localKubeConfig := cluster.GetLocalKubeConfig(ctx.String("cluster-file"))
serverVersion, err := cluster.GetK8sVersion(localKubeConfig)
if err != nil {
return err
}
fmt.Printf("Server Version: %s\n", serverVersion)
return nil
}