-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
workflow_server.go
643 lines (560 loc) · 20.2 KB
/
workflow_server.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
package workflow
import (
"encoding/json"
"fmt"
"io"
"sort"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc/metadata"
corev1 "k8s.io/api/core/v1"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/argoproj/argo-workflows/v3/errors"
"github.com/argoproj/argo-workflows/v3/persist/sqldb"
workflowpkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/workflow"
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/pkg/client/clientset/versioned"
"github.com/argoproj/argo-workflows/v3/server/auth"
argoutil "github.com/argoproj/argo-workflows/v3/util"
"github.com/argoproj/argo-workflows/v3/util/fields"
"github.com/argoproj/argo-workflows/v3/util/instanceid"
"github.com/argoproj/argo-workflows/v3/util/logs"
"github.com/argoproj/argo-workflows/v3/workflow/common"
"github.com/argoproj/argo-workflows/v3/workflow/creator"
"github.com/argoproj/argo-workflows/v3/workflow/hydrator"
"github.com/argoproj/argo-workflows/v3/workflow/templateresolution"
"github.com/argoproj/argo-workflows/v3/workflow/util"
"github.com/argoproj/argo-workflows/v3/workflow/validate"
)
type workflowServer struct {
instanceIDService instanceid.Service
offloadNodeStatusRepo sqldb.OffloadNodeStatusRepo
hydrator hydrator.Interface
}
const latestAlias = "@latest"
// NewWorkflowServer returns a new workflowServer
func NewWorkflowServer(instanceIDService instanceid.Service, offloadNodeStatusRepo sqldb.OffloadNodeStatusRepo) workflowpkg.WorkflowServiceServer {
return &workflowServer{instanceIDService, offloadNodeStatusRepo, hydrator.New(offloadNodeStatusRepo)}
}
func (s *workflowServer) CreateWorkflow(ctx context.Context, req *workflowpkg.WorkflowCreateRequest) (*wfv1.Workflow, error) {
wfClient := auth.GetWfClient(ctx)
if req.Workflow == nil {
return nil, fmt.Errorf("workflow body not specified")
}
if req.Workflow.Namespace == "" {
req.Workflow.Namespace = req.Namespace
}
s.instanceIDService.Label(req.Workflow)
creator.Label(ctx, req.Workflow)
wftmplGetter := templateresolution.WrapWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().WorkflowTemplates(req.Namespace))
cwftmplGetter := templateresolution.WrapClusterWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates())
err := validate.ValidateWorkflow(wftmplGetter, cwftmplGetter, req.Workflow, validate.ValidateOpts{})
if err != nil {
return nil, err
}
// if we are doing a normal dryRun, just return the workflow un-altered
if req.CreateOptions != nil && len(req.CreateOptions.DryRun) > 0 {
return req.Workflow, nil
}
if req.ServerDryRun {
return util.CreateServerDryRun(ctx, req.Workflow, wfClient)
}
wf, err := wfClient.ArgoprojV1alpha1().Workflows(req.Namespace).Create(ctx, req.Workflow, metav1.CreateOptions{})
if err != nil {
if apierr.IsServerTimeout(err) && req.Workflow.GenerateName != "" && req.Workflow.Name != "" {
errWithHint := fmt.Errorf(`create request failed due to timeout, but it's possible that workflow "%s" already exists. Original error: %w`, req.Workflow.Name, err)
log.WithError(errWithHint).Error(errWithHint.Error())
return nil, errWithHint
}
log.WithError(err).Error("Create request failed")
return nil, err
}
return wf, nil
}
func (s *workflowServer) GetWorkflow(ctx context.Context, req *workflowpkg.WorkflowGetRequest) (*wfv1.Workflow, error) {
wfGetOption := metav1.GetOptions{}
if req.GetOptions != nil {
wfGetOption = *req.GetOptions
}
wfClient := auth.GetWfClient(ctx)
wf, err := s.getWorkflow(ctx, wfClient, req.Namespace, req.Name, wfGetOption)
if err != nil {
return nil, err
}
err = s.validateWorkflow(wf)
if err != nil {
return nil, err
}
cleaner := fields.NewCleaner(req.Fields)
if !cleaner.WillExclude("status.nodes") {
if err := s.hydrator.Hydrate(wf); err != nil {
return nil, err
}
}
newWf := &wfv1.Workflow{}
if ok, err := cleaner.Clean(wf, &newWf); err != nil {
return nil, fmt.Errorf("unable to CleanFields in request: %w", err)
} else if ok {
return newWf, nil
}
return wf, nil
}
func (s *workflowServer) ListWorkflows(ctx context.Context, req *workflowpkg.WorkflowListRequest) (*wfv1.WorkflowList, error) {
wfClient := auth.GetWfClient(ctx)
listOption := &metav1.ListOptions{}
if req.ListOptions != nil {
listOption = req.ListOptions
}
s.instanceIDService.With(listOption)
wfList, err := wfClient.ArgoprojV1alpha1().Workflows(req.Namespace).List(ctx, *listOption)
if err != nil {
return nil, err
}
cleaner := fields.NewCleaner(req.Fields)
if s.offloadNodeStatusRepo.IsEnabled() && !cleaner.WillExclude("items.status.nodes") {
offloadedNodes, err := s.offloadNodeStatusRepo.List(req.Namespace)
if err != nil {
return nil, err
}
for i, wf := range wfList.Items {
if wf.Status.IsOffloadNodeStatus() {
if s.offloadNodeStatusRepo.IsEnabled() {
wfList.Items[i].Status.Nodes = offloadedNodes[sqldb.UUIDVersion{UID: string(wf.UID), Version: wf.GetOffloadNodeStatusVersion()}]
} else {
log.WithFields(log.Fields{"namespace": wf.Namespace, "name": wf.Name}).Warn(sqldb.OffloadNodeStatusDisabled)
}
}
}
}
// we make no promises about the overall list sorting, we just sort each page
sort.Sort(wfList.Items)
res := &wfv1.WorkflowList{ListMeta: metav1.ListMeta{Continue: wfList.Continue, ResourceVersion: wfList.ResourceVersion}, Items: wfList.Items}
newRes := &wfv1.WorkflowList{}
if ok, err := cleaner.Clean(res, &newRes); err != nil {
return nil, fmt.Errorf("unable to CleanFields in request: %w", err)
} else if ok {
return newRes, nil
}
return res, nil
}
func (s *workflowServer) WatchWorkflows(req *workflowpkg.WatchWorkflowsRequest, ws workflowpkg.WorkflowService_WatchWorkflowsServer) error {
ctx := ws.Context()
wfClient := auth.GetWfClient(ctx)
opts := &metav1.ListOptions{}
if req.ListOptions != nil {
opts = req.ListOptions
wfName := argoutil.RecoverWorkflowNameFromSelectorStringIfAny(opts.FieldSelector)
if wfName != "" {
// If we are using an alias (such as `@latest`) we need to dereference it.
// s.getWorkflow does that for us
wf, err := s.getWorkflow(ctx, wfClient, req.Namespace, wfName, metav1.GetOptions{})
if err != nil {
return err
}
opts.FieldSelector = argoutil.GenerateFieldSelectorFromWorkflowName(wf.Name)
}
}
s.instanceIDService.With(opts)
wfIf := wfClient.ArgoprojV1alpha1().Workflows(req.Namespace)
watch, err := wfIf.Watch(ctx, *opts)
if err != nil {
return err
}
defer watch.Stop()
cleaner := fields.NewCleaner(req.Fields).WithoutPrefix("result.object.")
clean := func(x *wfv1.Workflow) (*wfv1.Workflow, error) {
y := &wfv1.Workflow{}
if clean, err := cleaner.Clean(x, y); err != nil {
return nil, err
} else if clean {
return y, nil
} else {
return x, nil
}
}
log.Debug("Piping events to channel")
defer log.Debug("Result channel done")
// Eagerly send the headers so that we can begin our keepalive loop if no results are received
// immediately. Without this, we cannot detect a streaming response, and we can't write to the
// response since a subsequent write by the stream causes an error.
err = ws.SendHeader(metadata.MD{})
if err != nil {
return err
}
for {
select {
case <-ctx.Done():
return nil
case event, open := <-watch.ResultChan():
if !open {
return io.EOF
}
log.Debug("Received workflow event")
wf, ok := event.Object.(*wfv1.Workflow)
if !ok {
// object is probably metav1.Status, `FromObject` can deal with anything
return apierr.FromObject(event.Object)
}
logCtx := log.WithFields(log.Fields{"workflow": wf.Name, "type": event.Type, "phase": wf.Status.Phase})
if !cleaner.WillExclude("status.nodes") {
if err := s.hydrator.Hydrate(wf); err != nil {
return err
}
}
newWf, err := clean(wf)
if err != nil {
return fmt.Errorf("unable to CleanFields in request: %w", err)
}
logCtx.Debug("Sending workflow event")
err = ws.Send(&workflowpkg.WorkflowWatchEvent{Type: string(event.Type), Object: newWf})
if err != nil {
return err
}
}
}
}
func (s *workflowServer) WatchEvents(req *workflowpkg.WatchEventsRequest, ws workflowpkg.WorkflowService_WatchEventsServer) error {
ctx := ws.Context()
kubeClient := auth.GetKubeClient(ctx)
opts := &metav1.ListOptions{}
if req.ListOptions != nil {
opts = req.ListOptions
}
s.instanceIDService.With(opts)
eventInterface := kubeClient.CoreV1().Events(req.Namespace)
watch, err := eventInterface.Watch(ctx, *opts)
if err != nil {
return err
}
defer watch.Stop()
log.Debug("Piping events to channel")
defer log.Debug("Result channel done")
err = ws.SendHeader(metadata.MD{})
if err != nil {
return err
}
for {
select {
case <-ctx.Done():
return nil
case event, open := <-watch.ResultChan():
if !open {
return io.EOF
}
log.Debug("Received event")
e, ok := event.Object.(*corev1.Event)
if !ok {
// object is probably probably metav1.Status, `FromObject` can deal with anything
return apierr.FromObject(event.Object)
}
log.Debug("Sending event")
err = ws.Send(e)
if err != nil {
return err
}
}
}
}
func (s *workflowServer) DeleteWorkflow(ctx context.Context, req *workflowpkg.WorkflowDeleteRequest) (*workflowpkg.WorkflowDeleteResponse, error) {
wfClient := auth.GetWfClient(ctx)
wf, err := s.getWorkflow(ctx, wfClient, req.Namespace, req.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = s.validateWorkflow(wf)
if err != nil {
return nil, err
}
if req.Force {
_, err := auth.GetWfClient(ctx).ArgoprojV1alpha1().Workflows(wf.Namespace).Patch(ctx, wf.Name, types.MergePatchType, []byte("{\"metadata\":{\"finalizers\":null}}"), metav1.PatchOptions{})
if err != nil {
return nil, err
}
}
err = auth.GetWfClient(ctx).ArgoprojV1alpha1().Workflows(wf.Namespace).Delete(ctx, wf.Name, metav1.DeleteOptions{PropagationPolicy: argoutil.GetDeletePropagation()})
if err != nil {
return nil, err
}
return &workflowpkg.WorkflowDeleteResponse{}, nil
}
func (s *workflowServer) RetryWorkflow(ctx context.Context, req *workflowpkg.WorkflowRetryRequest) (*wfv1.Workflow, error) {
wfClient := auth.GetWfClient(ctx)
kubeClient := auth.GetKubeClient(ctx)
wf, err := s.getWorkflow(ctx, wfClient, req.Namespace, req.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = s.validateWorkflow(wf)
if err != nil {
return nil, err
}
err = s.hydrator.Hydrate(wf)
if err != nil {
return nil, err
}
wf, podsToDelete, err := util.FormulateRetryWorkflow(ctx, wf, req.RestartSuccessful, req.NodeFieldSelector, req.Parameters)
if err != nil {
return nil, err
}
for _, podName := range podsToDelete {
log.WithFields(log.Fields{"podDeleted": podName}).Info("Deleting pod")
err := kubeClient.CoreV1().Pods(wf.Namespace).Delete(ctx, podName, metav1.DeleteOptions{})
if err != nil && !apierr.IsNotFound(err) {
return nil, err
}
}
err = s.hydrator.Dehydrate(wf)
if err != nil {
return nil, err
}
wf, err = wfClient.ArgoprojV1alpha1().Workflows(req.Namespace).Update(ctx, wf, metav1.UpdateOptions{})
if err != nil {
return nil, err
}
return wf, nil
}
func (s *workflowServer) ResubmitWorkflow(ctx context.Context, req *workflowpkg.WorkflowResubmitRequest) (*wfv1.Workflow, error) {
wfClient := auth.GetWfClient(ctx)
wf, err := s.getWorkflow(ctx, wfClient, req.Namespace, req.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = s.validateWorkflow(wf)
if err != nil {
return nil, err
}
newWF, err := util.FormulateResubmitWorkflow(wf, req.Memoized, req.Parameters)
if err != nil {
return nil, err
}
created, err := util.SubmitWorkflow(ctx, wfClient.ArgoprojV1alpha1().Workflows(req.Namespace), wfClient, req.Namespace, newWF, &wfv1.SubmitOpts{})
if err != nil {
return nil, err
}
return created, nil
}
func (s *workflowServer) ResumeWorkflow(ctx context.Context, req *workflowpkg.WorkflowResumeRequest) (*wfv1.Workflow, error) {
wfClient := auth.GetWfClient(ctx)
wf, err := s.getWorkflow(ctx, wfClient, req.Namespace, req.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = s.validateWorkflow(wf)
if err != nil {
return nil, err
}
err = util.ResumeWorkflow(ctx, wfClient.ArgoprojV1alpha1().Workflows(req.Namespace), s.hydrator, wf.Name, req.NodeFieldSelector)
if err != nil {
log.WithFields(log.Fields{"name": wf.Name}).WithError(err).Warn("Failed to resume")
return nil, err
}
wf, err = wfClient.ArgoprojV1alpha1().Workflows(req.Namespace).Get(ctx, wf.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
return wf, nil
}
func (s *workflowServer) SuspendWorkflow(ctx context.Context, req *workflowpkg.WorkflowSuspendRequest) (*wfv1.Workflow, error) {
wfClient := auth.GetWfClient(ctx)
wf, err := s.getWorkflow(ctx, wfClient, req.Namespace, req.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = s.validateWorkflow(wf)
if err != nil {
return nil, err
}
err = util.SuspendWorkflow(ctx, wfClient.ArgoprojV1alpha1().Workflows(wf.Namespace), wf.Name)
if err != nil {
return nil, err
}
wf, err = wfClient.ArgoprojV1alpha1().Workflows(req.Namespace).Get(ctx, wf.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
return wf, nil
}
func (s *workflowServer) TerminateWorkflow(ctx context.Context, req *workflowpkg.WorkflowTerminateRequest) (*wfv1.Workflow, error) {
wfClient := auth.GetWfClient(ctx)
wf, err := s.getWorkflow(ctx, wfClient, req.Namespace, req.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = s.validateWorkflow(wf)
if err != nil {
return nil, err
}
err = util.TerminateWorkflow(ctx, wfClient.ArgoprojV1alpha1().Workflows(req.Namespace), wf.Name)
if err != nil {
return nil, err
}
wf, err = wfClient.ArgoprojV1alpha1().Workflows(req.Namespace).Get(ctx, wf.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
return wf, nil
}
func (s *workflowServer) StopWorkflow(ctx context.Context, req *workflowpkg.WorkflowStopRequest) (*wfv1.Workflow, error) {
wfClient := auth.GetWfClient(ctx)
wf, err := s.getWorkflow(ctx, wfClient, req.Namespace, req.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = s.validateWorkflow(wf)
if err != nil {
return nil, err
}
err = util.StopWorkflow(ctx, wfClient.ArgoprojV1alpha1().Workflows(req.Namespace), s.hydrator, wf.Name, req.NodeFieldSelector, req.Message)
if err != nil {
return nil, err
}
wf, err = wfClient.ArgoprojV1alpha1().Workflows(req.Namespace).Get(ctx, wf.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
return wf, nil
}
func (s *workflowServer) SetWorkflow(ctx context.Context, req *workflowpkg.WorkflowSetRequest) (*wfv1.Workflow, error) {
wfClient := auth.GetWfClient(ctx)
wf, err := s.getWorkflow(ctx, wfClient, req.Namespace, req.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = s.validateWorkflow(wf)
if err != nil {
return nil, err
}
phaseToSet := wfv1.NodePhase(req.Phase)
switch phaseToSet {
case wfv1.NodeSucceeded, wfv1.NodeFailed, wfv1.NodeError, "":
// Do nothing, passes validation
default:
return nil, fmt.Errorf("%s is an invalid phase to set to", req.Phase)
}
outputParams := make(map[string]string)
if req.OutputParameters != "" {
err = json.Unmarshal([]byte(req.OutputParameters), &outputParams)
if err != nil {
return nil, fmt.Errorf("unable to parse output parameter set request: %s", err)
}
}
operation := util.SetOperationValues{
Phase: phaseToSet,
Message: req.Message,
OutputParameters: outputParams,
}
err = util.SetWorkflow(ctx, wfClient.ArgoprojV1alpha1().Workflows(req.Namespace), s.hydrator, wf.Name, req.NodeFieldSelector, operation)
if err != nil {
return nil, err
}
wf, err = wfClient.ArgoprojV1alpha1().Workflows(req.Namespace).Get(ctx, wf.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
return wf, nil
}
func (s *workflowServer) LintWorkflow(ctx context.Context, req *workflowpkg.WorkflowLintRequest) (*wfv1.Workflow, error) {
if req.Workflow == nil {
return nil, fmt.Errorf("unable to get a workflow")
}
wfClient := auth.GetWfClient(ctx)
wftmplGetter := templateresolution.WrapWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().WorkflowTemplates(req.Namespace))
cwftmplGetter := templateresolution.WrapClusterWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates())
s.instanceIDService.Label(req.Workflow)
creator.Label(ctx, req.Workflow)
err := validate.ValidateWorkflow(wftmplGetter, cwftmplGetter, req.Workflow, validate.ValidateOpts{Lint: true})
if err != nil {
return nil, err
}
return req.Workflow, nil
}
func (s *workflowServer) PodLogs(req *workflowpkg.WorkflowLogRequest, ws workflowpkg.WorkflowService_PodLogsServer) error {
ctx := ws.Context()
wfClient := auth.GetWfClient(ctx)
kubeClient := auth.GetKubeClient(ctx)
wf, err := s.getWorkflow(ctx, wfClient, req.Namespace, req.Name, metav1.GetOptions{})
if err != nil {
return err
}
err = s.validateWorkflow(wf)
if err != nil {
return err
}
req.Name = wf.Name
err = ws.SendHeader(metadata.MD{})
if err != nil {
return err
}
return logs.WorkflowLogs(ctx, wfClient, kubeClient, req, ws)
}
func (s *workflowServer) WorkflowLogs(req *workflowpkg.WorkflowLogRequest, ws workflowpkg.WorkflowService_WorkflowLogsServer) error {
return s.PodLogs(req, ws)
}
func (s *workflowServer) getWorkflow(ctx context.Context, wfClient versioned.Interface, namespace string, name string, options metav1.GetOptions) (*wfv1.Workflow, error) {
if name == latestAlias {
latest, err := getLatestWorkflow(ctx, wfClient, namespace)
if err != nil {
return nil, err
}
log.Debugf("Resolved alias %s to workflow %s.\n", latestAlias, latest.Name)
return latest, nil
}
wf, err := wfClient.ArgoprojV1alpha1().Workflows(namespace).Get(ctx, name, options)
if err != nil {
return nil, err
}
return wf, nil
}
func (s *workflowServer) validateWorkflow(wf *wfv1.Workflow) error {
return s.instanceIDService.Validate(wf)
}
func getLatestWorkflow(ctx context.Context, wfClient versioned.Interface, namespace string) (*wfv1.Workflow, error) {
wfList, err := wfClient.ArgoprojV1alpha1().Workflows(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
if len(wfList.Items) < 1 {
return nil, fmt.Errorf("No workflows found.")
}
latest := wfList.Items[0]
for _, wf := range wfList.Items {
if latest.ObjectMeta.CreationTimestamp.Before(&wf.ObjectMeta.CreationTimestamp) {
latest = wf
}
}
return &latest, nil
}
func (s *workflowServer) SubmitWorkflow(ctx context.Context, req *workflowpkg.WorkflowSubmitRequest) (*wfv1.Workflow, error) {
wfClient := auth.GetWfClient(ctx)
var wf *wfv1.Workflow
switch req.ResourceKind {
case workflow.CronWorkflowKind, workflow.CronWorkflowSingular, workflow.CronWorkflowPlural, workflow.CronWorkflowShortName:
cronWf, err := wfClient.ArgoprojV1alpha1().CronWorkflows(req.Namespace).Get(ctx, req.ResourceName, metav1.GetOptions{})
if err != nil {
return nil, err
}
wf = common.ConvertCronWorkflowToWorkflow(cronWf)
case workflow.WorkflowTemplateKind, workflow.WorkflowTemplateSingular, workflow.WorkflowTemplatePlural, workflow.WorkflowTemplateShortName:
wf = common.NewWorkflowFromWorkflowTemplate(req.ResourceName, false)
case workflow.ClusterWorkflowTemplateKind, workflow.ClusterWorkflowTemplateSingular, workflow.ClusterWorkflowTemplatePlural, workflow.ClusterWorkflowTemplateShortName:
wf = common.NewWorkflowFromWorkflowTemplate(req.ResourceName, true)
default:
return nil, errors.Errorf(errors.CodeBadRequest, "Resource kind '%s' is not supported for submitting", req.ResourceKind)
}
s.instanceIDService.Label(wf)
creator.Label(ctx, wf)
err := util.ApplySubmitOpts(wf, req.SubmitOptions)
if err != nil {
return nil, err
}
wftmplGetter := templateresolution.WrapWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().WorkflowTemplates(req.Namespace))
cwftmplGetter := templateresolution.WrapClusterWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates())
err = validate.ValidateWorkflow(wftmplGetter, cwftmplGetter, wf, validate.ValidateOpts{Submit: true})
if err != nil {
return nil, err
}
return wfClient.ArgoprojV1alpha1().Workflows(req.Namespace).Create(ctx, wf, metav1.CreateOptions{})
}