forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator.go
1513 lines (1425 loc) · 52.1 KB
/
operator.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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package controller
import (
"encoding/json"
"fmt"
"reflect"
"regexp"
"runtime/debug"
"sort"
"strconv"
"strings"
"time"
argokubeerr "github.com/argoproj/pkg/kube/errors"
"github.com/argoproj/pkg/strftime"
jsonpatch "github.com/evanphx/json-patch"
log "github.com/sirupsen/logrus"
"github.com/valyala/fasttemplate"
apiv1 "k8s.io/api/core/v1"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
"github.com/argoproj/argo/errors"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo/pkg/client/clientset/versioned/typed/workflow/v1alpha1"
"github.com/argoproj/argo/util/retry"
"github.com/argoproj/argo/workflow/common"
"github.com/argoproj/argo/workflow/util"
"github.com/argoproj/argo/workflow/validate"
)
// wfOperationCtx is the context for evaluation and operation of a single workflow
type wfOperationCtx struct {
// wf is the workflow object
wf *wfv1.Workflow
// orig is the original workflow object for purposes of creating a patch
orig *wfv1.Workflow
// updated indicates whether or not the workflow object itself was updated
// and needs to be persisted back to kubernetes
updated bool
// log is an logrus logging context to corralate logs with a workflow
log *log.Entry
// controller reference to workflow controller
controller *WorkflowController
// globalParams holds any parameters that are available to be referenced
// in the global scope (e.g. workflow.parameters.XXX).
globalParams map[string]string
// map of pods which need to be labeled with completed=true
completedPods map[string]bool
// deadline is the dealine time in which this operation should relinquish
// its hold on the workflow so that an operation does not run for too long
// and starve other workqueue items. It also enables workflow progress to
// be periodically synced to the database.
deadline time.Time
// activePods tracks the number of active (Running/Pending) pods for controlling
// parallelism
activePods int64
// workflowDeadline is the deadline which the workflow is expected to complete before we
// terminate the workflow.
workflowDeadline *time.Time
}
var (
// ErrDeadlineExceeded indicates the operation exceeded its deadline for execution
ErrDeadlineExceeded = errors.New(errors.CodeTimeout, "Deadline exceeded")
// ErrParallelismReached indicates this workflow reached its parallelism limit
ErrParallelismReached = errors.New(errors.CodeForbidden, "Max parallelism reached")
)
// maxOperationTime is the maximum time a workflow operation is allowed to run
// for before requeuing the workflow onto the workqueue.
const maxOperationTime time.Duration = 10 * time.Second
// newWorkflowOperationCtx creates and initializes a new wfOperationCtx object.
func newWorkflowOperationCtx(wf *wfv1.Workflow, wfc *WorkflowController) *wfOperationCtx {
// NEVER modify objects from the store. It's a read-only, local cache.
// You can use DeepCopy() to make a deep copy of original object and modify this copy
// Or create a copy manually for better performance
woc := wfOperationCtx{
wf: wf.DeepCopyObject().(*wfv1.Workflow),
orig: wf,
updated: false,
log: log.WithFields(log.Fields{
"workflow": wf.ObjectMeta.Name,
"namespace": wf.ObjectMeta.Namespace,
}),
controller: wfc,
globalParams: make(map[string]string),
completedPods: make(map[string]bool),
deadline: time.Now().UTC().Add(maxOperationTime),
}
if woc.wf.Status.Nodes == nil {
woc.wf.Status.Nodes = make(map[string]wfv1.NodeStatus)
}
return &woc
}
// operate is the main operator logic of a workflow. It evaluates the current state of the workflow,
// and its pods and decides how to proceed down the execution path.
// TODO: an error returned by this method should result in requeuing the workflow to be retried at a
// later time
func (woc *wfOperationCtx) operate() {
defer woc.persistUpdates()
defer func() {
if r := recover(); r != nil {
if rerr, ok := r.(error); ok {
woc.markWorkflowError(rerr, true)
} else {
woc.markWorkflowPhase(wfv1.NodeError, true, fmt.Sprintf("%v", r))
}
woc.log.Errorf("Recovered from panic: %+v\n%s", r, debug.Stack())
}
}()
woc.log.Infof("Processing workflow")
// Perform one-time workflow validation
if woc.wf.Status.Phase == "" {
woc.markWorkflowRunning()
err := validate.ValidateWorkflow(woc.wf)
if err != nil {
woc.markWorkflowFailed(fmt.Sprintf("invalid spec: %s", err.Error()))
return
}
woc.workflowDeadline = woc.getWorkflowDeadline()
} else {
woc.workflowDeadline = woc.getWorkflowDeadline()
err := woc.podReconciliation()
if err != nil {
woc.log.Errorf("%s error: %+v", woc.wf.ObjectMeta.Name, err)
// TODO: we need to re-add to the workqueue, but should happen in caller
return
}
}
if woc.wf.Spec.Suspend != nil && *woc.wf.Spec.Suspend {
woc.log.Infof("workflow suspended")
return
}
if woc.wf.Spec.Parallelism != nil {
woc.activePods = woc.countActivePods()
}
woc.setGlobalParameters()
err := woc.createPVCs()
if err != nil {
woc.log.Errorf("%s pvc create error: %+v", woc.wf.ObjectMeta.Name, err)
woc.markWorkflowError(err, true)
return
}
var workflowStatus wfv1.NodePhase
var workflowMessage string
node, _ := woc.executeTemplate(woc.wf.Spec.Entrypoint, woc.wf.Spec.Arguments, woc.wf.ObjectMeta.Name, "")
if node == nil || !node.Completed() {
// node can be nil if a workflow created immediately in a parallelism == 0 state
return
}
workflowStatus = node.Phase
if !node.Successful() && util.IsWorkflowTerminated(woc.wf) {
workflowMessage = "terminated"
} else {
workflowMessage = node.Message
}
var onExitNode *wfv1.NodeStatus
if woc.wf.Spec.OnExit != "" {
if workflowStatus == wfv1.NodeSkipped {
// treat skipped the same as Succeeded for workflow.status
woc.globalParams[common.GlobalVarWorkflowStatus] = string(wfv1.NodeSucceeded)
} else {
woc.globalParams[common.GlobalVarWorkflowStatus] = string(workflowStatus)
}
woc.log.Infof("Running OnExit handler: %s", woc.wf.Spec.OnExit)
onExitNodeName := woc.wf.ObjectMeta.Name + ".onExit"
onExitNode, _ = woc.executeTemplate(woc.wf.Spec.OnExit, woc.wf.Spec.Arguments, onExitNodeName, "")
if onExitNode == nil || !onExitNode.Completed() {
return
}
}
err = woc.deletePVCs()
if err != nil {
woc.log.Errorf("%s error: %+v", woc.wf.ObjectMeta.Name, err)
// Mark the workflow with an error message and return, but intentionally do not
// markCompletion so that we can retry PVC deletion (TODO: use workqueue.ReAdd())
// This error phase may be cleared if a subsequent delete attempt is successful.
woc.markWorkflowError(err, false)
return
}
// If we get here, the workflow completed, all PVCs were deleted successfully, and
// exit handlers were executed. We now need to infer the workflow phase from the
// node phase.
switch workflowStatus {
case wfv1.NodeSucceeded, wfv1.NodeSkipped:
if onExitNode != nil && !onExitNode.Successful() {
// if main workflow succeeded, but the exit node was unsuccessful
// the workflow is now considered unsuccessful.
woc.markWorkflowPhase(onExitNode.Phase, true, onExitNode.Message)
} else {
woc.markWorkflowSuccess()
}
case wfv1.NodeFailed:
woc.markWorkflowFailed(workflowMessage)
case wfv1.NodeError:
woc.markWorkflowPhase(wfv1.NodeError, true, workflowMessage)
default:
// NOTE: we should never make it here because if the the node was 'Running'
// we should have returned earlier.
err = errors.InternalErrorf("Unexpected node phase %s: %+v", woc.wf.ObjectMeta.Name, err)
woc.markWorkflowError(err, true)
}
}
func (woc *wfOperationCtx) getWorkflowDeadline() *time.Time {
if woc.wf.Spec.ActiveDeadlineSeconds == nil {
return nil
}
if woc.wf.Status.StartedAt.IsZero() {
return nil
}
if *woc.wf.Spec.ActiveDeadlineSeconds == 0 {
// A zero value for ActiveDeadlineSeconds has special meaning (killed).
// Return a zero value time object
return &time.Time{}
}
startedAt := woc.wf.Status.StartedAt.Truncate(time.Second)
deadline := startedAt.Add(time.Duration(*woc.wf.Spec.ActiveDeadlineSeconds) * time.Second).UTC()
return &deadline
}
// setGlobalParameters sets the globalParam map with global parameters
func (woc *wfOperationCtx) setGlobalParameters() {
woc.globalParams[common.GlobalVarWorkflowName] = woc.wf.ObjectMeta.Name
woc.globalParams[common.GlobalVarWorkflowNamespace] = woc.wf.ObjectMeta.Namespace
woc.globalParams[common.GlobalVarWorkflowUID] = string(woc.wf.ObjectMeta.UID)
woc.globalParams[common.GlobalVarWorkflowCreationTimestamp] = woc.wf.ObjectMeta.CreationTimestamp.String()
for char := range strftime.FormatChars {
cTimeVar := fmt.Sprintf("%s.%s", common.GlobalVarWorkflowCreationTimestamp, string(char))
woc.globalParams[cTimeVar] = strftime.Format("%"+string(char), woc.wf.ObjectMeta.CreationTimestamp.Time)
}
for _, param := range woc.wf.Spec.Arguments.Parameters {
woc.globalParams["workflow.parameters."+param.Name] = *param.Value
}
if woc.wf.Status.Outputs != nil {
for _, param := range woc.wf.Status.Outputs.Parameters {
woc.globalParams["workflow.outputs.parameters."+param.Name] = *param.Value
}
}
}
func (woc *wfOperationCtx) getNodeByName(nodeName string) *wfv1.NodeStatus {
nodeID := woc.wf.NodeID(nodeName)
node, ok := woc.wf.Status.Nodes[nodeID]
if !ok {
return nil
}
return &node
}
// persistUpdates will update a workflow with any updates made during workflow operation.
// It also labels any pods as completed if we have extracted everything we need from it.
// NOTE: a previous implementation used Patch instead of Update, but Patch does not work with
// the fake CRD clientset which makes unit testing extremely difficult.
func (woc *wfOperationCtx) persistUpdates() {
if !woc.updated {
return
}
wfClient := woc.controller.wfclientset.ArgoprojV1alpha1().Workflows(woc.wf.ObjectMeta.Namespace)
_, err := wfClient.Update(woc.wf)
if err != nil {
woc.log.Warnf("Error updating workflow: %v", err)
if argokubeerr.IsRequestEntityTooLargeErr(err) {
woc.persistWorkflowSizeLimitErr(wfClient, err)
return
}
if !apierr.IsConflict(err) {
return
}
woc.log.Info("Re-appying updates on latest version and retrying update")
err = woc.reapplyUpdate(wfClient)
if err != nil {
woc.log.Infof("Failed to re-apply update: %+v", err)
return
}
}
woc.log.Info("Workflow update successful")
// HACK(jessesuen) after we successfully persist an update to the workflow, the informer's
// cache is now invalid. It's very common that we will need to immediately re-operate on a
// workflow due to queuing by the pod workers. The following sleep gives a *chance* for the
// informer's cache to catch up to the version of the workflow we just persisted. Without
// this sleep, the next worker to work on this workflow will very likely operate on a stale
// object and redo work.
time.Sleep(1 * time.Second)
// It is important that we *never* label pods as completed until we successfully updated the workflow
// Failing to do so means we can have inconsistent state.
for podName := range woc.completedPods {
woc.controller.completedPods <- fmt.Sprintf("%s/%s", woc.wf.ObjectMeta.Namespace, podName)
}
}
// persistWorkflowSizeLimitErr will fail a the workflow with an error when we hit the resource size limit
// See https://github.com/argoproj/argo/issues/913
func (woc *wfOperationCtx) persistWorkflowSizeLimitErr(wfClient v1alpha1.WorkflowInterface, err error) {
woc.wf = woc.orig.DeepCopy()
woc.markWorkflowError(err, true)
_, err = wfClient.Update(woc.wf)
if err != nil {
woc.log.Warnf("Error updating workflow: %v", err)
}
}
// reapplyUpdate GETs the latest version of the workflow, re-applies the updates and
// retries the UPDATE multiple times. For reasoning behind this technique, see:
// https://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md#concurrency-control-and-consistency
func (woc *wfOperationCtx) reapplyUpdate(wfClient v1alpha1.WorkflowInterface) error {
// First generate the patch
oldData, err := json.Marshal(woc.orig)
if err != nil {
return errors.InternalWrapError(err)
}
newData, err := json.Marshal(woc.wf)
if err != nil {
return errors.InternalWrapError(err)
}
patchBytes, err := jsonpatch.CreateMergePatch(oldData, newData)
if err != nil {
return errors.InternalWrapError(err)
}
// Next get latest version of the workflow, apply the patch and retyr the Update
attempt := 1
for {
currWf, err := wfClient.Get(woc.wf.ObjectMeta.Name, metav1.GetOptions{})
if !retry.IsRetryableKubeAPIError(err) {
return errors.InternalWrapError(err)
}
currWfBytes, err := json.Marshal(currWf)
if err != nil {
return errors.InternalWrapError(err)
}
newWfBytes, err := jsonpatch.MergePatch(currWfBytes, patchBytes)
if err != nil {
return errors.InternalWrapError(err)
}
var newWf wfv1.Workflow
err = json.Unmarshal(newWfBytes, &newWf)
if err != nil {
return errors.InternalWrapError(err)
}
_, err = wfClient.Update(&newWf)
if err == nil {
woc.log.Infof("Update retry attempt %d successful", attempt)
return nil
}
attempt++
woc.log.Warnf("Update retry attempt %d failed: %v", attempt, err)
if attempt > 5 {
return err
}
}
}
// requeue this workflow onto the workqueue for later processing
func (woc *wfOperationCtx) requeue() {
key, err := cache.MetaNamespaceKeyFunc(woc.wf)
if err != nil {
woc.log.Errorf("Failed to requeue workflow %s: %v", woc.wf.ObjectMeta.Name, err)
return
}
woc.controller.wfQueue.Add(key)
}
func (woc *wfOperationCtx) processNodeRetries(node *wfv1.NodeStatus, retryStrategy wfv1.RetryStrategy) error {
if node.Completed() {
return nil
}
lastChildNode, err := woc.getLastChildNode(node)
if err != nil {
return fmt.Errorf("Failed to find last child of node " + node.Name)
}
if lastChildNode == nil {
return nil
}
if !lastChildNode.Completed() {
// last child node is still running.
return nil
}
if lastChildNode.Successful() {
node.Outputs = lastChildNode.Outputs.DeepCopy()
woc.wf.Status.Nodes[node.ID] = *node
woc.markNodePhase(node.Name, wfv1.NodeSucceeded)
return nil
}
if !lastChildNode.CanRetry() {
woc.log.Infof("Node cannot be retried. Marking it failed")
woc.markNodePhase(node.Name, wfv1.NodeFailed, lastChildNode.Message)
return nil
}
if retryStrategy.Limit != nil && int32(len(node.Children)) > *retryStrategy.Limit {
woc.log.Infoln("No more retries left. Failing...")
woc.markNodePhase(node.Name, wfv1.NodeFailed, "No more retries left")
return nil
}
woc.log.Infof("%d child nodes of %s failed. Trying again...", len(node.Children), node.Name)
return nil
}
// podReconciliation is the process by which a workflow will examine all its related
// pods and update the node state before continuing the evaluation of the workflow.
// Records all pods which were observed completed, which will be labeled completed=true
// after successful persist of the workflow.
func (woc *wfOperationCtx) podReconciliation() error {
podList, err := woc.getAllWorkflowPods()
if err != nil {
return err
}
seenPods := make(map[string]bool)
performAssessment := func(pod *apiv1.Pod) {
nodeNameForPod := pod.Annotations[common.AnnotationKeyNodeName]
nodeID := woc.wf.NodeID(nodeNameForPod)
seenPods[nodeID] = true
if node, ok := woc.wf.Status.Nodes[nodeID]; ok {
if newState := assessNodeStatus(pod, &node); newState != nil {
woc.wf.Status.Nodes[nodeID] = *newState
woc.addOutputsToScope("workflow", node.Outputs, nil)
woc.updated = true
}
if woc.wf.Status.Nodes[pod.ObjectMeta.Name].Completed() {
woc.completedPods[pod.ObjectMeta.Name] = true
}
}
}
for _, pod := range podList.Items {
performAssessment(&pod)
err = woc.applyExecutionControl(&pod)
if err != nil {
woc.log.Warnf("Failed to apply execution control to pod %s", pod.Name)
}
}
// Now check for deleted pods. Iterate our nodes. If any one of our nodes does not show up in
// the seen list it implies that the pod was deleted without the controller seeing the event.
// It is now impossible to infer pod status. The only thing we can do at this point is to mark
// the node with Error.
for nodeID, node := range woc.wf.Status.Nodes {
if node.Type != wfv1.NodeTypePod || node.Completed() {
// node is not a pod, or it is already complete
continue
}
if _, ok := seenPods[nodeID]; !ok {
node.Message = "pod deleted"
node.Phase = wfv1.NodeError
woc.wf.Status.Nodes[nodeID] = node
woc.log.Warnf("pod %s deleted", nodeID)
woc.updated = true
}
}
return nil
}
// countActivePods counts the number of active (Pending/Running) pods.
// Optionally restricts it to a template invocation (boundaryID)
func (woc *wfOperationCtx) countActivePods(boundaryIDs ...string) int64 {
var boundaryID = ""
if len(boundaryIDs) > 0 {
boundaryID = boundaryIDs[0]
}
var activePods int64
// if we care about parallelism, count the active pods at the template level
for _, node := range woc.wf.Status.Nodes {
if node.Type != wfv1.NodeTypePod {
continue
}
if boundaryID != "" && node.BoundaryID != boundaryID {
continue
}
switch node.Phase {
case wfv1.NodePending, wfv1.NodeRunning:
activePods++
}
}
return activePods
}
// getAllWorkflowPods returns all pods related to the current workflow
func (woc *wfOperationCtx) getAllWorkflowPods() (*apiv1.PodList, error) {
options := metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s",
common.LabelKeyWorkflow,
woc.wf.ObjectMeta.Name),
}
podList, err := woc.controller.kubeclientset.CoreV1().Pods(woc.wf.Namespace).List(options)
if err != nil {
return nil, errors.InternalWrapError(err)
}
return podList, nil
}
// assessNodeStatus compares the current state of a pod with its corresponding node
// and returns the new node status if something changed
func assessNodeStatus(pod *apiv1.Pod, node *wfv1.NodeStatus) *wfv1.NodeStatus {
var newPhase wfv1.NodePhase
var newDaemonStatus *bool
var message string
updated := false
f := false
switch pod.Status.Phase {
case apiv1.PodPending:
newPhase = wfv1.NodePending
newDaemonStatus = &f
message = getPendingReason(pod)
case apiv1.PodSucceeded:
newPhase = wfv1.NodeSucceeded
newDaemonStatus = &f
case apiv1.PodFailed:
newPhase, message = inferFailedReason(pod)
newDaemonStatus = &f
case apiv1.PodRunning:
newPhase = wfv1.NodeRunning
tmplStr, ok := pod.Annotations[common.AnnotationKeyTemplate]
if !ok {
log.Warnf("%s missing template annotation", pod.ObjectMeta.Name)
return nil
}
var tmpl wfv1.Template
err := json.Unmarshal([]byte(tmplStr), &tmpl)
if err != nil {
log.Warnf("%s template annotation unreadable: %v", pod.ObjectMeta.Name, err)
return nil
}
if tmpl.Daemon != nil && *tmpl.Daemon {
// pod is running and template is marked daemon. check if everything is ready
for _, ctrStatus := range pod.Status.ContainerStatuses {
if !ctrStatus.Ready {
return nil
}
}
// proceed to mark node status as succeeded (and daemoned)
newPhase = wfv1.NodeSucceeded
t := true
newDaemonStatus = &t
log.Infof("Processing ready daemon pod: %v", pod.ObjectMeta.SelfLink)
}
default:
newPhase = wfv1.NodeError
message = fmt.Sprintf("Unexpected pod phase for %s: %s", pod.ObjectMeta.Name, pod.Status.Phase)
log.Error(message)
}
if newDaemonStatus != nil {
if *newDaemonStatus == false {
// if the daemon status switched to false, we prefer to just unset daemoned status field
// (as opposed to setting it to false)
newDaemonStatus = nil
}
if (newDaemonStatus != nil && node.Daemoned == nil) || (newDaemonStatus == nil && node.Daemoned != nil) {
log.Infof("Setting node %v daemoned: %v -> %v", node, node.Daemoned, newDaemonStatus)
node.Daemoned = newDaemonStatus
updated = true
if pod.Status.PodIP != "" && pod.Status.PodIP != node.PodIP {
// only update Pod IP for daemoned nodes to reduce number of updates
log.Infof("Updating daemon node %s IP %s -> %s", node, node.PodIP, pod.Status.PodIP)
node.PodIP = pod.Status.PodIP
}
}
}
outputStr, ok := pod.Annotations[common.AnnotationKeyOutputs]
if ok && node.Outputs == nil {
updated = true
log.Infof("Setting node %v outputs", node)
var outputs wfv1.Outputs
err := json.Unmarshal([]byte(outputStr), &outputs)
if err != nil {
log.Errorf("Failed to unmarshal %s outputs from pod annotation: %v", pod.Name, err)
node.Phase = wfv1.NodeError
} else {
node.Outputs = &outputs
}
}
if node.Phase != newPhase {
log.Infof("Updating node %s status %s -> %s", node, node.Phase, newPhase)
// if we are transitioning from Pending to a different state, clear out pending message
if node.Phase == wfv1.NodePending {
node.Message = ""
}
updated = true
node.Phase = newPhase
}
if message != "" && node.Message != message {
log.Infof("Updating node %s message: %s", node, message)
updated = true
node.Message = message
}
if node.Completed() && node.FinishedAt.IsZero() {
updated = true
if !node.IsDaemoned() {
node.FinishedAt = getLatestFinishedAt(pod)
}
if node.FinishedAt.IsZero() {
// If we get here, the container is daemoned so the
// finishedAt might not have been set.
node.FinishedAt = metav1.Time{Time: time.Now().UTC()}
}
}
if updated {
return node
}
return nil
}
// getLatestFinishedAt returns the latest finishAt timestamp from all the
// containers of this pod.
func getLatestFinishedAt(pod *apiv1.Pod) metav1.Time {
var latest metav1.Time
for _, ctr := range pod.Status.InitContainerStatuses {
if ctr.State.Terminated != nil && ctr.State.Terminated.FinishedAt.After(latest.Time) {
latest = ctr.State.Terminated.FinishedAt
}
}
for _, ctr := range pod.Status.ContainerStatuses {
if ctr.State.Terminated != nil && ctr.State.Terminated.FinishedAt.After(latest.Time) {
latest = ctr.State.Terminated.FinishedAt
}
}
return latest
}
func getPendingReason(pod *apiv1.Pod) string {
for _, ctrStatus := range pod.Status.ContainerStatuses {
if ctrStatus.State.Waiting != nil {
if ctrStatus.State.Waiting.Message != "" {
return fmt.Sprintf("%s: %s", ctrStatus.State.Waiting.Reason, ctrStatus.State.Waiting.Message)
}
return ctrStatus.State.Waiting.Reason
}
}
// Example:
// - lastProbeTime: null
// lastTransitionTime: 2018-08-29T06:38:36Z
// message: '0/3 nodes are available: 2 Insufficient cpu, 3 MatchNodeSelector.'
// reason: Unschedulable
// status: "False"
// type: PodScheduled
for _, cond := range pod.Status.Conditions {
if cond.Reason == apiv1.PodReasonUnschedulable {
if cond.Message != "" {
return fmt.Sprintf("%s: %s", cond.Reason, cond.Message)
}
return cond.Reason
}
}
return ""
}
// inferFailedReason returns metadata about a Failed pod to be used in its NodeStatus
// Returns a tuple of the new phase and message
func inferFailedReason(pod *apiv1.Pod) (wfv1.NodePhase, string) {
if pod.Status.Message != "" {
// Pod has a nice error message. Use that.
return wfv1.NodeFailed, pod.Status.Message
}
annotatedMsg := pod.Annotations[common.AnnotationKeyNodeMessage]
// We only get one message to set for the overall node status.
// If multiple containers failed, in order of preference:
// init, main (annotated), main (exit code), wait, sidecars
for _, ctr := range pod.Status.InitContainerStatuses {
if ctr.State.Terminated == nil {
// We should never get here
log.Warnf("Pod %s phase was Failed but %s did not have terminated state", pod.ObjectMeta.Name, ctr.Name)
continue
}
if ctr.State.Terminated.ExitCode == 0 {
continue
}
errMsg := fmt.Sprintf("failed to load artifacts")
for _, msg := range []string{annotatedMsg, ctr.State.Terminated.Message} {
if msg != "" {
errMsg += ": " + msg
break
}
}
// NOTE: we consider artifact load issues as Error instead of Failed
return wfv1.NodeError, errMsg
}
failMessages := make(map[string]string)
for _, ctr := range pod.Status.ContainerStatuses {
if ctr.State.Terminated == nil {
// We should never get here
log.Warnf("Pod %s phase was Failed but %s did not have terminated state", pod.ObjectMeta.Name, ctr.Name)
continue
}
if ctr.State.Terminated.ExitCode == 0 {
continue
}
if ctr.Name == common.WaitContainerName {
errDetails := ""
for _, msg := range []string{annotatedMsg, ctr.State.Terminated.Message} {
if msg != "" {
errDetails = msg
break
}
}
if errDetails == "" {
// executor is expected to annotate a message to the pod upon any errors.
// If we failed to see the annotated message, it is likely the pod ran with
// insufficient privileges. Give a hint to that effect.
errDetails = fmt.Sprintf("verify serviceaccount %s:%s has necessary privileges", pod.ObjectMeta.Namespace, pod.Spec.ServiceAccountName)
}
errMsg := fmt.Sprintf("failed to save outputs: %s", errDetails)
failMessages[ctr.Name] = errMsg
continue
}
if ctr.State.Terminated.Message != "" {
failMessages[ctr.Name] = ctr.State.Terminated.Message
continue
}
if ctr.State.Terminated.Reason == "OOMKilled" {
failMessages[ctr.Name] = ctr.State.Terminated.Reason
continue
}
errMsg := fmt.Sprintf("failed with exit code %d", ctr.State.Terminated.ExitCode)
if ctr.Name != common.MainContainerName {
if ctr.State.Terminated.ExitCode == 137 {
// if the sidecar was SIGKILL'd (exit code 137) assume it was because argoexec
// forcibly killed the container, which we ignore the error for.
log.Infof("Ignoring %d exit code of sidecar '%s'", ctr.State.Terminated.ExitCode, ctr.Name)
continue
}
errMsg = fmt.Sprintf("sidecar '%s' %s", ctr.Name, errMsg)
}
failMessages[ctr.Name] = errMsg
}
if failMsg, ok := failMessages[common.MainContainerName]; ok {
_, ok = failMessages[common.WaitContainerName]
isResourceTemplate := !ok
if isResourceTemplate && annotatedMsg != "" {
// For resource templates, we prefer the annotated message
// over the vanilla exit code 1 error
return wfv1.NodeFailed, annotatedMsg
}
return wfv1.NodeFailed, failMsg
}
if failMsg, ok := failMessages[common.WaitContainerName]; ok {
return wfv1.NodeError, failMsg
}
// If we get here, both the main and wait container succeeded. Iterate the fail messages to
// identify the sidecar which failed and return the message.
for _, failMsg := range failMessages {
return wfv1.NodeFailed, failMsg
}
// If we get here, we have detected that the main/wait containers succeed but the sidecar(s)
// were SIGKILL'd. The executor may have had to forcefully terminate the sidecar (kill -9),
// resulting in a 137 exit code (which we had ignored earlier). If failMessages is empty, it
// indicates that this is the case and we return Success instead of Failure.
return wfv1.NodeSucceeded, ""
}
func (woc *wfOperationCtx) createPVCs() error {
if woc.wf.Status.Phase != wfv1.NodeRunning {
// Only attempt to create PVCs if workflow transitioned to Running state
// (e.g. passed validation, or didn't already complete)
return nil
}
if len(woc.wf.Spec.VolumeClaimTemplates) == len(woc.wf.Status.PersistentVolumeClaims) {
// If we have already created the PVCs, then there is nothing to do.
// This will also handle the case where workflow has no volumeClaimTemplates.
return nil
}
if len(woc.wf.Status.PersistentVolumeClaims) == 0 {
woc.wf.Status.PersistentVolumeClaims = make([]apiv1.Volume, len(woc.wf.Spec.VolumeClaimTemplates))
}
pvcClient := woc.controller.kubeclientset.CoreV1().PersistentVolumeClaims(woc.wf.ObjectMeta.Namespace)
for i, pvcTmpl := range woc.wf.Spec.VolumeClaimTemplates {
if pvcTmpl.ObjectMeta.Name == "" {
return errors.Errorf(errors.CodeBadRequest, "volumeClaimTemplates[%d].metadata.name is required", i)
}
pvcTmpl = *pvcTmpl.DeepCopy()
// PVC name will be <workflowname>-<volumeclaimtemplatename>
refName := pvcTmpl.ObjectMeta.Name
pvcName := fmt.Sprintf("%s-%s", woc.wf.ObjectMeta.Name, pvcTmpl.ObjectMeta.Name)
woc.log.Infof("Creating pvc %s", pvcName)
pvcTmpl.ObjectMeta.Name = pvcName
pvcTmpl.OwnerReferences = []metav1.OwnerReference{
*metav1.NewControllerRef(woc.wf, wfv1.SchemaGroupVersionKind),
}
pvc, err := pvcClient.Create(&pvcTmpl)
if err != nil {
return err
}
vol := apiv1.Volume{
Name: refName,
VolumeSource: apiv1.VolumeSource{
PersistentVolumeClaim: &apiv1.PersistentVolumeClaimVolumeSource{
ClaimName: pvc.ObjectMeta.Name,
},
},
}
woc.wf.Status.PersistentVolumeClaims[i] = vol
woc.updated = true
}
return nil
}
func (woc *wfOperationCtx) deletePVCs() error {
totalPVCs := len(woc.wf.Status.PersistentVolumeClaims)
if totalPVCs == 0 {
// PVC list already empty. nothing to do
return nil
}
pvcClient := woc.controller.kubeclientset.CoreV1().PersistentVolumeClaims(woc.wf.ObjectMeta.Namespace)
newPVClist := make([]apiv1.Volume, 0)
// Attempt to delete all PVCs. Record first error encountered
var firstErr error
for _, pvc := range woc.wf.Status.PersistentVolumeClaims {
woc.log.Infof("Deleting PVC %s", pvc.PersistentVolumeClaim.ClaimName)
err := pvcClient.Delete(pvc.PersistentVolumeClaim.ClaimName, nil)
if err != nil {
if !apierr.IsNotFound(err) {
woc.log.Errorf("Failed to delete pvc %s: %v", pvc.PersistentVolumeClaim.ClaimName, err)
newPVClist = append(newPVClist, pvc)
if firstErr == nil {
firstErr = err
}
}
}
}
if len(newPVClist) != totalPVCs {
// we were successful in deleting one ore more PVCs
woc.log.Infof("Deleted %d/%d PVCs", totalPVCs-len(newPVClist), totalPVCs)
woc.wf.Status.PersistentVolumeClaims = newPVClist
woc.updated = true
}
return firstErr
}
func (woc *wfOperationCtx) getLastChildNode(node *wfv1.NodeStatus) (*wfv1.NodeStatus, error) {
if len(node.Children) <= 0 {
return nil, nil
}
lastChildNodeName := node.Children[len(node.Children)-1]
lastChildNode, ok := woc.wf.Status.Nodes[lastChildNodeName]
if !ok {
return nil, fmt.Errorf("Failed to find node " + lastChildNodeName)
}
return &lastChildNode, nil
}
// executeTemplate executes the template with the given arguments and returns the created NodeStatus
// for the created node (if created). Nodes may not be created if parallelism or deadline exceeded.
// nodeName is the name to be used as the name of the node, and boundaryID indicates which template
// boundary this node belongs to.
func (woc *wfOperationCtx) executeTemplate(templateName string, args wfv1.Arguments, nodeName string, boundaryID string) (*wfv1.NodeStatus, error) {
woc.log.Debugf("Evaluating node %s: template: %s", nodeName, templateName)
node := woc.getNodeByName(nodeName)
if node != nil && node.Completed() {
woc.log.Debugf("Node %s already completed", nodeName)
return node, nil
}
// Check if we took too long operating on this workflow and immediately return if we did
if time.Now().UTC().After(woc.deadline) {
woc.log.Warnf("Deadline exceeded")
woc.requeue()
return node, ErrDeadlineExceeded
}
// Check if we exceeded template or workflow parallelism and immediately return if we did
tmpl := woc.wf.GetTemplate(templateName)
if tmpl == nil {
err := errors.Errorf(errors.CodeBadRequest, "Node %v error: template '%s' undefined", node, templateName)
return woc.initializeNode(nodeName, wfv1.NodeTypeSkipped, "", boundaryID, wfv1.NodeError, err.Error()), err
}
if err := woc.checkParallelism(tmpl, node, boundaryID); err != nil {
return node, err
}
// Perform parameter substitution of the template
localParams := make(map[string]string)
if tmpl.IsPodType() {
localParams[common.LocalVarPodName] = woc.wf.NodeID(nodeName)
}
tmpl, err := common.ProcessArgs(tmpl, args, woc.globalParams, localParams, false)
if err != nil {
return woc.initializeNode(nodeName, wfv1.NodeTypeSkipped, templateName, boundaryID, wfv1.NodeError, err.Error()), err
}
// If the user has specified retries, node becomes a special retry node.
// This node acts as a parent of all retries that will be done for
// the container. The status of this node should be "Success" if any
// of the retries succeed. Otherwise, it is "Failed".
workNodeName := nodeName
retryNodeName := ""
if tmpl.IsLeaf() && tmpl.RetryStrategy != nil {
retryNodeName = nodeName
if node == nil {
node = woc.initializeNode(nodeName, wfv1.NodeTypeRetry, "", boundaryID, wfv1.NodeRunning)
}
if err := woc.processNodeRetries(node, *tmpl.RetryStrategy); err != nil {
woc.markNodeError(nodeName, err)
return node, err
}
node = woc.getNodeByName(retryNodeName)
//woc.log.Infof("Node %s: Status: %s", retryNodeName, node.Phase)
// The retry node might have completed by now.
if node.Completed() {
return node, nil
}
lastChildNode, err := woc.getLastChildNode(node)
if err != nil {
woc.markNodeError(retryNodeName, err)
return node, err
}
if lastChildNode != nil && !lastChildNode.Completed() {
// Last child node is still running.
return node, nil
}
childNodeName := fmt.Sprintf("%s(%d)", retryNodeName, len(node.Children))
// All work is done in a child
workNodeName = childNodeName
}
switch tmpl.GetType() {
case wfv1.TemplateTypeContainer:
node = woc.executeContainer(workNodeName, tmpl, boundaryID)
case wfv1.TemplateTypeSteps:
node = woc.executeSteps(workNodeName, tmpl, boundaryID)
case wfv1.TemplateTypeScript:
node = woc.executeScript(workNodeName, tmpl, boundaryID)
case wfv1.TemplateTypeResource:
node = woc.executeResource(workNodeName, tmpl, boundaryID)
case wfv1.TemplateTypeDAG:
node = woc.executeDAG(workNodeName, tmpl, boundaryID)
case wfv1.TemplateTypeSuspend:
node = woc.executeSuspend(workNodeName, tmpl, boundaryID)
default:
err = errors.Errorf(errors.CodeBadRequest, "Template '%s' missing specification", tmpl.Name)
node = woc.initializeNode(workNodeName, wfv1.NodeTypeSkipped, templateName, boundaryID, wfv1.NodeError, err.Error())
}
// Swap the node back to retry node and add worker node as child.
if retryNodeName != "" {
woc.addChildNode(retryNodeName, workNodeName)
node = woc.getNodeByName(retryNodeName)
}
// Set the input values to the node. This is presented in the UI
if tmpl.Inputs.HasInputs() && node.Inputs == nil {
node.Inputs = &tmpl.Inputs
woc.wf.Status.Nodes[node.ID] = *node
woc.updated = true
}
return node, nil
}
// markWorkflowPhase is a convenience method to set the phase of the workflow with optional message
// optionally marks the workflow completed, which sets the finishedAt timestamp and completed label
func (woc *wfOperationCtx) markWorkflowPhase(phase wfv1.NodePhase, markCompleted bool, message ...string) {
if woc.wf.Status.Phase != phase {
woc.log.Infof("Updated phase %s -> %s", woc.wf.Status.Phase, phase)
woc.updated = true
woc.wf.Status.Phase = phase
if woc.wf.ObjectMeta.Labels == nil {
woc.wf.ObjectMeta.Labels = make(map[string]string)
}
woc.wf.ObjectMeta.Labels[common.LabelKeyPhase] = string(phase)
}
if woc.wf.Status.StartedAt.IsZero() {
woc.updated = true
woc.wf.Status.StartedAt = metav1.Time{Time: time.Now().UTC()}
}
if len(message) > 0 && woc.wf.Status.Message != message[0] {
woc.log.Infof("Updated message %s -> %s", woc.wf.Status.Message, message[0])
woc.updated = true
woc.wf.Status.Message = message[0]
}
switch phase {
case wfv1.NodeSucceeded, wfv1.NodeFailed, wfv1.NodeError:
if markCompleted {
woc.log.Infof("Marking workflow completed")
woc.wf.Status.FinishedAt = metav1.Time{Time: time.Now().UTC()}
if woc.wf.ObjectMeta.Labels == nil {
woc.wf.ObjectMeta.Labels = make(map[string]string)