/
upgrade.go
168 lines (137 loc) · 5.21 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
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package talos
import (
"context"
"errors"
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"
"time"
"github.com/siderolabs/gen/maps"
"github.com/siderolabs/gen/xslices"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
"github.com/siderolabs/talos/cmd/talosctl/pkg/talos/action"
"github.com/siderolabs/talos/cmd/talosctl/pkg/talos/helpers"
"github.com/siderolabs/talos/pkg/cli"
"github.com/siderolabs/talos/pkg/images"
"github.com/siderolabs/talos/pkg/machinery/api/machine"
"github.com/siderolabs/talos/pkg/machinery/client"
"github.com/siderolabs/talos/pkg/machinery/version"
)
var upgradeCmdFlags struct {
trackableActionCmdFlags
upgradeImage string
rebootMode string
preserve bool
stage bool
force bool
insecure bool
}
// upgradeCmd represents the processes command.
var upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "Upgrade Talos on the target node",
Long: ``,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if upgradeCmdFlags.debug {
upgradeCmdFlags.wait = true
}
if upgradeCmdFlags.wait && upgradeCmdFlags.insecure {
return errors.New("cannot use --wait and --insecure together")
}
rebootModeStr := strings.ToUpper(upgradeCmdFlags.rebootMode)
rebootMode, rebootModeOk := machine.UpgradeRequest_RebootMode_value[rebootModeStr]
if !rebootModeOk {
return fmt.Errorf("invalid reboot mode: %s", upgradeCmdFlags.rebootMode)
}
opts := []client.UpgradeOption{
client.WithUpgradeImage(upgradeCmdFlags.upgradeImage),
client.WithUpgradeRebootMode(machine.UpgradeRequest_RebootMode(rebootMode)),
client.WithUpgradePreserve(upgradeCmdFlags.preserve),
client.WithUpgradeStage(upgradeCmdFlags.stage),
client.WithUpgradeForce(upgradeCmdFlags.force),
}
if !upgradeCmdFlags.wait {
return runUpgradeNoWait(opts)
}
return action.NewTracker(
&GlobalArgs,
action.MachineReadyEventFn,
func(ctx context.Context, c *client.Client) (string, error) {
return upgradeGetActorID(ctx, c, opts)
},
action.WithPostCheck(action.BootIDChangedPostCheckFn),
action.WithDebug(upgradeCmdFlags.debug),
action.WithTimeout(upgradeCmdFlags.timeout),
).Run()
},
}
func runUpgradeNoWait(opts []client.UpgradeOption) error {
upgradeFn := func(ctx context.Context, c *client.Client) error {
if err := helpers.ClientVersionCheck(ctx, c); err != nil {
return err
}
var remotePeer peer.Peer
opts = append(opts, client.WithUpgradeGRPCCallOptions(grpc.Peer(&remotePeer)))
// TODO: See if we can validate version and prevent starting upgrades to an unknown version
resp, err := c.UpgradeWithOptions(ctx, opts...)
if err != nil {
if resp == nil {
return fmt.Errorf("error performing upgrade: %s", err)
}
cli.Warning("%s", err)
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
fmt.Fprintln(w, "NODE\tACK\tSTARTED")
defaultNode := client.AddrFromPeer(&remotePeer)
for _, msg := range resp.Messages {
node := defaultNode
if msg.Metadata != nil {
node = msg.Metadata.Hostname
}
fmt.Fprintf(w, "%s\t%s\t%s\t\n", node, msg.Ack, time.Now())
}
return w.Flush()
}
if upgradeCmdFlags.insecure {
return WithClientMaintenance(nil, upgradeFn)
}
return WithClient(upgradeFn)
}
func upgradeGetActorID(ctx context.Context, c *client.Client, opts []client.UpgradeOption) (string, error) {
resp, err := c.UpgradeWithOptions(ctx, opts...)
if err != nil {
return "", err
}
if len(resp.GetMessages()) == 0 {
return "", errors.New("no messages returned from action run")
}
return resp.GetMessages()[0].GetActorId(), nil
}
func init() {
rebootModes := maps.Keys(machine.UpgradeRequest_RebootMode_value)
sort.Slice(rebootModes, func(i, j int) bool {
return machine.UpgradeRequest_RebootMode_value[rebootModes[i]] < machine.UpgradeRequest_RebootMode_value[rebootModes[j]]
})
rebootModes = xslices.Map(rebootModes, strings.ToLower)
upgradeCmd.Flags().StringVarP(&upgradeCmdFlags.upgradeImage, "image", "i",
fmt.Sprintf("%s/%s/installer:%s", images.Registry, images.Username, version.Trim(version.Tag)),
"the container image to use for performing the install")
upgradeCmd.Flags().StringVarP(&upgradeCmdFlags.rebootMode, "reboot-mode", "m", strings.ToLower(machine.UpgradeRequest_DEFAULT.String()),
fmt.Sprintf("select the reboot mode during upgrade. Mode %q bypasses kexec. Valid values are: %q.",
strings.ToLower(machine.UpgradeRequest_POWERCYCLE.String()),
rebootModes))
upgradeCmd.Flags().BoolVarP(&upgradeCmdFlags.preserve, "preserve", "p", false, "preserve data")
upgradeCmd.Flags().BoolVarP(&upgradeCmdFlags.stage, "stage", "s", false, "stage the upgrade to perform it after a reboot")
upgradeCmd.Flags().BoolVarP(&upgradeCmdFlags.force, "force", "f", false, "force the upgrade (skip checks on etcd health and members, might lead to data loss)")
upgradeCmd.Flags().BoolVar(&upgradeCmdFlags.insecure, "insecure", false, "upgrade using the insecure (encrypted with no auth) maintenance service")
upgradeCmdFlags.addTrackActionFlags(upgradeCmd)
addCommand(upgradeCmd)
}