Skip to content

Commit

Permalink
Improve rendering errors handling
Browse files Browse the repository at this point in the history
Signed-off-by: Raul Sevilla <rsevilla@redhat.com>
  • Loading branch information
rsevilla87 committed Dec 5, 2020
1 parent 203ca06 commit 55d67df
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
24 changes: 15 additions & 9 deletions pkg/burner/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func setupCreateJob(jobConfig config.Job) Executor {
var f io.Reader
var err error
log.Infof("Preparing create job: %s", jobConfig.Name)
var empty interface{}
selector := util.NewSelector()
selector.Configure("", fmt.Sprintf("kube-burner-job=%s", jobConfig.Name), "")
ex := Executor{
Expand All @@ -72,15 +71,19 @@ func setupCreateJob(jobConfig config.Job) Executor {
}
// Deserialize YAML
uns := &unstructured.Unstructured{}
renderedObj := renderTemplate(t, empty, missingKeyDefault)
_, gvk := yamlToUnstructured(renderedObj, uns)
cleanTemplate, err := prepareTemplate(t)
if err != nil {
log.Fatalf("Error preparing template %s: %s", o.ObjectTemplate, err)
}
_, gvk := yamlToUnstructured(cleanTemplate, uns)
gvr, _ := meta.UnsafeGuessKindToResource(*gvk)
obj := object{
gvr: gvr,
objectSpec: t,
replicas: o.Replicas,
unstructured: uns,
inputVars: o.InputVars,
gvr: gvr,
objectSpec: t,
objectTemplate: o.ObjectTemplate,
replicas: o.Replicas,
unstructured: uns,
inputVars: o.InputVars,
}
log.Infof("Job %s: %d iterations with %d %s replicas", jobConfig.Name, jobConfig.JobIterations, obj.replicas, gvk.Kind)
ex.objects = append(ex.objects, obj)
Expand Down Expand Up @@ -163,7 +166,10 @@ func (ex *Executor) replicaHandler(objectIndex int, obj object, ns string, itera
for r := 1; r <= obj.replicas; r++ {
newObject := &unstructured.Unstructured{}
templateData[replica] = r
renderedObj := renderTemplate(obj.objectSpec, templateData, missingKeyError)
renderedObj, err := renderTemplate(obj.objectSpec, templateData, missingKeyError)
if err != nil {
log.Fatalf("Template error in %s: %s", obj.objectTemplate, err)
}
// Re-decode rendered object
yamlToUnstructured(renderedObj, newObject)
for k, v := range newObject.GetLabels() {
Expand Down
13 changes: 7 additions & 6 deletions pkg/burner/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ import (
)

type object struct {
gvr schema.GroupVersionResource
objectSpec []byte
replicas int
unstructured *unstructured.Unstructured
inputVars map[string]string
labelSelector map[string]string
gvr schema.GroupVersionResource
objectTemplate string
objectSpec []byte
replicas int
unstructured *unstructured.Unstructured
inputVars map[string]string
labelSelector map[string]string
}

// Executor contains the information required to execute a job
Expand Down
29 changes: 24 additions & 5 deletions pkg/burner/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"bytes"
"context"
"fmt"
"regexp"
"strings"
"text/template"
"time"

Expand All @@ -38,21 +40,34 @@ const (
retryBackoffFactor = 3
retryBackoffJitter = 0
retryBackoffSteps = 3
missingKeyDefault templateOption = "missingkey=default"
missingKeyError templateOption = "missingkey=error"
)

func renderTemplate(original []byte, data interface{}, options templateOption) []byte {
func prepareTemplate(original []byte) ([]byte, error) {
// Removing all placeholder from template.
// This needs to be done due to placeholders not being valid yaml.
if isEmpty(original) {
return nil, fmt.Errorf("template is empty")
}
r, err := regexp.Compile(`\{\{.*\}\}`)
if err != nil {
return nil, fmt.Errorf("regexp creation error: %v", err)
}
original = r.ReplaceAll(original, []byte{})
return original, nil
}

func renderTemplate(original []byte, data interface{}, options templateOption) ([]byte, error) {
var rendered bytes.Buffer
t, err := template.New("").Option(string(options)).Parse(string(original))
if err != nil {
log.Fatalf("Error parsing template: %s", err)
return nil, fmt.Errorf("Parsing error: %s", err)
}
err = t.Execute(&rendered, data)
if err != nil {
log.Fatalf("Error rendering template: %s", err)
return nil, fmt.Errorf("Rendering error: %s", err)
}
return rendered.Bytes()
return rendered.Bytes(), nil
}

func yamlToUnstructured(y []byte, uns *unstructured.Unstructured) (runtime.Object, *schema.GroupVersionKind) {
Expand Down Expand Up @@ -113,3 +128,7 @@ func RetryWithExponentialBackOff(fn wait.ConditionFunc) error {
}
return wait.ExponentialBackoff(backoff, fn)
}

func isEmpty(raw []byte) bool {
return strings.TrimSpace(string(raw)) == ""
}

0 comments on commit 55d67df

Please sign in to comment.