Skip to content

Commit 5881e65

Browse files
committed
fix(engine): merge chart Kubernetes documents into the bundle
From contract v1.14 the generated bundle already carries every Kubernetes document, filled with values only the generator knows: component images pinned to kubernetesVersion, the control-plane node label and taint, the kubelet seccomp default, endpoint-derived documents. A chart document of the same identity replaced the bundle's copy wholesale, so a chart could only emit these documents by re-implementing the generator. Kube* documents and delete selectors from the chart now go through the config patcher, the same strategic merge the apply path already uses. The first bundle pass sees the chart's KubeClusterConfig, so the regenerated bundle derives its endpoint-dependent documents from it. A full render carries the merged result; a non-full render keeps the chart's documents verbatim for apply to merge again. Every other kind keeps replace semantics: the bundle's HostnameConfig carries `auto: stable`, which Talos refuses next to a chart hostname. The hint for a v1alpha1/typed-document mix above v1.13 no longer claims the charts cannot emit the typed documents. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent cfc5a95 commit 5881e65

2 files changed

Lines changed: 211 additions & 6 deletions

File tree

‎pkg/engine/contract_multidoc_render_test.go‎

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,159 @@ func TestContract_BundleSerializesV1Alpha1First(t *testing.T) {
286286
})
287287
}
288288
}
289+
290+
// From contract v1.14 the bundle generates every Kubernetes document with the
291+
// values only the generator knows: component images pinned to
292+
// kubernetesVersion, the control-plane node label and taint, the kubelet
293+
// seccomp default. A chart writes only what it sets, so its Kubernetes
294+
// documents merge into the bundle's instead of replacing them.
295+
func TestContract_ChartKubeDocumentMergesIntoBundle(t *testing.T) {
296+
patch := `machine:
297+
type: controlplane
298+
---
299+
apiVersion: v1alpha1
300+
kind: KubeClusterConfig
301+
clusterName: prod
302+
endpoint: https://192.0.2.10:6443
303+
---
304+
apiVersion: v1alpha1
305+
kind: KubeAPIServerConfig
306+
extraArgs:
307+
oidc-issuer-url: https://issuer.example.com
308+
`
309+
310+
out, err := applyPatchesAndRenderConfig(
311+
Options{KubernetesVersion: "v1.34.3", TalosVersion: "v1.14", Full: true},
312+
[]string{patch})
313+
if err != nil {
314+
t.Fatalf("applyPatchesAndRenderConfig: %v", err)
315+
}
316+
317+
rendered := string(out)
318+
319+
for _, kind := range []string{"KubeAPIServerConfig", "KubeClusterConfig"} {
320+
if got := strings.Count(rendered, "kind: "+kind+"\n"); got != 1 {
321+
t.Errorf("render carries %d %s documents, want 1\n--- rendered ---\n%s", got, kind, rendered)
322+
}
323+
}
324+
325+
for _, want := range []string{
326+
"registry.k8s.io/kube-apiserver:v1.34.3",
327+
"oidc-issuer-url: https://issuer.example.com",
328+
"clusterName: prod",
329+
// The bundle is regenerated from the chart's endpoint, so documents
330+
// derived from it agree with KubeClusterConfig.
331+
"issuerURL: https://192.0.2.10:6443",
332+
} {
333+
if !strings.Contains(rendered, want) {
334+
t.Errorf("render is missing %q\n--- rendered ---\n%s", want, rendered)
335+
}
336+
}
337+
338+
if _, err := configloader.NewFromBytes(out); err != nil {
339+
t.Errorf("Talos refuses the rendered config: %v", err)
340+
}
341+
}
342+
343+
// A delete selector only means something to the patcher. Appended verbatim to a
344+
// full render it would leave the bundle's document in place and add a document
345+
// Talos cannot load.
346+
func TestContract_ChartDeleteSelectorRemovesBundleDocument(t *testing.T) {
347+
patch := `machine:
348+
type: controlplane
349+
---
350+
apiVersion: v1alpha1
351+
kind: KubeClusterConfig
352+
clusterName: prod
353+
endpoint: https://192.0.2.10:6443
354+
---
355+
apiVersion: v1alpha1
356+
kind: KubeFlannelCNIConfig
357+
$patch: delete
358+
---
359+
apiVersion: v1alpha1
360+
kind: DiscoveryServiceConfig
361+
name: default
362+
$patch: delete
363+
`
364+
365+
out, err := applyPatchesAndRenderConfig(
366+
Options{KubernetesVersion: "v1.34.3", TalosVersion: "v1.14", Full: true},
367+
[]string{patch})
368+
if err != nil {
369+
t.Fatalf("applyPatchesAndRenderConfig: %v", err)
370+
}
371+
372+
rendered := string(out)
373+
374+
for _, unwanted := range []string{"kind: KubeFlannelCNIConfig", "kind: DiscoveryServiceConfig", "$patch"} {
375+
if strings.Contains(rendered, unwanted) {
376+
t.Errorf("render still carries %q\n--- rendered ---\n%s", unwanted, rendered)
377+
}
378+
}
379+
380+
if _, err := configloader.NewFromBytes(out); err != nil {
381+
t.Errorf("Talos refuses the rendered config: %v", err)
382+
}
383+
}
384+
385+
// A non-full render is the node file: the chart's Kubernetes documents and
386+
// selectors stay as written, and apply merges them into the bundle again.
387+
func TestContract_NonFullRenderKeepsChartKubeDocumentsVerbatim(t *testing.T) {
388+
patch := `machine:
389+
type: controlplane
390+
---
391+
apiVersion: v1alpha1
392+
kind: KubeClusterConfig
393+
clusterName: prod
394+
endpoint: https://192.0.2.10:6443
395+
---
396+
apiVersion: v1alpha1
397+
kind: KubeAPIServerConfig
398+
extraArgs:
399+
oidc-issuer-url: https://issuer.example.com
400+
---
401+
apiVersion: v1alpha1
402+
kind: KubeFlannelCNIConfig
403+
$patch: delete
404+
`
405+
406+
opts := Options{KubernetesVersion: "v1.34.3", TalosVersion: "v1.14"}
407+
408+
out, err := applyPatchesAndRenderConfig(opts, []string{patch})
409+
if err != nil {
410+
t.Fatalf("applyPatchesAndRenderConfig: %v", err)
411+
}
412+
413+
rendered := string(out)
414+
415+
if !strings.Contains(rendered, "kind: KubeAPIServerConfig\nextraArgs:\n oidc-issuer-url: https://issuer.example.com") {
416+
t.Errorf("the chart's KubeAPIServerConfig did not survive verbatim\n--- rendered ---\n%s", rendered)
417+
}
418+
419+
if strings.Contains(rendered, "registry.k8s.io/kube-apiserver") {
420+
t.Errorf("the node file picked up the bundle's image\n--- rendered ---\n%s", rendered)
421+
}
422+
423+
if !strings.Contains(rendered, "kind: KubeFlannelCNIConfig\n$patch: delete") {
424+
t.Errorf("the delete selector was dropped from the node file\n--- rendered ---\n%s", rendered)
425+
}
426+
427+
configBundle, machineType, err := FullConfigProcess(opts, []string{rendered})
428+
if err != nil {
429+
t.Fatalf("FullConfigProcess on the node file: %v", err)
430+
}
431+
432+
applied, err := SerializeConfiguration(configBundle, machineType, opts.TalosVersion, opts.KubernetesVersion)
433+
if err != nil {
434+
t.Fatalf("SerializeConfiguration: %v", err)
435+
}
436+
437+
if strings.Contains(string(applied), "kind: KubeFlannelCNIConfig") {
438+
t.Errorf("applying the node file kept flannel\n--- applied ---\n%s", applied)
439+
}
440+
441+
if !strings.Contains(string(applied), "oidc-issuer-url: https://issuer.example.com") {
442+
t.Errorf("applying the node file lost the chart's apiserver args\n--- applied ---\n%s", applied)
443+
}
444+
}

‎pkg/engine/engine.go‎

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path/filepath"
1313
"reflect"
1414
"regexp"
15+
"slices"
1516
"strings"
1617
"unsafe"
1718

@@ -823,8 +824,8 @@ func isPatchDeleteDirective(n *yaml.Node) bool {
823824

824825
k, v := n.Content[0], n.Content[1]
825826

826-
return k.Kind == yaml.ScalarNode && k.Value == "$patch" &&
827-
v.Kind == yaml.ScalarNode && v.Value == "delete"
827+
return k.Kind == yaml.ScalarNode && k.Value == patchDirectiveKey &&
828+
v.Kind == yaml.ScalarNode && v.Value == patchDirectiveDelete
828829
}
829830

830831
// pruneBodyIdentitiesAgainstRendered removes from body every key whose value
@@ -1469,6 +1470,10 @@ const (
14691470
// k8sKeyKind is the standard Kubernetes/COSI document key
14701471
// used as part of the (apiVersion, kind, name) identity tuple.
14711472
k8sKeyKind = "kind"
1473+
// patchDirectiveKey and patchDirectiveDelete spell a strategic-merge
1474+
// delete directive, `$patch: delete`.
1475+
patchDirectiveKey = "$patch"
1476+
patchDirectiveDelete = "delete"
14721477
// k8sAPIVersionV1 is the value of apiVersion for the synthetic
14731478
// List envelope newLookupFunction emits when wrapping multi-item
14741479
// COSI lookup results.
@@ -1899,6 +1904,39 @@ func extractExtraDocuments(patches []string) ([]string, []string, error) {
18991904
return talosPatches, extraDocs, nil
19001905
}
19011906

1907+
// splitMergedDocuments picks out the chart documents that patch the bundle
1908+
// instead of replacing a document of the same identity: every Kube* kind and
1909+
// delete selectors. From contract v1.14 the bundle generates the Kubernetes
1910+
// documents with values only the generator knows (component images, the
1911+
// control-plane label and taint), so a chart writes just what it sets. The
1912+
// KubeSpan kinds match the prefix too; the generator emits none of them, so
1913+
// for those a merge is the same as a replace. A delete selector is
1914+
// meaningless anywhere but in the patcher. Every other kind keeps
1915+
// replace semantics: the bundle's HostnameConfig carries `auto: stable`, which
1916+
// Talos refuses next to the chart's hostname.
1917+
func splitMergedDocuments(extraDocs []string) ([]string, []string, error) {
1918+
var merged, replacing []string
1919+
1920+
for _, doc := range extraDocs {
1921+
var parsed map[string]any
1922+
1923+
if err := yaml.Unmarshal([]byte(doc), &parsed); err != nil {
1924+
return nil, nil, errors.Wrap(err, "unmarshaling extra document")
1925+
}
1926+
1927+
kind, _ := parsed[k8sKeyKind].(string)
1928+
selector, _ := parsed[patchDirectiveKey].(string)
1929+
1930+
if strings.HasPrefix(kind, "Kube") || selector == patchDirectiveDelete {
1931+
merged = append(merged, doc)
1932+
} else {
1933+
replacing = append(replacing, doc)
1934+
}
1935+
}
1936+
1937+
return merged, replacing, nil
1938+
}
1939+
19021940
// applyPatchesAndRenderConfig assembles the final Talos config bytes
19031941
// for the non-Full template path: split out extra documents, run two
19041942
// bundle-rebuild passes (TypeUnknown then resolved machine type),
@@ -1912,6 +1950,11 @@ func applyPatchesAndRenderConfig(opts Options, configPatches []string) ([]byte,
19121950
return nil, err
19131951
}
19141952

1953+
mergedDocs, replacingDocs, err := splitMergedDocuments(extraDocs)
1954+
if err != nil {
1955+
return nil, err
1956+
}
1957+
19151958
// Generate options for the configuration based on the provided flags
19161959
genOptions := []generate.Option{}
19171960

@@ -1949,7 +1992,7 @@ func applyPatchesAndRenderConfig(opts Options, configPatches []string) ([]byte,
19491992
return nil, errors.Wrap(err, "creating initial config bundle")
19501993
}
19511994

1952-
patches, err := configpatcher.LoadPatches(talosPatches)
1995+
patches, err := configpatcher.LoadPatches(append(slices.Clone(talosPatches), mergedDocs...))
19531996
if err != nil {
19541997
if opts.Debug {
19551998
debugPhase(opts, configPatches, "", "", machine.TypeUnknown)
@@ -2061,7 +2104,7 @@ func applyPatchesAndRenderConfig(opts Options, configPatches []string) ([]byte,
20612104
//
20622105
// A non-full render returns a patch rather than a config, so what it returns
20632106
// is deliberately not what gets checked here.
2064-
assembled, err := assembleTargetDocuments(configFull, nil, extraDocs)
2107+
assembled, err := assembleTargetDocuments(configFull, nil, replacingDocs)
20652108
if err != nil {
20662109
return nil, err
20672110
}
@@ -2070,9 +2113,12 @@ func applyPatchesAndRenderConfig(opts Options, configPatches []string) ([]byte,
20702113
return nil, err
20712114
}
20722115

2116+
// The merged documents are already inside configFull. The node file keeps
2117+
// them as the chart wrote them, and apply merges them again.
20732118
var target []byte
20742119
if opts.Full {
20752120
target = configFull
2121+
extraDocs = replacingDocs
20762122
} else {
20772123
target, err = yamltools.DiffYAMLs(configOrigin, configFull)
20782124
if err != nil {
@@ -2166,10 +2212,13 @@ func checkSupersededFields(rendered []byte, talosVersion string) error {
21662212
//nolint:wrapcheck // cockroachdb/errors.WithHint at boundary.
21672213
return errors.WithHintf(
21682214
conflict,
2169-
"templateOptions.talosVersion is %s. Pin it to %s or lower in Chart.yaml; above that "+
2170-
"contract Talos keeps these settings in documents of their own, which the charts do not emit.",
2215+
"templateOptions.talosVersion is %s; above %s Talos keeps these settings in documents of their own. "+
2216+
"Re-render the node files with `talm template` (the charts emit those documents when talosVersion is pinned) "+
2217+
"and move the reported fields out of v1alpha1 in your own patches. Pin it to %s or lower in Chart.yaml "+
2218+
"to keep the v1alpha1 shape.",
21712219
pinned,
21722220
config.TalosVersion1_13,
2221+
config.TalosVersion1_13,
21732222
)
21742223
}
21752224

0 commit comments

Comments
 (0)