-
Notifications
You must be signed in to change notification settings - Fork 14
/
upgrade.go
79 lines (67 loc) · 1.66 KB
/
upgrade.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
package op
import (
"context"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/well"
)
type upgradeOp struct {
current string
nodes []*cke.Node
}
// UpgradeOp returns an Operator to upgrade cluster configuration.
func UpgradeOp(current string, nodes []*cke.Node) cke.Operator {
return &upgradeOp{current: current, nodes: nodes}
}
func (u *upgradeOp) Name() string {
return "upgrade"
}
func (u *upgradeOp) NextCommand() cke.Commander {
switch u.current {
case "1":
u.current = "2"
return UpgradeToVersion2Command(u.nodes)
default:
return nil
}
}
func (u *upgradeOp) Targets() []string {
targets := make([]string, len(u.nodes))
for i, n := range u.nodes {
targets[i] = n.Address
}
return targets
}
type upgradeToVersion2Command struct {
nodes []*cke.Node
}
// UpgradeToVersion2Command returns a Commander to upgrade from version 1 to 2.
func UpgradeToVersion2Command(nodes []*cke.Node) cke.Commander {
return upgradeToVersion2Command{nodes}
}
func (u upgradeToVersion2Command) Run(ctx context.Context, inf cke.Infrastructure, leaderKey string) error {
env := well.NewEnvironment(ctx)
for _, n := range u.nodes {
ce := inf.Engine(n.Address)
env.Go(func(ctx context.Context) error {
exists, err := ce.VolumeExists(EtcdAddedMemberVolumeName)
if err != nil {
return err
}
if !exists {
return ce.VolumeCreate(EtcdAddedMemberVolumeName)
}
return nil
})
}
env.Stop()
if err := env.Wait(); err != nil {
return err
}
return inf.Storage().PutConfigVersion(ctx, leaderKey)
}
func (u upgradeToVersion2Command) Command() cke.Command {
return cke.Command{
Name: "upgrade-version-from-1-to-2",
Target: EtcdAddedMemberVolumeName,
}
}