forked from argoproj/applicationset
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add list generator support (argoproj#3)
* Add list generator support * update item to elements * Fix build error * trigger github * update
- Loading branch information
1 parent
a5eea21
commit 6e5adbb
Showing
13 changed files
with
136 additions
and
29 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,6 @@ | ||
package utils | ||
|
||
const ( | ||
ClusterListGeneratorKeyName = "cluster" | ||
UrlGeneratorKeyName = "url" | ||
) |
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,68 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
argov1alpha1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" | ||
"github.com/pkg/errors" | ||
"github.com/valyala/fasttemplate" | ||
"io" | ||
"strconv" | ||
) | ||
|
||
func RenderTemplateParams(tmpl *argov1alpha1.Application, 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 | ||
} | ||
|
||
tmplBytes, err := json.Marshal(tmpl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fstTmpl := fasttemplate.New(string(tmplBytes), "{{", "}}") | ||
replacedTmplStr, err := Replace(fstTmpl, params, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var replacedTmpl argov1alpha1.Application | ||
err = json.Unmarshal([]byte(replacedTmplStr), &replacedTmpl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &replacedTmpl, nil | ||
} | ||
|
||
// Replace executes basic string substitution of a template with replacement values. | ||
// allowUnresolved indicates whether or not it is acceptable to have unresolved variables | ||
// remaining in the substituted template. prefixFilter will apply the replacements only | ||
// to variables with the specified prefix | ||
func Replace(fstTmpl *fasttemplate.Template, replaceMap map[string]string, allowUnresolved bool) (string, error) { | ||
var unresolvedErr error | ||
replacedTmpl := fstTmpl.ExecuteFuncString(func(w io.Writer, tag string) (int, error) { | ||
replacement, ok := replaceMap[tag] | ||
if !ok { | ||
if allowUnresolved { | ||
// just write the same string back | ||
return w.Write([]byte(fmt.Sprintf("{{%s}}", tag))) | ||
} | ||
unresolvedErr = errors.Errorf("failed to resolve {{%s}}", tag) | ||
return 0, nil | ||
} | ||
// 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)) | ||
}) | ||
if unresolvedErr != nil { | ||
return "", unresolvedErr | ||
} | ||
return replacedTmpl, nil | ||
} |