Skip to content

Commit 3a88118

Browse files
committed
chore: improve error handling for system disk reset
This previously returned immediately on first error, preventing the "STATE was wiped but META wasn't" codepath from running. This patch instead collects errors, checking whether META/STATE were successfully wiped along the way, and unconditionally runs the "delete state encryption info from META" if STATE was wiped and META wasn't. Signed-off-by: Laura Brehm <laurabrehm@hey.com>
1 parent 859194e commit 3a88118

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

internal/app/machined/pkg/runtime/v1alpha1/v1alpha1_sequencer_tasks.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"log"
1818
"os"
1919
"path/filepath"
20-
"slices"
2120
"strconv"
2221
"strings"
2322
"syscall"
@@ -1246,31 +1245,36 @@ type SystemDiskTargets interface {
12461245
}
12471246

12481247
// ResetSystemDiskSpec represents the task to reset the system disk by spec.
1249-
func ResetSystemDiskSpec(_ runtime.Sequence, data any) (runtime.TaskExecutionFunc, string) {
1250-
return func(ctx context.Context, logger *log.Logger, r runtime.Runtime) (err error) {
1248+
func ResetSystemDiskSpec(_ runtime.Sequence, data any) (runtime.TaskExecutionFunc, string) { //nolint:gocyclo
1249+
return func(ctx context.Context, logger *log.Logger, r runtime.Runtime) error {
12511250
in, ok := data.(SystemDiskTargets)
12521251
if !ok {
12531252
return errors.New("unexpected runtime data")
12541253
}
12551254

1255+
var (
1256+
metaWiped, stateWiped bool
1257+
wipeErrors *multierror.Error
1258+
)
1259+
12561260
for _, target := range in.GetSystemDiskTargets() {
1257-
if err = target.Wipe(ctx, logger.Printf); err != nil {
1258-
return fmt.Errorf("failed wiping partition %s: %w", target, err)
1259-
}
1260-
}
1261+
if err := target.Wipe(ctx, logger.Printf); err != nil {
1262+
wipeErrors = multierror.Append(wipeErrors,
1263+
fmt.Errorf("failed wiping partition %s: %w", target, err))
12611264

1262-
stateWiped := slices.ContainsFunc(in.GetSystemDiskTargets(), func(t runtime.PartitionTarget) bool {
1263-
return t.GetLabel() == constants.StatePartitionLabel
1264-
})
1265+
continue
1266+
}
12651267

1266-
metaWiped := slices.ContainsFunc(in.GetSystemDiskTargets(), func(t runtime.PartitionTarget) bool {
1267-
return t.GetLabel() == constants.MetaPartitionLabel
1268-
})
1268+
switch target.GetLabel() {
1269+
case constants.MetaPartitionLabel:
1270+
metaWiped = true
1271+
case constants.StatePartitionLabel:
1272+
stateWiped = true
1273+
}
1274+
}
12691275

12701276
if stateWiped && !metaWiped {
1271-
var removed bool
1272-
1273-
removed, err = r.State().Machine().Meta().DeleteTag(ctx, metamachinery.StateEncryptionConfig)
1277+
removed, err := r.State().Machine().Meta().DeleteTag(ctx, metamachinery.StateEncryptionConfig)
12741278
if err != nil {
12751279
return fmt.Errorf("failed to remove state encryption META config tag: %w", err)
12761280
}
@@ -1284,6 +1288,10 @@ func ResetSystemDiskSpec(_ runtime.Sequence, data any) (runtime.TaskExecutionFun
12841288
}
12851289
}
12861290

1291+
if err := wipeErrors.ErrorOrNil(); err != nil {
1292+
return err
1293+
}
1294+
12871295
logger.Printf("successfully reset system disk by the spec")
12881296

12891297
return nil

0 commit comments

Comments
 (0)