-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdefinitions.go
More file actions
200 lines (169 loc) · 4.57 KB
/
definitions.go
File metadata and controls
200 lines (169 loc) · 4.57 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
package main
import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
)
type Workload struct {
Name string
Namespace string
Kind WorkloadKind
Replicas int32
Available int32
Ready bool
MigratePatched bool
ARMPatched bool
Priority int32
deployment *appsv1.Deployment
statefulSet *appsv1.StatefulSet
}
type WorkloadKind string
const (
WorkloadDeployment WorkloadKind = "Deployment"
WorkloadStatefulSet WorkloadKind = "StatefulSet"
)
var MigrateToleration = []corev1.Toleration{
//{
// Key: "cloudpilot.ai/provider-disable",
// Operator: corev1.TolerationOpEqual,
// Value: "true",
// Effect: corev1.TaintEffectNoSchedule,
//},
{
Key: "cloudpilot.ai/gradual-rebalance-only",
Operator: corev1.TolerationOpExists,
},
}
var MigrateNodeSelectorKey = "node.cloudpilot.ai/managed"
var MigrateNodeSelectorValue = "true"
func CheckWorkloadIsMigrated(nodeSelector map[string]string, tolerations []corev1.Toleration) bool {
if tolerations == nil || nodeSelector == nil {
return false
}
return nodeSelector[MigrateNodeSelectorKey] == MigrateNodeSelectorValue &&
CheckWorkloadHasMigrateToleration(tolerations)
}
func CheckWorkloadHasMigrateToleration(tolerations []corev1.Toleration) bool {
if len(tolerations) == 0 {
return false
}
for _, toleration := range tolerations {
for _, migrateTol := range MigrateToleration {
if toleration.Key == migrateTol.Key {
return true
}
}
}
return false
}
func AddMigrateToleration(tolerations []corev1.Toleration) []corev1.Toleration {
existingKeys := make(map[string]bool)
for _, t := range tolerations {
existingKeys[t.Key] = true
}
for _, migrateTol := range MigrateToleration {
if !existingKeys[migrateTol.Key] {
tolerations = append(tolerations, migrateTol)
}
}
return tolerations
}
func RemoveMigrateToleration(tolerations []corev1.Toleration) []corev1.Toleration {
migrateKeys := make(map[string]bool)
for _, migrateTol := range MigrateToleration {
migrateKeys[migrateTol.Key] = true
}
newTolerations := tolerations[:0]
for _, t := range tolerations {
if !migrateKeys[t.Key] {
newTolerations = append(newTolerations, t)
}
}
return newTolerations
}
var ArmPreferSchedulingTerm = corev1.PreferredSchedulingTerm{
Weight: 10,
Preference: corev1.NodeSelectorTerm{
MatchExpressions: []corev1.NodeSelectorRequirement{
{
Key: "kubernetes.io/arch",
Operator: corev1.NodeSelectorOpIn,
Values: []string{"arm64"},
},
},
},
}
func equalPref(a, b corev1.PreferredSchedulingTerm) bool {
return equality.Semantic.DeepEqual(a.Preference, b.Preference)
}
func AddArm64Preference(existing []corev1.PreferredSchedulingTerm) []corev1.PreferredSchedulingTerm {
for _, term := range existing {
if equalPref(term, ArmPreferSchedulingTerm) {
return existing
}
}
return append(existing, ArmPreferSchedulingTerm)
}
func RemoveArm64Preference(existing []corev1.PreferredSchedulingTerm) []corev1.PreferredSchedulingTerm {
filtered := existing[:0]
for _, term := range existing {
if !equalPref(term, ArmPreferSchedulingTerm) {
filtered = append(filtered, term)
}
}
return filtered
}
func HasArm64Preference(aff *corev1.Affinity) bool {
tmp := aff.DeepCopy()
tmp = ensurePreferAffinity(tmp)
for _, term := range tmp.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution {
if equalPref(term, ArmPreferSchedulingTerm) {
return true
}
}
return false
}
var ARM64Toleration = []corev1.Toleration{
{
Key: "node.cloudpilot.ai/arch-arm64",
Operator: corev1.TolerationOpExists,
},
}
func CheckWorkloadHasARM64Toleration(tolerations []corev1.Toleration) bool {
if len(tolerations) == 0 {
return false
}
for _, toleration := range tolerations {
for _, armTol := range ARM64Toleration {
if toleration.Key == armTol.Key {
return true
}
}
}
return false
}
func AddARM64Toleration(tolerations []corev1.Toleration) []corev1.Toleration {
existingKeys := make(map[string]bool)
for _, t := range tolerations {
existingKeys[t.Key] = true
}
for _, armTol := range ARM64Toleration {
if !existingKeys[armTol.Key] {
tolerations = append(tolerations, armTol)
}
}
return tolerations
}
func RemoveARM64Toleration(tolerations []corev1.Toleration) []corev1.Toleration {
armKeys := make(map[string]bool)
for _, armTol := range ARM64Toleration {
armKeys[armTol.Key] = true
}
newTolerations := tolerations[:0]
for _, t := range tolerations {
if !armKeys[t.Key] {
newTolerations = append(newTolerations, t)
}
}
return newTolerations
}