/
scope.go
133 lines (113 loc) · 3.45 KB
/
scope.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package controller
import (
"encoding/json"
"fmt"
"github.com/antonmedv/expr"
"github.com/argoproj/argo-workflows/v3/errors"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/util/expr/env"
"github.com/argoproj/argo-workflows/v3/util/template"
"github.com/argoproj/argo-workflows/v3/workflow/common"
)
// wfScope contains the current scope of variables available when executing a template
type wfScope struct {
tmpl *wfv1.Template
scope map[string]interface{}
}
func createScope(tmpl *wfv1.Template) *wfScope {
scope := &wfScope{
tmpl: tmpl,
scope: make(map[string]interface{}),
}
if tmpl != nil {
for _, param := range scope.tmpl.Inputs.Parameters {
key := fmt.Sprintf("inputs.parameters.%s", param.Name)
scope.scope[key] = scope.tmpl.Inputs.GetParameterByName(param.Name).Value.String()
}
for _, param := range scope.tmpl.Inputs.Artifacts {
key := fmt.Sprintf("inputs.artifacts.%s", param.Name)
scope.scope[key] = scope.tmpl.Inputs.GetArtifactByName(param.Name)
}
}
return scope
}
// getParameters returns a map of strings intended to be used simple string substitution
func (s *wfScope) getParameters() common.Parameters {
params := make(common.Parameters)
for key, val := range s.scope {
valStr, ok := val.(string)
if ok {
params[key] = valStr
}
}
return params
}
func (s *wfScope) addParamToScope(key, val string) {
s.scope[key] = val
}
func (s *wfScope) addArtifactToScope(key string, artifact wfv1.Artifact) {
s.scope[key] = artifact
}
// resolveVar resolves a parameter or artifact
func (s *wfScope) resolveVar(v string) (interface{}, error) {
m := make(map[string]interface{})
for k, v := range s.scope {
m[k] = v
}
if s.tmpl != nil {
for _, a := range s.tmpl.Inputs.Artifacts {
m["inputs.artifacts."+a.Name] = a // special case for artifacts
}
}
return template.ResolveVar(v, m)
}
func (s *wfScope) resolveParameter(p *wfv1.ValueFrom) (interface{}, error) {
if p == nil || (p.Parameter == "" && p.Expression == "") {
return "", nil
}
if p.Expression != "" {
env := env.GetFuncMap(s.scope)
return expr.Eval(p.Expression, env)
} else {
return s.resolveVar(p.Parameter)
}
}
func (s *wfScope) resolveArtifact(art *wfv1.Artifact) (*wfv1.Artifact, error) {
if art == nil || (art.From == "" && art.FromExpression == "") {
return nil, nil
}
var err error
var val interface{}
if art.FromExpression != "" {
env := env.GetFuncMap(s.scope)
val, err = expr.Eval(art.FromExpression, env)
} else {
val, err = s.resolveVar(art.From)
}
if err != nil {
return nil, err
}
valArt, ok := val.(wfv1.Artifact)
if !ok {
return nil, errors.Errorf(errors.CodeBadRequest, "Variable {{%v}} is not an artifact", art)
}
if art.SubPath != "" {
// Copy resolved artifact pointer before adding subpath
copyArt := valArt.DeepCopy()
subPathAsJson, err := json.Marshal(art.SubPath)
if err != nil {
return copyArt, errors.New(errors.CodeBadRequest, "failed to marshal artifact subpath for templating")
}
resolvedSubPathAsJson, err := template.Replace(string(subPathAsJson), s.getParameters(), true)
if err != nil {
return nil, err
}
var resolvedSubPath string
err = json.Unmarshal([]byte(resolvedSubPathAsJson), &resolvedSubPath)
if err != nil {
return copyArt, errors.New(errors.CodeBadRequest, "failed to unmarshal artifact subpath for templating")
}
return copyArt, copyArt.AppendToKey(resolvedSubPath)
}
return &valArt, nil
}