Skip to content

Commit 6e914ea

Browse files
authored
Merge pull request #208 from cozystack/feat/chart-cleanup-per-link-and-scope-filter
refactor(chart): area/chart cleanup batch (factor multi-doc, scope-filter symmetry, int() drop)
2 parents e8dcbc6 + c4a07a6 commit 6e914ea

6 files changed

Lines changed: 475 additions & 617 deletions

File tree

‎charts/cozystack/templates/_helpers.tpl‎

Lines changed: 1 addition & 307 deletions
Original file line numberDiff line numberDiff line change
@@ -161,313 +161,7 @@ cluster:
161161

162162
{{- /* Shared network document generation for v1.12+ multi-doc format */ -}}
163163
{{- define "talos.config.network.multidoc" }}
164-
{{- /* Multi-doc format reconstructs network config from discovery resources.
165-
Every configurable link on the node (physical NIC, bond, VLAN, bridge)
166-
gets its own document so a multi-NIC node ends up with all NICs
167-
configured rather than only the gateway-bearing one. The gateway-
168-
link's IPv4 default-route gateway is emitted only on that link's
169-
document; every other link gets its addresses without a default route.
170-
MTU is surfaced when discovery reports a value so non-default-MTU
171-
links (jumbo frames, GRE) survive a re-render.
172-
173-
existing_interfaces_configuration is not consulted here: v1.12 nodes
174-
store network config in separate documents (LinkConfig, BondConfig,
175-
VLANConfig), not in the legacy machine.network.interfaces field. The
176-
guardrail below catches the upgrade case where a node was originally
177-
bootstrapped on a chart that emitted the legacy schema and still
178-
carries non-empty machine.network.interfaces[] in its running
179-
MachineConfig — the renderer cannot translate those entries today
180-
and would otherwise silently drop them on the next apply. */ -}}
181-
{{- $legacyInterfaces := include "talm.discovered.existing_interfaces_configuration" . }}
182-
{{- if $legacyInterfaces }}
183-
{{- fail (printf "talm: the multi-doc renderer cannot translate legacy machine.network.interfaces[] from the running MachineConfig. Move the interfaces, vlans, and addresses below into per-node body overlays as v1.12 typed documents (LinkConfig, VLANConfig, BondConfig, RouteConfig) before re-running talm apply, or pin templateOptions.talosVersion to v1.11 in Chart.yaml until the translator lands.\n\nDetected legacy block:\n%s" $legacyInterfaces) }}
184-
{{- end }}
185-
{{- (include "talm.discovered.physical_links_info" .) }}
186-
---
187-
apiVersion: v1alpha1
188-
kind: HostnameConfig
189-
hostname: {{ include "talm.discovered.hostname" . | quote }}
190-
---
191-
apiVersion: v1alpha1
192-
kind: ResolverConfig
193-
nameservers:
194-
{{- $resolvers := include "talm.discovered.default_resolvers" . }}
195-
{{- if $resolvers }}
196-
{{- range fromJsonArray $resolvers }}
197-
- address: {{ . | quote }}
198-
{{- end }}
199-
{{- else }}
200-
[]
201-
{{- end }}
202-
{{- /* Coerce .Values.floatingIP to its string form once at the
203-
top of the multi-doc body and reuse the result everywhere
204-
a downstream lookup or formatter needs it. The per-link
205-
addresses_by_link strip on every link emission below
206-
depends on the same stringified value — a worker render
207-
with `floatingIP: 192168` would otherwise feed printf
208-
"%s/" an int, producing `%!s(int=192168)/` that never
209-
matches a CIDR. The coercion isolates that trap to one
210-
place and lets the rest of the template treat the value
211-
uniformly.
212-
213-
"<nil>" is Sprig's serialisation of nil and "" is the
214-
unset string; both mean "operator did not supply a
215-
value". The shared talm.validate_floatingIP partial below
216-
handles the actual fail-fast — invoke it here AND in the
217-
legacy define so a malformed value fails at render time
218-
regardless of the rendered Talos version. */}}
219-
{{- $fipStr := .Values.floatingIP | toString }}
220-
{{- $fipIsSet := and (ne $fipStr "") (ne $fipStr "<nil>") }}
221-
{{- include "talm.validate_floatingIP" . }}
222-
{{- /* Operator-declared vipLink override: emit Layer2VIPConfig
223-
regardless of discovery state. Useful when the target link
224-
does not yet exist on the live system at first apply (typical
225-
case: a VLAN sub-interface this template is about to bring up).
226-
The discovery-derived block below skips its own Layer2VIPConfig
227-
when this branch fires, so we never emit duplicates. */}}
228-
{{- if and $fipIsSet .Values.vipLink (eq .MachineType "controlplane") }}
229-
---
230-
apiVersion: v1alpha1
231-
kind: Layer2VIPConfig
232-
name: {{ $fipStr | quote }}
233-
link: {{ .Values.vipLink }}
234-
{{- end }}
235-
{{- $defaultLinkName := include "talm.discovered.default_link_name_by_gateway" . }}
236-
{{- $configurableLinks := fromJsonArray (include "talm.discovered.configurable_link_names" .) }}
237-
{{- range $linkName := $configurableLinks }}
238-
{{- $link := lookup "links" "" $linkName }}
239-
{{- if $link }}
240-
{{- $kind := $link.spec.kind | toString }}
241-
{{- $isGatewayLink := eq $linkName $defaultLinkName }}
242-
{{- $rawAddresses := fromJsonArray (include "talm.discovered.addresses_by_link" $linkName) }}
243-
{{- /* Strip the operator-declared floatingIP from per-link addresses
244-
so the VIP currently held by this leader does not leak into
245-
LinkConfig.addresses. Talos's VIP operator installs the VIP
246-
as a regular global-scope address indistinguishable from a
247-
permanent one in COSI; without the filter, a re-render against
248-
the VIP-active node would declare the VIP both as a permanent
249-
address and as the Layer2VIPConfig target, putting the leader
250-
and follower configs out of sync. */}}
251-
{{- $addresses := list }}
252-
{{- range $rawAddresses }}
253-
{{- /* Use the hoisted $fipStr/$fipIsSet from the top of the
254-
define so the strip honours the same coerced value the
255-
validation block above used. Going through `printf "%s/"
256-
$.Values.floatingIP` directly would emit
257-
`%!s(int=192168)/` for a numeric YAML scalar on a worker
258-
render (controlplane was caught by the fail-fast). */ -}}
259-
{{- if not (and $fipIsSet (hasPrefix (printf "%s/" $fipStr) .)) }}
260-
{{- $addresses = append $addresses . }}
261-
{{- end }}
262-
{{- end }}
263-
{{- $linkGateway := "" }}
264-
{{- if $isGatewayLink }}
265-
{{- $linkGateway = include "talm.discovered.gateway_by_link" $linkName }}
266-
{{- end }}
267-
{{- if eq $kind "bridge" }}
268-
{{- /* BridgeConfig emission. Discovers bridge ports (members) via
269-
talm.discovered.bridge_slaves and emits a typed v1.12+
270-
BridgeConfig document with the same address / route / mtu
271-
shape as the other branches. STP and VLAN filtering are
272-
opt-in: they are emitted only when the bridge controller
273-
reported a non-nil spec.bridgeMaster.stp / spec.bridgeMaster
274-
value, so a default-state bridge stays minimal. */ -}}
275-
{{- $bridgeMaster := $link.spec.bridgeMaster }}
276-
{{- $bridgePorts := fromJsonArray (include "talm.discovered.bridge_slaves" $link.spec.index) }}
277-
---
278-
apiVersion: v1alpha1
279-
kind: BridgeConfig
280-
name: {{ $linkName }}
281-
{{- if $bridgePorts }}
282-
links:
283-
{{- range $bridgePorts }}
284-
- {{ . }}
285-
{{- end }}
286-
{{- end }}
287-
{{- if $bridgeMaster }}
288-
{{- if $bridgeMaster.stp }}
289-
{{- if hasKey $bridgeMaster.stp "enabled" }}
290-
stp:
291-
enabled: {{ $bridgeMaster.stp.enabled }}
292-
{{- end }}
293-
{{- end }}
294-
{{- /* COSI's BridgeVLANSpec serialises FilteringEnabled as
295-
yaml:"filteringEnabled" (verified against
296-
siderolabs/talos pkg/machinery/resources/network/link.go).
297-
The output-side BridgeConfig schema uses the shorter
298-
yaml:"filtering,omitempty" key — so we read the long form
299-
from discovery and emit the short form into the rendered
300-
document. */ -}}
301-
{{- if $bridgeMaster.vlan }}
302-
{{- if hasKey $bridgeMaster.vlan "filteringEnabled" }}
303-
vlan:
304-
filtering: {{ $bridgeMaster.vlan.filteringEnabled }}
305-
{{- end }}
306-
{{- end }}
307-
{{- end }}
308-
{{- if $addresses }}
309-
addresses:
310-
{{- range $addresses }}
311-
- address: {{ . }}
312-
{{- end }}
313-
{{- end }}
314-
{{- if $linkGateway }}
315-
routes:
316-
- gateway: {{ $linkGateway }}
317-
{{- end }}
318-
{{- if $link.spec.mtu }}
319-
mtu: {{ $link.spec.mtu }}
320-
{{- end }}
321-
{{- else if eq $kind "bond" }}
322-
{{- $bondMaster := $link.spec.bondMaster }}
323-
{{- $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 }}
336-
---
337-
apiVersion: v1alpha1
338-
kind: BondConfig
339-
name: {{ $linkName }}
340-
links:
341-
{{- range $slaves }}
342-
- {{ . }}
343-
{{- end }}
344-
{{- if $bondMaster }}
345-
{{- if $bondMaster.mode }}
346-
bondMode: {{ $bondMaster.mode }}
347-
{{- end }}
348-
{{- if $bondMaster.xmitHashPolicy }}
349-
xmitHashPolicy: {{ $bondMaster.xmitHashPolicy }}
350-
{{- end }}
351-
{{- if $bondMaster.lacpRate }}
352-
lacpRate: {{ $bondMaster.lacpRate }}
353-
{{- end }}
354-
{{- if $bondMaster.miimon }}
355-
miimon: {{ $bondMaster.miimon }}
356-
{{- end }}
357-
{{- if $bondMaster.updelay }}
358-
updelay: {{ $bondMaster.updelay }}
359-
{{- end }}
360-
{{- if $bondMaster.downdelay }}
361-
downdelay: {{ $bondMaster.downdelay }}
362-
{{- end }}
363-
{{- end }}
364-
{{- if $addresses }}
365-
addresses:
366-
{{- range $addresses }}
367-
- address: {{ . }}
368-
{{- end }}
369-
{{- end }}
370-
{{- if $linkGateway }}
371-
routes:
372-
- gateway: {{ $linkGateway }}
373-
{{- end }}
374-
{{- if $link.spec.mtu }}
375-
mtu: {{ $link.spec.mtu }}
376-
{{- end }}
377-
{{- end }}
378-
{{- else if eq $kind "vlan" }}
379-
{{- $parentLinkName := include "talm.discovered.parent_link_name" $linkName }}
380-
{{- $vlanID := include "talm.discovered.vlan_id" $linkName }}
381-
{{- if not $parentLinkName }}
382-
{{- /* VLANConfig requires the parent field on the wire. Emitting one
383-
without it produces a document Talos rejects on apply. Treat the
384-
partial-discovery case as fail-fast — a VLAN with an unresolvable
385-
linkIndex is a discovery bug, not a config we can render. */ -}}
386-
{{- fail (printf "talm: discovered VLAN %q has no resolvable parent link (spec.linkIndex points at a non-existent link). VLANConfig requires the parent field; refusing to emit an invalid document. Fix the discovery state or declare the VLAN explicitly via a per-node body overlay." $linkName) }}
387-
{{- end }}
388-
{{- if not $vlanID }}
389-
{{- /* VLANConfig also requires vlanID. Symmetric guardrail to the
390-
missing-parent case above — discovery without spec.vlan.vlanID
391-
cannot produce a valid VLANConfig. */ -}}
392-
{{- fail (printf "talm: discovered VLAN %q has no resolvable vlanID (spec.vlan.vlanID is unset). VLANConfig requires vlanID; refusing to emit an invalid document. Fix the discovery state or declare the VLAN explicitly via a per-node body overlay." $linkName) }}
393-
{{- end }}
394-
---
395-
apiVersion: v1alpha1
396-
kind: VLANConfig
397-
name: {{ $linkName }}
398-
vlanID: {{ $vlanID }}
399-
parent: {{ $parentLinkName }}
400-
{{- if $addresses }}
401-
addresses:
402-
{{- range $addresses }}
403-
- address: {{ . }}
404-
{{- end }}
405-
{{- end }}
406-
{{- if $linkGateway }}
407-
routes:
408-
- gateway: {{ $linkGateway }}
409-
{{- end }}
410-
{{- if $link.spec.mtu }}
411-
mtu: {{ $link.spec.mtu }}
412-
{{- end }}
413-
{{- else }}
414-
---
415-
apiVersion: v1alpha1
416-
kind: LinkConfig
417-
name: {{ $linkName }}
418-
{{- if $addresses }}
419-
addresses:
420-
{{- range $addresses }}
421-
- address: {{ . }}
422-
{{- end }}
423-
{{- end }}
424-
{{- if $linkGateway }}
425-
routes:
426-
- gateway: {{ $linkGateway }}
427-
{{- end }}
428-
{{- if $link.spec.mtu }}
429-
mtu: {{ $link.spec.mtu }}
430-
{{- end }}
431-
{{- end }}
432-
{{- end }}
433-
{{- end }}
434-
{{- /* Discovery-derived Layer2VIPConfig: skipped when the operator
435-
has set .Values.vipLink, since the override-path block above
436-
has already emitted the document with the operator's chosen
437-
link.
438-
439-
Link selection prefers the link whose discovered addresses
440-
contain the floatingIP (talm.discovered.link_name_for_address),
441-
so a VIP in a private subnet hosted on a VLAN child lands on
442-
that VLAN — not on the IPv4-default-route NIC. The
443-
default-gateway link stays as the fallback for topologies
444-
where the VIP isn't on any discovered subnet (typical for
445-
upstream-routable VIPs that arrive via the default-route
446-
link). When neither resolves a link, no Layer2VIPConfig is
447-
emitted, matching the prior behaviour. */}}
448-
{{- if and $fipIsSet (not .Values.vipLink) (eq .MachineType "controlplane") }}
449-
{{- $vipLink := include "talm.discovered.link_name_for_address" $fipStr }}
450-
{{- /* Default-gateway fallback must also point at a configurable
451-
link — otherwise an unmanaged default-route NIC (Wireguard,
452-
a slave NIC of a bond, anything outside the configurable
453-
set) would silently win selection and the rendered
454-
Layer2VIPConfig would dangle on a link the chart never
455-
emits a per-link document for. Mirror the same
456-
configurable-link gate link_name_for_address applies inside
457-
its own iteration. */ -}}
458-
{{- if not $vipLink }}
459-
{{- if has $defaultLinkName $configurableLinks }}
460-
{{- $vipLink = $defaultLinkName }}
461-
{{- end }}
462-
{{- end }}
463-
{{- if $vipLink }}
464-
---
465-
apiVersion: v1alpha1
466-
kind: Layer2VIPConfig
467-
name: {{ $fipStr | quote }}
468-
link: {{ $vipLink }}
469-
{{- end }}
470-
{{- end }}
164+
{{- include "talm.config.network.multidoc" . }}
471165
{{- end }}
472166

473167
{{- /* Shared legacy network section for machine.network */ -}}

0 commit comments

Comments
 (0)