Skip to content

Commit 44c6520

Browse files
committed
fix(charts,commands): skip empty bond config and honor --endpoints in init
Two operator-visible bugs in the talm init / multidoc-render flow. * charts: cozystack + generic presets emit BondConfig with empty `links:` for any discovered link with `spec.kind == "bond"`. The Talos link controller can auto-create a bond stub in COSI link state before any operator configuration enslaves physical NICs to it — the master link carries a `bondMaster` spec but no other link has `slaveKind: bond` + `masterIndex` pointing at it. Rendering the document under those conditions ships `BondConfig{name: bond0, links: <empty>, bondMode: ...}` to Talos, which rejects on apply with `BondConfig/bond0: at least one link must be specified`. Skip the document when `talm.discovered.bond_slaves` returns an empty list: a discovered bond without enslaved links is a stub, not a user-configured bond. An operator who intends a bond declares its slaves via a per-node body overlay. Pattern mirrors the existing fail-fast guardrail for VLAN-without-parent in the same files; chose skip over fail-fast because the operator does not control the Talos-side stub creation. * commands: `talm init --endpoints X` silently wrote `127.0.0.1` to the generated talosconfig. The assignment site at init.go:598 hardcoded `defaultLocalEndpoint`, discarding the operator's flag. Extract `resolveTalosconfigEndpoints`: when `GlobalArgs.Endpoints` is non-empty, propagate; otherwise fall back to the loopback placeholder (yaml requires a non-empty endpoints list; operator edits after init in that case). Helper returns a defensive copy so callers mutating the talosconfig field do not alias the operator-visible `GlobalArgs.Endpoints` across init invocations. Tests: - TestContract_NetworkMultidoc_BondWithoutSlaves_SkipsBondConfig pins the cozystack-side bond-skip contract: rendered output must contain neither `kind: BondConfig` nor `name: bond0` when slaves is empty. Positive-shape assertions verify that eth0 (non-enslaved, has an address) still emits as a LinkConfig so a future guard regression swallowing all link emission does not slip through. - TestContract_NetworkMultidoc_BondWithoutSlaves_SkipsBondConfig_Generic pins the generic-preset counterpart against the same shape. - TestResolveTalosconfigEndpoints_{GlobalNonEmpty_UsesGlobal, GlobalEmpty_UsesLoopbackFallback, ReturnsCopy, MultipleEndpoints} pin the helper's four behaviour axes: operator-supplied propagation, fallback shape, defensive copy, multi-endpoint shape. emptyBondTopologyLookup fixture mirrors bondTopologyLookup but drops eth0/eth1 enslavement so talm.discovered.bond_slaves returns an empty list — exactly the Talos auto-stub shape. Existing TestContract_NetworkMultidoc_BondRendersBondConfig and TestContract_NetworkMultidoc_BondSlavesNotEmittedAsLinkConfig keep passing: slaves-present case unaffected. Scope: covers both multidoc (v1.12+) presets cozystack + generic. The legacy v1.11 nested form in charts/talm/templates/_helpers.tpl ::talm.discovered.bond_config exhibits the same empty-slaves shape; deliberately out of scope because the multidoc presets are where the reported reproduction lives and the legacy path has different downstream test coverage. Follow up if 1.11 operators hit this shape. Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent d3c602c commit 44c6520

6 files changed

Lines changed: 295 additions & 1 deletion

File tree

‎charts/cozystack/templates/_helpers.tpl‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,18 @@ mtu: {{ $link.spec.mtu }}
321321
{{- else if eq $kind "bond" }}
322322
{{- $bondMaster := $link.spec.bondMaster }}
323323
{{- $slaves := fromJsonArray (include "talm.discovered.bond_slaves" $link.spec.index) }}
324+
{{- if not $slaves }}
325+
{{- /* Talos's link controller can auto-create a bond stub in COSI
326+
link state before any operator configuration enslaves
327+
physical NICs to it: the master link carries a bondMaster
328+
spec but no other link has slaveKind: bond + masterIndex
329+
pointing at it. Emitting BondConfig with empty `links:`
330+
produces a document Talos rejects on apply ("at least one
331+
link must be specified"). Treat empty-slaves as not-a-
332+
user-bond and skip the document. An operator who intends
333+
a bond must declare its slaves via a per-node body overlay;
334+
until then the discovered stub is not promoted to config. */ -}}
335+
{{- else }}
324336
---
325337
apiVersion: v1alpha1
326338
kind: BondConfig
@@ -362,6 +374,7 @@ routes:
362374
{{- if $link.spec.mtu }}
363375
mtu: {{ $link.spec.mtu }}
364376
{{- end }}
377+
{{- end }}
365378
{{- else if eq $kind "vlan" }}
366379
{{- $parentLinkName := include "talm.discovered.parent_link_name" $linkName }}
367380
{{- $vlanID := include "talm.discovered.vlan_id" $linkName }}

‎charts/generic/templates/_helpers.tpl‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,18 @@ mtu: {{ $link.spec.mtu }}
245245
{{- else if eq $kind "bond" }}
246246
{{- $bondMaster := $link.spec.bondMaster }}
247247
{{- $slaves := fromJsonArray (include "talm.discovered.bond_slaves" $link.spec.index) }}
248+
{{- if not $slaves }}
249+
{{- /* Talos's link controller can auto-create a bond stub in COSI
250+
link state before any operator configuration enslaves
251+
physical NICs to it: the master link carries a bondMaster
252+
spec but no other link has slaveKind: bond + masterIndex
253+
pointing at it. Emitting BondConfig with empty `links:`
254+
produces a document Talos rejects on apply ("at least one
255+
link must be specified"). Treat empty-slaves as not-a-
256+
user-bond and skip the document. An operator who intends
257+
a bond must declare its slaves via a per-node body overlay;
258+
until then the discovered stub is not promoted to config. */ -}}
259+
{{- else }}
248260
---
249261
apiVersion: v1alpha1
250262
kind: BondConfig
@@ -286,6 +298,7 @@ routes:
286298
{{- if $link.spec.mtu }}
287299
mtu: {{ $link.spec.mtu }}
288300
{{- end }}
301+
{{- end }}
289302
{{- else if eq $kind "vlan" }}
290303
{{- $parentLinkName := include "talm.discovered.parent_link_name" $linkName }}
291304
{{- $vlanID := include "talm.discovered.vlan_id" $linkName }}

‎pkg/commands/init.go‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,31 @@ const (
8181
reportVerbUpdated = "Updated"
8282
)
8383

84+
// resolveTalosconfigEndpoints picks the endpoint list to embed in
85+
// the generated talosconfig's context. If the operator passed
86+
// --endpoints, those values propagate; otherwise we seed the
87+
// loopback placeholder so the rendered context is yaml-valid (a
88+
// talosconfig context with empty endpoints fails downstream
89+
// client construction). The operator edits the placeholder to a
90+
// real endpoint after init if they did not supply --endpoints.
91+
//
92+
// Earlier versions hardcoded defaultLocalEndpoint at the
93+
// assignment site and silently discarded the operator's
94+
// --endpoints flag; the helper centralises the resolution so a
95+
// future caller (e.g. talosconfig regenerate flow) inherits the
96+
// same shape.
97+
//
98+
// Returns a fresh slice so callers mutating the talosconfig field
99+
// don't alias the operator-visible GlobalArgs.Endpoints across
100+
// init invocations in the same process.
101+
func resolveTalosconfigEndpoints(globalEndpoints []string) []string {
102+
if len(globalEndpoints) > 0 {
103+
return append([]string(nil), globalEndpoints...)
104+
}
105+
106+
return []string{defaultLocalEndpoint}
107+
}
108+
84109
// initCmdFlags is the package-level flag struct backing the init
85110
// subcommand; cobra binds Flags() entries directly to these fields,
86111
// which forces a global. The global also exposes the flag values to
@@ -595,7 +620,7 @@ var initCmd = &cobra.Command{
595620
return errors.Wrap(err, "generating talos config bundle")
596621
}
597622

598-
configBundle.TalosConfig().Contexts[clusterName].Endpoints = []string{defaultLocalEndpoint}
623+
configBundle.TalosConfig().Contexts[clusterName].Endpoints = resolveTalosconfigEndpoints(GlobalArgs.Endpoints)
599624

600625
data, err := yaml.Marshal(configBundle.TalosConfig())
601626
if err != nil {

‎pkg/commands/init_test.go‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,76 @@ func TestValidateImageOverride(t *testing.T) {
459459
// redundant validateFileExists call was dropped — the gate now lives
460460
// only inside writeSecureToDestination, and this test would fail if
461461
// that inner check were ever removed too.
462+
463+
// TestResolveTalosconfigEndpoints_GlobalNonEmpty_UsesGlobal pins
464+
// that when the operator passes --endpoints on talm init, the
465+
// talosconfig generated for the new project carries those
466+
// endpoints in its context, NOT a hardcoded loopback. Without
467+
// this contract, init writes a talosconfig with
468+
// `endpoints: - 127.0.0.1` and the operator's --endpoints flag
469+
// is silently discarded.
470+
func TestResolveTalosconfigEndpoints_GlobalNonEmpty_UsesGlobal(t *testing.T) {
471+
t.Parallel()
472+
473+
got := resolveTalosconfigEndpoints([]string{"10.0.80.201"})
474+
475+
if len(got) != 1 || got[0] != "10.0.80.201" {
476+
t.Errorf("non-empty --endpoints must propagate to talosconfig; got %v, want [10.0.80.201]", got)
477+
}
478+
}
479+
480+
// TestResolveTalosconfigEndpoints_GlobalEmpty_UsesLoopbackFallback
481+
// pins the fallback shape: when --endpoints is omitted the
482+
// generated talosconfig still needs a non-empty endpoints list
483+
// (yaml schema requirement), so we seed the loopback placeholder.
484+
// The operator edits it to a real endpoint after init.
485+
func TestResolveTalosconfigEndpoints_GlobalEmpty_UsesLoopbackFallback(t *testing.T) {
486+
t.Parallel()
487+
488+
got := resolveTalosconfigEndpoints(nil)
489+
if len(got) != 1 || got[0] != defaultLocalEndpoint {
490+
t.Errorf("empty --endpoints must fall back to defaultLocalEndpoint; got %v, want [%s]", got, defaultLocalEndpoint)
491+
}
492+
}
493+
494+
// TestResolveTalosconfigEndpoints_ReturnsCopy pins that the
495+
// returned slice is not aliased with the caller's slice. Without
496+
// this, downstream mutation of the talosconfig's endpoints would
497+
// also mutate the operator-visible GlobalArgs.Endpoints between
498+
// init runs in a single process (which the test harness exercises).
499+
func TestResolveTalosconfigEndpoints_ReturnsCopy(t *testing.T) {
500+
t.Parallel()
501+
502+
input := []string{"10.0.80.201"}
503+
504+
got := resolveTalosconfigEndpoints(input)
505+
got[0] = "mutated"
506+
507+
if input[0] == "mutated" {
508+
t.Errorf("returned slice must be a copy; mutating it also mutated the input (aliased)")
509+
}
510+
}
511+
512+
// TestResolveTalosconfigEndpoints_MultipleEndpoints pins that all
513+
// supplied endpoints propagate (StringSlice flag accepts repeated
514+
// or comma-separated values). Operators with a HA cluster may
515+
// pass multiple --endpoints on init.
516+
func TestResolveTalosconfigEndpoints_MultipleEndpoints(t *testing.T) {
517+
t.Parallel()
518+
519+
got := resolveTalosconfigEndpoints([]string{"10.0.80.201", "10.0.80.202", "10.0.80.203"})
520+
if len(got) != 3 {
521+
t.Fatalf("multi-endpoint input must all propagate; got %v", got)
522+
}
523+
524+
want := []string{"10.0.80.201", "10.0.80.202", "10.0.80.203"}
525+
for i := range want {
526+
if got[i] != want[i] {
527+
t.Errorf("endpoint[%d]: got %q, want %q", i, got[i], want[i])
528+
}
529+
}
530+
}
531+
462532
func TestWriteSecretsBundleToFile_StillRefusesOverwrite(t *testing.T) {
463533
forceOrig := initCmdFlags.force
464534
rootOrig := Config.RootDir

‎pkg/engine/contract_network_multidoc_test.go‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,66 @@ func TestContract_NetworkMultidoc_BondRendersBondConfig(t *testing.T) {
203203
assertContains(t, out, "miimon: 100")
204204
}
205205

206+
// TestContract_NetworkMultidoc_BondWithoutSlaves_SkipsBondConfig
207+
// pins the empty-slaves guardrail in the cozystack preset. Talos's
208+
// link controller can auto-create a bond stub in COSI link state
209+
// before any operator configuration enslaves physical NICs to it;
210+
// the master link carries a `bondMaster` spec, but no other link
211+
// has `slaveKind: bond` + `masterIndex` pointing at it. Without
212+
// the guardrail, `talm.discovered.bond_slaves` returns an empty
213+
// list and the renderer emits `BondConfig{name: bond0,
214+
// links: <empty>}` — Talos validates the document on apply and
215+
// rejects with `at least one link must be specified`. The fix is
216+
// to skip the bond emission entirely when slaves is empty: a
217+
// discovered bond without enslaved links is a stub, not a
218+
// user-configured bond.
219+
//
220+
// The test also pins a positive-shape invariant: eth0 (the
221+
// non-enslaved physical link from the fixture) must still surface
222+
// as a LinkConfig with its address. A future regression where the
223+
// empty-slaves guard accidentally swallows ALL link emission would
224+
// pass the negative assertions alone; the positive checks pin
225+
// "skip the bond stub, keep the rest of the link graph".
226+
func TestContract_NetworkMultidoc_BondWithoutSlaves_SkipsBondConfig(t *testing.T) {
227+
out := renderCozystackWith(t, emptyBondTopologyLookup(), map[string]any{
228+
"advertisedSubnets": []any{testAdvertisedSubnet},
229+
})
230+
231+
if strings.Contains(out, "kind: BondConfig") {
232+
t.Errorf("empty-slaves bond must NOT emit BondConfig (Talos rejects it on apply with 'at least one link must be specified'); got:\n%s", out)
233+
}
234+
235+
if strings.Contains(out, "name: bond0") {
236+
t.Errorf("empty-slaves bond's master link should not surface as a config document; got:\n%s", out)
237+
}
238+
239+
// Positive-shape pin: eth0 (non-enslaved, has an address) must
240+
// still render as a LinkConfig.
241+
assertContains(t, out, "kind: LinkConfig")
242+
assertContains(t, out, "name: eth0")
243+
assertContains(t, out, "192.168.1.100/24")
244+
}
245+
246+
// TestContract_NetworkMultidoc_BondWithoutSlaves_SkipsBondConfig_Generic
247+
// is the generic-preset counterpart to the cozystack pin. The
248+
// preset's bond emission shape is identical to cozystack's
249+
// (charts/generic/templates/_helpers.tpl) and exhibits the same
250+
// empty-slaves leak — the guardrail is applied in both presets
251+
// and the contract is pinned in both.
252+
func TestContract_NetworkMultidoc_BondWithoutSlaves_SkipsBondConfig_Generic(t *testing.T) {
253+
out := renderGenericWith(t, emptyBondTopologyLookup(), map[string]any{
254+
"advertisedSubnets": []any{testAdvertisedSubnet},
255+
})
256+
257+
if strings.Contains(out, "kind: BondConfig") {
258+
t.Errorf("generic preset: empty-slaves bond must NOT emit BondConfig; got:\n%s", out)
259+
}
260+
261+
if strings.Contains(out, "name: bond0") {
262+
t.Errorf("generic preset: bond stub should not surface as a config document; got:\n%s", out)
263+
}
264+
}
265+
206266
// Contract: bond slaves never appear as standalone LinkConfig
207267
// documents. configurable_link_names filters them out via spec.slaveKind.
208268
func TestContract_NetworkMultidoc_BondSlavesNotEmittedAsLinkConfig(t *testing.T) {

‎pkg/engine/render_test.go‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,119 @@ func bondTopologyLookup() func(string, string, string) (map[string]any, error) {
823823
}
824824
}
825825

826+
// emptyBondTopologyLookup returns a LookupFunc emulating a bond
827+
// stub created by Talos 1.13's link controller before any operator
828+
// configuration enslaves physical NICs to it. The COSI link state
829+
// has a `bond0` master link with `kind: bond` and a `bondMaster`
830+
// spec, but no other link carries `slaveKind: bond` + `masterIndex`
831+
// pointing at it — so `talm.discovered.bond_slaves` returns an
832+
// empty list. Reused by the empty-slaves regression pin.
833+
func emptyBondTopologyLookup() func(string, string, string) (map[string]any, error) {
834+
bondLink := map[string]any{
835+
"metadata": map[string]any{"id": "bond0"},
836+
"spec": map[string]any{
837+
"kind": "bond",
838+
"index": 10,
839+
"bondMaster": map[string]any{
840+
"mode": "802.3ad",
841+
"xmitHashPolicy": "layer3+4",
842+
"lacpRate": "fast",
843+
"miimon": 100,
844+
},
845+
"hardwareAddr": "aa:bb:cc:dd:ee:ff",
846+
"busPath": "pci-0000:00:1f.6",
847+
},
848+
}
849+
// One physical link that is NOT enslaved — operator has not
850+
// configured the bond yet. Carries an address so the route
851+
// resolution still has somewhere to point.
852+
eth0 := map[string]any{
853+
"metadata": map[string]any{"id": "eth0"},
854+
"spec": map[string]any{
855+
"kind": "physical",
856+
"hardwareAddr": "aa:bb:cc:dd:ee:00",
857+
"busPath": "pci-0000:00:1f.0",
858+
},
859+
}
860+
routesList := map[string]any{
861+
"apiVersion": "v1",
862+
"kind": "List",
863+
"items": []any{
864+
map[string]any{
865+
"spec": map[string]any{
866+
"dst": "",
867+
"gateway": "192.168.1.1",
868+
"outLinkName": "eth0",
869+
"family": "inet4",
870+
"table": "main",
871+
},
872+
},
873+
},
874+
}
875+
linksList := map[string]any{
876+
"apiVersion": "v1",
877+
"kind": "List",
878+
"items": []any{bondLink, eth0},
879+
}
880+
addressesList := map[string]any{
881+
"apiVersion": "v1",
882+
"kind": "List",
883+
"items": []any{
884+
map[string]any{
885+
"spec": map[string]any{
886+
"linkName": "eth0",
887+
"address": "192.168.1.100/24",
888+
"family": "inet4",
889+
"scope": "global",
890+
},
891+
},
892+
},
893+
}
894+
nodeDefault := map[string]any{
895+
"spec": map[string]any{
896+
"addresses": []any{"192.168.1.100/24"},
897+
},
898+
}
899+
resolvers := map[string]any{
900+
"spec": map[string]any{
901+
"dnsServers": []any{"8.8.8.8", "1.1.1.1"},
902+
},
903+
}
904+
905+
return func(resource, _, id string) (map[string]any, error) {
906+
switch resource {
907+
case "routes":
908+
return routesList, nil
909+
case "links":
910+
if id == "bond0" {
911+
return bondLink, nil
912+
}
913+
914+
if id == "eth0" {
915+
return eth0, nil
916+
}
917+
918+
if id == "" {
919+
return linksList, nil
920+
}
921+
922+
return map[string]any{}, nil
923+
case "addresses":
924+
return addressesList, nil
925+
case "nodeaddress":
926+
if id == "default" {
927+
return nodeDefault, nil
928+
}
929+
case "resolvers":
930+
if id == "resolvers" {
931+
return resolvers, nil
932+
}
933+
}
934+
935+
return map[string]any{}, nil
936+
}
937+
}
938+
826939
// vlanOnBondTopologyLookup returns a LookupFunc emulating a VLAN interface
827940
// stacked on top of a bond. Used by VLANConfig rendering tests.
828941
func vlanOnBondTopologyLookup() func(string, string, string) (map[string]any, error) {

0 commit comments

Comments
 (0)