Skip to content

Commit 30628df

Browse files
committed
feat(helpers): add per-link discovery helpers for multi-NIC templates
The existing default_*_by_gateway helpers expose only the primary link (the one carrying the default route). Templates targeting nodes with secondary NICs — a storage uplink on a control-plane, a second public link, etc. — had no way to enumerate every physical NIC or read addresses, gateway, MAC, or bus path by link name. Add the following helpers under talm.discovered: - physical_links: JSON list of physical NIC names (eno|eth|enp|enx|ens with a busPath) - configurable_links: physical NICs plus bond/vlan/bridge top-level links - addresses_by_link: JSON list of CIDRs configured on the link, excluding host scope - gateway_by_link: scalar gateway IP for the default route on the link, in the main table - routes_by_link: JSON list of non-default routes on the link as flat {dst, gateway, family, table, priority} objects - mac_by_link, bus_by_link: scalar accessors - link_selector_by_name: YAML 'busPath: ...' fragment for use as a Talos deviceSelector Default cozystack/generic templates are unchanged; the new helpers are building blocks for user templates. Refs #125 Assisted-By: Claude <noreply@anthropic.com> Signed-off-by: Aleksei Sviridkin <f@lex.la>
1 parent 5e137a8 commit 30628df

1 file changed

Lines changed: 103 additions & 0 deletions

File tree

charts/talm/templates/_helpers.tpl

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,106 @@ vlans:
270270
- vlanId: {{ $link.spec.vlan.vlanID }}
271271
{{- end -}}
272272
{{- end -}}
273+
274+
{{- /*
275+
Multi-NIC discovery helpers (#125).
276+
277+
The `default_*_by_gateway` family above resolves only the link carrying the
278+
default route (primary). Templates targeting nodes with secondary NICs
279+
(storage links on a control-plane, second uplink, etc.) need to enumerate
280+
every physical link and read its addresses/routes/MAC by name. The helpers
281+
below are the by-name building blocks; existing default_*_by_gateway helpers
282+
remain wrappers that resolve the primary link and call into these.
283+
*/ -}}
284+
285+
{{- /* JSON list of physical link names (raw NICs only — not bond/vlan masters). */ -}}
286+
{{- define "talm.discovered.physical_links" -}}
287+
{{- $names := list -}}
288+
{{- range (lookup "links" "" "").items -}}
289+
{{- if and .spec.busPath (regexMatch "^(eno|eth|enp|enx|ens)" (.metadata.id | toString)) -}}
290+
{{- $names = append $names .metadata.id -}}
291+
{{- end -}}
292+
{{- end -}}
293+
{{- toJson $names -}}
294+
{{- end -}}
295+
296+
{{- /* JSON list of every link a user template can configure: physical NICs
297+
plus bond / vlan / bridge top-level links. */ -}}
298+
{{- define "talm.discovered.configurable_links" -}}
299+
{{- $names := list -}}
300+
{{- range (lookup "links" "" "").items -}}
301+
{{- $isPhysical := and .spec.busPath (regexMatch "^(eno|eth|enp|enx|ens)" (.metadata.id | toString)) -}}
302+
{{- $isVirtual := has (.spec.kind | toString) (list "bond" "vlan" "bridge") -}}
303+
{{- if or $isPhysical $isVirtual -}}
304+
{{- $names = append $names .metadata.id -}}
305+
{{- end -}}
306+
{{- end -}}
307+
{{- toJson $names -}}
308+
{{- end -}}
309+
310+
{{- /* JSON list of CIDR addresses configured on the given link (any family),
311+
excluding host-scoped addresses. Caller is responsible for filtering
312+
VIPs or family if needed. */ -}}
313+
{{- define "talm.discovered.addresses_by_link" -}}
314+
{{- $linkName := . -}}
315+
{{- $addresses := list -}}
316+
{{- range (lookup "addresses" "" "").items -}}
317+
{{- if and (eq .spec.linkName $linkName) (not (eq .spec.scope "host")) -}}
318+
{{- $addresses = append $addresses .spec.address -}}
319+
{{- end -}}
320+
{{- end -}}
321+
{{- toJson $addresses -}}
322+
{{- end -}}
323+
324+
{{- /* Scalar gateway IP for the default route (dst="", main table) on the
325+
given link. Empty if no default route uses this link. */ -}}
326+
{{- define "talm.discovered.gateway_by_link" -}}
327+
{{- $linkName := . -}}
328+
{{- range (lookup "routes" "" "").items -}}
329+
{{- if and (eq .spec.outLinkName $linkName) (eq .spec.dst "") (not (eq .spec.gateway "")) (eq .spec.table "main") -}}
330+
{{- .spec.gateway -}}
331+
{{- break -}}
332+
{{- end -}}
333+
{{- end -}}
334+
{{- end -}}
335+
336+
{{- /* JSON list of non-default routes on the given link. Each entry is a flat
337+
map {dst, gateway, family, table, priority} so consumers can
338+
fromJsonArray + range + dig. */ -}}
339+
{{- define "talm.discovered.routes_by_link" -}}
340+
{{- $linkName := . -}}
341+
{{- $routes := list -}}
342+
{{- range (lookup "routes" "" "").items -}}
343+
{{- if and (eq .spec.outLinkName $linkName) (not (eq .spec.dst "")) -}}
344+
{{- $entry := dict "dst" .spec.dst "gateway" (.spec.gateway | toString) "family" (.spec.family | toString) "table" (.spec.table | toString) "priority" (.spec.priority | toString) -}}
345+
{{- $routes = append $routes $entry -}}
346+
{{- end -}}
347+
{{- end -}}
348+
{{- toJson $routes -}}
349+
{{- end -}}
350+
351+
{{- /* Scalar MAC address for the given link, or empty. */ -}}
352+
{{- define "talm.discovered.mac_by_link" -}}
353+
{{- $link := lookup "links" "" . -}}
354+
{{- if $link -}}
355+
{{- $link.spec.hardwareAddr | toString -}}
356+
{{- end -}}
357+
{{- end -}}
358+
359+
{{- /* Scalar PCI / bus path for the given link, or empty. */ -}}
360+
{{- define "talm.discovered.bus_by_link" -}}
361+
{{- $link := lookup "links" "" . -}}
362+
{{- if $link -}}
363+
{{- $link.spec.busPath | toString -}}
364+
{{- end -}}
365+
{{- end -}}
366+
367+
{{- /* YAML fragment `busPath: <path>` for use as a Talos deviceSelector by
368+
link name. Prefer this over emitting `interface:` when you need the
369+
config to be portable across renames (e.g. predictable network names). */ -}}
370+
{{- define "talm.discovered.link_selector_by_name" -}}
371+
{{- $link := lookup "links" "" . -}}
372+
{{- if and $link $link.spec.busPath -}}
373+
busPath: {{ $link.spec.busPath }}
374+
{{- end -}}
375+
{{- end -}}

0 commit comments

Comments
 (0)