Skip to content

Commit

Permalink
Refine error tips when rendering template (#492)
Browse files Browse the repository at this point in the history
Signed-off-by: johnniang <johnniang@fastmail.com>
  • Loading branch information
JohnNiang authored Mar 9, 2022
1 parent d8e08e8 commit e744581
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pkg/kapis/devops/v1alpha3/template/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package template

import (
"bytes"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog"
"kubesphere.io/devops/pkg/api/devops"
"kubesphere.io/devops/pkg/api/devops/v1alpha3"
tmpl "text/template"
Expand All @@ -33,9 +36,14 @@ type Parameter struct {
func render(templateObject v1alpha3.TemplateObject, parameters []Parameter) (v1alpha3.TemplateObject, error) {
templateObject = templateObject.DeepCopyObject().(v1alpha3.TemplateObject)
rawTemplate := templateObject.TemplateSpec().Template
template := tmpl.New("pipeline-template")
templateName := types.NamespacedName{
Name: templateObject.GetName(),
Namespace: templateObject.GetNamespace(),
}.String()
template := tmpl.New(templateName)
if _, err := template.Parse(rawTemplate); err != nil {
return nil, err
klog.Errorf("failed to parse template: %s, and err = %v", templateName, err)
return nil, errors.NewBadRequest("Failed to render template, please check the pipeline template for syntax error.")
}

// TODO Verify required parameters and default parameters
Expand All @@ -50,7 +58,8 @@ func render(templateObject v1alpha3.TemplateObject, parameters []Parameter) (v1a

buffer := &bytes.Buffer{}
if err := template.Execute(buffer, parametersData); err != nil {
return nil, err
klog.Errorf("failed to apply a parsed template(%s) to the specified data object(%v), and err = %v ", templateName, parametersData, err)
return nil, errors.NewBadRequest("Failed to render template, please check the parameters you provided.")
}
renderResult := buffer.String()

Expand Down

0 comments on commit e744581

Please sign in to comment.