Skip to content

Commit

Permalink
feat: add reboot-mode flag to talosctl upgrade
Browse files Browse the repository at this point in the history
Allow specifying the reboot mode during upgrades by introducing `--reboot-mode` flag, similar to the `--mode` flag of the reboot command.

Closes siderolabs#7302.

Signed-off-by: Utku Ozdemir <utku.ozdemir@siderolabs.com>
  • Loading branch information
utkuozdemir committed Jun 26, 2023
1 parent 7ce87f2 commit 0d313b9
Show file tree
Hide file tree
Showing 9 changed files with 1,984 additions and 1,791 deletions.
5 changes: 5 additions & 0 deletions api/machine/machine.proto
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,15 @@ message ShutdownResponse {

// rpc upgrade
message UpgradeRequest {
enum RebootMode {
DEFAULT = 0;
POWERCYCLE = 1;
}
string image = 1;
bool preserve = 2;
bool stage = 3;
bool force = 4;
RebootMode reboot_mode = 5;
}

message Upgrade {
Expand Down
58 changes: 39 additions & 19 deletions cmd/talosctl/cmd/talos/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"context"
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"
"time"

"github.com/siderolabs/gen/maps"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
Expand All @@ -20,13 +23,15 @@ import (
"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/version"
)

var upgradeCmdFlags struct {
trackableActionCmdFlags
upgradeImage string
rebootMode string
preserve bool
stage bool
force bool
Expand All @@ -48,40 +53,52 @@ var upgradeCmd = &cobra.Command{
return fmt.Errorf("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()
return runUpgradeNoWait(opts)
}

common.SuppressErrors = true

return action.NewTracker(
&GlobalArgs,
action.MachineReadyEventFn,
upgradeGetActorID,
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() error {
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.Upgrade(
ctx,
upgradeCmdFlags.upgradeImage,
upgradeCmdFlags.preserve,
upgradeCmdFlags.stage,
upgradeCmdFlags.force,
grpc.Peer(&remotePeer),
)
resp, err := c.UpgradeWithOptions(ctx, opts...)
if err != nil {
if resp == nil {
return fmt.Errorf("error performing upgrade: %s", err)
Expand Down Expand Up @@ -115,14 +132,8 @@ func runUpgradeNoWait() error {
return WithClient(upgradeFn)
}

func upgradeGetActorID(ctx context.Context, c *client.Client) (string, error) {
resp, err := c.Upgrade(
ctx,
upgradeCmdFlags.upgradeImage,
upgradeCmdFlags.preserve,
upgradeCmdFlags.stage,
upgradeCmdFlags.force,
)
func upgradeGetActorID(ctx context.Context, c *client.Client, opts []client.UpgradeOption) (string, error) {
resp, err := c.UpgradeWithOptions(ctx, opts...)
if err != nil {
return "", err
}
Expand All @@ -135,9 +146,18 @@ func upgradeGetActorID(ctx context.Context, c *client.Client) (string, error) {
}

func init() {
rebootModes := maps.KeysFunc(machine.UpgradeRequest_RebootMode_value, strings.ToLower)
sort.Slice(rebootModes, func(i, j int) bool {
return machine.UpgradeRequest_RebootMode_value[rebootModes[i]] > machine.UpgradeRequest_RebootMode_value[rebootModes[j]]
})

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)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ func (s *Server) Upgrade(ctx context.Context, in *machine.UpgradeRequest) (*mach
return nil, err
}

log.Printf("upgrade request received: preserve %v, staged %v, force %v", in.GetPreserve(), in.GetStage(), in.GetForce())
log.Printf("upgrade request received: preserve %v, staged %v, force %v, reboot mode %v", in.GetPreserve(), in.GetStage(), in.GetForce(), in.GetRebootMode().String())

log.Printf("validating %q", in.GetImage())

Expand Down
10 changes: 6 additions & 4 deletions internal/app/machined/pkg/runtime/v1alpha1/v1alpha1_sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func (*Sequencer) StageUpgrade(r runtime.Runtime, in *machineapi.UpgradeRequest)
"leave",
LeaveEtcd,
).AppendList(
stopAllPhaselist(r, true),
stopAllPhaselist(r, in.GetRebootMode() == machineapi.UpgradeRequest_DEFAULT),
).Append(
"reboot",
Reboot,
Expand All @@ -430,7 +430,7 @@ func (*Sequencer) StageUpgrade(r runtime.Runtime, in *machineapi.UpgradeRequest)
}

// MaintenanceUpgrade is the upgrade sequence in maintenance mode.
func (*Sequencer) MaintenanceUpgrade(r runtime.Runtime, _ *machineapi.UpgradeRequest) []runtime.Phase {
func (*Sequencer) MaintenanceUpgrade(r runtime.Runtime, in *machineapi.UpgradeRequest) []runtime.Phase {
phases := PhaseList{}

switch r.State().Platform().Mode() { //nolint:exhaustive
Expand All @@ -449,7 +449,8 @@ func (*Sequencer) MaintenanceUpgrade(r runtime.Runtime, _ *machineapi.UpgradeReq
).Append(
"meta",
ReloadMeta,
).Append(
).AppendWhen(
in.GetRebootMode() == machineapi.UpgradeRequest_DEFAULT,
"kexec",
KexecPrepare,
).Append(
Expand Down Expand Up @@ -517,7 +518,8 @@ func (*Sequencer) Upgrade(r runtime.Runtime, in *machineapi.UpgradeRequest) []ru
).Append(
"meta",
ReloadMeta,
).Append(
).AppendWhen(
in.GetRebootMode() == machineapi.UpgradeRequest_DEFAULT,
"kexec",
KexecPrepare,
).Append(
Expand Down
Loading

0 comments on commit 0d313b9

Please sign in to comment.