-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathapply.go
More file actions
325 lines (290 loc) · 11.8 KB
/
apply.go
File metadata and controls
325 lines (290 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright Cozystack Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package commands
import (
"context"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/cozystack/talm/pkg/engine"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/durationpb"
"github.com/siderolabs/talos/cmd/talosctl/pkg/talos/helpers"
machineapi "github.com/siderolabs/talos/pkg/machinery/api/machine"
"github.com/siderolabs/talos/pkg/machinery/client"
"github.com/siderolabs/talos/pkg/machinery/constants"
)
var applyCmdFlags struct {
helpers.Mode
certFingerprints []string
insecure bool
configFiles []string // -f/--files
talosVersion string
withSecrets string
debug bool
kubernetesVersion string
dryRun bool
preserve bool
stage bool
force bool
configTryTimeout time.Duration
nodesFromArgs bool
endpointsFromArgs bool
}
var applyCmd = &cobra.Command{
Use: "apply",
Short: "Apply config to a Talos node",
Long: ``,
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Flags().Changed("talos-version") {
applyCmdFlags.talosVersion = Config.TemplateOptions.TalosVersion
}
if !cmd.Flags().Changed("with-secrets") {
applyCmdFlags.withSecrets = Config.TemplateOptions.WithSecrets
}
if !cmd.Flags().Changed("kubernetes-version") {
applyCmdFlags.kubernetesVersion = Config.TemplateOptions.KubernetesVersion
}
if !cmd.Flags().Changed("debug") {
applyCmdFlags.debug = Config.TemplateOptions.Debug
}
if !cmd.Flags().Changed("preserve") {
applyCmdFlags.preserve = Config.UpgradeOptions.Preserve
}
if !cmd.Flags().Changed("stage") {
applyCmdFlags.stage = Config.UpgradeOptions.Stage
}
if !cmd.Flags().Changed("force") {
applyCmdFlags.force = Config.UpgradeOptions.Force
}
applyCmdFlags.nodesFromArgs = len(GlobalArgs.Nodes) > 0
applyCmdFlags.endpointsFromArgs = len(GlobalArgs.Endpoints) > 0
// Set dummy endpoint to avoid errors on building client
if len(GlobalArgs.Endpoints) == 0 {
GlobalArgs.Endpoints = append(GlobalArgs.Endpoints, "127.0.0.1")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return apply(args)
},
}
func apply(args []string) error {
ctx := context.Background()
// Expand directories to YAML files
expandedFiles, err := ExpandFilePaths(applyCmdFlags.configFiles)
if err != nil {
return err
}
// Detect root from files if specified, otherwise fallback to cwd
if err := DetectAndSetRootFromFiles(expandedFiles); err != nil {
return err
}
for _, configFile := range expandedFiles {
modelineTemplates, err := processModelineAndUpdateGlobals(configFile, applyCmdFlags.nodesFromArgs, applyCmdFlags.endpointsFromArgs, true)
if err != nil {
return err
}
// Resolve secrets.yaml path relative to project root if not absolute
withSecretsPath := ResolveSecretsPath(applyCmdFlags.withSecrets)
if len(modelineTemplates) > 0 {
// Template rendering path: connect to the node first, render templates
// online (so lookup() functions resolve real discovery data), then apply.
opts := buildApplyRenderOptions(modelineTemplates, withSecretsPath)
err = withApplyClient(func(ctx context.Context, c *client.Client) error {
fmt.Printf("- talm: file=%s, nodes=%s, endpoints=%s\n", configFile, GlobalArgs.Nodes, GlobalArgs.Endpoints)
result, err := engine.Render(ctx, c, opts)
if err != nil {
return fmt.Errorf("template rendering error: %w", err)
}
resp, err := c.ApplyConfiguration(ctx, &machineapi.ApplyConfigurationRequest{
Data: result,
Mode: applyCmdFlags.Mode.Mode,
DryRun: applyCmdFlags.dryRun,
TryModeTimeout: durationpb.New(applyCmdFlags.configTryTimeout),
})
if err != nil {
return fmt.Errorf("error applying new configuration: %s", err)
}
helpers.PrintApplyResults(resp)
return nil
})
} else {
// Direct patch path: apply config file as patch against empty bundle
opts := buildApplyPatchOptions(withSecretsPath)
patches := []string{"@" + configFile}
configBundle, machineType, err := engine.FullConfigProcess(ctx, opts, patches)
if err != nil {
return fmt.Errorf("full config processing error: %w", err)
}
result, err := engine.SerializeConfiguration(configBundle, machineType)
if err != nil {
return fmt.Errorf("error serializing configuration: %w", err)
}
err = withApplyClient(func(ctx context.Context, c *client.Client) error {
fmt.Printf("- talm: file=%s, nodes=%s, endpoints=%s\n", configFile, GlobalArgs.Nodes, GlobalArgs.Endpoints)
resp, err := c.ApplyConfiguration(ctx, &machineapi.ApplyConfigurationRequest{
Data: result,
Mode: applyCmdFlags.Mode.Mode,
DryRun: applyCmdFlags.dryRun,
TryModeTimeout: durationpb.New(applyCmdFlags.configTryTimeout),
})
if err != nil {
return fmt.Errorf("error applying new configuration: %s", err)
}
helpers.PrintApplyResults(resp)
return nil
})
}
if err != nil {
return err
}
// Reset args
if !applyCmdFlags.nodesFromArgs {
GlobalArgs.Nodes = []string{}
}
if !applyCmdFlags.endpointsFromArgs {
GlobalArgs.Endpoints = []string{}
}
}
return nil
}
// withApplyClient creates a Talos client appropriate for the current apply mode
// and invokes the given action with it.
func withApplyClient(f func(ctx context.Context, c *client.Client) error) error {
if applyCmdFlags.insecure {
// Maintenance mode connects directly to the node IP without talosconfig;
// node context injection is not needed — the maintenance client handles
// node targeting internally via GlobalArgs.Nodes.
return WithClientMaintenance(applyCmdFlags.certFingerprints, f)
}
wrappedF := wrapWithNodeContext(f)
if GlobalArgs.SkipVerify {
return WithClientSkipVerify(wrappedF)
}
return WithClientNoNodes(wrappedF)
}
// buildApplyRenderOptions constructs engine.Options for the template rendering path.
// Offline is false because templates need a live Talos client for lookup() functions
// (e.g., discovering interface names, addresses, routes). The caller creates the
// client and passes it to engine.Render together with these options.
func buildApplyRenderOptions(modelineTemplates []string, withSecretsPath string) engine.Options {
resolvedTemplates := resolveTemplatePaths(modelineTemplates, Config.RootDir)
return engine.Options{
Insecure: applyCmdFlags.insecure,
TalosVersion: applyCmdFlags.talosVersion,
WithSecrets: withSecretsPath,
KubernetesVersion: applyCmdFlags.kubernetesVersion,
Debug: applyCmdFlags.debug,
Full: true,
Root: Config.RootDir,
TemplateFiles: resolvedTemplates,
}
}
// buildApplyPatchOptions constructs engine.Options for the direct patch path.
func buildApplyPatchOptions(withSecretsPath string) engine.Options {
return engine.Options{
TalosVersion: applyCmdFlags.talosVersion,
WithSecrets: withSecretsPath,
KubernetesVersion: applyCmdFlags.kubernetesVersion,
Debug: applyCmdFlags.debug,
}
}
// wrapWithNodeContext wraps a client action function to resolve and inject node
// context. If GlobalArgs.Nodes is already set, uses those directly. Otherwise,
// attempts to resolve nodes from the client's config context.
// This function does not mutate GlobalArgs. It reads GlobalArgs.Nodes at
// invocation time (not at wrapper creation time) and makes a defensive copy.
func wrapWithNodeContext(f func(ctx context.Context, c *client.Client) error) func(ctx context.Context, c *client.Client) error {
return func(ctx context.Context, c *client.Client) error {
nodes := append([]string(nil), GlobalArgs.Nodes...)
if len(nodes) < 1 {
if c == nil {
return fmt.Errorf("failed to resolve config context: no client available")
}
configContext := c.GetConfigContext()
if configContext == nil {
return fmt.Errorf("failed to resolve config context")
}
nodes = configContext.Nodes
}
ctx = client.WithNodes(ctx, nodes...)
return f(ctx, c)
}
}
// resolveTemplatePaths resolves template file paths relative to the project root,
// normalizing them for the Helm engine (forward slashes).
// Relative paths from the modeline are resolved against rootDir, not CWD.
//
// Note: template.go has similar path resolution in generateOutput() but resolves
// against CWD via filepath.Abs and has an additional fallback that tries
// templates/<basename> when a path resolves outside the root. This function
// intentionally resolves against rootDir (modeline paths are root-relative by
// convention) and does not perform the basename fallback to avoid silently
// substituting a different file.
func resolveTemplatePaths(templates []string, rootDir string) []string {
resolved := make([]string, len(templates))
if rootDir == "" {
// No rootDir specified — normalize paths only, don't resolve against CWD
for i, p := range templates {
resolved[i] = engine.NormalizeTemplatePath(p)
}
return resolved
}
absRootDir, rootErr := filepath.Abs(rootDir)
if rootErr != nil {
for i, p := range templates {
resolved[i] = engine.NormalizeTemplatePath(p)
}
return resolved
}
for i, templatePath := range templates {
var absTemplatePath string
if filepath.IsAbs(templatePath) {
absTemplatePath = templatePath
} else {
// Resolve relative paths against rootDir, not CWD
absTemplatePath = filepath.Join(absRootDir, templatePath)
}
relPath, relErr := filepath.Rel(absRootDir, absTemplatePath)
if relErr != nil {
resolved[i] = engine.NormalizeTemplatePath(templatePath)
continue
}
relPath = filepath.Clean(relPath)
if strings.HasPrefix(relPath, "..") {
// Path goes outside project root — use original path as-is
resolved[i] = engine.NormalizeTemplatePath(templatePath)
continue
}
resolved[i] = engine.NormalizeTemplatePath(relPath)
}
return resolved
}
func init() {
applyCmd.Flags().BoolVarP(&applyCmdFlags.insecure, "insecure", "i", false, "apply using the insecure (encrypted with no auth) maintenance service")
applyCmd.Flags().StringSliceVarP(&applyCmdFlags.configFiles, "file", "f", nil, "specify config files or patches in a YAML file (can specify multiple)")
applyCmd.Flags().StringVar(&applyCmdFlags.talosVersion, "talos-version", "", "the desired Talos version to generate config for (backwards compatibility, e.g. v0.8)")
applyCmd.Flags().StringVar(&applyCmdFlags.withSecrets, "with-secrets", "", "use a secrets file generated using 'gen secrets'")
applyCmd.Flags().StringVar(&applyCmdFlags.kubernetesVersion, "kubernetes-version", constants.DefaultKubernetesVersion, "desired kubernetes version to run")
applyCmd.Flags().BoolVarP(&applyCmdFlags.debug, "debug", "", false, "show only rendered patches")
applyCmd.Flags().BoolVar(&applyCmdFlags.dryRun, "dry-run", false, "check how the config change will be applied in dry-run mode")
applyCmd.Flags().DurationVar(&applyCmdFlags.configTryTimeout, "timeout", constants.ConfigTryTimeout, "the config will be rolled back after specified timeout (if try mode is selected)")
applyCmd.Flags().StringSliceVar(&applyCmdFlags.certFingerprints, "cert-fingerprint", nil, "list of server certificate fingeprints to accept (defaults to no check)")
applyCmd.Flags().BoolVar(&applyCmdFlags.force, "force", false, "will overwrite existing files")
helpers.AddModeFlags(&applyCmdFlags.Mode, applyCmd)
addCommand(applyCmd)
}