Skip to content

Commit e451cb3

Browse files
committed
add SetDelim to provide an alternative delimiters for template parse
1 parent 6109ce1 commit e451cb3

File tree

8 files changed

+94
-45
lines changed

8 files changed

+94
-45
lines changed

pkg/machinery/interfaces.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ type Template interface {
4444
GetBody() string
4545
// SetTemplateDefaults sets the default values for templates
4646
SetTemplateDefaults() error
47+
// SetDelim sets an action delimiters to replace default delimiters: {{ }}
48+
SetDelim(left, right string)
49+
// GetDelim returns the alternative delimiters
50+
GetDelim() (string, string)
4751
}
4852

4953
// Inserter is a file builder that inserts code fragments in marked positions

pkg/machinery/mixins.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,27 @@ type TemplateMixin struct {
4848
IfExistsActionMixin
4949

5050
// TemplateBody is the template body to execute
51-
TemplateBody string
51+
TemplateBody string
52+
parseDelimLeft string
53+
parseDelimRight string
5254
}
5355

5456
// GetBody implements Template
5557
func (t *TemplateMixin) GetBody() string {
5658
return t.TemplateBody
5759
}
5860

61+
// SetDelim implements Template
62+
func (t *TemplateMixin) SetDelim(left, right string) {
63+
t.parseDelimLeft = left
64+
t.parseDelimRight = right
65+
}
66+
67+
// GetDelim implements Template
68+
func (t *TemplateMixin) GetDelim() (string, string) {
69+
return t.parseDelimLeft, t.parseDelimRight
70+
}
71+
5972
// InserterMixin is the mixin that should be embedded in Inserter builders
6073
type InserterMixin struct {
6174
PathMixin

pkg/machinery/scaffold.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ func (Scaffold) buildFileModel(t Template, models map[string]*File) error {
196196
func doTemplate(t Template) ([]byte, error) {
197197
// Create a new template.Template using the type of the Template as the name
198198
temp := template.New(fmt.Sprintf("%T", t))
199+
leftDelim, rightDelim := t.GetDelim()
200+
if leftDelim != "" && rightDelim != "" {
201+
temp.Delims(leftDelim, rightDelim)
202+
}
199203

200204
// Set the function map to be used
201205
fm := DefaultFuncMap()

pkg/machinery/scaffold_test.go

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,32 @@ var _ = Describe("Scaffold", func() {
135135
},
136136
Entry("should write the file",
137137
path, content,
138-
fakeTemplate{fakeBuilder: fakeBuilder{path: path}, body: content},
138+
&fakeTemplate{fakeBuilder: fakeBuilder{path: path}, body: content},
139139
),
140140
Entry("should skip optional models if already have one",
141141
path, content,
142-
fakeTemplate{fakeBuilder: fakeBuilder{path: path}, body: content},
143-
fakeTemplate{fakeBuilder: fakeBuilder{path: path}},
142+
&fakeTemplate{fakeBuilder: fakeBuilder{path: path}, body: content},
143+
&fakeTemplate{fakeBuilder: fakeBuilder{path: path}},
144144
),
145145
Entry("should overwrite required models if already have one",
146146
path, content,
147-
fakeTemplate{fakeBuilder: fakeBuilder{path: path}},
148-
fakeTemplate{fakeBuilder: fakeBuilder{path: path, ifExistsAction: OverwriteFile}, body: content},
147+
&fakeTemplate{fakeBuilder: fakeBuilder{path: path}},
148+
&fakeTemplate{fakeBuilder: fakeBuilder{path: path, ifExistsAction: OverwriteFile}, body: content},
149149
),
150150
Entry("should format a go file",
151151
pathGo, "package file\n",
152-
fakeTemplate{fakeBuilder: fakeBuilder{path: pathGo}, body: "package file"},
152+
&fakeTemplate{fakeBuilder: fakeBuilder{path: pathGo}, body: "package file"},
153+
),
154+
155+
Entry("should render actions correctly",
156+
path, "package testValue",
157+
&fakeTemplate{fakeBuilder: fakeBuilder{path: path, TestField: "testValue"}, body: "package {{.TestField}}"},
158+
),
159+
160+
Entry("should render actions with alternative delimiters correctly",
161+
path, "package testValue",
162+
&fakeTemplate{fakeBuilder: fakeBuilder{path: path, TestField: "testValue"},
163+
body: "package [[.TestField]]", parseDelimLeft: "[[", parseDelimRight: "]]"},
153164
),
154165
)
155166

@@ -165,17 +176,17 @@ var _ = Describe("Scaffold", func() {
165176
),
166177
Entry("should fail if unable to set default values for a template",
167178
&SetTemplateDefaultsError{},
168-
fakeTemplate{err: testErr},
179+
&fakeTemplate{err: testErr},
169180
),
170181
Entry("should fail if an unexpected previous model is found",
171182
&ModelAlreadyExistsError{},
172-
fakeTemplate{fakeBuilder: fakeBuilder{path: path}},
173-
fakeTemplate{fakeBuilder: fakeBuilder{path: path, ifExistsAction: Error}},
183+
&fakeTemplate{fakeBuilder: fakeBuilder{path: path}},
184+
&fakeTemplate{fakeBuilder: fakeBuilder{path: path, ifExistsAction: Error}},
174185
),
175186
Entry("should fail if behavior if-exists-action is not defined",
176187
&UnknownIfExistsActionError{},
177-
fakeTemplate{fakeBuilder: fakeBuilder{path: path}},
178-
fakeTemplate{fakeBuilder: fakeBuilder{path: path, ifExistsAction: -1}},
188+
&fakeTemplate{fakeBuilder: fakeBuilder{path: path}},
189+
&fakeTemplate{fakeBuilder: fakeBuilder{path: path, ifExistsAction: -1}},
179190
),
180191
)
181192

@@ -188,15 +199,15 @@ var _ = Describe("Scaffold", func() {
188199
},
189200
Entry("should fail if a template is broken",
190201
"template: ",
191-
fakeTemplate{body: "{{ .Field }"},
202+
&fakeTemplate{body: "{{ .Field }"},
192203
),
193204
Entry("should fail if a template params aren't provided",
194205
"template: ",
195-
fakeTemplate{body: "{{ .Field }}"},
206+
&fakeTemplate{body: "{{ .Field }}"},
196207
),
197208
Entry("should fail if unable to format a go file",
198209
"expected 'package', found ",
199-
fakeTemplate{fakeBuilder: fakeBuilder{path: pathGo}, body: content},
210+
&fakeTemplate{fakeBuilder: fakeBuilder{path: pathGo}, body: content},
200211
),
201212
)
202213

@@ -255,7 +266,7 @@ var b int
255266
2
256267
#+kubebuilder:scaffold:-
257268
`,
258-
fakeTemplate{fakeBuilder: fakeBuilder{path: pathYaml, ifExistsAction: OverwriteFile}, body: `
269+
&fakeTemplate{fakeBuilder: fakeBuilder{path: pathYaml, ifExistsAction: OverwriteFile}, body: `
259270
#+kubebuilder:scaffold:-
260271
`},
261272
fakeInserter{
@@ -273,7 +284,7 @@ var b int
273284
2
274285
#+kubebuilder:scaffold:-
275286
`,
276-
fakeTemplate{fakeBuilder: fakeBuilder{path: pathYaml, ifExistsAction: OverwriteFile}, body: `
287+
&fakeTemplate{fakeBuilder: fakeBuilder{path: pathYaml, ifExistsAction: OverwriteFile}, body: `
277288
#+kubebuilder:scaffold:-
278289
`},
279290
fakeInserter{
@@ -293,7 +304,7 @@ var b int
293304
2
294305
#+kubebuilder:scaffold:-
295306
`,
296-
fakeTemplate{fakeBuilder: fakeBuilder{path: pathYaml}, body: content},
307+
&fakeTemplate{fakeBuilder: fakeBuilder{path: pathYaml}, body: content},
297308
fakeInserter{
298309
fakeBuilder: fakeBuilder{path: pathYaml},
299310
codeFragments: CodeFragmentsMap{
@@ -403,12 +414,12 @@ func init() {
403414
},
404415
Entry("should fail if inserting into a model that fails when a file exists and it does exist",
405416
&FileAlreadyExistsError{},
406-
fakeTemplate{fakeBuilder: fakeBuilder{path: "filename", ifExistsAction: Error}},
417+
&fakeTemplate{fakeBuilder: fakeBuilder{path: "filename", ifExistsAction: Error}},
407418
fakeInserter{fakeBuilder: fakeBuilder{path: "filename"}},
408419
),
409420
Entry("should fail if inserting into a model with unknown behavior if the file exists and it does exist",
410421
&UnknownIfExistsActionError{},
411-
fakeTemplate{fakeBuilder: fakeBuilder{path: "filename", ifExistsAction: -1}},
422+
&fakeTemplate{fakeBuilder: fakeBuilder{path: "filename", ifExistsAction: -1}},
412423
fakeInserter{fakeBuilder: fakeBuilder{path: "filename"}},
413424
),
414425
)
@@ -419,7 +430,7 @@ func init() {
419430
})
420431

421432
It("should skip the file by default", func() {
422-
Expect(s.Execute(fakeTemplate{
433+
Expect(s.Execute(&fakeTemplate{
423434
fakeBuilder: fakeBuilder{path: path},
424435
body: content,
425436
})).To(Succeed())
@@ -430,7 +441,7 @@ func init() {
430441
})
431442

432443
It("should write the file if configured to do so", func() {
433-
Expect(s.Execute(fakeTemplate{
444+
Expect(s.Execute(&fakeTemplate{
434445
fakeBuilder: fakeBuilder{path: path, ifExistsAction: OverwriteFile},
435446
body: content,
436447
})).To(Succeed())
@@ -441,7 +452,7 @@ func init() {
441452
})
442453

443454
It("should error if configured to do so", func() {
444-
err := s.Execute(fakeTemplate{
455+
err := s.Execute(&fakeTemplate{
445456
fakeBuilder: fakeBuilder{path: path, ifExistsAction: Error},
446457
body: content,
447458
})
@@ -458,6 +469,7 @@ var _ Builder = fakeBuilder{}
458469
type fakeBuilder struct {
459470
path string
460471
ifExistsAction IfExistsAction
472+
TestField string // test go template actions
461473
}
462474

463475
// GetPath implements Builder
@@ -484,23 +496,34 @@ func (f fakeRequiresValidation) Validate() error {
484496
return f.validateErr
485497
}
486498

487-
var _ Template = fakeTemplate{}
499+
var _ Template = &fakeTemplate{}
488500

489501
// fakeTemplate is used to mock a File in order to test Scaffold
490502
type fakeTemplate struct {
491503
fakeBuilder
492504

493-
body string
494-
err error
505+
body string
506+
err error
507+
parseDelimLeft string
508+
parseDelimRight string
509+
}
510+
511+
func (f *fakeTemplate) SetDelim(left, right string) {
512+
f.parseDelimLeft = left
513+
f.parseDelimRight = right
514+
}
515+
516+
func (f *fakeTemplate) GetDelim() (string, string) {
517+
return f.parseDelimLeft, f.parseDelimRight
495518
}
496519

497520
// GetBody implements Template
498-
func (f fakeTemplate) GetBody() string {
521+
func (f *fakeTemplate) GetBody() string {
499522
return f.body
500523
}
501524

502525
// SetTemplateDefaults implements Template
503-
func (f fakeTemplate) SetTemplateDefaults() error {
526+
func (f *fakeTemplate) SetTemplateDefaults() error {
504527
if f.err != nil {
505528
return f.err
506529
}

pkg/plugins/optional/grafana/v1alpha/scaffolds/internal/templates/resources.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ func (f *ResourcesManifest) SetTemplateDefaults() error {
3535
f.Path = filepath.Join("grafana", "controller-resources-metrics.json")
3636
}
3737

38+
// Grafana syntax use {{ }} quite often, which is collided with default delimiter for go template parsing.
39+
// Provide an alternative delimiter here to avoid overlaps.
40+
f.SetDelim("[[", "]]")
3841
f.TemplateBody = controllerResourcesTemplate
3942

4043
f.IfExistsAction = machinery.OverwriteFile
@@ -169,7 +172,7 @@ const controllerResourcesTemplate = `{
169172
"format": "time_series",
170173
"interval": "",
171174
"intervalFactor": 2,
172-
"legendFormat": "Pod: {{"{{pod}}"}} | Container: {{"{{container}}"}}",
175+
"legendFormat": "Pod: {{pod}} | Container: {{container}}",
173176
"refId": "A",
174177
"step": 10
175178
}
@@ -259,7 +262,7 @@ const controllerResourcesTemplate = `{
259262
"format": "time_series",
260263
"interval": "",
261264
"intervalFactor": 2,
262-
"legendFormat": "Pod: {{"{{pod}}"}} | Container: {{"{{container}}"}}",
265+
"legendFormat": "Pod: {{pod}} | Container: {{container}}",
263266
"refId": "A",
264267
"step": 10
265268
}

pkg/plugins/optional/grafana/v1alpha/scaffolds/internal/templates/runtime.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ func (f *RuntimeManifest) SetTemplateDefaults() error {
3535
f.Path = filepath.Join("grafana", "controller-runtime-metrics.json")
3636
}
3737

38+
// Grafana syntax use {{ }} quite often, which is collided with default delimiter for go template parsing.
39+
// Provide an alternative delimiter here to avoid overlaps.
40+
f.SetDelim("[[", "]]")
3841
f.TemplateBody = controllerRuntimeTemplate
39-
4042
f.IfExistsAction = machinery.OverwriteFile
4143

4244
return nil
@@ -182,7 +184,7 @@ const controllerRuntimeTemplate = `{
182184
"exemplar": true,
183185
"expr": "sum(rate(controller_runtime_reconcile_total{job=\"$job\", namespace=\"$namespace\"}[5m])) by (instance, pod)",
184186
"interval": "",
185-
"legendFormat": "{{"{{instance}}"}} {{"{{pod}}"}}",
187+
"legendFormat": "{{instance}} {{pod}}",
186188
"range": true,
187189
"refId": "A"
188190
}
@@ -269,7 +271,7 @@ const controllerRuntimeTemplate = `{
269271
"exemplar": true,
270272
"expr": "sum(rate(controller_runtime_reconcile_errors_total{job=\"$job\", namespace=\"$namespace\"}[5m])) by (instance, pod)",
271273
"interval": "",
272-
"legendFormat": "{{"{{instance}}"}} {{"{{pod}}"}}",
274+
"legendFormat": "{{instance}} {{pod}}",
273275
"range": true,
274276
"refId": "A"
275277
}
@@ -371,7 +373,7 @@ const controllerRuntimeTemplate = `{
371373
"exemplar": true,
372374
"expr": "histogram_quantile(0.50, sum(rate(workqueue_queue_duration_seconds_bucket{job=\"$job\", namespace=\"$namespace\"}[5m])) by (instance, name, le))",
373375
"interval": "",
374-
"legendFormat": "P50 {{"{{name}}"}} {{"{{instance}}"}} ",
376+
"legendFormat": "P50 {{name}} {{instance}} ",
375377
"refId": "A"
376378
},
377379
{
@@ -380,7 +382,7 @@ const controllerRuntimeTemplate = `{
380382
"expr": "histogram_quantile(0.90, sum(rate(workqueue_queue_duration_seconds_bucket{job=\"$job\", namespace=\"$namespace\"}[5m])) by (instance, name, le))",
381383
"hide": false,
382384
"interval": "",
383-
"legendFormat": "P90 {{"{{name}}"}} {{"{{instance}}"}} ",
385+
"legendFormat": "P90 {{name}} {{instance}} ",
384386
"refId": "B"
385387
},
386388
{
@@ -389,7 +391,7 @@ const controllerRuntimeTemplate = `{
389391
"expr": "histogram_quantile(0.99, sum(rate(workqueue_queue_duration_seconds_bucket{job=\"$job\", namespace=\"$namespace\"}[5m])) by (instance, name, le))",
390392
"hide": false,
391393
"interval": "",
392-
"legendFormat": "P99 {{"{{name}}"}} {{"{{instance}}"}} ",
394+
"legendFormat": "P99 {{name}} {{instance}} ",
393395
"refId": "C"
394396
}
395397
],
@@ -474,7 +476,7 @@ const controllerRuntimeTemplate = `{
474476
"exemplar": true,
475477
"expr": "sum(rate(workqueue_adds_total{job=\"$job\", namespace=\"$namespace\"}[5m])) by (instance, name)",
476478
"interval": "",
477-
"legendFormat": "{{"{{name}}"}} {{"{{instance}}"}}",
479+
"legendFormat": "{{name}} {{instance}}",
478480
"refId": "A"
479481
}
480482
],
@@ -562,7 +564,7 @@ const controllerRuntimeTemplate = `{
562564
"exemplar": true,
563565
"expr": "histogram_quantile(0.50, sum(rate(workqueue_work_duration_seconds_bucket{job=\"$job\", namespace=\"$namespace\"}[5m])) by (instance, name, le))",
564566
"interval": "",
565-
"legendFormat": "P50 {{"{{name}}"}} {{"{{instance}}"}} ",
567+
"legendFormat": "P50 {{name}} {{instance}} ",
566568
"refId": "A"
567569
},
568570
{
@@ -571,7 +573,7 @@ const controllerRuntimeTemplate = `{
571573
"expr": "histogram_quantile(0.90, sum(rate(workqueue_work_duration_seconds_bucket{job=\"$job\", namespace=\"$namespace\"}[5m])) by (instance, name, le))",
572574
"hide": false,
573575
"interval": "",
574-
"legendFormat": "P90 {{"{{name}}"}} {{"{{instance}}"}} ",
576+
"legendFormat": "P90 {{name}} {{instance}} ",
575577
"refId": "B"
576578
},
577579
{
@@ -580,7 +582,7 @@ const controllerRuntimeTemplate = `{
580582
"expr": "histogram_quantile(0.99, sum(rate(workqueue_work_duration_seconds_bucket{job=\"$job\", namespace=\"$namespace\"}[5m])) by (instance, name, le))",
581583
"hide": false,
582584
"interval": "",
583-
"legendFormat": "P99 {{"{{name}}"}} {{"{{instance}}"}} ",
585+
"legendFormat": "P99 {{name}} {{instance}} ",
584586
"refId": "C"
585587
}
586588
],
@@ -665,7 +667,7 @@ const controllerRuntimeTemplate = `{
665667
"exemplar": true,
666668
"expr": "sum(rate(workqueue_retries_total{job=\"$job\", namespace=\"$namespace\"}[5m])) by (instance, name)",
667669
"interval": "",
668-
"legendFormat": "{{"{{name}}"}} {{"{{instance}}"}} ",
670+
"legendFormat": "{{name}} {{instance}} ",
669671
"refId": "A"
670672
}
671673
],

testdata/project-v3-addon-and-grafana/grafana/controller-resources-metrics.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
"format": "time_series",
125125
"interval": "",
126126
"intervalFactor": 2,
127-
"legendFormat": "Pod: {{pod}} | Container: {{container}}",
127+
"legendFormat": "Pod: {{pod}} | Container: {{container}}",
128128
"refId": "A",
129129
"step": 10
130130
}
@@ -214,7 +214,7 @@
214214
"format": "time_series",
215215
"interval": "",
216216
"intervalFactor": 2,
217-
"legendFormat": "Pod: {{pod}} | Container: {{container}}",
217+
"legendFormat": "Pod: {{pod}} | Container: {{container}}",
218218
"refId": "A",
219219
"step": 10
220220
}

0 commit comments

Comments
 (0)