Skip to content

Commit 82b5ef6

Browse files
committed
feat(apply): honor value files and --set flags consistent with template
talm apply re-renders config from the modeline at apply time but, unlike talm template, ignored every user value source on its template-rendering path: it had no --values / --set* flags and did not read templateOptions.{valueFiles,values,...} from Chart.yaml. A value supplied via a value file rendered correctly under template yet was silently dropped at apply (rendered empty, or a required helper failed), making it impossible to inject user values that templates consume. Add the six value-source flags to apply (--values, --set, --set-string, --set-file, --set-json, --set-literal) and merge Chart.yaml's templateOptions over them, mirroring template's ordering so CLI flags win over config defaults. Thread the merged sources into the template-render apply path (buildApplyRenderOptions). The direct-patch path does not render chart templates (FullConfigProcess applies the file as a patch onto a secrets-generated base and never calls loadValues), so value sources have nothing to render into there and are intentionally left off. Chart.yaml-declared value files are project-relative, so resolve them against the detected project root (as withSecrets and template paths already are) rather than the caller's CWD; CLI --values stay CWD-relative. Apply the same resolution in template so the two commands point at the same file. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 2881d54 commit 82b5ef6

6 files changed

Lines changed: 366 additions & 3 deletions

File tree

‎docs/manual-test-plan.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,31 @@ talm apply --dry-run \
391391

392392
Expected: each node renders / diffs independently; per-node gate output sections.
393393

394+
### C3a. Value files and `--set` honored at apply (consistent with template)
395+
396+
Add a value-consuming line to a template (e.g. `clusterDomain: {{ .Values.clusterDomain | quote }}` in `templates/controlplane.yaml`), then:
397+
398+
```bash
399+
# 1. A value supplied only via --set must apply, not just template.
400+
talm template -f nodes/node0.yaml --set clusterDomain=set.example | grep dnsDomain
401+
talm apply --dry-run -f nodes/node0.yaml --set clusterDomain=set.example
402+
# Expected: the dry-run drift shows dnsDomain -> set.example. Before this
403+
# change, apply re-rendered from the modeline and dropped --set entirely.
404+
405+
# 2. A Chart.yaml templateOptions.valueFiles entry must apply too.
406+
echo 'clusterDomain: fromfile.example' > extra-values.yaml
407+
# add `valueFiles: [extra-values.yaml]` under templateOptions in Chart.yaml
408+
talm apply --dry-run -f nodes/node0.yaml # expect dnsDomain -> fromfile.example
409+
410+
# 3. Chart.yaml-relative value files resolve against the project root,
411+
# not the caller's CWD — run apply from elsewhere with an absolute -f:
412+
cd /tmp && talm apply --dry-run -f "$PROJECT/nodes/node0.yaml"
413+
# Expected: still finds extra-values.yaml (joined with the project root).
414+
# A "failed to read values file" error here is the pre-fix regression.
415+
```
416+
417+
Regression anchor: `talm template` and `talm apply` must render the SAME config for the same value inputs (`--values` / `--set` / Chart.yaml `templateOptions.valueFiles`). A value that renders under `template` but is empty / `required`-fails under `apply` is the #221 blocker resurfacing.
418+
394419
### C4. Stage mode
395420

396421
```bash

‎pkg/commands/apply.go‎

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"io"
2121
"os"
2222
"path/filepath"
23+
"slices"
2324
"strings"
2425
"time"
2526

@@ -66,6 +67,12 @@ var applyCmdFlags struct {
6667
certFingerprints []string
6768
insecure bool
6869
configFiles []string // -f/--files
70+
valueFiles []string // --values
71+
stringValues []string // --set-string
72+
values []string // --set
73+
fileValues []string // --set-file
74+
jsonValues []string // --set-json
75+
literalValues []string // --set-literal
6976
talosVersion string
7077
withSecrets string
7178
debug bool
@@ -1007,7 +1014,7 @@ func renderMergeAndApply(ctx context.Context, c *client.Client, opts engine.Opti
10071014
func buildApplyRenderOptions(modelineTemplates []string, withSecretsPath string) engine.Options {
10081015
resolvedTemplates := resolveTemplatePaths(modelineTemplates, Config.RootDir)
10091016

1010-
return engine.Options{
1017+
opts := engine.Options{
10111018
TalosVersion: applyCmdFlags.talosVersion,
10121019
WithSecrets: withSecretsPath,
10131020
KubernetesVersion: applyCmdFlags.kubernetesVersion,
@@ -1018,9 +1025,18 @@ func buildApplyRenderOptions(modelineTemplates []string, withSecretsPath string)
10181025
CommandName: applyCommandName,
10191026
TalosEndpoints: append([]string(nil), GlobalArgs.Endpoints...),
10201027
}
1028+
setApplyValueOptions(&opts)
1029+
1030+
return opts
10211031
}
10221032

1023-
// buildApplyPatchOptions constructs engine.Options for the direct patch path.
1033+
// buildApplyPatchOptions constructs engine.Options for the direct-patch path
1034+
// (a non-modelined `-f` file). This path does NOT render chart templates — it
1035+
// generates the base config from secrets and applies the file as a patch via
1036+
// FullConfigProcess, which never calls loadValues. So value sources
1037+
// (--values / --set* / templateOptions.valueFiles) have nothing to render into
1038+
// and are intentionally not carried here; they apply only on the
1039+
// template-rendering path (buildApplyRenderOptions).
10241040
func buildApplyPatchOptions(withSecretsPath string) engine.Options {
10251041
return engine.Options{
10261042
TalosVersion: applyCmdFlags.talosVersion,
@@ -1030,6 +1046,22 @@ func buildApplyPatchOptions(withSecretsPath string) engine.Options {
10301046
}
10311047
}
10321048

1049+
// setApplyValueOptions populates the six value sources on opts by merging
1050+
// Chart.yaml's templateOptions.* (the base layer) with apply's CLI flags (the
1051+
// override layer). Ordering matches `talm template` (config first, CLI
1052+
// appended): the engine's loadValues applies sources left-to-right, so the CLI
1053+
// flags appended here win over the Chart.yaml defaults. Chart.yaml-declared
1054+
// value files are resolved against the project root; CLI --values paths stay
1055+
// CWD-relative.
1056+
func setApplyValueOptions(opts *engine.Options) {
1057+
opts.ValueFiles = append(resolveProjectValueFiles(Config.TemplateOptions.ValueFiles, Config.RootDir), applyCmdFlags.valueFiles...)
1058+
opts.Values = slices.Concat(Config.TemplateOptions.Values, applyCmdFlags.values)
1059+
opts.StringValues = slices.Concat(Config.TemplateOptions.StringValues, applyCmdFlags.stringValues)
1060+
opts.FileValues = slices.Concat(Config.TemplateOptions.FileValues, applyCmdFlags.fileValues)
1061+
opts.JsonValues = slices.Concat(Config.TemplateOptions.JsonValues, applyCmdFlags.jsonValues)
1062+
opts.LiteralValues = slices.Concat(Config.TemplateOptions.LiteralValues, applyCmdFlags.literalValues)
1063+
}
1064+
10331065
// wrapWithNodeContext wraps a client action function to resolve and inject node
10341066
// context. If GlobalArgs.Nodes is already set, uses those directly. Otherwise,
10351067
// attempts to resolve nodes from the client's config context.
@@ -1135,6 +1167,12 @@ func resolveTemplatePaths(templates []string, rootDir string) []string {
11351167
func init() {
11361168
applyCmd.Flags().BoolVarP(&applyCmdFlags.insecure, "insecure", "i", false, "apply using the insecure (encrypted with no auth) maintenance service")
11371169
applyCmd.Flags().StringSliceVarP(&applyCmdFlags.configFiles, "file", "f", nil, "node config files / patches (`.yaml` / `.yml`; shell completion narrows to these extensions). First -f is the modelined anchor (must live under a `talm init`'d project root); subsequent -f files are side-patches stacked onto the anchor's rendered config and may live anywhere.")
1170+
applyCmd.Flags().StringSliceVar(&applyCmdFlags.valueFiles, "values", []string{}, "specify values in a YAML file (can specify multiple). Must match `talm template` — apply re-renders from the modeline and would otherwise drop value files supplied at template time.")
1171+
applyCmd.Flags().StringArrayVar(&applyCmdFlags.values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2). For IP / CIDR / version literals use --set-string — dots in --set values are interpreted as YAML key nesting.")
1172+
applyCmd.Flags().StringArrayVar(&applyCmdFlags.stringValues, "set-string", []string{}, "set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2). Use for IP addresses, CIDR blocks, version strings, or any literal value where dots must NOT be interpreted as YAML key nesting.")
1173+
applyCmd.Flags().StringArrayVar(&applyCmdFlags.fileValues, "set-file", []string{}, "set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)")
1174+
applyCmd.Flags().StringArrayVar(&applyCmdFlags.jsonValues, "set-json", []string{}, "set JSON values on the command line (can specify multiple or separate values with commas: key1=jsonval1,key2=jsonval2)")
1175+
applyCmd.Flags().StringArrayVar(&applyCmdFlags.literalValues, "set-literal", []string{}, "set a literal STRING value on the command line")
11381176
applyCmd.Flags().StringVar(&applyCmdFlags.talosVersion, "talos-version", "", "the desired Talos version to generate config for (backwards compatibility, e.g. v0.8)")
11391177
applyCmd.Flags().StringVar(&applyCmdFlags.withSecrets, "with-secrets", "", "use a secrets file generated using 'gen secrets'")
11401178
applyCmd.Flags().StringVar(&applyCmdFlags.kubernetesVersion, "kubernetes-version", constants.DefaultKubernetesVersion, "desired kubernetes version to run")
@@ -1158,6 +1196,7 @@ func init() {
11581196
// in cobra's __complete path; wiring it would pin dead surface.
11591197
_ = applyCmd.RegisterFlagCompletionFunc("mode", completeApplyMode)
11601198
_ = applyCmd.RegisterFlagCompletionFunc("file", completeNodeFiles)
1199+
_ = applyCmd.RegisterFlagCompletionFunc("values", completeYAMLFiles)
11611200
_ = applyCmd.RegisterFlagCompletionFunc("with-secrets", completeYAMLFiles)
11621201

11631202
addCommand(applyCmd)
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Copyright Cozystack Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package commands
16+
17+
import (
18+
"slices"
19+
"testing"
20+
)
21+
22+
// applyValueFlagNames is the set of value-source flags `talm apply` must
23+
// expose so it renders identically to `talm template`. apply re-renders from
24+
// the modeline at apply time, so a value supplied only at template time would
25+
// otherwise be silently dropped (issue #221).
26+
//
27+
//nolint:gochecknoglobals // test fixture: shared flag-name list for the table tests below.
28+
var applyValueFlagNames = []string{"values", "set", "set-string", "set-file", "set-json", "set-literal"}
29+
30+
// TestApplyFlags_ValueSourcesRegistered pins that apply registers every value
31+
// source template has. Without these flags, the template↔apply value
32+
// inconsistency (#221) cannot be closed.
33+
func TestApplyFlags_ValueSourcesRegistered(t *testing.T) {
34+
for _, name := range applyValueFlagNames {
35+
if applyCmd.Flags().Lookup(name) == nil {
36+
t.Errorf("apply must register the --%s flag to stay consistent with `talm template`; got nil", name)
37+
}
38+
}
39+
}
40+
41+
// TestApplyFlags_ValueFlagTypesMatchTemplate pins that the apply value flags
42+
// use the same cobra value type as template. --values is a StringSlice;
43+
// --set* are StringArray (a StringSlice would split a --set value on commas
44+
// inside it, corrupting JSON / literal payloads). A type mismatch between the
45+
// two commands would parse the same CLI input differently.
46+
func TestApplyFlags_ValueFlagTypesMatchTemplate(t *testing.T) {
47+
for _, name := range applyValueFlagNames {
48+
applyFlag := applyCmd.Flags().Lookup(name)
49+
templateFlag := templateCmd.Flags().Lookup(name)
50+
51+
if applyFlag == nil || templateFlag == nil {
52+
t.Fatalf("both apply and template must register --%s", name)
53+
}
54+
55+
if applyFlag.Value.Type() != templateFlag.Value.Type() {
56+
t.Errorf("--%s type mismatch: apply=%s template=%s — the same CLI input would parse differently",
57+
name, applyFlag.Value.Type(), templateFlag.Value.Type())
58+
}
59+
}
60+
}
61+
62+
// TestSetApplyValueOptions_MergesConfigThenCLI pins the merge contract:
63+
// Chart.yaml templateOptions.* form the base layer and apply's CLI flags are
64+
// appended on top. The engine's loadValues applies sources left-to-right, so
65+
// the CLI flags (appended last) must win over the Chart.yaml defaults — the
66+
// same ordering `talm template` uses.
67+
func TestSetApplyValueOptions_MergesConfigThenCLI(t *testing.T) {
68+
restore := snapshotApplyValueState()
69+
defer restore()
70+
71+
Config.RootDir = testProjectRoot
72+
Config.TemplateOptions.Values = []string{"a=fromconfig"}
73+
Config.TemplateOptions.StringValues = []string{"s=fromconfig"}
74+
Config.TemplateOptions.FileValues = []string{"f=config.txt"}
75+
Config.TemplateOptions.JsonValues = []string{`j={"k":1}`}
76+
Config.TemplateOptions.LiteralValues = []string{"l=fromconfig"}
77+
78+
applyCmdFlags.values = []string{"a=fromcli"}
79+
applyCmdFlags.stringValues = []string{"s=fromcli"}
80+
applyCmdFlags.fileValues = []string{"f=cli.txt"}
81+
applyCmdFlags.jsonValues = []string{`j={"k":2}`}
82+
applyCmdFlags.literalValues = []string{"l=fromcli"}
83+
84+
opts := buildApplyRenderOptions([]string{testTemplateControlplaneRel}, testProjectRoot+"/secrets.yaml")
85+
86+
assertConfigThenCLI(t, "Values", opts.Values, "a=fromconfig", "a=fromcli")
87+
assertConfigThenCLI(t, "StringValues", opts.StringValues, "s=fromconfig", "s=fromcli")
88+
assertConfigThenCLI(t, "FileValues", opts.FileValues, "f=config.txt", "f=cli.txt")
89+
assertConfigThenCLI(t, "JsonValues", opts.JsonValues, `j={"k":1}`, `j={"k":2}`)
90+
assertConfigThenCLI(t, "LiteralValues", opts.LiteralValues, "l=fromconfig", "l=fromcli")
91+
}
92+
93+
// TestSetApplyValueOptions_ResolvesConfigValueFilesAgainstRoot pins that a
94+
// Chart.yaml-declared (relative) value file is anchored on the project root,
95+
// while a CLI --values path is left CWD-relative and appended after.
96+
func TestSetApplyValueOptions_ResolvesConfigValueFilesAgainstRoot(t *testing.T) {
97+
restore := snapshotApplyValueState()
98+
defer restore()
99+
100+
Config.RootDir = testProjectRoot
101+
Config.TemplateOptions.ValueFiles = []string{"values-secret.encrypted.yaml", "/abs/extra.yaml"}
102+
applyCmdFlags.valueFiles = []string{"cli-relative.yaml"}
103+
104+
opts := buildApplyRenderOptions([]string{testTemplateControlplaneRel}, testProjectRoot+"/secrets.yaml")
105+
106+
want := []string{
107+
testProjectRoot + "/values-secret.encrypted.yaml", // config-origin relative → joined with root
108+
"/abs/extra.yaml", // config-origin absolute → passthrough
109+
"cli-relative.yaml", // CLI-origin → CWD-relative, appended last
110+
}
111+
if !slices.Equal(opts.ValueFiles, want) {
112+
t.Errorf("ValueFiles resolution mismatch:\n got=%v\nwant=%v", opts.ValueFiles, want)
113+
}
114+
}
115+
116+
// TestBuildApplyPatchOptions_OmitsValueSources pins that the direct-patch path
117+
// (a non-modelined `-f` file) does NOT carry value sources. That path generates
118+
// the base config from secrets and applies the file as a patch via
119+
// FullConfigProcess — it never renders chart templates, so values have nothing
120+
// to render into. Threading them here would be dead config; values apply only
121+
// on the template-rendering path (buildApplyRenderOptions).
122+
func TestBuildApplyPatchOptions_OmitsValueSources(t *testing.T) {
123+
restore := snapshotApplyValueState()
124+
defer restore()
125+
126+
Config.RootDir = testProjectRoot
127+
Config.TemplateOptions.ValueFiles = []string{"values-secret.encrypted.yaml"}
128+
applyCmdFlags.values = []string{"x=1"}
129+
130+
opts := buildApplyPatchOptions(testProjectRoot + "/secrets.yaml")
131+
132+
if len(opts.ValueFiles) != 0 {
133+
t.Errorf("direct-patch path must not carry value files (no chart render); got %v", opts.ValueFiles)
134+
}
135+
if len(opts.Values) != 0 {
136+
t.Errorf("direct-patch path must not carry --set values (no chart render); got %v", opts.Values)
137+
}
138+
// The bundle-generation options it DOES need are still present.
139+
if opts.WithSecrets != testProjectRoot+"/secrets.yaml" {
140+
t.Errorf("direct-patch path must carry WithSecrets; got %q", opts.WithSecrets)
141+
}
142+
}
143+
144+
// snapshotApplyValueState saves and restores the package-level Config and
145+
// applyCmdFlags fields these tests mutate, so they do not leak into sibling
146+
// tests that share the same globals.
147+
func snapshotApplyValueState() func() {
148+
origRoot := Config.RootDir
149+
origTmpl := Config.TemplateOptions
150+
origFlags := applyCmdFlags
151+
152+
return func() {
153+
Config.RootDir = origRoot
154+
Config.TemplateOptions = origTmpl
155+
applyCmdFlags = origFlags
156+
}
157+
}
158+
159+
func assertConfigThenCLI(t *testing.T, field string, got []string, configVal, cliVal string) {
160+
t.Helper()
161+
162+
want := []string{configVal, cliVal}
163+
if !slices.Equal(got, want) {
164+
t.Errorf("%s must merge config-first then CLI (so CLI wins in loadValues):\n got=%v\nwant=%v", field, got, want)
165+
}
166+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright Cozystack Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package commands
16+
17+
import (
18+
"path/filepath"
19+
"slices"
20+
"testing"
21+
)
22+
23+
// TestResolveProjectValueFiles pins the resolution contract for Chart.yaml-
24+
// declared value files: relative entries are joined with the project root
25+
// (so they resolve identically from any CWD, like ResolveSecretsPath), and
26+
// absolute entries pass through unchanged. An empty input yields nil.
27+
func TestResolveProjectValueFiles(t *testing.T) {
28+
abs := filepath.Join(testProjectRoot, "abs.yaml")
29+
30+
cases := []struct {
31+
name string
32+
in []string
33+
root string
34+
want []string
35+
}{
36+
{name: "nil input", in: nil, root: testProjectRoot, want: nil},
37+
{name: "empty input", in: []string{}, root: testProjectRoot, want: nil},
38+
{
39+
name: "relative joined with root",
40+
in: []string{"values-secret.encrypted.yaml"},
41+
root: testProjectRoot,
42+
want: []string{filepath.Join(testProjectRoot, "values-secret.encrypted.yaml")},
43+
},
44+
{
45+
name: "absolute passthrough",
46+
in: []string{abs},
47+
root: testProjectRoot,
48+
want: []string{abs},
49+
},
50+
{
51+
name: "mixed relative and absolute",
52+
in: []string{"a.yaml", abs, "sub/b.yaml"},
53+
root: testProjectRoot,
54+
want: []string{filepath.Join(testProjectRoot, "a.yaml"), abs, filepath.Join(testProjectRoot, "sub/b.yaml")},
55+
},
56+
}
57+
58+
for _, tc := range cases {
59+
t.Run(tc.name, func(t *testing.T) {
60+
got := resolveProjectValueFiles(tc.in, tc.root)
61+
if !slices.Equal(got, tc.want) {
62+
t.Errorf("resolveProjectValueFiles(%v, %q) = %v, want %v", tc.in, tc.root, got, tc.want)
63+
}
64+
})
65+
}
66+
}
67+
68+
// TestTemplateAndApply_ResolveConfigValueFilesIdentically pins the cross-
69+
// command invariant: given the same Chart.yaml value files and project root,
70+
// `talm template` (via its PreRunE merge) and `talm apply` (via
71+
// setApplyValueOptions) must resolve the Chart.yaml-origin paths to the exact
72+
// same absolute form. If they diverged, the secret-sealing path (PR C) would
73+
// seal one file at template -I time and look for a different one at apply.
74+
func TestTemplateAndApply_ResolveConfigValueFilesIdentically(t *testing.T) {
75+
applyRestore := snapshotApplyValueState()
76+
defer applyRestore()
77+
78+
origTmplValueFiles := templateCmdFlags.valueFiles
79+
defer func() { templateCmdFlags.valueFiles = origTmplValueFiles }()
80+
81+
Config.RootDir = testProjectRoot
82+
Config.TemplateOptions.ValueFiles = []string{"values-secret.encrypted.yaml", "/abs/extra.yaml"}
83+
84+
// apply side: no CLI value files, so opts.ValueFiles is exactly the
85+
// resolved Chart.yaml set.
86+
applyCmdFlags.valueFiles = nil
87+
applyOpts := buildApplyRenderOptions([]string{testTemplateControlplaneRel}, testProjectRoot+"/secrets.yaml")
88+
89+
// template side: replicate the PreRunE merge with no CLI value files.
90+
templateCmdFlags.valueFiles = nil
91+
templateResolved := append(resolveProjectValueFiles(Config.TemplateOptions.ValueFiles, Config.RootDir), templateCmdFlags.valueFiles...)
92+
93+
if !slices.Equal(applyOpts.ValueFiles, templateResolved) {
94+
t.Errorf("template and apply must resolve Chart.yaml value files identically:\n apply=%v\ntmpl =%v", applyOpts.ValueFiles, templateResolved)
95+
}
96+
}

0 commit comments

Comments
 (0)