@@ -21,6 +21,7 @@ import (
21
21
"io/ioutil"
22
22
"os"
23
23
"regexp"
24
+ "strings"
24
25
"text/template"
25
26
26
27
"github.com/imdario/mergo"
@@ -218,7 +219,7 @@ func (a *App) render(configFile string) (*string, *string, *[]byte, error) {
218
219
// Self-render the main.yaml with gomplate functions and datasources
219
220
// This does not apply to the app.yaml files.
220
221
contents := string (data )
221
- renderedContents , err := selfRender (contents , true )
222
+ renderedContents , err := selfRender (contents )
222
223
if err != nil {
223
224
return nil , nil , nil , fmt .Errorf ("Failed to selfRender configFile %v: %v" , configFile , err )
224
225
}
@@ -241,7 +242,6 @@ func (a *App) render(configFile string) (*string, *string, *[]byte, error) {
241
242
// combining literal with app.yaml
242
243
data = append (data , appData ... )
243
244
244
-
245
245
// Fakechart to send to helm rendering engine
246
246
fakeChart := & chart.Chart {
247
247
Metadata : & chart.Metadata {
@@ -294,64 +294,70 @@ func (a *App) render(configFile string) (*string, *string, *[]byte, error) {
294
294
return & chart , & chartVersion , & overrides , nil
295
295
}
296
296
297
-
298
- func selfRender (templateValuesStr string , enableGomplate bool ) (string , error ) {
297
+ func selfRender (templateValuesStr string ) (string , error ) {
299
298
/*
300
- This function will accept an input string and run it through the
301
- templating engine as both the values dictionary as well as the
302
- template string. It will repeat the process until the templating
303
- result stops changing.
299
+ This function will accept an input string and run it through the
300
+ templating engine as both the values dictionary as well as the
301
+ template string. It will repeat the process until the templating
302
+ result stops changing.
304
303
*/
305
304
306
- type Values map [string ]interface {}
305
+ type gomplateConfig struct {
306
+ Gomplate gomplate.Config `yaml:"gomplate,omitempty"`
307
+ }
308
+ var gomp gomplateConfig
309
+ err := yaml .Unmarshal ([]byte (templateValuesStr ), & gomp )
310
+ if err != nil {
311
+ return "" , err
312
+ }
313
+
314
+ funcs := template.FuncMap {
315
+ //Add case insensitive handling as marshalled inline structures are public by default, but may be lowercase templates
316
+ "MyEq" : strings .EqualFold ,
317
+ }
318
+ if len (gomp .Gomplate .DataSources ) > 0 {
319
+ d , err := data .NewData (gomp .Gomplate .DataSources , gomp .Gomplate .DataSourceHeaders )
320
+ if err != nil {
321
+ return "" , err
322
+ }
323
+ for k , v := range gomplate .Funcs (d ) {
324
+ funcs [k ] = v
325
+ }
326
+ }
327
+ tmpl := template .New ("SelfTemplate" ).
328
+ Delims ("[[" , "]]" ).
329
+ Option ("missingkey=error" ).
330
+ Funcs (funcs )
307
331
308
332
lastRender := templateValuesStr
309
333
for i := 0 ; i < 10 ; i ++ {
310
334
311
335
// Unmarshal the file as a values dict
312
- vals := Values {}
313
- err := yaml .Unmarshal ([]byte (templateValuesStr ), & vals )
314
- if err != nil { return "" , err }
315
-
316
- tmpl := template .New ("SelfTemplate" )
317
- tmpl .Delims ("[[" , "]]" )
318
-
319
- if enableGomplate {
320
- // Read the defined gomplate datasources and
321
- // datasourcehaders from the values dict
322
- var dataSources []string
323
- var dataSourceHeaders []string
324
- var gompData gomplate.Config
325
- yaml .Unmarshal ([]byte (templateValuesStr ), & gompData )
326
- dataSources = gompData .DataSources
327
- dataSourceHeaders = gompData .DataSourceHeaders
328
-
329
- // Access gomplate datasources and function library
330
- d , err := data .NewData (dataSources , dataSourceHeaders )
331
- if err != nil { return "" , err }
332
-
333
- // Configure go/text/template to use gomplate
334
- // datasources and function library.
335
- tmpl .Option ("missingkey=error" )
336
- tmpl .Funcs (gomplate .Funcs (d ))
336
+ vals := map [string ]interface {}{}
337
+ err := yaml .Unmarshal ([]byte (lastRender ), & vals )
338
+ if err != nil {
339
+ return "" , err
337
340
}
338
341
339
342
// Run the the file through the tempating engine as both values
340
343
// file and template file
341
- tmpl .Parse (string (templateValuesStr ))
342
- if err != nil { return "" , err }
344
+ tmpl .Parse (string (lastRender ))
345
+ if err != nil {
346
+ return "" , err
347
+ }
343
348
out := new (bytes.Buffer )
344
349
err = tmpl .Execute (out , vals )
345
- if err != nil { return "" , err }
350
+ if err != nil {
351
+ return "" , err
352
+ }
346
353
347
354
newRender := out .String ()
348
355
if lastRender == newRender {
349
356
return newRender , nil // self-templating succeeded
350
357
} else {
351
358
lastRender = newRender
352
- templateValuesStr = newRender
353
359
}
354
360
}
355
361
356
- return templateValuesStr , errors .New ("Self-templating failed" )
362
+ return lastRender , errors .New ("Self-templating failed" )
357
363
}
0 commit comments