|
| 1 | +// Copyright Cozystack Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package commands |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/cockroachdb/errors" |
| 19 | + "github.com/spf13/cobra" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + resetCmdName = "reset" |
| 24 | + |
| 25 | + // resetSafeDefaultLabels are the system partition labels talm's |
| 26 | + // wrapper populates into `--system-labels-to-wipe` when an |
| 27 | + // operator runs `talm reset` without explicitly choosing a wipe |
| 28 | + // scope. Wiping STATE clears node-specific persistent state |
| 29 | + // (machine config, identity); wiping EPHEMERAL clears the |
| 30 | + // container/runtime layer. Leaving META untouched is the key |
| 31 | + // property: META carries the bootstrap config Talos uses to |
| 32 | + // rejoin the cluster on the next boot, so a reset with only |
| 33 | + // these two labels self-recovers without operator intervention. |
| 34 | + resetSafeDefaultLabels = "STATE,EPHEMERAL" |
| 35 | +) |
| 36 | + |
| 37 | +// wrapResetCommand flips talm's `talm reset` default away from |
| 38 | +// upstream's destructive `--wipe-mode=all` toward the META-preserving |
| 39 | +// selective-wipe recipe. The flip only fires when the operator passed |
| 40 | +// neither `--wipe-mode` nor `--system-labels-to-wipe` on the CLI: |
| 41 | +// |
| 42 | +// - No wipe flags: PreRunE pre-populates |
| 43 | +// `--system-labels-to-wipe=STATE,EPHEMERAL`. The server-side |
| 44 | +// reset codepath, when SystemPartitionsToWipe is non-empty, |
| 45 | +// takes the label-driven path and "keep[s] other partitions |
| 46 | +// intact" per upstream's `--system-labels-to-wipe` flag doc in |
| 47 | +// `cmd/talosctl/cmd/talos/reset.go`. META survives; on the next |
| 48 | +// boot Talos rejoins the cluster from META without operator |
| 49 | +// intervention. |
| 50 | +// - Operator passed `--wipe-mode=...`: the safety override is |
| 51 | +// skipped. `--wipe-mode=all` remains the explicit destructive |
| 52 | +// opt-in; `--wipe-mode=system-disk` / `--wipe-mode=user-disks` |
| 53 | +// also bypass the flip on the assumption that the operator |
| 54 | +// stated wipe-scope intent. |
| 55 | +// - Operator passed `--system-labels-to-wipe=...`: the operator's |
| 56 | +// list is honored byte-for-byte. The wrapper does not silently |
| 57 | +// expand a narrower selection (e.g. STATE alone) to the safe |
| 58 | +// default — operators choosing a narrower scope are doing so |
| 59 | +// deliberately. |
| 60 | +// |
| 61 | +// Help-text overrides on both flags spell out the divergence so |
| 62 | +// `talm reset --help` carries the operator-facing story. |
| 63 | +// |
| 64 | +// Chain order: capture the wrapTalosCommand-installed PreRunE first, |
| 65 | +// run the flip BEFORE chaining. Order is not load-bearing here |
| 66 | +// (modeline does not touch wipe flags), but matching the shape of |
| 67 | +// the crashdump / rotate-ca wrappers keeps the dispatch site |
| 68 | +// readable. |
| 69 | +func wrapResetCommand(wrappedCmd *cobra.Command) { |
| 70 | + if wipeFlag := wrappedCmd.Flag("wipe-mode"); wipeFlag != nil { |
| 71 | + wipeFlag.Usage = "disk reset mode (talm default: --system-labels-to-wipe=" + resetSafeDefaultLabels + |
| 72 | + " preserves META so the node self-recovers; pass --wipe-mode=all or --wipe-mode=system-disk explicitly for upstream's destructive behaviour — both destroy META)" |
| 73 | + } |
| 74 | + |
| 75 | + if labelsFlag := wrappedCmd.Flag("system-labels-to-wipe"); labelsFlag != nil { |
| 76 | + labelsFlag.Usage = "wipe selected system disk partitions by label, keeping others intact (talm default when no wipe flag is set: " + |
| 77 | + resetSafeDefaultLabels + ")" |
| 78 | + } |
| 79 | + |
| 80 | + originalPreRunE := wrappedCmd.PreRunE |
| 81 | + |
| 82 | + wrappedCmd.PreRunE = func(cmd *cobra.Command, args []string) error { |
| 83 | + if !cmd.Flags().Changed("wipe-mode") && !cmd.Flags().Changed("system-labels-to-wipe") { |
| 84 | + if err := cmd.Flags().Set("system-labels-to-wipe", resetSafeDefaultLabels); err != nil { |
| 85 | + return errors.WithHint( |
| 86 | + errors.Wrap(err, "applying talm safe-default wipe labels"), |
| 87 | + "this should not happen at runtime; if it does, fall back to passing --system-labels-to-wipe=STATE,EPHEMERAL explicitly", |
| 88 | + ) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if originalPreRunE != nil { |
| 93 | + return originalPreRunE(cmd, args) |
| 94 | + } |
| 95 | + |
| 96 | + return nil |
| 97 | + } |
| 98 | +} |
0 commit comments