This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
completed_workflows.go
87 lines (77 loc) · 2.95 KB
/
completed_workflows.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
package controller
import (
"strconv"
"time"
"github.com/lyft/flytepropeller/pkg/apis/flyteworkflow/v1alpha1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const controllerAgentName = "flyteworkflow-controller"
const workflowTerminationStatusKey = "termination-status"
const workflowTerminatedValue = "terminated"
const hourOfDayCompletedKey = "hour-of-day"
// This function creates a label selector, that will ignore all objects (in this case workflow) that DOES NOT have a
// label key=workflowTerminationStatusKey with a value=workflowTerminatedValue
func IgnoreCompletedWorkflowsLabelSelector() *v1.LabelSelector {
return &v1.LabelSelector{
MatchExpressions: []v1.LabelSelectorRequirement{
{
Key: workflowTerminationStatusKey,
Operator: v1.LabelSelectorOpNotIn,
Values: []string{workflowTerminatedValue},
},
},
}
}
// Creates a new LabelSelector that selects all workflows that have the completed Label
func CompletedWorkflowsLabelSelector() *v1.LabelSelector {
return &v1.LabelSelector{
MatchLabels: map[string]string{
workflowTerminationStatusKey: workflowTerminatedValue,
},
}
}
func SetCompletedLabel(w *v1alpha1.FlyteWorkflow, currentTime time.Time) {
if w.Labels == nil {
w.Labels = make(map[string]string)
}
w.Labels[workflowTerminationStatusKey] = workflowTerminatedValue
w.Labels[hourOfDayCompletedKey] = strconv.Itoa(currentTime.Hour())
}
func HasCompletedLabel(w *v1alpha1.FlyteWorkflow) bool {
if w.Labels != nil {
v, ok := w.Labels[workflowTerminationStatusKey]
if ok {
return v == workflowTerminatedValue
}
}
return false
}
// Calculates a list of all the hours that should be deleted given the current hour of the day and the retentionperiod in hours
// Usually this is a list of all hours out of the 24 hours in the day - retention period - the current hour of the day
func CalculateHoursToDelete(retentionPeriodHours, currentHourOfDay int) []string {
numberOfHoursToDelete := 24 - retentionPeriodHours
hoursToDelete := make([]string, 0, numberOfHoursToDelete)
for i := 0; i < currentHourOfDay-retentionPeriodHours; i++ {
hoursToDelete = append(hoursToDelete, strconv.Itoa(i))
}
maxHourOfDay := 24
if currentHourOfDay-retentionPeriodHours < 0 {
maxHourOfDay = 24 + (currentHourOfDay - retentionPeriodHours)
}
for i := currentHourOfDay + 1; i < maxHourOfDay; i++ {
hoursToDelete = append(hoursToDelete, strconv.Itoa(i))
}
return hoursToDelete
}
// Creates a new selector that selects all completed workflows and workflows with completed hour label outside of the
// retention window
func CompletedWorkflowsSelectorOutsideRetentionPeriod(retentionPeriodHours int, currentTime time.Time) *v1.LabelSelector {
hoursToDelete := CalculateHoursToDelete(retentionPeriodHours, currentTime.Hour())
s := CompletedWorkflowsLabelSelector()
s.MatchExpressions = append(s.MatchExpressions, v1.LabelSelectorRequirement{
Key: hourOfDayCompletedKey,
Operator: v1.LabelSelectorOpIn,
Values: hoursToDelete,
})
return s
}