/
boot.go
128 lines (113 loc) · 2.71 KB
/
boot.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
package etcd
import (
"context"
"strings"
"time"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/cke/op"
"github.com/cybozu-go/cke/op/common"
)
type bootOp struct {
endpoints []string
nodes []*cke.Node
params cke.EtcdParams
step int
files *common.FilesBuilder
}
// BootOp returns an Operator to bootstrap etcd cluster.
func BootOp(nodes []*cke.Node, params cke.EtcdParams) cke.Operator {
return &bootOp{
endpoints: etcdEndpoints(nodes),
nodes: nodes,
params: params,
files: common.NewFilesBuilder(nodes),
}
}
func (o *bootOp) Name() string {
return "etcd-bootstrap"
}
func (o *bootOp) NextCommand() cke.Commander {
volname := op.EtcdVolumeName(o.params)
switch o.step {
case 0:
o.step++
return common.ImagePullCommand(o.nodes, cke.EtcdImage)
case 1:
o.step++
return prepareEtcdCertificatesCommand{o.files}
case 2:
o.step++
return o.files
case 3:
o.step++
return common.VolumeCreateCommand(o.nodes, volname)
case 4:
o.step++
opts := []string{
"--mount",
"type=volume,src=" + volname + ",dst=/var/lib/etcd",
}
initialCluster := make([]string, len(o.nodes))
for i, n := range o.nodes {
initialCluster[i] = n.Address + "=https://" + n.Address + ":2380"
}
paramsMap := make(map[string]cke.ServiceParams)
for _, n := range o.nodes {
paramsMap[n.Address] = BuiltInParams(n, initialCluster, "new")
}
return common.RunContainerCommand(o.nodes, op.EtcdContainerName, cke.EtcdImage,
common.WithOpts(opts),
common.WithParamsMap(paramsMap),
common.WithExtra(o.params.ServiceParams))
case 5:
o.step++
return waitEtcdSyncCommand{o.endpoints, false}
case 6:
o.step++
return setupEtcdAuthCommand{o.endpoints}
case 7:
o.step++
return common.VolumeCreateCommand(o.nodes, op.EtcdAddedMemberVolumeName)
default:
return nil
}
}
func (o *bootOp) Targets() []string {
ips := make([]string, len(o.nodes))
for i, n := range o.nodes {
ips[i] = n.Address
}
return ips
}
type setupEtcdAuthCommand struct {
endpoints []string
}
func (c setupEtcdAuthCommand) Run(ctx context.Context, inf cke.Infrastructure, _ string) error {
cli, err := inf.NewEtcdClient(ctx, c.endpoints)
if err != nil {
return err
}
defer cli.Close()
ctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
defer cancel()
err = cke.AddUserRole(ctx, cli, "root", "")
if err != nil {
return err
}
_, err = cli.UserGrantRole(ctx, "root", "root")
if err != nil {
return err
}
err = cke.AddUserRole(ctx, cli, "kube-apiserver", "/registry/")
if err != nil {
return err
}
_, err = cli.AuthEnable(ctx)
return err
}
func (c setupEtcdAuthCommand) Command() cke.Command {
return cke.Command{
Name: "setup-etcd-auth",
Target: strings.Join(c.endpoints, ","),
}
}