-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathoperator.go
More file actions
609 lines (538 loc) · 20.8 KB
/
operator.go
File metadata and controls
609 lines (538 loc) · 20.8 KB
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
package cron
import (
"context"
"encoding/json"
"errors"
"fmt"
"sort"
"time"
"github.com/Knetic/govaluate"
"github.com/robfig/cron/v3"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
argoerrs "github.com/argoproj/argo-workflows/v4/errors"
"github.com/argoproj/argo-workflows/v4/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v4/pkg/client/clientset/versioned"
typed "github.com/argoproj/argo-workflows/v4/pkg/client/clientset/versioned/typed/workflow/v1alpha1"
wfextvv1alpha1 "github.com/argoproj/argo-workflows/v4/pkg/client/informers/externalversions/workflow/v1alpha1"
errorsutil "github.com/argoproj/argo-workflows/v4/util/errors"
"github.com/argoproj/argo-workflows/v4/util/expr/argoexpr"
"github.com/argoproj/argo-workflows/v4/util/logging"
"github.com/argoproj/argo-workflows/v4/util/retry"
"github.com/argoproj/argo-workflows/v4/util/template"
waitutil "github.com/argoproj/argo-workflows/v4/util/wait"
"github.com/argoproj/argo-workflows/v4/workflow/common"
"github.com/argoproj/argo-workflows/v4/workflow/metrics"
"github.com/argoproj/argo-workflows/v4/workflow/controller/informer"
"github.com/argoproj/argo-workflows/v4/workflow/util"
"github.com/argoproj/argo-workflows/v4/workflow/validate"
)
const (
variablePrefix string = `cronworkflow`
)
type cronWfOperationCtx struct {
// CronWorkflow is the CronWorkflow to be run
cronWf *v1alpha1.CronWorkflow
wfClientset versioned.Interface
wfClient typed.WorkflowInterface
wfDefaults *v1alpha1.Workflow
cronWfIf typed.CronWorkflowInterface
wftmplInformer wfextvv1alpha1.WorkflowTemplateInformer
cwftmplInformer wfextvv1alpha1.ClusterWorkflowTemplateInformer
log logging.Logger
metrics *metrics.Metrics
// scheduledTimeFunc returns the last scheduled time when it is called
scheduledTimeFunc ScheduledTimeFunc
//nolint: containedctx
ctx context.Context
// modifiedLabels and modifiedAnnotations track metadata keys that the controller
// has explicitly modified during this operation. Only these keys are included
// in the persistUpdate patch, avoiding overwrite of externally-set metadata.
modifiedLabels map[string]string
modifiedAnnotations map[string]string
}
func newCronWfOperationCtx(ctx context.Context, cronWorkflow *v1alpha1.CronWorkflow, wfClientset versioned.Interface,
metrics *metrics.Metrics, wftmplInformer wfextvv1alpha1.WorkflowTemplateInformer,
cwftmplInformer wfextvv1alpha1.ClusterWorkflowTemplateInformer, wfDefaults *v1alpha1.Workflow,
) *cronWfOperationCtx {
log := logging.RequireLoggerFromContext(ctx)
return &cronWfOperationCtx{
cronWf: cronWorkflow,
wfClientset: wfClientset,
wfClient: wfClientset.ArgoprojV1alpha1().Workflows(cronWorkflow.Namespace),
wfDefaults: wfDefaults,
cronWfIf: wfClientset.ArgoprojV1alpha1().CronWorkflows(cronWorkflow.Namespace),
wftmplInformer: wftmplInformer,
cwftmplInformer: cwftmplInformer,
log: log.WithFields(logging.Fields{
"workflow": cronWorkflow.Name,
"namespace": cronWorkflow.Namespace,
}),
metrics: metrics,
// inferScheduledTime returns an inferred scheduled time based on the current time and only works if it is called
// within 59 seconds of the scheduled time. Here it acts as a placeholder until it is replaced by a similar
// function that returns the last scheduled time deterministically from the cron engine. Since we are only able
// to generate the latter function after the job is scheduled, there is a tiny chance that the job is run before
// the deterministic function is supplanted. If that happens, we use the infer function as the next-best thing
scheduledTimeFunc: inferScheduledTime,
ctx: ctx,
}
}
// Run handles the running of a cron workflow
// It fits the github.com/robfig/cron.Job interface
func (woc *cronWfOperationCtx) Run() {
woc.run(woc.ctx, woc.scheduledTimeFunc(woc.ctx))
}
func (woc *cronWfOperationCtx) run(ctx context.Context, scheduledRuntime time.Time) {
defer woc.persistUpdate(ctx)
woc.log.Info(ctx, "Running")
// If the cron workflow has a schedule that was just updated, update its annotation
if woc.cronWf.IsUsingNewSchedule() {
schedule := woc.cronWf.Spec.GetScheduleWithTimezoneString()
woc.cronWf.SetSchedule(schedule)
woc.setModifiedAnnotation(woc.cronWf.GetScheduleAnnotationKey(), schedule)
}
err := woc.validateCronWorkflow(ctx)
if err != nil {
return
}
completed, err := woc.checkStopingCondition()
if err != nil {
woc.reportCronWorkflowError(ctx, v1alpha1.ConditionTypeSpecError, fmt.Sprintf("failed to check CronWorkflow '%s' stopping condition: %s", woc.cronWf.Name, err))
return
} else if completed {
woc.setAsCompleted()
}
proceed, err := woc.enforceRuntimePolicy(ctx)
if err != nil {
woc.reportCronWorkflowError(ctx, v1alpha1.ConditionTypeSubmissionError, fmt.Sprintf("run policy error: %s", err))
return
} else if !proceed {
return
}
woc.metrics.CronWfTrigger(ctx, woc.cronWf.Name, woc.cronWf.Namespace)
wf := common.ConvertCronWorkflowToWorkflowWithProperties(ctx, woc.cronWf, getChildWorkflowName(woc.cronWf.Name, scheduledRuntime), scheduledRuntime)
runWf, err := util.SubmitWorkflow(ctx, woc.wfClient, woc.wfClientset, woc.cronWf.Namespace, wf, woc.wfDefaults, &v1alpha1.SubmitOpts{})
if err != nil {
// If the workflow already exists (i.e. this is a duplicate submission), do not report an error
if apierrors.IsAlreadyExists(err) {
return
}
woc.reportCronWorkflowError(ctx, v1alpha1.ConditionTypeSubmissionError, fmt.Sprintf("Failed to submit Workflow: %s", err))
return
}
woc.cronWf.Status.Active = append(woc.cronWf.Status.Active, getWorkflowObjectReference(wf, runWf))
woc.cronWf.Status.Phase = v1alpha1.ActivePhase
woc.cronWf.Status.LastScheduledTime = &v1.Time{Time: scheduledRuntime}
woc.cronWf.Status.Conditions.RemoveCondition(v1alpha1.ConditionTypeSubmissionError)
}
func (woc *cronWfOperationCtx) validateCronWorkflow(ctx context.Context) error {
wftmplGetter := informer.NewWorkflowTemplateFromInformerGetter(woc.wftmplInformer, woc.cronWf.Namespace)
cwftmplGetter := informer.NewClusterWorkflowTemplateFromInformerGetter(woc.cwftmplInformer)
err := validate.CronWorkflow(ctx, wftmplGetter, cwftmplGetter, woc.cronWf, woc.wfDefaults)
if err != nil {
woc.reportCronWorkflowError(ctx, v1alpha1.ConditionTypeSpecError, fmt.Sprint(err))
} else {
woc.cronWf.Status.Conditions.RemoveCondition(v1alpha1.ConditionTypeSpecError)
}
return err
}
func getWorkflowObjectReference(wf *v1alpha1.Workflow, runWf *v1alpha1.Workflow) corev1.ObjectReference {
// This is a bit of a hack. Ideally we'd use ref.GetReference, but for some reason the `runWf` object is coming back
// without `Kind` and `APIVersion` set (even though it it set on `wf`). To fix this, we hard code those values.
return corev1.ObjectReference{
Kind: wf.Kind,
APIVersion: wf.APIVersion,
Name: runWf.GetName(),
Namespace: runWf.GetNamespace(),
UID: runWf.GetUID(),
ResourceVersion: runWf.GetResourceVersion(),
}
}
func (woc *cronWfOperationCtx) setModifiedLabel(key, value string) {
if woc.modifiedLabels == nil {
woc.modifiedLabels = map[string]string{}
}
woc.modifiedLabels[key] = value
}
func (woc *cronWfOperationCtx) setModifiedAnnotation(key, value string) {
if woc.modifiedAnnotations == nil {
woc.modifiedAnnotations = map[string]string{}
}
woc.modifiedAnnotations[key] = value
}
func (woc *cronWfOperationCtx) persistUpdate(ctx context.Context) {
patch := map[string]any{
"status": woc.cronWf.Status,
}
meta := map[string]any{}
if len(woc.modifiedLabels) > 0 {
meta["labels"] = woc.modifiedLabels
}
if len(woc.modifiedAnnotations) > 0 {
meta["annotations"] = woc.modifiedAnnotations
}
if len(meta) > 0 {
patch["metadata"] = meta
}
woc.patch(ctx, patch)
}
func (woc *cronWfOperationCtx) persistCurrentWorkflowStatus(ctx context.Context) {
woc.patch(ctx, map[string]any{"status": map[string]any{"active": woc.cronWf.Status.Active, "succeeded": woc.cronWf.Status.Succeeded, "failed": woc.cronWf.Status.Failed, "phase": woc.cronWf.Status.Phase}})
}
func (woc *cronWfOperationCtx) patch(ctx context.Context, patch map[string]any) {
data, err := json.Marshal(patch)
if err != nil {
woc.log.WithError(err).Error(ctx, "failed to marshall cron workflow status.active data")
return
}
err = waitutil.Backoff(retry.DefaultRetry(ctx), func() (bool, error) {
cronWf, patchErr := woc.cronWfIf.Patch(ctx, woc.cronWf.Name, types.MergePatchType, data, v1.PatchOptions{})
if patchErr != nil {
return !errorsutil.IsTransientErr(ctx, patchErr), patchErr
}
woc.cronWf = cronWf
return true, nil
})
if err != nil {
woc.log.WithError(err).Error(ctx, "failed to update cron workflow")
return
}
}
// TODO: refactor shouldExecute in steps.go
func shouldExecute(when string) (bool, error) {
if when == "" {
return true, nil
}
expression, err := govaluate.NewEvaluableExpression(when)
if err != nil {
return false, err
}
result, err := expression.Evaluate(nil)
if err != nil {
return false, err
}
boolRes, ok := result.(bool)
if !ok {
return false, argoerrs.Errorf(argoerrs.CodeBadRequest, "Expected boolean evaluation for '%s'. Got %v", when, result)
}
return boolRes, nil
}
func evalWhen(ctx context.Context, cron *v1alpha1.CronWorkflow) (bool, error) {
if cron.Spec.When == "" {
return true, nil
}
t, err := template.NewTemplate(cron.Spec.When)
if err != nil {
return false, err
}
env := make(map[string]any)
addSetField := func(name string, value any) {
env[fmt.Sprintf("%s.%s", variablePrefix, name)] = value
}
err = expressionEnv(cron, addSetField)
if err != nil {
return false, err
}
newWhenStr, err := t.Replace(ctx, env, false)
if err != nil {
return false, err
}
newCron := cron.DeepCopy()
newCron.Spec.When = newWhenStr
return shouldExecute(newCron.Spec.When)
}
func (woc *cronWfOperationCtx) enforceRuntimePolicy(ctx context.Context) (bool, error) {
if woc.cronWf.Spec.Suspend {
woc.log.Info(ctx, "CronWorkflow suspended, skipping execution")
return false, nil
}
if woc.cronWf.Status.Phase == v1alpha1.StoppedPhase {
woc.log.Info(ctx, "CronWorkflow is marked as stopped since it achieved the stopping condition")
return false, nil
}
canProceed, err := evalWhen(ctx, woc.cronWf)
if err != nil || !canProceed {
return canProceed, err
}
if woc.cronWf.Spec.ConcurrencyPolicy != "" {
switch woc.cronWf.Spec.ConcurrencyPolicy {
case v1alpha1.AllowConcurrent, "":
// Do nothing
case v1alpha1.ForbidConcurrent:
if len(woc.cronWf.Status.Active) > 0 {
woc.metrics.CronWfPolicy(ctx, woc.cronWf.Name, woc.cronWf.Namespace, v1alpha1.ForbidConcurrent)
woc.log.Info(ctx, "'ConcurrencyPolicy: Forbid' and has an active Workflow so it was not run")
return false, nil
}
case v1alpha1.ReplaceConcurrent:
if len(woc.cronWf.Status.Active) > 0 {
woc.metrics.CronWfPolicy(ctx, woc.cronWf.Name, woc.cronWf.Namespace, v1alpha1.ReplaceConcurrent)
woc.log.Info(ctx, "'ConcurrencyPolicy: Replace' and has active Workflows")
err := woc.terminateOutstandingWorkflows(ctx)
if err != nil {
return false, err
}
}
default:
return false, fmt.Errorf("invalid ConcurrencyPolicy: %s", woc.cronWf.Spec.ConcurrencyPolicy)
}
}
return true, nil
}
func (woc *cronWfOperationCtx) terminateOutstandingWorkflows(ctx context.Context) error {
for _, wfObjectRef := range woc.cronWf.Status.Active {
woc.log.WithField("name", wfObjectRef.Name).Info(ctx, "stopping")
err := util.TerminateWorkflow(ctx, woc.wfClient, wfObjectRef.Name)
if err != nil {
if apierrors.IsNotFound(err) {
woc.log.WithField("name", wfObjectRef.Name).Warn(ctx, "workflow not found when trying to terminate outstanding workflows")
continue
}
var alreadyShutdownErr util.AlreadyShutdownError
if errors.As(err, &alreadyShutdownErr) {
woc.log.Warn(ctx, alreadyShutdownErr.Error())
continue
}
return fmt.Errorf("error stopping workflow %s: %w", wfObjectRef.Name, err)
}
}
return nil
}
func (woc *cronWfOperationCtx) runOutstandingWorkflows(ctx context.Context) (bool, error) {
missedExecutionTime, err := woc.shouldOutstandingWorkflowsBeRun(ctx)
if err != nil {
return false, err
}
if !missedExecutionTime.IsZero() {
woc.run(ctx, missedExecutionTime)
return true, nil
}
return false, nil
}
func (woc *cronWfOperationCtx) shouldOutstandingWorkflowsBeRun(ctx context.Context) (time.Time, error) {
// If the CronWorkflow schedule was just updated, then do not run any outstanding workflows.
if woc.cronWf.IsUsingNewSchedule() {
return time.Time{}, nil
}
// If this CronWorkflow has been run before, check if we have missed any scheduled executions
if woc.cronWf.Status.LastScheduledTime != nil {
for _, schedule := range woc.cronWf.Spec.GetSchedulesWithTimezone() {
var now time.Time
var cronSchedule cron.Schedule
now = time.Now()
cronSchedule, err := cron.ParseStandard(schedule)
if err != nil {
return time.Time{}, err
}
var missedExecutionTime time.Time
nextScheduledRunTime := cronSchedule.Next(woc.cronWf.Status.LastScheduledTime.Time)
// Workflow should have ran
for nextScheduledRunTime.Before(now) {
missedExecutionTime = nextScheduledRunTime
nextScheduledRunTime = cronSchedule.Next(missedExecutionTime)
}
// We missed the latest execution time
if !missedExecutionTime.IsZero() {
// if missedExecutionTime is within StartDeadlineSeconds, We are still within the deadline window, run the Workflow
if woc.cronWf.Spec.StartingDeadlineSeconds != nil && now.Before(missedExecutionTime.Add(time.Duration(*woc.cronWf.Spec.StartingDeadlineSeconds)*time.Second)) {
woc.log.WithFields(logging.Fields{"name": woc.cronWf.Name, "missedExecutionTime": missedExecutionTime.Format("Mon Jan _2 15:04:05 2006")}).Info(ctx, "missed an execution and is within StartingDeadline")
return missedExecutionTime, nil
}
}
}
}
return time.Time{}, nil
}
type fulfilledWfsPhase struct {
fulfilled bool
phase v1alpha1.WorkflowPhase
}
func (woc *cronWfOperationCtx) reconcileActiveWfs(ctx context.Context, workflows []v1alpha1.Workflow) error {
updated := false
currentWfsFulfilled := make(map[types.UID]fulfilledWfsPhase, len(workflows))
for _, wf := range workflows {
currentWfsFulfilled[wf.UID] = fulfilledWfsPhase{
fulfilled: wf.Status.Fulfilled(),
phase: wf.Status.Phase,
}
if !woc.cronWf.Status.HasActiveUID(wf.UID) && !wf.Status.Fulfilled() {
updated = true
woc.cronWf.Status.Active = append(woc.cronWf.Status.Active, getWorkflowObjectReference(&wf, &wf))
}
}
for _, objectRef := range woc.cronWf.Status.Active {
if fulfilled, found := currentWfsFulfilled[objectRef.UID]; !found || fulfilled.fulfilled {
updated = true
woc.removeFromActiveList(objectRef.UID)
if found && fulfilled.fulfilled {
woc.updateWfPhaseCounter(fulfilled.phase)
completed, err := woc.checkStopingCondition()
if err != nil {
return fmt.Errorf("failed to check CronWorkflow '%s' stopping condition: %w", woc.cronWf.Name, err)
} else if completed {
woc.setAsCompleted()
}
}
}
}
if updated {
woc.persistCurrentWorkflowStatus(ctx)
}
return nil
}
func (woc *cronWfOperationCtx) removeFromActiveList(uid types.UID) {
var newActive []corev1.ObjectReference
for _, ref := range woc.cronWf.Status.Active {
if ref.UID != uid {
newActive = append(newActive, ref)
}
}
woc.cronWf.Status.Active = newActive
}
func (woc *cronWfOperationCtx) enforceHistoryLimit(ctx context.Context, workflows []v1alpha1.Workflow) error {
woc.log.WithField("name", woc.cronWf.Name).Debug(ctx, "Enforcing history limit")
var successfulWorkflows []v1alpha1.Workflow
var failedWorkflows []v1alpha1.Workflow
for _, wf := range workflows {
if wf.Labels[common.LabelKeyCronWorkflow] != woc.cronWf.Name {
continue
}
if wf.Status.Fulfilled() {
if wf.Status.Successful() {
successfulWorkflows = append(successfulWorkflows, wf)
} else {
failedWorkflows = append(failedWorkflows, wf)
}
}
}
workflowsToKeep := int32(3)
if woc.cronWf.Spec.SuccessfulJobsHistoryLimit != nil && *woc.cronWf.Spec.SuccessfulJobsHistoryLimit >= 0 {
workflowsToKeep = *woc.cronWf.Spec.SuccessfulJobsHistoryLimit
}
err := woc.deleteOldestWorkflows(ctx, successfulWorkflows, int(workflowsToKeep))
if err != nil {
return fmt.Errorf("unable to delete Successful Workflows of CronWorkflow '%s': %w", woc.cronWf.Name, err)
}
workflowsToKeep = int32(1)
if woc.cronWf.Spec.FailedJobsHistoryLimit != nil && *woc.cronWf.Spec.FailedJobsHistoryLimit >= 0 {
workflowsToKeep = *woc.cronWf.Spec.FailedJobsHistoryLimit
}
err = woc.deleteOldestWorkflows(ctx, failedWorkflows, int(workflowsToKeep))
if err != nil {
return fmt.Errorf("unable to delete Failed Workflows of CronWorkflow '%s': %w", woc.cronWf.Name, err)
}
return nil
}
func (woc *cronWfOperationCtx) deleteOldestWorkflows(ctx context.Context, jobList []v1alpha1.Workflow, workflowsToKeep int) error {
if workflowsToKeep >= len(jobList) {
return nil
}
sort.SliceStable(jobList, func(i, j int) bool {
return jobList[i].Status.FinishedAt.After(jobList[j].Status.FinishedAt.Time)
})
for _, wf := range jobList[workflowsToKeep:] {
err := woc.wfClient.Delete(ctx, wf.Name, v1.DeleteOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
woc.log.WithField("workflow", wf.Name).Info(ctx, "Workflow was already deleted")
continue
}
return fmt.Errorf("error deleting workflow '%s': %w", wf.Name, err)
}
woc.log.WithField("workflow", wf.Name).Info(ctx, "Deleted Workflow due to CronWorkflow history limit")
}
return nil
}
func (woc *cronWfOperationCtx) reportCronWorkflowError(ctx context.Context, conditionType v1alpha1.ConditionType, errString string) {
woc.log.WithField("conditionType", conditionType).Error(ctx, errString)
woc.cronWf.Status.Conditions.UpsertCondition(v1alpha1.Condition{
Type: conditionType,
Message: errString,
Status: v1.ConditionTrue,
})
if conditionType == v1alpha1.ConditionTypeSpecError {
woc.metrics.CronWorkflowSpecError(ctx)
} else {
if conditionType == v1alpha1.ConditionTypeSubmissionError {
woc.cronWf.Status.Failed++
}
woc.metrics.CronWorkflowSubmissionError(ctx)
}
}
func (woc *cronWfOperationCtx) updateWfPhaseCounter(phase v1alpha1.WorkflowPhase) {
switch phase {
case v1alpha1.WorkflowError, v1alpha1.WorkflowFailed:
woc.cronWf.Status.Failed++
case v1alpha1.WorkflowSucceeded:
woc.cronWf.Status.Succeeded++
}
}
func expressionEnv(cron *v1alpha1.CronWorkflow, addSetField func(name string, value any)) error {
addSetField("name", cron.Name)
addSetField("namespace", cron.Namespace)
addSetField("labels", cron.Labels)
addSetField("annotations", cron.Labels)
addSetField("failed", cron.Status.Failed)
addSetField("succeeded", cron.Status.Succeeded)
labelsStr, err := json.Marshal(&cron.Labels)
if err != nil {
return err
}
annotationsStr, err := json.Marshal(&cron.Annotations)
if err != nil {
return err
}
addSetField("annotations.json", annotationsStr)
addSetField("labels.json", labelsStr)
var tm *time.Time
tm = nil
if cron.Status.LastScheduledTime != nil {
tm = &cron.Status.LastScheduledTime.Time
}
addSetField("lastScheduledTime", tm)
return nil
}
func (woc *cronWfOperationCtx) checkStopingCondition() (bool, error) {
if woc.cronWf.Spec.StopStrategy == nil {
return false, nil
}
prefixedEnv := make(map[string]any)
addSetField := func(name string, value any) {
prefixedEnv[name] = value
}
env := make(map[string]any)
env[variablePrefix] = prefixedEnv
err := expressionEnv(woc.cronWf, addSetField)
if err != nil {
return false, err
}
suspend, err := argoexpr.EvalBool(woc.cronWf.Spec.StopStrategy.Expression, env)
if err != nil {
return false, fmt.Errorf("failed to evaluate stop expression: %w", err)
}
return suspend, nil
}
func (woc *cronWfOperationCtx) setAsCompleted() {
woc.cronWf.Status.Phase = v1alpha1.StoppedPhase
if woc.cronWf.Labels == nil {
woc.cronWf.Labels = map[string]string{}
}
woc.cronWf.Labels[common.LabelKeyCronWorkflowCompleted] = "true"
woc.setModifiedLabel(common.LabelKeyCronWorkflowCompleted, "true")
}
func inferScheduledTime(ctx context.Context) time.Time {
// Infer scheduled runtime by getting current time and zeroing out current seconds and nanoseconds
// This works because the finest possible scheduled runtime is a minute. It is unlikely to ever be used, since this
// function is quickly supplanted by a deterministic function from the cron engine.
now := time.Now().UTC()
scheduledTime := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, now.Location())
log := logging.RequireLoggerFromContext(ctx)
log.WithField("scheduledTime", scheduledTime).Info(ctx, "inferred scheduled time")
return scheduledTime
}
func getChildWorkflowName(cronWorkflowName string, scheduledRuntime time.Time) string {
return fmt.Sprintf("%s-%d", cronWorkflowName, scheduledRuntime.Unix())
}