-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Go template with sprig for application template render in applicationset #9873
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"strconv" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/Masterminds/sprig/v3" | ||
argov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" | ||
argoprojiov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/applicationset/v1alpha1" | ||
"github.com/valyala/fasttemplate" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
type Renderer interface { | ||
RenderTemplateParams(tmpl *argov1alpha1.Application, untypedTemplate *argoprojiov1alpha1.ApplicationSetUntypedTemplate, syncPolicy *argoprojiov1alpha1.ApplicationSetSyncPolicy, params map[string]string) (*argov1alpha1.Application, error) | ||
} | ||
type Render struct { | ||
} | ||
|
||
func (r *Render) RenderTemplateParams(tmpl *argov1alpha1.Application, untypedTemplate *argoprojiov1alpha1.ApplicationSetUntypedTemplate, syncPolicy *argoprojiov1alpha1.ApplicationSetSyncPolicy, params map[string]string) (*argov1alpha1.Application, error) { | ||
if tmpl == nil { | ||
return nil, fmt.Errorf("application template is empty ") | ||
} | ||
if len(params) == 0 { | ||
return tmpl, nil | ||
} | ||
var replacedTmpl argov1alpha1.Application | ||
var replacedTmplStr string | ||
if untypedTemplate == nil { | ||
// interpolates the `${expression}` first and simple `{reference}` right after | ||
tmplBytes, err := json.Marshal(tmpl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
replacedTmplStr, err = renderWithFastTemplateAndGoTemplate(string(tmplBytes), params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
replacedTmplStr = renderWithFastTemplate(replacedTmplStr, params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = json.Unmarshal([]byte(replacedTmplStr), &replacedTmpl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
replacedTmplStr, err := renderWithGoTemplate(string(*untypedTemplate), params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// UnmarshalStrict to fail early and raise the fact that template | ||
// result produced not what is expected | ||
err = yaml.UnmarshalStrict([]byte(replacedTmplStr), &replacedTmpl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
// Add the 'resources-finalizer' finalizer if: | ||
// The template application doesn't have any finalizers, and: | ||
// a) there is no syncPolicy, or | ||
// b) there IS a syncPolicy, but preserveResourcesOnDeletion is set to false | ||
// See TestRenderTemplateParamsFinalizers in util_test.go for test-based definition of behaviour | ||
if (syncPolicy == nil || !syncPolicy.PreserveResourcesOnDeletion) && | ||
(replacedTmpl.ObjectMeta.Finalizers == nil || len(replacedTmpl.ObjectMeta.Finalizers) == 0) { | ||
replacedTmpl.ObjectMeta.Finalizers = []string{"resources-finalizer.argocd.argoproj.io"} | ||
} | ||
return &replacedTmpl, nil | ||
} | ||
|
||
// renderWithGoTemplate executes gotemplate with sprig functions against the raw (untyped) template | ||
func renderWithGoTemplate(rawTemplate string, data map[string]string) (string, error) { | ||
goTemplate, err := template.New("").Option("missingkey=zero").Funcs(createFuncMap()).Parse(rawTemplate) | ||
if err != nil { | ||
return "", err | ||
} | ||
var tplString bytes.Buffer | ||
err = goTemplate.Execute(&tplString, data) | ||
if err != nil { | ||
return "", err | ||
} | ||
return tplString.String(), nil | ||
} | ||
|
||
// renderWithFastTemplateAndGoTemplate executes string substitution with the result of gotemplate run | ||
// for every token found | ||
func renderWithFastTemplateAndGoTemplate(rawTemplate string, data map[string]string) (string, error) { | ||
fstTmpl := fasttemplate.New(rawTemplate, "${{", "}}") | ||
replacedTmplStr, err := fstTmpl.ExecuteFuncStringWithErr(func(w io.Writer, tag string) (int, error) { | ||
trimmedTag := strings.TrimSpace(tag) | ||
// json control characters here are double escaped, what was a double quote " becomes \" when | ||
// unmarshalled to ApplicationSet and then \\\" when marshaled to json | ||
// this becomes a problem with gotemplate trying to parse the token, so unquote the string | ||
unquotedTag, err := strconv.Unquote(`"` + trimmedTag + `"`) | ||
if err != nil { | ||
return 0, err | ||
} | ||
// wrapping back in {{}} for gotemplate to identify the expression | ||
gotemplateTag := fmt.Sprintf("{{%s}}", unquotedTag) | ||
goTemplate, err := template.New("").Option("missingkey=zero").Funcs(createFuncMap()).Parse(gotemplateTag) | ||
if err != nil { | ||
return 0, err | ||
} | ||
var tplString bytes.Buffer | ||
err = goTemplate.Execute(&tplString, data) | ||
if err != nil { | ||
return 0, err | ||
} | ||
// The following escapes any special characters (e.g. newlines, tabs, etc...) | ||
// in preparation for substitution | ||
replacement := strconv.Quote(tplString.String()) | ||
replacement = replacement[1 : len(replacement)-1] | ||
return w.Write([]byte(replacement)) | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
return replacedTmplStr, nil | ||
} | ||
|
||
// replaceWithFastTemplate executes basic string substitution of a template with replacement values. | ||
func renderWithFastTemplate(rawTemplate string, data map[string]string) string { | ||
fstTmpl := fasttemplate.New(rawTemplate, "{{", "}}") | ||
replacedTmplStr := fstTmpl.ExecuteFuncString(func(w io.Writer, tag string) (int, error) { | ||
trimmedTag := strings.TrimSpace(tag) | ||
replacement, ok := data[trimmedTag] | ||
if len(trimmedTag) == 0 || !ok { | ||
return w.Write([]byte(fmt.Sprintf("{{%s}}", tag))) | ||
} | ||
// The following escapes any special characters (e.g. newlines, tabs, etc...) | ||
// in preparation for substitution | ||
replacement = strconv.Quote(replacement) | ||
replacement = replacement[1 : len(replacement)-1] | ||
return w.Write([]byte(replacement)) | ||
}) | ||
return replacedTmplStr | ||
} | ||
func ToYaml(v interface{}) (string, error) { | ||
data, err := yaml.Marshal(v) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(data), nil | ||
} | ||
func createFuncMap() template.FuncMap { | ||
funcMap := sprig.TxtFuncMap() | ||
extraFuncMap := template.FuncMap{ | ||
"toYaml": ToYaml, | ||
} | ||
for name, f := range extraFuncMap { | ||
funcMap[name] = f | ||
} | ||
return funcMap | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this something we can't support? Seems that we could 1) render the untypedTemplate, 2) unmarshal the rendered template, 3) merge it with the generator template and 4) render the merged template.