-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
context.go
276 lines (237 loc) · 9.69 KB
/
context.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package templateresolution
import (
"context"
"fmt"
log "github.com/sirupsen/logrus"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/argoproj/argo-workflows/v3/errors"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
typed "github.com/argoproj/argo-workflows/v3/pkg/client/clientset/versioned/typed/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/workflow/common"
)
// maxResolveDepth is the limit of template reference resolution.
const maxResolveDepth int = 10
// workflowTemplateInterfaceWrapper is an internal struct to wrap clientset.
type workflowTemplateInterfaceWrapper struct {
clientset typed.WorkflowTemplateInterface
}
func WrapWorkflowTemplateInterface(clientset typed.WorkflowTemplateInterface) WorkflowTemplateNamespacedGetter {
return &workflowTemplateInterfaceWrapper{clientset: clientset}
}
// Get retrieves the WorkflowTemplate of a given name.
func (wrapper *workflowTemplateInterfaceWrapper) Get(name string) (*wfv1.WorkflowTemplate, error) {
ctx := context.TODO()
return wrapper.clientset.Get(ctx, name, metav1.GetOptions{})
}
// WorkflowTemplateNamespaceLister helps get WorkflowTemplates.
type WorkflowTemplateNamespacedGetter interface {
// Get retrieves the WorkflowTemplate from the indexer for a given name.
Get(name string) (*wfv1.WorkflowTemplate, error)
}
// clusterWorkflowTemplateInterfaceWrapper is an internal struct to wrap clientset.
type clusterWorkflowTemplateInterfaceWrapper struct {
clientset typed.ClusterWorkflowTemplateInterface
}
// WorkflowTemplateNamespaceLister helps get WorkflowTemplates.
type ClusterWorkflowTemplateGetter interface {
// Get retrieves the WorkflowTemplate from the indexer for a given name.
Get(name string) (*wfv1.ClusterWorkflowTemplate, error)
}
func WrapClusterWorkflowTemplateInterface(clusterClientset typed.ClusterWorkflowTemplateInterface) ClusterWorkflowTemplateGetter {
return &clusterWorkflowTemplateInterfaceWrapper{clientset: clusterClientset}
}
type NullClusterWorkflowTemplateGetter struct{}
func (n *NullClusterWorkflowTemplateGetter) Get(name string) (*wfv1.ClusterWorkflowTemplate, error) {
return nil, errors.Errorf("", "invalid spec: clusterworkflowtemplates.argoproj.io `%s` is "+
"forbidden: User cannot get resource 'clusterworkflowtemplates' in API group argoproj.io at the cluster scope", name)
}
// Get retrieves the WorkflowTemplate of a given name.
func (wrapper *clusterWorkflowTemplateInterfaceWrapper) Get(name string) (*wfv1.ClusterWorkflowTemplate, error) {
ctx := context.TODO()
return wrapper.clientset.Get(ctx, name, metav1.GetOptions{})
}
// Context is a context of template search.
type Context struct {
// wftmplGetter is an interface to get WorkflowTemplates.
wftmplGetter WorkflowTemplateNamespacedGetter
// cwftmplGetter is an interface to get ClusterWorkflowTemplates
cwftmplGetter ClusterWorkflowTemplateGetter
// tmplBase is the base of local template search.
tmplBase wfv1.TemplateHolder
// workflow is the Workflow where templates will be stored
workflow *wfv1.Workflow
// log is a logrus entry.
log *log.Entry
}
// NewContext returns new Context.
func NewContext(wftmplGetter WorkflowTemplateNamespacedGetter, cwftmplGetter ClusterWorkflowTemplateGetter, tmplBase wfv1.TemplateHolder, workflow *wfv1.Workflow) *Context {
return &Context{
wftmplGetter: wftmplGetter,
cwftmplGetter: cwftmplGetter,
tmplBase: tmplBase,
workflow: workflow,
log: log.WithFields(log.Fields{}),
}
}
// NewContext returns new Context.
func NewContextFromClientset(wftmplClientset typed.WorkflowTemplateInterface, clusterWftmplClient typed.ClusterWorkflowTemplateInterface, tmplBase wfv1.TemplateHolder, workflow *wfv1.Workflow) *Context {
return &Context{
wftmplGetter: WrapWorkflowTemplateInterface(wftmplClientset),
cwftmplGetter: WrapClusterWorkflowTemplateInterface(clusterWftmplClient),
tmplBase: tmplBase,
workflow: workflow,
log: log.WithFields(log.Fields{}),
}
}
// GetTemplateByName returns a template by name in the context.
func (ctx *Context) GetTemplateByName(name string) (*wfv1.Template, error) {
ctx.log.Debug("Getting the template by name")
tmpl := ctx.tmplBase.GetTemplateByName(name)
if tmpl == nil {
return nil, errors.Errorf(errors.CodeNotFound, "template %s not found", name)
}
return tmpl.DeepCopy(), nil
}
func (ctx *Context) GetTemplateGetterFromRef(tmplRef *wfv1.TemplateRef) (wfv1.TemplateHolder, error) {
if tmplRef.ClusterScope {
return ctx.cwftmplGetter.Get(tmplRef.Name)
}
return ctx.wftmplGetter.Get(tmplRef.Name)
}
// GetTemplateFromRef returns a template found by a given template ref.
func (ctx *Context) GetTemplateFromRef(tmplRef *wfv1.TemplateRef) (*wfv1.Template, error) {
ctx.log.Debug("Getting the template from ref")
var template *wfv1.Template
var wftmpl wfv1.TemplateHolder
var err error
if tmplRef.ClusterScope {
wftmpl, err = ctx.cwftmplGetter.Get(tmplRef.Name)
} else {
wftmpl, err = ctx.wftmplGetter.Get(tmplRef.Name)
}
if err != nil {
if apierr.IsNotFound(err) {
return nil, errors.Errorf(errors.CodeNotFound, "workflow template %s not found", tmplRef.Name)
}
return nil, err
}
template = wftmpl.GetTemplateByName(tmplRef.Template)
if template == nil {
return nil, errors.Errorf(errors.CodeNotFound, "template %s not found in workflow template %s", tmplRef.Template, tmplRef.Name)
}
return template.DeepCopy(), nil
}
// GetTemplate returns a template found by template name or template ref.
func (ctx *Context) GetTemplate(tmplHolder wfv1.TemplateReferenceHolder) (*wfv1.Template, error) {
ctx.log.Debug("Getting the template")
tmplName := tmplHolder.GetTemplateName()
tmplRef := tmplHolder.GetTemplateRef()
if tmplRef != nil {
return ctx.GetTemplateFromRef(tmplRef)
} else if tmplName != "" {
return ctx.GetTemplateByName(tmplName)
}
return nil, errors.Errorf(errors.CodeInternal, "failed to get a template")
}
// GetCurrentTemplateBase returns the current template base of the context.
func (ctx *Context) GetCurrentTemplateBase() wfv1.TemplateHolder {
return ctx.tmplBase
}
func (ctx *Context) GetTemplateScope() string {
return string(ctx.tmplBase.GetResourceScope()) + "/" + ctx.tmplBase.GetName()
}
// ResolveTemplate digs into referenes and returns a merged template.
// This method is the public start point of template resolution.
func (ctx *Context) ResolveTemplate(tmplHolder wfv1.TemplateReferenceHolder) (*Context, *wfv1.Template, bool, error) {
return ctx.resolveTemplateImpl(tmplHolder, 0)
}
// resolveTemplateImpl digs into references and returns a merged template.
// This method processes inputs and arguments so the inputs of the final
// resolved template include intermediate parameter passing.
// The other fields are just merged and shallower templates overwrite deeper.
func (ctx *Context) resolveTemplateImpl(tmplHolder wfv1.TemplateReferenceHolder, depth int) (*Context, *wfv1.Template, bool, error) {
ctx.log = ctx.log.WithFields(log.Fields{
"depth": depth,
"base": common.GetTemplateGetterString(ctx.tmplBase),
"tmpl": common.GetTemplateHolderString(tmplHolder),
})
// Avoid infinite references
if depth > maxResolveDepth {
return nil, nil, false, errors.Errorf(errors.CodeBadRequest, "template reference exceeded max depth (%d)", maxResolveDepth)
}
ctx.log.Debug("Resolving the template")
templateStored := false
var tmpl *wfv1.Template
if ctx.workflow != nil {
// Check if the template has been stored.
scope := ctx.tmplBase.GetResourceScope()
resourceName := ctx.tmplBase.GetName()
tmpl = ctx.workflow.GetStoredTemplate(scope, resourceName, tmplHolder)
}
if tmpl != nil {
ctx.log.Debug("Found stored template")
} else {
// Find newly appeared template.
newTmpl, err := ctx.GetTemplate(tmplHolder)
if err != nil {
return nil, nil, false, err
}
// Stored the found template.
if ctx.workflow != nil {
scope := ctx.tmplBase.GetResourceScope()
resourceName := ctx.tmplBase.GetName()
stored, err := ctx.workflow.SetStoredTemplate(scope, resourceName, tmplHolder, newTmpl)
if err != nil {
return nil, nil, false, err
}
if stored {
ctx.log.Debug("Stored the template")
templateStored = true
}
}
tmpl = newTmpl
}
// Update the template base of the context.
newTmplCtx, err := ctx.WithTemplateHolder(tmplHolder)
if err != nil {
return nil, nil, false, err
}
if tmpl.GetType() == wfv1.TemplateTypeUnknown {
return nil, nil, false, fmt.Errorf("template '%s' type is unknown", tmpl.Name)
}
return newTmplCtx, tmpl, templateStored, nil
}
// WithTemplateHolder creates new context with a template base of a given template holder.
func (ctx *Context) WithTemplateHolder(tmplHolder wfv1.TemplateReferenceHolder) (*Context, error) {
tmplRef := tmplHolder.GetTemplateRef()
if tmplRef != nil {
tmplName := tmplRef.Name
if tmplRef.ClusterScope {
return ctx.WithClusterWorkflowTemplate(tmplName)
} else {
return ctx.WithWorkflowTemplate(tmplName)
}
}
return ctx.WithTemplateBase(ctx.tmplBase), nil
}
// WithTemplateBase creates new context with a wfv1.TemplateHolder.
func (ctx *Context) WithTemplateBase(tmplBase wfv1.TemplateHolder) *Context {
return NewContext(ctx.wftmplGetter, ctx.cwftmplGetter, tmplBase, ctx.workflow)
}
// WithWorkflowTemplate creates new context with a wfv1.TemplateHolder.
func (ctx *Context) WithWorkflowTemplate(name string) (*Context, error) {
wftmpl, err := ctx.wftmplGetter.Get(name)
if err != nil {
return nil, err
}
return ctx.WithTemplateBase(wftmpl), nil
}
// WithWorkflowTemplate creates new context with a wfv1.TemplateHolder.
func (ctx *Context) WithClusterWorkflowTemplate(name string) (*Context, error) {
cwftmpl, err := ctx.cwftmplGetter.Get(name)
if err != nil {
return nil, err
}
return ctx.WithTemplateBase(cwftmpl), nil
}