-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathretry.go
121 lines (105 loc) · 3.99 KB
/
retry.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
package retry
import (
apiv1 "k8s.io/api/core/v1"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
)
// GetFailHosts iterates over the node subtree and find pod in error or fail
func GetFailHosts(nodes wfv1.Nodes, retryNodeName string) []string {
toVisit := []string{retryNodeName}
hostNames := []string{}
for len(toVisit) > 0 {
n := len(toVisit) - 1
nodeToVisit := toVisit[n]
toVisit = toVisit[:n]
if x, ok := nodes[nodeToVisit]; ok {
if (x.Phase == wfv1.NodeFailed || x.Phase == wfv1.NodeError) && x.Type == wfv1.NodeTypePod {
hostNames = append(hostNames, x.HostNodeName)
}
for i := 0; i < len(x.Children); i++ {
childNode := x.Children[i]
if y, ok := nodes[childNode]; ok {
toVisit = append(toVisit, y.ID)
}
}
}
}
return RemoveDuplicates(hostNames)
}
// RemoveDuplicates removes duplicate strings from slice
func RemoveDuplicates(strSlice []string) []string {
keys := make(map[string]bool)
outputList := []string{}
for _, strEntry := range strSlice {
if _, value := keys[strEntry]; !value {
keys[strEntry] = true
outputList = append(outputList, strEntry)
}
}
return outputList
}
// AddHostnamesToAffinity will add unique hostNames to existing matchExpressions in targetAffinity with
// key hostSelector or insert new matchExpressions with operator NotIn.
func AddHostnamesToAffinity(hostSelector string, hostNames []string, targetAffinity *apiv1.Affinity) *apiv1.Affinity {
if len(hostNames) == 0 {
return targetAffinity
}
nodeSelectorRequirement := apiv1.NodeSelectorRequirement{
Key: hostSelector,
Operator: apiv1.NodeSelectorOpNotIn,
Values: hostNames,
}
sourceAffinity := &apiv1.Affinity{
NodeAffinity: &apiv1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &apiv1.NodeSelector{
NodeSelectorTerms: []apiv1.NodeSelectorTerm{
{
MatchExpressions: []apiv1.NodeSelectorRequirement{
nodeSelectorRequirement,
},
},
},
},
},
}
if targetAffinity == nil {
targetAffinity = sourceAffinity
return targetAffinity
}
if targetAffinity.NodeAffinity == nil {
targetAffinity.NodeAffinity = sourceAffinity.NodeAffinity
return targetAffinity
}
targetExecution := targetAffinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution
sourceExecution := sourceAffinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution
if targetExecution == nil {
targetAffinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution =
sourceAffinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution
return targetAffinity
}
if len(targetExecution.NodeSelectorTerms) == 0 {
targetAffinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms =
sourceAffinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms
return targetAffinity
}
// find if specific NodeSelectorTerm exists and append
for i := range targetExecution.NodeSelectorTerms {
if len(targetExecution.NodeSelectorTerms[i].MatchExpressions) == 0 {
targetExecution.NodeSelectorTerms[i].MatchExpressions =
append(targetExecution.NodeSelectorTerms[i].MatchExpressions, sourceExecution.NodeSelectorTerms[0].MatchExpressions[0])
return targetAffinity
}
for j := range targetExecution.NodeSelectorTerms[i].MatchExpressions {
if targetExecution.NodeSelectorTerms[i].MatchExpressions[j].Key == hostSelector &&
targetExecution.NodeSelectorTerms[i].MatchExpressions[j].Operator == apiv1.NodeSelectorOpNotIn {
targetExecution.NodeSelectorTerms[i].MatchExpressions[j].Values =
append(targetExecution.NodeSelectorTerms[i].MatchExpressions[j].Values, hostNames...)
targetExecution.NodeSelectorTerms[i].MatchExpressions[j].Values =
RemoveDuplicates(targetExecution.NodeSelectorTerms[i].MatchExpressions[j].Values)
return targetAffinity
}
}
}
targetExecution.NodeSelectorTerms[0].MatchExpressions =
append(targetExecution.NodeSelectorTerms[0].MatchExpressions, nodeSelectorRequirement)
return targetAffinity
}