-
Notifications
You must be signed in to change notification settings - Fork 108
/
steps.go
593 lines (505 loc) · 17.4 KB
/
steps.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// Copyright 2021 Flant JSC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// TODO structure these functions into classes
// TODO move states saving to operations/bootstrap/state.go
package bootstrap
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"fmt"
"math/big"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/deckhouse/deckhouse/dhctl/pkg/app"
"github.com/deckhouse/deckhouse/dhctl/pkg/config"
"github.com/deckhouse/deckhouse/dhctl/pkg/kubernetes/actions/converge"
"github.com/deckhouse/deckhouse/dhctl/pkg/kubernetes/actions/deckhouse"
"github.com/deckhouse/deckhouse/dhctl/pkg/kubernetes/client"
"github.com/deckhouse/deckhouse/dhctl/pkg/log"
"github.com/deckhouse/deckhouse/dhctl/pkg/state"
"github.com/deckhouse/deckhouse/dhctl/pkg/state/cache"
"github.com/deckhouse/deckhouse/dhctl/pkg/system/ssh"
"github.com/deckhouse/deckhouse/dhctl/pkg/system/ssh/frontend"
"github.com/deckhouse/deckhouse/dhctl/pkg/template"
"github.com/deckhouse/deckhouse/dhctl/pkg/terraform"
"github.com/deckhouse/deckhouse/dhctl/pkg/util/retry"
"github.com/deckhouse/deckhouse/dhctl/pkg/util/tomb"
"github.com/deckhouse/deckhouse/go_lib/registry-packages-proxy/proxy"
"github.com/deckhouse/deckhouse/go_lib/registry-packages-proxy/registry"
)
const (
ManifestCreatedInClusterCacheKey = "tf-state-and-manifests-in-cluster"
MasterHostsCacheKey = "cluster-hosts"
BastionHostCacheKey = "bastion-hosts"
)
func BootstrapMaster(sshClient *ssh.Client, bundleName, nodeIP string, metaConfig *config.MetaConfig, controller *template.Controller) error {
return log.Process("bootstrap", "Initial bootstrap", func() error {
if err := template.PrepareBootstrap(controller, nodeIP, bundleName, metaConfig); err != nil {
return fmt.Errorf("prepare bootstrap: %v", err)
}
err := log.Process("bootstrap", fmt.Sprintf("Prepare %s", app.NodeDeckhouseDirectoryPath), func() error {
if err := sshClient.Command("mkdir", "-p", app.DeckhouseNodeBinPath).Sudo().Run(); err != nil {
return fmt.Errorf("ssh: mkdir -p %s: %w", app.DeckhouseNodeBinPath, err)
}
if err := sshClient.Command("mkdir", "-p", "-m", "1777", app.DeckhouseNodeTmpPath).Sudo().Run(); err != nil {
return fmt.Errorf("ssh: mkdir -p -m 1777 %s: %w", app.DeckhouseNodeTmpPath, err)
}
return nil
})
if err != nil {
return fmt.Errorf("cannot create %s directories: %w", app.NodeDeckhouseDirectoryPath, err)
}
for _, bootstrapScript := range []string{"bootstrap.sh", "bootstrap-networks.sh"} {
scriptPath := filepath.Join(controller.TmpDir, "bootstrap", bootstrapScript)
err := log.Process("default", bootstrapScript, func() error {
if _, err := os.Stat(scriptPath); err != nil {
if os.IsNotExist(err) {
log.InfoF("Script %s wasn't found\n", scriptPath)
return nil
}
return fmt.Errorf("script path: %v", err)
}
logs := make([]string, 0)
cmd := sshClient.UploadScript(scriptPath).
WithStdoutHandler(func(l string) {
logs = append(logs, l)
log.DebugLn(l)
}).Sudo()
_, err := cmd.Execute()
if err != nil {
log.ErrorLn(strings.Join(logs, "\n"))
return fmt.Errorf("run %s: %w", scriptPath, err)
}
return nil
})
if err != nil {
return err
}
}
return nil
})
}
func PrepareBashibleBundle(bundleName, nodeIP, devicePath string, metaConfig *config.MetaConfig, controller *template.Controller) error {
return log.Process("bootstrap", "Prepare Bashible Bundle", func() error {
return template.PrepareBundle(controller, nodeIP, bundleName, devicePath, metaConfig)
})
}
func ExecuteBashibleBundle(sshClient *ssh.Client, tmpDir string) error {
return log.Process("bootstrap", "Execute Bashible Bundle", func() error {
bundleCmd := sshClient.UploadScript("bashible.sh", "--local").Sudo()
parentDir := tmpDir + "/var/lib"
bundleDir := "bashible"
_, err := bundleCmd.ExecuteBundle(parentDir, bundleDir)
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
return fmt.Errorf("bundle '%s' error: %v\nstderr: %s", bundleDir, err, string(ee.Stderr))
}
return fmt.Errorf("bundle '%s' error: %v", bundleDir, err)
}
return nil
})
}
const (
bashibleInstalledMessage = `Bashible is already installed and healthy!
%s
`
bashibleIsNotReadyMessage = `Bashible is not ready! Let's try to install it ...
Reason: %s
`
)
func CheckBashibleBundle(sshClient *ssh.Client) bool {
var bashibleUpToDate bool
_ = log.Process("bootstrap", "Check Bashible", func() error {
bashibleCmd := sshClient.Command("/var/lib/bashible/bashible.sh", "--local").
Sudo().WithTimeout(20 * time.Second)
var output string
err := bashibleCmd.WithStdoutHandler(func(l string) {
if output == "" {
output = l
} else {
return
}
switch {
case strings.Contains(output, "Can't acquire lockfile /var/lock/bashible."):
fallthrough
case strings.Contains(output, "Configuration is in sync, nothing to do."):
log.InfoF(bashibleInstalledMessage, output)
bashibleUpToDate = true
default:
log.InfoF(bashibleIsNotReadyMessage, output)
}
}).Run()
if err != nil {
log.DebugLn(err.Error())
}
return nil
})
return bashibleUpToDate
}
func SetupSSHTunnelToRegistryPackagesProxy(sshCl *ssh.Client) (*frontend.ReverseTunnel, error) {
tun := sshCl.ReverseTunnel("5444:127.0.0.1:5444")
err := tun.Up()
if err != nil {
return nil, err
}
return tun, nil
}
type registryClientConfigGetter struct {
registry.ClientConfig
}
func newRegistryClientConfigGetter(config config.RegistryData) (*registryClientConfigGetter, error) {
auth, err := config.Auth()
if err != nil {
return nil, fmt.Errorf("registry auth: %v", err)
}
return ®istryClientConfigGetter{
ClientConfig: registry.ClientConfig{
Repository: strings.Join([]string{config.Address, config.Path}, "/"),
Scheme: config.Scheme,
CA: config.CA,
Auth: auth,
},
}, nil
}
func (r *registryClientConfigGetter) Get(_ string) (*registry.ClientConfig, error) {
return &r.ClientConfig, nil
}
func StartRegistryPackagesProxy(config config.RegistryData, clusterDomain string) error {
cert, err := generateTLSCertificate(clusterDomain)
if err != nil {
return fmt.Errorf("failed to generate TLS certificate: %v", err)
}
listener, err := tls.Listen("tcp", "127.0.0.1:5444", &tls.Config{
Certificates: []tls.Certificate{*cert},
})
if err != nil {
return fmt.Errorf("failed to listen: %v", err)
}
clientConfigGetter, err := newRegistryClientConfigGetter(config)
if err != nil {
return fmt.Errorf("failed to create registry client config getter: %v", err)
}
proxy := proxy.NewProxy(&http.Server{}, listener, clientConfigGetter, registryPackagesProxyLogger{}, ®istry.DefaultClient{})
go proxy.Serve()
return nil
}
type registryPackagesProxyLogger struct{}
func (r registryPackagesProxyLogger) Errorf(format string, args ...interface{}) {
log.ErrorF(format, args...)
}
func (r registryPackagesProxyLogger) Infof(format string, args ...interface{}) {
log.InfoF(format, args...)
}
func (r registryPackagesProxyLogger) Warnf(format string, args ...interface{}) {
log.WarnF(format, args...)
}
func (r registryPackagesProxyLogger) Debugf(format string, args ...interface{}) {
log.DebugF(format, args...)
}
func (r registryPackagesProxyLogger) Error(args ...interface{}) {
log.ErrorLn(args...)
}
func generateTLSCertificate(clusterDomain string) (*tls.Certificate, error) {
now := time.Now()
subjectKeyId := make([]byte, 10)
_, err := rand.Read(subjectKeyId)
if err != nil {
return nil, fmt.Errorf("failed to generate subject key id: %v", err)
}
certTemplate := &x509.Certificate{
SerialNumber: big.NewInt(now.Unix()),
Subject: pkix.Name{
CommonName: fmt.Sprintf("registry-packages-proxy.%s", clusterDomain),
Country: []string{"Unknown"},
Organization: []string{clusterDomain},
OrganizationalUnit: []string{"registry-packages-proxy"},
},
NotBefore: now,
NotAfter: now.AddDate(0, 0, 1), // Valid for one day
SubjectKeyId: subjectKeyId,
BasicConstraintsValid: true,
IsCA: true,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageKeyEncipherment |
x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
}
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, fmt.Errorf("failed to generate private key: %v", err)
}
cert, err := x509.CreateCertificate(rand.Reader, certTemplate, certTemplate,
priv.Public(), priv)
if err != nil {
return nil, fmt.Errorf("failed to create certificate: %v", err)
}
tlsCert := &tls.Certificate{
Certificate: [][]byte{cert},
PrivateKey: priv,
}
return tlsCert, nil
}
func RunBashiblePipeline(sshClient *ssh.Client, cfg *config.MetaConfig, nodeIP, devicePath string) error {
if err := CheckDHCTLDependencies(sshClient); err != nil {
return err
}
bundleName, err := DetermineBundleName(sshClient)
if err != nil {
return err
}
templateController := template.NewTemplateController("")
_ = log.Process("bootstrap", "Rendered templates directory", func() error {
log.InfoLn(templateController.TmpDir)
return nil
})
if err := BootstrapMaster(sshClient, bundleName, nodeIP, cfg, templateController); err != nil {
return err
}
if ok := CheckBashibleBundle(sshClient); ok {
return nil
}
if err = PrepareBashibleBundle(bundleName, nodeIP, devicePath, cfg, templateController); err != nil {
return err
}
tomb.RegisterOnShutdown("Delete templates temporary directory", func() {
if !app.IsDebug {
_ = os.RemoveAll(templateController.TmpDir)
}
})
return ExecuteBashibleBundle(sshClient, templateController.TmpDir)
}
const dependencyCmd = "type"
func CheckDHCTLDependencies(sshClient *ssh.Client) error {
return log.Process("bootstrap", "Check DHCTL Dependencies", func() error {
dependencyArgs := []string{"sudo", "rm", "tar", "mount", "awk", "grep", "cut", "sed", "shopt",
"mkdir", "cp", "join"}
for _, args := range dependencyArgs {
output, err := sshClient.Command(dependencyCmd, args).CombinedOutput()
if err != nil {
return fmt.Errorf("bashible dependency error: %s",
string(output),
)
}
}
log.InfoLn("OK!")
return nil
})
}
func DetermineBundleName(sshClient *ssh.Client) (string, error) {
var bundleName string
err := log.Process("bootstrap", "Detect Bashible Bundle", func() error {
file, err := template.RenderAndSaveDetectBundle(make(map[string]interface{}))
if err != nil {
return err
}
return retry.NewSilentLoop("Get bundle", 3, 1*time.Second).Run(func() error {
// run detect bundle type
detectCmd := sshClient.UploadScript(file)
stdout, err := detectCmd.Execute()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
return fmt.Errorf("detect_bundle.sh: %v, %s", err, string(ee.Stderr))
}
return fmt.Errorf("detect_bundle.sh: %v", err)
}
bundleName = strings.Trim(string(stdout), "\n ")
if bundleName == "" {
return fmt.Errorf("detect_bundle.sh: empty bundle was detected")
}
log.InfoF("Detected bundle: %s\n", bundleName)
return nil
})
})
return bundleName, err
}
func WaitForSSHConnectionOnMaster(sshClient *ssh.Client) error {
return log.Process("bootstrap", "Wait for SSH on Master become Ready", func() error {
availabilityCheck := sshClient.Check()
_ = log.Process("default", "Connection string", func() error {
log.InfoLn(availabilityCheck.String())
return nil
})
if err := availabilityCheck.WithDelaySeconds(1).AwaitAvailability(); err != nil {
return fmt.Errorf("await master to become available: %v", err)
}
return nil
})
}
func InstallDeckhouse(kubeCl *client.KubernetesClient, config *config.DeckhouseInstaller) error {
return log.Process("bootstrap", "Install Deckhouse", func() error {
err := CheckPreventBreakAnotherBootstrappedCluster(kubeCl, config)
if err != nil {
return err
}
err = deckhouse.CreateDeckhouseManifests(kubeCl, config)
if err != nil {
return fmt.Errorf("deckhouse create manifests: %v", err)
}
err = cache.Global().Save(ManifestCreatedInClusterCacheKey, []byte("yes"))
if err != nil {
return fmt.Errorf("set manifests in cluster flag to cache: %v", err)
}
err = deckhouse.WaitForReadiness(kubeCl)
if err != nil {
return fmt.Errorf("deckhouse install: %v", err)
}
return nil
})
}
const rebootExitCode = 255
func RebootMaster(sshClient *ssh.Client) error {
return log.Process("bootstrap", "Reboot Master️", func() error {
rebootCmd := sshClient.Command("reboot").Sudo().
WithSSHArgs("-o", "ServerAliveInterval=15", "-o", "ServerAliveCountMax=2")
if err := rebootCmd.Run(); err != nil {
var ee *exec.ExitError
if errors.As(err, &ee) {
if ee.ExitCode() == rebootExitCode {
return nil
}
}
return fmt.Errorf("shutdown error: exit_code: %v stdout: %s stderr: %s %v",
ee.ExitCode(),
rebootCmd.StdoutBuffer.String(),
rebootCmd.StderrBuffer.String(),
err,
)
}
log.InfoLn("OK!")
return nil
})
}
func BootstrapTerraNodes(kubeCl *client.KubernetesClient, metaConfig *config.MetaConfig, terraNodeGroups []config.TerraNodeGroupSpec, terraformContext *terraform.TerraformContext) error {
for _, ng := range terraNodeGroups {
err := log.Process("bootstrap", fmt.Sprintf("Create %s NodeGroup", ng.Name), func() error {
err := converge.CreateNodeGroup(kubeCl, ng.Name, metaConfig.NodeGroupManifest(ng))
if err != nil {
return err
}
cloudConfig, err := converge.GetCloudConfig(kubeCl, ng.Name, converge.ShowDeckhouseLogs)
if err != nil {
return err
}
for i := 0; i < ng.Replicas; i++ {
err = converge.BootstrapAdditionalNode(kubeCl, metaConfig, i, "static-node", ng.Name, cloudConfig, false, terraformContext)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
}
return nil
}
func SaveMasterHostsToCache(hosts map[string]string) {
if err := cache.Global().SaveStruct(MasterHostsCacheKey, hosts); err != nil {
log.DebugF("Cannot save ssh hosts %v", err)
}
}
func GetMasterHostsIPs() ([]string, error) {
var hosts map[string]string
err := cache.Global().LoadStruct(MasterHostsCacheKey, &hosts)
if err != nil {
return nil, err
}
mastersIPs := make([]string, 0, len(hosts))
for _, ip := range hosts {
mastersIPs = append(mastersIPs, ip)
}
sort.Strings(mastersIPs)
return mastersIPs, nil
}
func SaveBastionHostToCache(host string) {
if err := cache.Global().Save(BastionHostCacheKey, []byte(host)); err != nil {
log.ErrorF("Cannot save ssh hosts: %v\n", err)
}
}
func GetBastionHostFromCache() (string, error) {
exists, err := cache.Global().InCache(BastionHostCacheKey)
if err != nil {
return "", err
}
if !exists {
return "", nil
}
host, err := cache.Global().Load(BastionHostCacheKey)
if err != nil {
return "", err
}
return string(host), nil
}
func BootstrapAdditionalMasterNodes(kubeCl *client.KubernetesClient, metaConfig *config.MetaConfig, addressTracker map[string]string, terraformContext *terraform.TerraformContext) error {
if metaConfig.MasterNodeGroupSpec.Replicas == 1 {
log.DebugF("Skip bootstrap additional master nodes because replicas == 1")
return nil
}
return log.Process("bootstrap", "Bootstrap additional master nodes", func() error {
masterCloudConfig, err := converge.GetCloudConfig(kubeCl, converge.MasterNodeGroupName, converge.ShowDeckhouseLogs)
if err != nil {
return err
}
for i := 1; i < metaConfig.MasterNodeGroupSpec.Replicas; i++ {
outputs, err := converge.BootstrapAdditionalMasterNode(kubeCl, metaConfig, i, masterCloudConfig, false, terraformContext)
if err != nil {
return err
}
addressTracker[fmt.Sprintf("%s-master-%d", metaConfig.ClusterPrefix, i)] = outputs.MasterIPForSSH
SaveMasterHostsToCache(addressTracker)
}
return nil
})
}
func BootstrapGetNodesFromCache(metaConfig *config.MetaConfig, stateCache state.Cache) (map[string]map[int]string, error) {
nodeGroupRegex := fmt.Sprintf("^%s-(.*)-([0-9]+)\\.tfstate$", metaConfig.ClusterPrefix)
groupsReg, _ := regexp.Compile(nodeGroupRegex)
nodesFromCache := make(map[string]map[int]string)
err := stateCache.Iterate(func(name string, content []byte) error {
switch {
case strings.HasSuffix(name, ".backup"):
fallthrough
case strings.HasPrefix(name, "base-infrastructure"):
fallthrough
case strings.HasPrefix(name, "uuid"):
fallthrough
case !groupsReg.MatchString(name):
return nil
}
nodeGroupNameAndNodeIndex := groupsReg.FindStringSubmatch(name)
nodeGroupName := nodeGroupNameAndNodeIndex[1]
rawIndex := nodeGroupNameAndNodeIndex[2]
index, convErr := strconv.Atoi(rawIndex)
if convErr != nil {
return fmt.Errorf("can't convert %q to integer: %v", rawIndex, convErr)
}
if _, ok := nodesFromCache[nodeGroupName]; !ok {
nodesFromCache[nodeGroupName] = make(map[int]string)
}
nodesFromCache[nodeGroupName][index] = strings.TrimSuffix(name, ".tfstate")
return nil
})
return nodesFromCache, err
}