Skip to content

Commit

Permalink
fix: Improve templating diagnostics. Fixes argoproj#8311 (argoproj#10741
Browse files Browse the repository at this point in the history
)

Signed-off-by: weafscast <[email protected]>
  • Loading branch information
weafscast authored and JPZ13 committed Jul 4, 2023
1 parent 396674d commit a5afaf7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 7 additions & 1 deletion util/template/expression_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/antonmedv/expr/file"
"github.com/antonmedv/expr/parser/lexer"
"github.com/doublerebel/bellows"
log "github.com/sirupsen/logrus"
)

func init() {
Expand All @@ -24,6 +25,7 @@ func expressionReplace(w io.Writer, expression string, env map[string]interface{
var unmarshalledExpression string
err := json.Unmarshal([]byte(fmt.Sprintf(`"%s"`, expression)), &unmarshalledExpression)
if err != nil && allowUnresolved {
log.WithError(err).Debug("unresolved is allowed ")
return w.Write([]byte(fmt.Sprintf("{{%s%s}}", kindExpression, expression)))
}
if err != nil {
Expand All @@ -33,6 +35,7 @@ func expressionReplace(w io.Writer, expression string, env map[string]interface{
if _, ok := env["retries"]; !ok && hasRetries(unmarshalledExpression) && allowUnresolved {
// this is to make sure expressions like `sprig.int(retries)` don't get resolved to 0 when `retries` don't exist in the env
// See https://github.com/argoproj/argo-workflows/issues/5388
log.WithError(err).Debug("Retries are present and unresolved is allowed")
return w.Write([]byte(fmt.Sprintf("{{%s%s}}", kindExpression, expression)))
}

Expand All @@ -48,7 +51,9 @@ func expressionReplace(w io.Writer, expression string, env map[string]interface{
}

result, err := expr.Eval(unmarshalledExpression, env)
if (err != nil || result == nil) && allowUnresolved { // <nil> result is also un-resolved, and any error can be unresolved
if (err != nil || result == nil) && allowUnresolved {
// <nil> result is also un-resolved, and any error can be unresolved
log.WithError(err).Debug("Result and error are unresolved")
return w.Write([]byte(fmt.Sprintf("{{%s%s}}", kindExpression, expression)))
}
if err != nil {
Expand All @@ -59,6 +64,7 @@ func expressionReplace(w io.Writer, expression string, env map[string]interface{
}
resultMarshaled, err := json.Marshal(fmt.Sprintf("%v", result))
if (err != nil || resultMarshaled == nil) && allowUnresolved {
log.WithError(err).Debug("resultMarshaled is nil and unresolved is allowed ")
return w.Write([]byte(fmt.Sprintf("{{%s%s}}", kindExpression, expression)))
}
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions util/template/simple_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strconv"
"strings"

log "github.com/sirupsen/logrus"

"github.com/argoproj/argo-workflows/v3/errors"
)

Expand All @@ -24,6 +26,7 @@ func simpleReplace(w io.Writer, tag string, replaceMap map[string]string, allowU
}
if allowUnresolved {
// just write the same string back
log.WithError(errors.InternalError("unresolved")).Debug("unresolved is allowed ")
return w.Write([]byte(fmt.Sprintf("{{%s}}", tag)))
}
return 0, errors.Errorf(errors.CodeBadRequest, "failed to resolve {{%s}}", tag)
Expand Down

0 comments on commit a5afaf7

Please sign in to comment.