/
template_loader.go
351 lines (281 loc) · 11.2 KB
/
template_loader.go
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright 2024 The Carvel Authors.
// SPDX-License-Identifier: Apache-2.0
package workspace
import (
"fmt"
"strings"
"carvel.dev/ytt/pkg/cmd/ui"
"carvel.dev/ytt/pkg/files"
"carvel.dev/ytt/pkg/template"
"carvel.dev/ytt/pkg/texttemplate"
"carvel.dev/ytt/pkg/workspace/datavalues"
"carvel.dev/ytt/pkg/yamlmeta"
"carvel.dev/ytt/pkg/yamltemplate"
"carvel.dev/ytt/pkg/yttlibrary"
"github.com/k14s/starlark-go/starlark"
)
type TemplateLoader struct {
ui ui.UI
values *datavalues.Envelope
libraryValuess []*datavalues.Envelope
librarySchemas []*datavalues.SchemaEnvelope
opts TemplateLoaderOpts
compiledTemplates map[string]*template.CompiledTemplate
libraryExecFactory *LibraryExecutionFactory
}
// TemplateLoaderOpts holds configuration options that adjust how each individual template is executed/evaluated.
type TemplateLoaderOpts struct {
IgnoreUnknownComments bool
ImplicitMapKeyOverrides bool
StrictYAML bool
}
// TemplateLoaderOptsOverrides hold potential overriding values to be merged over a TemplateLoaderOpts.
// Only non-null values are merged.
type TemplateLoaderOptsOverrides struct {
IgnoreUnknownComments *bool
ImplicitMapKeyOverrides *bool
StrictYAML *bool
}
// NewTemplateLoader instantiates a new TemplateLoader.
func NewTemplateLoader(values *datavalues.Envelope, libraryValuess []*datavalues.Envelope, librarySchemas []*datavalues.SchemaEnvelope, opts TemplateLoaderOpts, libraryExecFactory *LibraryExecutionFactory, ui ui.UI) *TemplateLoader {
if values == nil {
panic("Expected values to be non-nil")
}
return &TemplateLoader{
ui: ui,
values: values,
libraryValuess: libraryValuess,
librarySchemas: librarySchemas,
opts: opts,
compiledTemplates: map[string]*template.CompiledTemplate{},
libraryExecFactory: libraryExecFactory,
}
}
// FindCompiledTemplate returns the template.CompiledTemplate that was produced from the file at "path".
//
// If no such template exists, returns nil.
func (l *TemplateLoader) FindCompiledTemplate(path string) *template.CompiledTemplate {
return l.compiledTemplates[path]
}
func (l *TemplateLoader) Load(thread *starlark.Thread, module string) (starlark.StringDict, error) {
libraryCtx := LibraryExecutionContext{
Current: l.getCurrentLibrary(thread),
Root: l.getRootLibrary(thread),
}
filePath := module
if strings.HasPrefix(module, "@") {
pieces := strings.SplitN(module[1:], ":", 2)
if len(pieces) != 2 {
return nil, fmt.Errorf("Expected library path to be in format '@name:path' " +
"e.g. '@github.com/vmware-tanzu/test:test.star' or '@ytt:base64'")
}
if pieces[0] == "ytt" {
return l.getYTTLibrary(thread).FindModule(pieces[1])
}
foundLib, err := libraryCtx.Current.FindAccessibleLibrary(pieces[0])
if err != nil {
return nil, err
}
libraryCtx = LibraryExecutionContext{Current: foundLib, Root: foundLib}
filePath = pieces[1]
}
var libraryWithFile *Library = libraryCtx.Current
// If path starts from a root, then it should be relative to root library
if files.IsRootPath(filePath) {
libraryWithFile = libraryCtx.Root
filePath = files.StripRootPath(filePath)
}
fileInLib, err := libraryWithFile.FindFile(filePath)
if err != nil {
return nil, err
}
// File might be inside nested libraries, make sure to update current library
libraryCtx = LibraryExecutionContext{Current: fileInLib.Library, Root: libraryCtx.Root}
file := fileInLib.File
if !file.IsLibrary() {
return nil, fmt.Errorf("Expected file '%s' to be a library file, but was not "+
"(hint: library filename must end with '.lib.yml' or '.star'; use data.read(...) for loading non-templated file contents)", file.RelativePath())
}
switch file.Type() {
case files.TypeYAML:
globals, _, err := l.EvalYAML(libraryCtx, file)
return globals, err
case files.TypeStarlark:
return l.EvalStarlark(libraryCtx, file)
case files.TypeText:
globals, _, err := l.EvalText(libraryCtx, file)
return globals, err
default:
return nil, fmt.Errorf("File '%s' type is not a known", file.RelativePath())
}
}
func (l *TemplateLoader) EvalPlainYAML(file *files.File) (*yamlmeta.DocumentSet, error) {
fileBs, err := file.Bytes()
if err != nil {
return nil, err
}
docSetOpts := yamlmeta.DocSetOpts{
AssociatedName: file.RelativePath(),
WithoutComments: !file.IsTemplate() && !file.IsLibrary(),
Strict: l.opts.StrictYAML,
}
l.ui.Debugf("## file %s (opts %#v)\n", file.RelativePath(), docSetOpts)
docSet, err := yamlmeta.NewDocumentSetFromBytes(fileBs, docSetOpts)
if err != nil {
return nil, fmt.Errorf("Unmarshaling YAML template '%s': %s", file.RelativePath(), err)
}
return docSet, nil
}
// EvalYAML parses the file into a Node AST, then compiles that Node tree into a starlark program.
//
// Returns the templated file by evaluating the compiled starlark program.
func (l *TemplateLoader) EvalYAML(libraryCtx LibraryExecutionContext, file *files.File) (starlark.StringDict, *yamlmeta.DocumentSet, error) {
docSet, err := l.EvalPlainYAML(file)
if err != nil {
return nil, nil, err
}
l.ui.Debugf("### ast ")
// is this plain YAML?
if !file.IsTemplate() && !file.IsLibrary() || !yamltemplate.HasTemplating(docSet) {
// YAML spec requires map keys to be unique.
// Tools retain just the last instance: each subsequent map item overrides the value of any previous.
docSet.OverrideMapKeys()
l.ui.Debugf("(plain)\n")
docSet.Print(l.ui.DebugWriter())
return nil, docSet, nil
}
l.ui.Debugf("(templated)\n")
docSet.Print(l.ui.DebugWriter())
tplOpts := yamltemplate.TemplateOpts{
IgnoreUnknownComments: l.opts.IgnoreUnknownComments,
ImplicitMapKeyOverrides: l.opts.ImplicitMapKeyOverrides,
}
compiledTemplate, err := yamltemplate.NewTemplate(file.RelativePath(), tplOpts).Compile(docSet)
if err != nil {
return nil, nil, fmt.Errorf("Compiling YAML template '%s': %s", file.RelativePath(), err)
}
l.addCompiledTemplate(file.RelativePath(), compiledTemplate)
l.ui.Debugf("### template\n%s", compiledTemplate.DebugCodeAsString())
yttLibrary := yttlibrary.NewAPI(compiledTemplate.TplReplaceNode,
yttlibrary.NewDataModule(l.values.Doc, DataLoader{libraryCtx}),
NewLibraryModule(libraryCtx, l.libraryExecFactory, l.libraryValuess, l.librarySchemas).AsModule(), l.ui)
thread := l.newThread(libraryCtx, yttLibrary, file)
globals, resultVal, err := compiledTemplate.Eval(thread, l)
if err != nil {
return nil, nil, err
}
return globals, resultVal.(*yamlmeta.DocumentSet), nil
}
func (l *TemplateLoader) EvalText(libraryCtx LibraryExecutionContext, file *files.File) (starlark.StringDict, *texttemplate.NodeRoot, error) {
fileBs, err := file.Bytes()
if err != nil {
return nil, nil, err
}
l.ui.Debugf("## file %s\n", file.RelativePath())
if !file.IsTemplate() && !file.IsLibrary() {
plainRootNode := &texttemplate.NodeRoot{
Items: []interface{}{&texttemplate.NodeText{Content: string(fileBs)}},
}
return nil, plainRootNode, nil
}
textRoot, err := texttemplate.NewParser().Parse(fileBs, file.RelativePath())
if err != nil {
return nil, nil, fmt.Errorf("Parsing text template '%s': %s", file.RelativePath(), err)
}
compiledTemplate, err := texttemplate.NewTemplate(file.RelativePath()).Compile(textRoot)
if err != nil {
return nil, nil, fmt.Errorf("Compiling text template '%s': %s", file.RelativePath(), err)
}
l.addCompiledTemplate(file.RelativePath(), compiledTemplate)
l.ui.Debugf("### template\n%s", compiledTemplate.DebugCodeAsString())
yttLibrary := yttlibrary.NewAPI(compiledTemplate.TplReplaceNode,
yttlibrary.NewDataModule(l.values.Doc, DataLoader{libraryCtx}),
NewLibraryModule(libraryCtx, l.libraryExecFactory, l.libraryValuess, l.librarySchemas).AsModule(), l.ui)
thread := l.newThread(libraryCtx, yttLibrary, file)
globals, resultVal, err := compiledTemplate.Eval(thread, l)
if err != nil {
return nil, nil, fmt.Errorf("Evaluating text template: %s", err)
}
return globals, resultVal.(*texttemplate.NodeRoot), nil
}
func (l *TemplateLoader) EvalStarlark(libraryCtx LibraryExecutionContext, file *files.File) (starlark.StringDict, error) {
fileBs, err := file.Bytes()
if err != nil {
return nil, err
}
l.ui.Debugf("## file %s\n", file.RelativePath())
instructions := template.NewInstructionSet()
compiledTemplate := template.NewCompiledTemplate(
file.RelativePath(), template.NewCodeFromBytes(fileBs, instructions),
instructions, template.NewNodes(), template.EvaluationCtxDialects{})
l.addCompiledTemplate(file.RelativePath(), compiledTemplate)
l.ui.Debugf("### template\n%s", compiledTemplate.DebugCodeAsString())
yttLibrary := yttlibrary.NewAPI(compiledTemplate.TplReplaceNode,
yttlibrary.NewDataModule(l.values.Doc, DataLoader{libraryCtx}),
NewLibraryModule(libraryCtx, l.libraryExecFactory, l.libraryValuess, l.librarySchemas).AsModule(), l.ui)
thread := l.newThread(libraryCtx, yttLibrary, file)
globals, _, err := compiledTemplate.Eval(thread, l)
if err != nil {
return nil, fmt.Errorf("Evaluating starlark template: %s", err)
}
return globals, nil
}
const (
threadCurrentLibraryKey = "ytt.curr_library_key"
threadRootLibraryKey = "ytt.root_library_key"
threadYTTLibraryKey = "ytt.ytt_library_key"
)
func (l *TemplateLoader) getCurrentLibrary(thread *starlark.Thread) *Library {
lib, ok := thread.Local(threadCurrentLibraryKey).(*Library)
if !ok || lib == nil {
panic("Expected to find library associated with thread")
}
return lib
}
func (l *TemplateLoader) setCurrentLibrary(thread *starlark.Thread, library *Library) {
thread.SetLocal(threadCurrentLibraryKey, library)
}
func (l *TemplateLoader) getRootLibrary(thread *starlark.Thread) *Library {
lib, ok := thread.Local(threadRootLibraryKey).(*Library)
if !ok || lib == nil {
panic("Expected to find root library associated with thread")
}
return lib
}
func (l *TemplateLoader) setRootLibrary(thread *starlark.Thread, library *Library) {
thread.SetLocal(threadRootLibraryKey, library)
}
func (l *TemplateLoader) getYTTLibrary(thread *starlark.Thread) yttlibrary.API {
yttLibrary, ok := thread.Local(threadYTTLibraryKey).(yttlibrary.API)
if !ok {
panic("Expected to find YTT library associated with thread")
}
return yttLibrary
}
func (l *TemplateLoader) setYTTLibrary(thread *starlark.Thread, yttLibrary yttlibrary.API) {
thread.SetLocal(threadYTTLibraryKey, yttLibrary)
}
func (l *TemplateLoader) newThread(libraryCtx LibraryExecutionContext,
yttLibrary yttlibrary.API, file *files.File) *starlark.Thread {
thread := &starlark.Thread{Name: "template=" + file.RelativePath(), Load: l.Load}
l.setCurrentLibrary(thread, libraryCtx.Current)
l.setRootLibrary(thread, libraryCtx.Root)
l.setYTTLibrary(thread, yttLibrary)
return thread
}
func (l *TemplateLoader) addCompiledTemplate(path string, ct *template.CompiledTemplate) {
l.compiledTemplates[path] = ct
}
func (opts TemplateLoaderOpts) Merge(overrides TemplateLoaderOptsOverrides) TemplateLoaderOpts {
optsCopy := opts
if overrides.IgnoreUnknownComments != nil {
optsCopy.IgnoreUnknownComments = *overrides.IgnoreUnknownComments
}
if overrides.ImplicitMapKeyOverrides != nil {
optsCopy.ImplicitMapKeyOverrides = *overrides.ImplicitMapKeyOverrides
}
if overrides.StrictYAML != nil {
optsCopy.StrictYAML = *overrides.StrictYAML
}
return optsCopy
}