-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
745 lines (686 loc) · 22.6 KB
/
util.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
package util
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
apiv1 "k8s.io/api/core/v1"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/informers/internalinterfaces"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/utils/pointer"
"sigs.k8s.io/yaml"
"github.com/argoproj/argo/errors"
"github.com/argoproj/argo/pkg/apis/workflow"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
wfclientset "github.com/argoproj/argo/pkg/client/clientset/versioned"
"github.com/argoproj/argo/pkg/client/clientset/versioned/typed/workflow/v1alpha1"
cmdutil "github.com/argoproj/argo/util/cmd"
"github.com/argoproj/argo/util/retry"
unstructutil "github.com/argoproj/argo/util/unstructured"
"github.com/argoproj/argo/workflow/common"
"github.com/argoproj/argo/workflow/packer"
"github.com/argoproj/argo/workflow/templateresolution"
"github.com/argoproj/argo/workflow/validate"
)
// NewWorkflowInformer returns the workflow informer used by the controller. This is actually
// a custom built UnstructuredInformer which is in actuality returning unstructured.Unstructured
// objects. We no longer return WorkflowInformer due to:
// https://github.com/kubernetes/kubernetes/issues/57705
// https://github.com/argoproj/argo/issues/632
func NewWorkflowInformer(cfg *rest.Config, ns string, resyncPeriod time.Duration, tweakListOptions internalinterfaces.TweakListOptionsFunc) cache.SharedIndexInformer {
dclient, err := dynamic.NewForConfig(cfg)
if err != nil {
panic(err)
}
resource := schema.GroupVersionResource{
Group: workflow.Group,
Version: "v1alpha1",
Resource: workflow.WorkflowPlural,
}
informer := unstructutil.NewFilteredUnstructuredInformer(
resource,
dclient,
ns,
resyncPeriod,
cache.Indexers{},
tweakListOptions,
)
return informer
}
// InstanceIDRequirement returns the label requirement to filter against a controller instance (or not)
func InstanceIDRequirement(instanceID string) labels.Requirement {
var instanceIDReq *labels.Requirement
var err error
if instanceID != "" {
instanceIDReq, err = labels.NewRequirement(common.LabelKeyControllerInstanceID, selection.Equals, []string{instanceID})
} else {
instanceIDReq, err = labels.NewRequirement(common.LabelKeyControllerInstanceID, selection.DoesNotExist, nil)
}
if err != nil {
panic(err)
}
return *instanceIDReq
}
// WorkflowLister implements the List() method of v1alpha.WorkflowLister interface but does so using
// an Unstructured informer and converting objects to workflows. Ignores objects that failed to convert.
type WorkflowLister interface {
List() ([]*wfv1.Workflow, error)
}
type workflowLister struct {
informer cache.SharedIndexInformer
}
func (l *workflowLister) List() ([]*wfv1.Workflow, error) {
workflows := make([]*wfv1.Workflow, 0)
for _, m := range l.informer.GetStore().List() {
wf, err := FromUnstructured(m.(*unstructured.Unstructured))
if err != nil {
log.Warnf("Failed to unmarshal workflow %v object: %v", m, err)
continue
}
workflows = append(workflows, wf)
}
return workflows, nil
}
// NewWorkflowLister returns a new workflow lister
func NewWorkflowLister(informer cache.SharedIndexInformer) WorkflowLister {
return &workflowLister{
informer: informer,
}
}
// FromUnstructured converts an unstructured object to a workflow
func FromUnstructured(un *unstructured.Unstructured) (*wfv1.Workflow, error) {
var wf wfv1.Workflow
err := runtime.DefaultUnstructuredConverter.FromUnstructured(un.Object, &wf)
if wf.Spec.TTLSecondsAfterFinished != nil {
if wf.Spec.TTLStrategy == nil {
ttlstrategy := wfv1.TTLStrategy{SecondsAfterCompletion: wf.Spec.TTLSecondsAfterFinished}
wf.Spec.TTLStrategy = &ttlstrategy
} else if wf.Spec.TTLStrategy.SecondsAfterCompletion == nil {
wf.Spec.TTLStrategy.SecondsAfterCompletion = wf.Spec.TTLSecondsAfterFinished
}
}
return &wf, err
}
// ToUnstructured converts an workflow to an Unstructured object
func ToUnstructured(wf *wfv1.Workflow) (*unstructured.Unstructured, error) {
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(wf)
return &unstructured.Unstructured{Object: obj}, err
}
// IsWorkflowCompleted returns whether or not a workflow is considered completed
func IsWorkflowCompleted(wf *wfv1.Workflow) bool {
if wf.ObjectMeta.Labels != nil {
return wf.ObjectMeta.Labels[common.LabelKeyCompleted] == "true"
}
return false
}
// SubmitOpts are workflow submission options
type SubmitOpts struct {
Name string // --name
GenerateName string // --generate-name
InstanceID string // --instanceid
Entrypoint string // --entrypoint
Parameters []string // --parameter
ParameterFile string // --parameter-file
ServiceAccount string // --serviceaccount
DryRun bool // --dry-run
ServerDryRun bool // --server-dry-run
Labels string // --labels
OwnerReference *metav1.OwnerReference // useful if your custom controller creates argo workflow resources
}
// SubmitWorkflow validates and submit a single workflow and override some of the fields of the workflow
func SubmitWorkflow(wfIf v1alpha1.WorkflowInterface, wfClientset wfclientset.Interface, namespace string, wf *wfv1.Workflow, opts *SubmitOpts) (*wfv1.Workflow, error) {
err := ApplySubmitOpts(wf, opts)
if err != nil {
return nil, err
}
wftmplGetter := templateresolution.WrapWorkflowTemplateInterface(wfClientset.ArgoprojV1alpha1().WorkflowTemplates(namespace))
err = validate.ValidateWorkflow(wftmplGetter, wf, validate.ValidateOpts{})
if err != nil {
return nil, err
}
if opts.DryRun {
return wf, nil
} else if opts.ServerDryRun {
wf, err := CreateServerDryRun(wf, wfClientset)
if err != nil {
return nil, err
}
return wf, err
} else {
return wfIf.Create(wf)
}
}
// CreateServerDryRun fills the workflow struct with the server's representation without creating it and returns an error, if there is any
func CreateServerDryRun(wf *wfv1.Workflow, wfClientset wfclientset.Interface) (*wfv1.Workflow, error) {
// Keep the workflow metadata because it will be overwritten by the Post request
workflowTypeMeta := wf.TypeMeta
err := wfClientset.ArgoprojV1alpha1().RESTClient().Post().
Namespace(wf.Namespace).
Resource("workflows").
Body(wf).
Param("dryRun", "All").
Do().
Into(wf)
wf.TypeMeta = workflowTypeMeta
return wf, err
}
// Apply the Submit options into workflow object
func ApplySubmitOpts(wf *wfv1.Workflow, opts *SubmitOpts) error {
if opts == nil {
opts = &SubmitOpts{}
}
if opts.Entrypoint != "" {
wf.Spec.Entrypoint = opts.Entrypoint
}
if opts.ServiceAccount != "" {
wf.Spec.ServiceAccountName = opts.ServiceAccount
}
labels := wf.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
if opts.Labels != "" {
passedLabels, err := cmdutil.ParseLabels(opts.Labels)
if err != nil {
return fmt.Errorf("Expected labels of the form: NAME1=VALUE2,NAME2=VALUE2. Received: %s", opts.Labels)
}
for k, v := range passedLabels {
labels[k] = v
}
}
if opts.InstanceID != "" {
labels[common.LabelKeyControllerInstanceID] = opts.InstanceID
}
wf.SetLabels(labels)
if len(opts.Parameters) > 0 || opts.ParameterFile != "" {
newParams := make([]wfv1.Parameter, 0)
passedParams := make(map[string]bool)
for _, paramStr := range opts.Parameters {
parts := strings.SplitN(paramStr, "=", 2)
if len(parts) == 1 {
return fmt.Errorf("Expected parameter of the form: NAME=VALUE. Received: %s", paramStr)
}
param := wfv1.Parameter{
Name: parts[0],
Value: &parts[1],
}
newParams = append(newParams, param)
passedParams[param.Name] = true
}
// Add parameters from a parameter-file, if one was provided
if opts.ParameterFile != "" {
var body []byte
var err error
if cmdutil.IsURL(opts.ParameterFile) {
body, err = ReadFromUrl(opts.ParameterFile)
if err != nil {
return errors.InternalWrapError(err)
}
} else {
body, err = ioutil.ReadFile(opts.ParameterFile)
if err != nil {
return errors.InternalWrapError(err)
}
}
yamlParams := map[string]json.RawMessage{}
err = yaml.Unmarshal(body, &yamlParams)
if err != nil {
return errors.InternalWrapError(err)
}
for k, v := range yamlParams {
// We get quoted strings from the yaml file.
value, err := strconv.Unquote(string(v))
if err != nil {
// the string is already clean.
value = string(v)
}
param := wfv1.Parameter{
Name: k,
Value: &value,
}
if _, ok := passedParams[param.Name]; ok {
// this parameter was overridden via command line
continue
}
newParams = append(newParams, param)
passedParams[param.Name] = true
}
}
for _, param := range wf.Spec.Arguments.Parameters {
if _, ok := passedParams[param.Name]; ok {
// this parameter was overridden via command line
continue
}
newParams = append(newParams, param)
}
wf.Spec.Arguments.Parameters = newParams
}
if opts.GenerateName != "" {
wf.ObjectMeta.GenerateName = opts.GenerateName
}
if opts.Name != "" {
wf.ObjectMeta.Name = opts.Name
}
if opts.OwnerReference != nil {
wf.SetOwnerReferences(append(wf.GetOwnerReferences(), *opts.OwnerReference))
}
return nil
}
// SuspendWorkflow suspends a workflow by setting spec.suspend to true. Retries conflict errors
func SuspendWorkflow(wfIf v1alpha1.WorkflowInterface, workflowName string) error {
err := wait.ExponentialBackoff(retry.DefaultRetry, func() (bool, error) {
wf, err := wfIf.Get(workflowName, metav1.GetOptions{})
if err != nil {
return false, err
}
if IsWorkflowCompleted(wf) {
return false, errSuspendedCompletedWorkflow
}
if wf.Spec.Suspend == nil || !*wf.Spec.Suspend {
wf.Spec.Suspend = pointer.BoolPtr(true)
_, err = wfIf.Update(wf)
if err != nil {
if apierr.IsConflict(err) {
return false, nil
}
return false, err
}
}
return true, nil
})
return err
}
// ResumeWorkflow resumes a workflow by setting spec.suspend to nil and any suspended nodes to Successful.
// Retries conflict errors
func ResumeWorkflow(wfIf v1alpha1.WorkflowInterface, workflowName string) error {
err := wait.ExponentialBackoff(retry.DefaultRetry, func() (bool, error) {
wf, err := wfIf.Get(workflowName, metav1.GetOptions{})
if err != nil {
return false, err
}
err = packer.DecompressWorkflow(wf)
if err != nil {
log.Fatal(err)
}
updated := false
if wf.Spec.Suspend != nil && *wf.Spec.Suspend {
wf.Spec.Suspend = nil
updated = true
}
// To resume a workflow with a suspended node we simply mark the node as Successful
for nodeID, node := range wf.Status.Nodes {
if node.Type == wfv1.NodeTypeSuspend && node.Phase == wfv1.NodeRunning {
node.Phase = wfv1.NodeSucceeded
node.FinishedAt = metav1.Time{Time: time.Now().UTC()}
wf.Status.Nodes[nodeID] = node
updated = true
}
}
if updated {
_, err = wfIf.Update(wf)
if err != nil {
if apierr.IsConflict(err) {
return false, nil
}
return false, err
}
}
return true, nil
})
return err
}
const letters = "abcdefghijklmnopqrstuvwxyz0123456789"
func init() {
rand.Seed(time.Now().UnixNano())
}
func randString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
// FormulateResubmitWorkflow formulate a new workflow from a previous workflow, optionally re-using successful nodes
func FormulateResubmitWorkflow(wf *wfv1.Workflow, memoized bool) (*wfv1.Workflow, error) {
newWF := wfv1.Workflow{}
newWF.TypeMeta = wf.TypeMeta
// Resubmitted workflow will use generated names
if wf.ObjectMeta.GenerateName != "" {
newWF.ObjectMeta.GenerateName = wf.ObjectMeta.GenerateName
} else {
newWF.ObjectMeta.GenerateName = wf.ObjectMeta.Name + "-"
}
// When resubmitting workflow with memoized nodes, we need to use a predetermined workflow name
// in order to formulate the node statuses. Which means we cannot reuse metadata.generateName
// The following simulates the behavior of generateName
if memoized {
switch wf.Status.Phase {
case wfv1.NodeFailed, wfv1.NodeError:
default:
return nil, errors.Errorf(errors.CodeBadRequest, "workflow must be Failed/Error to resubmit in memoized mode")
}
newWF.ObjectMeta.Name = newWF.ObjectMeta.GenerateName + randString(5)
}
// carry over the unmodified spec
newWF.Spec = wf.Spec
if newWF.Spec.ActiveDeadlineSeconds != nil && *newWF.Spec.ActiveDeadlineSeconds == 0 {
// if it was terminated, unset the deadline
newWF.Spec.ActiveDeadlineSeconds = nil
}
// carry over user labels and annotations from previous workflow.
// skip any argoproj.io labels except for the controller instanceID label.
for key, val := range wf.ObjectMeta.Labels {
if strings.HasPrefix(key, workflow.WorkflowFullName+"/") && key != common.LabelKeyControllerInstanceID {
continue
}
if newWF.ObjectMeta.Labels == nil {
newWF.ObjectMeta.Labels = make(map[string]string)
}
newWF.ObjectMeta.Labels[key] = val
}
for key, val := range wf.ObjectMeta.Annotations {
if newWF.ObjectMeta.Annotations == nil {
newWF.ObjectMeta.Annotations = make(map[string]string)
}
newWF.ObjectMeta.Annotations[key] = val
}
if !memoized {
return &newWF, nil
}
// Iterate the previous nodes.
replaceRegexp := regexp.MustCompile("^" + wf.ObjectMeta.Name)
newWF.Status.Nodes = make(map[string]wfv1.NodeStatus)
onExitNodeName := wf.ObjectMeta.Name + ".onExit"
err := packer.DecompressWorkflow(wf)
if err != nil {
log.Fatal(err)
}
for _, node := range wf.Status.Nodes {
newNode := node.DeepCopy()
if strings.HasPrefix(node.Name, onExitNodeName) {
continue
}
originalID := node.ID
newNode.Name = replaceRegexp.ReplaceAllString(node.Name, newWF.ObjectMeta.Name)
newNode.ID = newWF.NodeID(newNode.Name)
if node.BoundaryID != "" {
newNode.BoundaryID = convertNodeID(&newWF, replaceRegexp, node.BoundaryID, wf.Status.Nodes)
}
if !newNode.Successful() && newNode.Type == wfv1.NodeTypePod {
newNode.StartedAt = metav1.Time{}
newNode.FinishedAt = metav1.Time{}
} else {
newNode.StartedAt = metav1.Time{Time: time.Now().UTC()}
newNode.FinishedAt = newNode.StartedAt
}
newChildren := make([]string, len(node.Children))
for i, childID := range node.Children {
newChildren[i] = convertNodeID(&newWF, replaceRegexp, childID, wf.Status.Nodes)
}
newNode.Children = newChildren
newOutboundNodes := make([]string, len(node.OutboundNodes))
for i, outboundID := range node.OutboundNodes {
newOutboundNodes[i] = convertNodeID(&newWF, replaceRegexp, outboundID, wf.Status.Nodes)
}
newNode.OutboundNodes = newOutboundNodes
if newNode.Successful() && newNode.Type == wfv1.NodeTypePod {
newNode.Phase = wfv1.NodeSkipped
newNode.Type = wfv1.NodeTypeSkipped
newNode.Message = fmt.Sprintf("original pod: %s", originalID)
} else {
newNode.Phase = wfv1.NodePending
newNode.Message = ""
}
newWF.Status.Nodes[newNode.ID] = *newNode
}
newWF.Status.StoredTemplates = make(map[string]wfv1.Template)
for id, tmpl := range wf.Status.StoredTemplates {
newWF.Status.StoredTemplates[id] = tmpl
}
newWF.Status.Phase = wfv1.NodePending
return &newWF, nil
}
// convertNodeID converts an old nodeID to a new nodeID
func convertNodeID(newWf *wfv1.Workflow, regex *regexp.Regexp, oldNodeID string, oldNodes map[string]wfv1.NodeStatus) string {
node := oldNodes[oldNodeID]
newNodeName := regex.ReplaceAllString(node.Name, newWf.ObjectMeta.Name)
return newWf.NodeID(newNodeName)
}
// RetryWorkflow updates a workflow, deleting all failed steps as well as the onExit node (and children)
func RetryWorkflow(kubeClient kubernetes.Interface, wfClient v1alpha1.WorkflowInterface, wf *wfv1.Workflow) (*wfv1.Workflow, error) {
switch wf.Status.Phase {
case wfv1.NodeFailed, wfv1.NodeError:
default:
return nil, errors.Errorf(errors.CodeBadRequest, "workflow must be Failed/Error to retry")
}
newWF := wf.DeepCopy()
podIf := kubeClient.CoreV1().Pods(wf.ObjectMeta.Namespace)
// Delete/reset fields which indicate workflow completed
delete(newWF.Labels, common.LabelKeyCompleted)
newWF.ObjectMeta.Labels[common.LabelKeyPhase] = string(wfv1.NodeRunning)
newWF.Status.Phase = wfv1.NodeRunning
newWF.Status.Message = ""
newWF.Status.FinishedAt = metav1.Time{}
if newWF.Spec.ActiveDeadlineSeconds != nil && *newWF.Spec.ActiveDeadlineSeconds == 0 {
// if it was terminated, unset the deadline
newWF.Spec.ActiveDeadlineSeconds = nil
}
// Iterate the previous nodes. If it was successful Pod carry it forward
newWF.Status.Nodes = make(map[string]wfv1.NodeStatus)
onExitNodeName := wf.ObjectMeta.Name + ".onExit"
for _, node := range wf.Status.Nodes {
switch node.Phase {
case wfv1.NodeSucceeded, wfv1.NodeSkipped:
if !strings.HasPrefix(node.Name, onExitNodeName) {
newWF.Status.Nodes[node.ID] = node
continue
}
case wfv1.NodeError, wfv1.NodeFailed:
if !strings.HasPrefix(node.Name, onExitNodeName) && node.Type == wfv1.NodeTypeDAG {
newNode := node.DeepCopy()
newNode.Phase = wfv1.NodeRunning
newNode.Message = ""
newNode.FinishedAt = metav1.Time{}
newWF.Status.Nodes[newNode.ID] = *newNode
continue
}
// do not add this status to the node. pretend as if this node never existed.
default:
// Do not allow retry of workflows with pods in Running/Pending phase
return nil, errors.InternalErrorf("Workflow cannot be retried with node %s in %s phase", node.Name, node.Phase)
}
if node.Type == wfv1.NodeTypePod {
log.Infof("Deleting pod: %s", node.ID)
err := podIf.Delete(node.ID, &metav1.DeleteOptions{})
if err != nil && !apierr.IsNotFound(err) {
return nil, errors.InternalWrapError(err)
}
} else if node.Name == wf.ObjectMeta.Name {
newNode := node.DeepCopy()
newNode.Phase = wfv1.NodeRunning
newNode.Message = ""
newNode.FinishedAt = metav1.Time{}
newWF.Status.Nodes[newNode.ID] = *newNode
continue
}
}
newWF.Status.StoredTemplates = make(map[string]wfv1.Template)
for id, tmpl := range wf.Status.StoredTemplates {
newWF.Status.StoredTemplates[id] = tmpl
}
return wfClient.Update(newWF)
}
var errSuspendedCompletedWorkflow = errors.Errorf(errors.CodeBadRequest, "cannot suspend completed workflows")
// IsWorkflowSuspended returns whether or not a workflow is considered suspended
func IsWorkflowSuspended(wf *wfv1.Workflow) bool {
if wf.Spec.Suspend != nil && *wf.Spec.Suspend {
return true
}
for _, node := range wf.Status.Nodes {
if node.Type == wfv1.NodeTypeSuspend && node.Phase == wfv1.NodeRunning {
return true
}
}
return false
}
// IsWorkflowTerminated returns whether or not a workflow is considered terminated
func IsWorkflowTerminated(wf *wfv1.Workflow) bool {
if wf.Spec.ActiveDeadlineSeconds != nil && *wf.Spec.ActiveDeadlineSeconds == 0 {
return true
}
return false
}
// TerminateWorkflow terminates a workflow by setting its activeDeadlineSeconds to 0
func TerminateWorkflow(wfClient v1alpha1.WorkflowInterface, name string) error {
patchObj := map[string]interface{}{
"spec": map[string]interface{}{
"activeDeadlineSeconds": 0,
},
}
var err error
patch, err := json.Marshal(patchObj)
if err != nil {
return errors.InternalWrapError(err)
}
for attempt := 0; attempt < 10; attempt++ {
_, err = wfClient.Patch(name, types.MergePatchType, patch)
if err != nil {
if !apierr.IsConflict(err) {
return err
}
} else {
break
}
time.Sleep(100 * time.Millisecond)
}
return err
}
// Reads from stdin
func ReadFromStdin() ([]byte, error) {
reader := bufio.NewReader(os.Stdin)
body, err := ioutil.ReadAll(reader)
if err != nil {
return []byte{}, err
}
return body, err
}
// Reads the content of a url
func ReadFromUrl(url string) ([]byte, error) {
response, err := http.Get(url)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(response.Body)
_ = response.Body.Close()
if err != nil {
return nil, err
}
return body, err
}
// ReadFromFilePathsOrUrls reads the content of a single or a list of file paths and/or urls
func ReadFromFilePathsOrUrls(filePathsOrUrls ...string) ([][]byte, error) {
var fileContents [][]byte
var body []byte
var err error
for _, filePathOrUrl := range filePathsOrUrls {
if cmdutil.IsURL(filePathOrUrl) {
body, err = ReadFromUrl(filePathOrUrl)
if err != nil {
return [][]byte{}, err
}
} else {
body, err = ioutil.ReadFile(filePathOrUrl)
if err != nil {
return [][]byte{}, err
}
}
fileContents = append(fileContents, body)
}
return fileContents, err
}
// ReadManifest reads from stdin, a single file/url, or a list of files and/or urls
func ReadManifest(manifestPaths ...string) ([][]byte, error) {
var manifestContents [][]byte
var err error
if len(manifestPaths) == 1 && manifestPaths[0] == "-" {
body, err := ReadFromStdin()
if err != nil {
return [][]byte{}, err
}
manifestContents = append(manifestContents, body)
} else {
manifestContents, err = ReadFromFilePathsOrUrls(manifestPaths...)
if err != nil {
return [][]byte{}, err
}
}
return manifestContents, err
}
func IsJSONStr(str string) bool {
str = strings.TrimSpace(str)
return len(str) > 0 && str[0] == '{'
}
func ConvertYAMLToJSON(str string) (string, error) {
if !IsJSONStr(str) {
jsonStr, err := yaml.YAMLToJSON([]byte(str))
if err != nil {
return str, err
}
return string(jsonStr), nil
}
return str, nil
}
// PodSpecPatchMerge will do strategic merge the workflow level PodSpecPatch and template level PodSpecPatch
func PodSpecPatchMerge(wf *wfv1.Workflow, tmpl *wfv1.Template) (string, error) {
var wfPatch, tmplPatch, mergedPatch string
var err error
if wf.Spec.HasPodSpecPatch() {
wfPatch, err = ConvertYAMLToJSON(wf.Spec.PodSpecPatch)
if err != nil {
return "", err
}
}
if tmpl.HasPodSpecPatch() {
tmplPatch, err = ConvertYAMLToJSON(tmpl.PodSpecPatch)
if err != nil {
return "", err
}
if wfPatch != "" {
mergedByte, err := strategicpatch.StrategicMergePatch([]byte(wfPatch), []byte(tmplPatch), apiv1.PodSpec{})
if err != nil {
return "", err
}
mergedPatch = string(mergedByte)
} else {
mergedPatch = tmplPatch
}
} else {
mergedPatch = wfPatch
}
return mergedPatch, nil
}
func ValidateJsonStr(jsonStr string, schema interface{}) bool {
err := json.Unmarshal([]byte(jsonStr), &schema)
return err == nil
}