Skip to content

Commit 4ec3b5e

Browse files
committed
fix(commands): keep meta --insecure reachable on Talos v1.14
Talos v1.14 registers the meta command's --insecure on its local flag set rather than its persistent one. A local flag on a command that only hosts subcommands reaches nothing: cobra merges a parent's persistent flags into a child at parse time but not its local ones, and the parent itself takes no arguments. Both `meta write --insecure` and `meta --insecure write` stopped parsing, and with them the only way to write a META key over the maintenance service, which is what an operator does before a node has a machine config. Re-publish a wrapped container command's local flags as persistent ones, so they reach the subcommand that consumes them. The flag object is reused rather than redeclared, so it still writes to the variable upstream's RunE reads, and a flag that is already persistent is left alone, which makes this inert once the fix lands upstream. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 5b4ede3 commit 4ec3b5e

6 files changed

Lines changed: 175 additions & 46 deletions

File tree

‎docs/manual-test-plan.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,16 +1341,17 @@ talm get metakey --nodes $NODE --endpoints $NODE
13411341

13421342
Expected: table of META keys with their values.
13431343

1344-
### G1a. `--insecure` does not reach `meta` subcommands on Talos v1.14
1344+
### G1a. `--insecure` reaches `meta` subcommands
13451345

13461346
```bash
13471347
talm meta write --insecure 0x0a "test-value" --nodes $NODE --endpoints $NODE
1348+
talm meta write -i 0x0a "test-value" --nodes $NODE --endpoints $NODE
13481349
talm meta --insecure write 0x0a "test-value" --nodes $NODE --endpoints $NODE
13491350
```
13501351

1351-
Expected on v1.14: both forms fail with `unknown flag: --insecure`. Upstream registers the flag on `metaCmd.Flags()` rather than `PersistentFlags()`, so it reaches neither the subcommand (a local parent flag is not inherited) nor the parent (which takes no args), and `talosctl` behaves the same way. talm mirrors upstream instead of compensating, so META writes against a node in maintenance mode need a Talos-side fix.
1352+
Expected: all three reach the node over the maintenance service. Run this against a node in maintenance mode, before it has a machine config, which is the case the flag exists for.
13521353

1353-
Tracked upstream as [siderolabs/talos#14346](https://github.com/siderolabs/talos/issues/14346). Re-run this case after every Talos bump: once upstream restores the flag, the first form starts working again and this case flips to pinning that.
1354+
Talos v1.14 registers the flag on `metaCmd.Flags()` rather than `PersistentFlags()`, where it reaches neither the subcommands nor the parent, so plain `talosctl` rejects all three spellings. talm re-publishes container-command flags as persistent to keep this path working ([siderolabs/talos#14346](https://github.com/siderolabs/talos/issues/14346), fixed by [#14347](https://github.com/siderolabs/talos/pull/14347), merged to `main` and not yet in a v1.14 release). Re-run after every Talos bump: once the fix ships in the release talm tracks, the wrapper step is redundant and should be removed.
13541355

13551356
### G2. Write a test key
13561357

‎docs/operations/talosctl-commands.md‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ Talos v1.14 registers `meta`'s `--insecure` on that command's local flag set ins
3232
That is the only way to write a META key over the maintenance service, which is what you do before a node has a machine config, so talm re-publishes such flags as persistent on its wrapper. `talm meta write --insecure` and the `-i` shorthand keep working, and `--cert-fingerprint` travels with them.
3333

3434
Reported upstream as [siderolabs/talos#14346](https://github.com/siderolabs/talos/issues/14346) and fixed by [siderolabs/talos#14347](https://github.com/siderolabs/talos/pull/14347), which is merged to `main` but not backported to the v1.14 branch. Once the fix ships in a Talos release talm depends on, the wrapper step becomes redundant and is dropped.
35-
3635
## `talm apply` — `--mode=reboot` is gone on Talos v1.14
3736

3837
Upstream stopped registering `reboot` among the values of the apply mode flag in v1.14, so `talm apply --mode=reboot` fails with `invalid argument "reboot" for "-m, --mode" flag`. Use `--mode=auto`, which the node promotes to a reboot when the change requires one. Shell completion reads the accepted values back from the flag, so it no longer suggests `reboot` either.
@@ -44,7 +43,6 @@ Upstream stopped registering `--insecure` on `upgrade` in v1.14 (it had been dep
4443
## `talm reset` — `--insecure` is gone on Talos v1.14
4544

4645
Upstream dropped `--insecure` from `reset` in v1.14, so `talm reset --insecure` fails with `unknown flag: --insecure`. Resetting a node that has no valid configuration is done from the maintenance side instead: boot the node into a maintenance image and apply a fresh config, rather than resetting over an unauthenticated connection.
47-
4846
## `talm reset` — META-preserving default
4947

5048
`talm reset` diverges from upstream `talosctl reset` on one default. Upstream defaults to `--wipe-mode=all`, which wipes the Talos META partition along with STATE and EPHEMERAL — the node cannot self-recover and comes up in maintenance mode requiring a full re-apply. Talm instead populates `--system-labels-to-wipe=STATE,EPHEMERAL` when neither `--wipe-mode` nor `--system-labels-to-wipe` was passed, which preserves META so the node rejoins the cluster from its META-stored bootstrap config on the next boot.

‎docs/reference/meta_delete.md‎

Lines changed: 11 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/reference/meta_write.md‎

Lines changed: 11 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkg/commands/talosctl_wrapper.go‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,47 @@ func propagatePersistentFlags(cmd, wrappedCmd *cobra.Command) {
141141
})
142142
}
143143

144+
// republishContainerFlags re-registers a container command's local flags as
145+
// persistent ones on the wrapper.
146+
//
147+
// A local flag on a command that only hosts subcommands cannot be used at all:
148+
// cobra merges a parent's persistent flags into a child at parse time but not
149+
// its local ones, and a container command takes no arguments of its own. Talos
150+
// v1.14 registers `meta`'s --insecure that way, which is what made
151+
// `talosctl meta write --insecure` stop parsing (siderolabs/talos#14346). The
152+
// flag object is reused rather than redeclared, so it still writes to the
153+
// upstream variable the command's RunE reads.
154+
//
155+
// TODO: remove once siderolabs/talos#14347 ships in a Talos release talm
156+
// depends on. It is merged upstream but sits on main, with no backport to the
157+
// v1.14 branch, so v1.14.x still needs this. A flag that is already persistent
158+
// is skipped, so the fix arriving turns this into a no-op, not a conflict.
159+
//
160+
//nolint:godox // deliberate removal marker tied to a specific upstream PR.
161+
func republishContainerFlags(cmd, wrappedCmd *cobra.Command) {
162+
if !cmd.HasSubCommands() {
163+
return
164+
}
165+
166+
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
167+
if _, shadowed := rootShadowedPersistentFlags[flag.Name]; shadowed {
168+
return
169+
}
170+
171+
if wrappedCmd.PersistentFlags().Lookup(flag.Name) != nil {
172+
return
173+
}
174+
175+
if flag.Shorthand == "f" {
176+
wrappedCmd.PersistentFlags().AddFlag(renameFlagShorthand(flag, "F"))
177+
178+
return
179+
}
180+
181+
wrappedCmd.PersistentFlags().AddFlag(flag)
182+
})
183+
}
184+
144185
// warnSkipVerifyUnsupported writes a warning to w when --skip-verify is set for
145186
// a wrapped talosctl passthrough command. Those commands run upstream RunE code
146187
// that builds its own client through upstream global.Args, which has no
@@ -211,6 +252,7 @@ func wrapTalosCommand(cmd *cobra.Command, cmdName string) *cobra.Command {
211252
// "f". Today no such upstream flag exists, but the cost is one
212253
// branch and the surface stays uniform with the local-flag loop.
213254
propagatePersistentFlags(cmd, wrappedCmd)
255+
republishContainerFlags(cmd, wrappedCmd)
214256

215257
// Add --file flag only if it doesn't already exist in the original command
216258
var configFiles []string

‎pkg/commands/talosctl_wrapper_test.go‎

Lines changed: 107 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -632,19 +632,17 @@ func TestWrapTalosCommand_RealCrashdumpPopulatesNodesFromControlPlane(t *testing
632632
}
633633
}
634634

635-
// TestWrapTalosCommand_RealMetaPropagatesInsecure pins that the wrapper copies
636-
// the upstream meta command's --insecure across, long form and -i shorthand
637-
// both, so a future regression that copies the flag but drops the shorthand
638-
// attribute is caught.
635+
// TestWrapTalosCommand_RealMetaReachesSubcommandsWithInsecure pins that
636+
// --insecure reaches `meta write`, long form and -i shorthand both.
639637
//
640-
// It asserts on `meta`, not on `meta write`. Talos v1.14.0 moved --insecure
641-
// from metaCmd.PersistentFlags() to metaCmd.Flags() when it introduced
642-
// global.InsecureFlags, and a local flag does not reach a subcommand, so
643-
// `talosctl meta write --insecure` stopped parsing upstream as well. talm
644-
// mirrors upstream here rather than papering over it; compensating would mean
645-
// talm accepting a flag talosctl rejects. Reported as
646-
// https://github.com/siderolabs/talos/issues/14346.
647-
func TestWrapTalosCommand_RealMetaPropagatesInsecure(t *testing.T) {
638+
// Talos v1.14.0 moved the flag from metaCmd.PersistentFlags() to metaCmd.Flags()
639+
// when it introduced global.InsecureFlags. A local flag on a container command
640+
// reaches neither its subcommands nor the command itself, which took away the
641+
// only way to write a META key over the maintenance service
642+
// (https://github.com/siderolabs/talos/issues/14346). The wrapper re-publishes
643+
// such flags as persistent, so writing META on a node in maintenance mode keeps
644+
// working while the upstream fix is in flight.
645+
func TestWrapTalosCommand_RealMetaReachesSubcommandsWithInsecure(t *testing.T) {
648646
var metaCmd *cobra.Command
649647

650648
for _, cmd := range taloscommands.Commands {
@@ -661,23 +659,109 @@ func TestWrapTalosCommand_RealMetaPropagatesInsecure(t *testing.T) {
661659

662660
wrapped := wrapTalosCommand(metaCmd, "meta")
663661

664-
// Long form.
665-
if err := wrapped.ParseFlags([]string{"--insecure"}); err != nil {
666-
t.Fatalf("ParseFlags --insecure on wrapped meta: %v", err)
662+
writeCmd, _, err := wrapped.Find([]string{"write"})
663+
if err != nil {
664+
t.Fatalf("Find write under wrapped meta: %v", err)
665+
}
666+
667+
// Long form on the subcommand, which is where operators put it.
668+
if err := writeCmd.ParseFlags([]string{"--insecure"}); err != nil {
669+
t.Fatalf("ParseFlags --insecure on wrapped meta write: %v", err)
667670
}
668671

669-
if wrapped.Flags().Lookup("insecure") == nil {
670-
t.Fatal("wrapped meta must carry --insecure across from the upstream command")
672+
if writeCmd.Flags().Lookup("insecure") == nil {
673+
t.Fatal("wrapped meta write must inherit --insecure from the wrapper")
671674
}
672675

673676
// Short form. Re-parse to exercise the -i alias path.
674-
if err := wrapped.ParseFlags([]string{"-i"}); err != nil {
675-
t.Errorf("ParseFlags -i on wrapped meta: %v — shorthand attribute lost during copy?", err)
677+
if err := writeCmd.ParseFlags([]string{"-i"}); err != nil {
678+
t.Errorf("ParseFlags -i on wrapped meta write: %v — shorthand attribute lost during copy?", err)
676679
}
677680

678-
// The subcommands still exist and still do not see the parent's local flag,
679-
// exactly as upstream leaves them.
680-
if _, _, err := wrapped.Find([]string{"write"}); err != nil {
681-
t.Fatalf("Find write under wrapped meta: %v", err)
681+
// The fingerprint flag travels with it; --insecure alone would drop the
682+
// pinning that makes the unauthenticated connection safe to use.
683+
if writeCmd.Flags().Lookup("cert-fingerprint") == nil {
684+
t.Error("wrapped meta write must inherit --cert-fingerprint alongside --insecure")
685+
}
686+
}
687+
688+
// TestRepublishContainerFlags_SkipsShadowedFlags pins that a container
689+
// command's local flag is dropped when talm's root already owns that name.
690+
// Re-publishing it as persistent would shadow the root flag for the whole
691+
// subtree, which is the collision rootShadowedPersistentFlags exists to avoid.
692+
func TestRepublishContainerFlags_SkipsShadowedFlags(t *testing.T) {
693+
upstream := &cobra.Command{Use: "upstream"}
694+
upstream.Flags().String(talosconfigName, "", "collides with talm's root flag")
695+
upstream.AddCommand(&cobra.Command{Use: "child"})
696+
697+
wrapped := &cobra.Command{Use: "wrapped"}
698+
699+
republishContainerFlags(upstream, wrapped)
700+
701+
if got := wrapped.PersistentFlags().Lookup(talosconfigName); got != nil {
702+
t.Errorf("--%s was re-published despite being owned by talm's root command", talosconfigName)
703+
}
704+
}
705+
706+
// TestRepublishContainerFlags_LeavesPersistentFlagsAlone pins the branch the
707+
// removal plan rests on: a flag upstream already registered as persistent is
708+
// left untouched. When siderolabs/talos#14347 lands, --insecure arrives
709+
// persistent and this pass has to become a no-op instead of a second
710+
// registration.
711+
func TestRepublishContainerFlags_LeavesPersistentFlagsAlone(t *testing.T) {
712+
upstream := &cobra.Command{Use: "upstream"}
713+
upstream.PersistentFlags().Bool("insecure", false, "already persistent upstream")
714+
upstream.AddCommand(&cobra.Command{Use: "child"})
715+
716+
wrapped := &cobra.Command{Use: "wrapped"}
717+
718+
propagatePersistentFlags(upstream, wrapped)
719+
720+
before := wrapped.PersistentFlags().Lookup("insecure")
721+
if before == nil {
722+
t.Fatal("the persistent pass must carry --insecure across")
723+
}
724+
725+
republishContainerFlags(upstream, wrapped)
726+
727+
if after := wrapped.PersistentFlags().Lookup("insecure"); after != before {
728+
t.Error("--insecure was re-registered; the pass must leave an already-persistent flag as it is")
729+
}
730+
}
731+
732+
// TestRepublishContainerFlags_RenamesShorthandF pins the same defensive rename
733+
// propagatePersistentFlags does: shorthand `f` belongs to talm's own --file, so
734+
// a container command's local `-f` must arrive as `-F`.
735+
func TestRepublishContainerFlags_RenamesShorthandF(t *testing.T) {
736+
upstream := &cobra.Command{Use: "upstream"}
737+
upstream.Flags().StringP("foo", "f", "default", "shadow shorthand f")
738+
upstream.AddCommand(&cobra.Command{Use: "child"})
739+
740+
wrapped := &cobra.Command{Use: "wrapped"}
741+
742+
republishContainerFlags(upstream, wrapped)
743+
744+
got := wrapped.PersistentFlags().Lookup("foo")
745+
if got == nil {
746+
t.Fatal("re-publishing must register --foo on wrappedCmd.PersistentFlags(); got nil")
747+
}
748+
749+
if got.Shorthand != "F" {
750+
t.Errorf("shorthand must be renamed from 'f' to 'F' to avoid colliding with talm's --file; got %q", got.Shorthand)
751+
}
752+
}
753+
754+
// A command without subcommands keeps its local flags local: re-publishing them
755+
// would widen their scope for no reason, since nothing inherits from it.
756+
func TestRepublishContainerFlags_IgnoresLeafCommands(t *testing.T) {
757+
upstream := &cobra.Command{Use: "upstream"}
758+
upstream.Flags().Bool("insecure", false, "local to a leaf")
759+
760+
wrapped := &cobra.Command{Use: "wrapped"}
761+
762+
republishContainerFlags(upstream, wrapped)
763+
764+
if wrapped.PersistentFlags().Lookup("insecure") != nil {
765+
t.Error("a leaf command's local flag must not become persistent on the wrapper")
682766
}
683767
}

0 commit comments

Comments
 (0)