Skip to content

Commit d82aa2a

Browse files
Fixed apply rendering logic
Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent 03e9b6e commit d82aa2a

2 files changed

Lines changed: 65 additions & 44 deletions

File tree

‎pkg/commands/apply.go‎

Lines changed: 63 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,33 @@ func apply(args []string) error {
112112
// Resolve secrets.yaml path relative to project root if not absolute
113113
withSecretsPath := ResolveSecretsPath(applyCmdFlags.withSecrets)
114114

115-
var result []byte
116115
if len(modelineTemplates) > 0 {
117-
// Template rendering path: render templates via Helm engine to produce full config
116+
// Template rendering path: connect to the node first, render templates
117+
// online (so lookup() functions resolve real discovery data), then apply.
118118
opts := buildApplyRenderOptions(modelineTemplates, withSecretsPath)
119-
result, err = engine.Render(ctx, nil, opts)
120-
if err != nil {
121-
return fmt.Errorf("template rendering error: %w", err)
122-
}
119+
120+
err = withApplyClient(func(ctx context.Context, c *client.Client) error {
121+
fmt.Printf("- talm: file=%s, nodes=%s, endpoints=%s\n", configFile, GlobalArgs.Nodes, GlobalArgs.Endpoints)
122+
123+
result, err := engine.Render(ctx, c, opts)
124+
if err != nil {
125+
return fmt.Errorf("template rendering error: %w", err)
126+
}
127+
128+
resp, err := c.ApplyConfiguration(ctx, &machineapi.ApplyConfigurationRequest{
129+
Data: result,
130+
Mode: applyCmdFlags.Mode.Mode,
131+
DryRun: applyCmdFlags.dryRun,
132+
TryModeTimeout: durationpb.New(applyCmdFlags.configTryTimeout),
133+
})
134+
if err != nil {
135+
return fmt.Errorf("error applying new configuration: %s", err)
136+
}
137+
138+
helpers.PrintApplyResults(resp)
139+
140+
return nil
141+
})
123142
} else {
124143
// Direct patch path: apply config file as patch against empty bundle
125144
opts := buildApplyPatchOptions(withSecretsPath)
@@ -129,46 +148,29 @@ func apply(args []string) error {
129148
return fmt.Errorf("full config processing error: %w", err)
130149
}
131150

132-
result, err = engine.SerializeConfiguration(configBundle, machineType)
151+
result, err := engine.SerializeConfiguration(configBundle, machineType)
133152
if err != nil {
134153
return fmt.Errorf("error serializing configuration: %w", err)
135154
}
136-
}
137-
138-
withClient := func(f func(ctx context.Context, c *client.Client) error) error {
139-
if applyCmdFlags.insecure {
140-
// Maintenance mode connects directly to the node IP without talosconfig;
141-
// node context injection is not needed — the maintenance client handles
142-
// node targeting internally via GlobalArgs.Nodes.
143-
return WithClientMaintenance(applyCmdFlags.certFingerprints, f)
144-
}
145-
146-
wrappedF := wrapWithNodeContext(f)
147155

148-
if GlobalArgs.SkipVerify {
149-
return WithClientSkipVerify(wrappedF)
150-
}
156+
err = withApplyClient(func(ctx context.Context, c *client.Client) error {
157+
fmt.Printf("- talm: file=%s, nodes=%s, endpoints=%s\n", configFile, GlobalArgs.Nodes, GlobalArgs.Endpoints)
151158

152-
return WithClientNoNodes(wrappedF)
153-
}
159+
resp, err := c.ApplyConfiguration(ctx, &machineapi.ApplyConfigurationRequest{
160+
Data: result,
161+
Mode: applyCmdFlags.Mode.Mode,
162+
DryRun: applyCmdFlags.dryRun,
163+
TryModeTimeout: durationpb.New(applyCmdFlags.configTryTimeout),
164+
})
165+
if err != nil {
166+
return fmt.Errorf("error applying new configuration: %s", err)
167+
}
154168

155-
err = withClient(func(ctx context.Context, c *client.Client) error {
156-
fmt.Printf("- talm: file=%s, nodes=%s, endpoints=%s\n", configFile, GlobalArgs.Nodes, GlobalArgs.Endpoints)
169+
helpers.PrintApplyResults(resp)
157170

158-
resp, err := c.ApplyConfiguration(ctx, &machineapi.ApplyConfigurationRequest{
159-
Data: result,
160-
Mode: applyCmdFlags.Mode.Mode,
161-
DryRun: applyCmdFlags.dryRun,
162-
TryModeTimeout: durationpb.New(applyCmdFlags.configTryTimeout),
171+
return nil
163172
})
164-
if err != nil {
165-
return fmt.Errorf("error applying new configuration: %s", err)
166-
}
167-
168-
helpers.PrintApplyResults(resp)
169-
170-
return nil
171-
})
173+
}
172174
if err != nil {
173175
return err
174176
}
@@ -184,19 +186,38 @@ func apply(args []string) error {
184186
return nil
185187
}
186188

189+
// withApplyClient creates a Talos client appropriate for the current apply mode
190+
// and invokes the given action with it.
191+
func withApplyClient(f func(ctx context.Context, c *client.Client) error) error {
192+
if applyCmdFlags.insecure {
193+
// Maintenance mode connects directly to the node IP without talosconfig;
194+
// node context injection is not needed — the maintenance client handles
195+
// node targeting internally via GlobalArgs.Nodes.
196+
return WithClientMaintenance(applyCmdFlags.certFingerprints, f)
197+
}
198+
199+
wrappedF := wrapWithNodeContext(f)
200+
201+
if GlobalArgs.SkipVerify {
202+
return WithClientSkipVerify(wrappedF)
203+
}
204+
205+
return WithClientNoNodes(wrappedF)
206+
}
207+
187208
// buildApplyRenderOptions constructs engine.Options for the template rendering path.
188-
// Offline is set to true because at this point we don't have a Talos client for
189-
// Helm lookup functions. Templates that use lookup() should be rendered via
190-
// 'talm template' which supports online mode.
209+
// Offline is false because templates need a live Talos client for lookup() functions
210+
// (e.g., discovering interface names, addresses, routes). The caller creates the
211+
// client and passes it to engine.Render together with these options.
191212
func buildApplyRenderOptions(modelineTemplates []string, withSecretsPath string) engine.Options {
192213
resolvedTemplates := resolveTemplatePaths(modelineTemplates, Config.RootDir)
193214
return engine.Options{
215+
Insecure: applyCmdFlags.insecure,
194216
TalosVersion: applyCmdFlags.talosVersion,
195217
WithSecrets: withSecretsPath,
196218
KubernetesVersion: applyCmdFlags.kubernetesVersion,
197219
Debug: applyCmdFlags.debug,
198220
Full: true,
199-
Offline: true,
200221
Root: Config.RootDir,
201222
TemplateFiles: resolvedTemplates,
202223
}

‎pkg/commands/apply_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func TestBuildApplyRenderOptions(t *testing.T) {
3636
if !opts.Full {
3737
t.Error("expected Full=true for template rendering path")
3838
}
39-
if !opts.Offline {
40-
t.Error("expected Offline=true for template rendering path")
39+
if opts.Offline {
40+
t.Error("expected Offline=false for online template rendering path")
4141
}
4242
if opts.Root != "/project" {
4343
t.Errorf("expected Root=/project, got %s", opts.Root)

0 commit comments

Comments
 (0)