-
Notifications
You must be signed in to change notification settings - Fork 39.5k
/
utils.go
270 lines (236 loc) · 8.21 KB
/
utils.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
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package topologycache
import (
"math"
v1 "k8s.io/api/core/v1"
discovery "k8s.io/api/discovery/v1"
"k8s.io/klog/v2"
endpointsliceutil "k8s.io/kubernetes/pkg/controller/util/endpointslice"
)
// RemoveHintsFromSlices removes topology hints on EndpointSlices and returns
// updated lists of EndpointSlices to create and update.
func RemoveHintsFromSlices(si *SliceInfo) ([]*discovery.EndpointSlice, []*discovery.EndpointSlice) {
// Remove hints on all EndpointSlices we were already going to change.
slices := append(si.ToCreate, si.ToUpdate...)
for _, slice := range slices {
for i := range slice.Endpoints {
slice.Endpoints[i].Hints = nil
}
}
// Remove hints on all unchanged EndpointSlices and mark them for update
// if any already had hints. We use j to track the number/index of slices
// that are still unchanged.
j := 0
for _, slice := range si.Unchanged {
changed := false
for i, endpoint := range slice.Endpoints {
if endpoint.Hints != nil {
// Unchanged slices are still direct copies from informer cache.
// Need to deep copy before we make any modifications to avoid
// accidentally changing informer cache.
slice = slice.DeepCopy()
slice.Endpoints[i].Hints = nil
changed = true
}
}
if changed {
si.ToUpdate = append(si.ToUpdate, slice)
} else {
si.Unchanged[j] = slice
j++
}
}
// truncate si.Unchanged so it only includes slices that are still
// unchanged.
si.Unchanged = si.Unchanged[:j]
return si.ToCreate, si.ToUpdate
}
// redistributeHints redistributes hints based in the provided EndpointSlices.
// It allocates endpoints from the provided givingZones to the provided
// receivingZones. This returns a map that represents the changes in allocated
// endpoints by zone.
func redistributeHints(slices []*discovery.EndpointSlice, givingZones, receivingZones map[string]int) map[string]int {
redistributions := map[string]int{}
for _, slice := range slices {
for i, endpoint := range slice.Endpoints {
if !endpointsliceutil.EndpointReady(endpoint) {
endpoint.Hints = nil
continue
}
if len(givingZones) == 0 || len(receivingZones) == 0 {
return redistributions
}
if endpoint.Zone == nil || *endpoint.Zone == "" {
// This should always be caught earlier in AddHints()
klog.Warningf("Endpoint found without zone specified")
continue
}
givingZone := *endpoint.Zone
numToGive, ok := givingZones[givingZone]
if ok && numToGive > 0 {
for receivingZone, numToReceive := range receivingZones {
if numToReceive > 0 {
slice.Endpoints[i].Hints = &discovery.EndpointHints{ForZones: []discovery.ForZone{{Name: receivingZone}}}
if numToGive == 1 {
delete(givingZones, givingZone)
} else {
givingZones[givingZone]--
}
if numToReceive == 1 {
delete(receivingZones, receivingZone)
} else {
receivingZones[receivingZone]--
}
redistributions[receivingZone]++
redistributions[givingZone]--
break
}
}
}
}
}
return redistributions
}
// getGivingAndReceivingZones returns the number of endpoints each zone should
// give to other zones along with the number of endpoints each zone should
// receive from other zones. This is calculated with the provided allocations
// (desired state) and allocatedHintsByZone (current state).
func getGivingAndReceivingZones(allocations map[string]Allocation, allocatedHintsByZone map[string]int) (map[string]int, map[string]int) {
// 1. Determine the precise number of additional endpoints each zone has
// (giving) or needs (receiving).
givingZonesDesired := map[string]float64{}
receivingZonesDesired := map[string]float64{}
for zone, allocation := range allocations {
allocatedHints, _ := allocatedHintsByZone[zone]
target := allocation.Desired
if float64(allocatedHints) > target {
givingZonesDesired[zone] = float64(allocatedHints) - target
} else if float64(allocatedHints) < target {
receivingZonesDesired[zone] = target - float64(allocatedHints)
}
}
// 2. Convert the precise numbers needed into ints representing real
// endpoints given from one zone to another.
givingZones := map[string]int{}
receivingZones := map[string]int{}
for {
givingZone, numToGive := getMost(givingZonesDesired)
receivingZone, numToReceive := getMost(receivingZonesDesired)
// return early if any of the following are true:
// - giving OR receiving zone are unspecified
// - giving AND receiving zones have less than 1 endpoint left to give or receive
// - giving OR receiving zones have less than 0.5 endpoints left to give or receive
if givingZone == "" || receivingZone == "" || (numToGive < 1.0 && numToReceive < 1.0) || numToGive < 0.5 || numToReceive < 0.5 {
break
}
givingZones[givingZone]++
givingZonesDesired[givingZone]--
receivingZones[receivingZone]++
receivingZonesDesired[receivingZone]--
}
return givingZones, receivingZones
}
// getMost accepts a map[string]float64 and returns the string and float64 that
// represent the greatest value in this provided map. This function is not very
// efficient but it is expected that len() will rarely be greater than 2.
func getMost(zones map[string]float64) (string, float64) {
zone := ""
num := 0.0
for z, n := range zones {
if n > num {
zone = z
num = n
}
}
return zone, num
}
// getHintsByZone returns the number of hints allocated to each zone by the
// provided EndpointSlice. This function returns nil to indicate that the
// current allocations are invalid and that the EndpointSlice needs to be
// updated. This could be caused by:
// - A hint for a zone that no longer requires any allocations.
// - An endpoint with no hints.
// - Hints that would make minimum allocations impossible.
func getHintsByZone(slice *discovery.EndpointSlice, allocatedHintsByZone EndpointZoneInfo, allocations map[string]Allocation) map[string]int {
hintsByZone := map[string]int{}
for _, endpoint := range slice.Endpoints {
if !endpointsliceutil.EndpointReady(endpoint) {
continue
}
if endpoint.Hints == nil || len(endpoint.Hints.ForZones) == 0 {
return nil
}
for i := range endpoint.Hints.ForZones {
zone := endpoint.Hints.ForZones[i].Name
if _, ok := allocations[zone]; !ok {
return nil
}
hintsByZone[zone]++
}
}
for zone, numHints := range hintsByZone {
alreadyAllocated, _ := allocatedHintsByZone[zone]
allocation, ok := allocations[zone]
if !ok || (numHints+alreadyAllocated) > allocation.Maximum {
return nil
}
}
return hintsByZone
}
// serviceOverloaded returns true if the Service has an insufficient amount of
// endpoints for any zone.
func serviceOverloaded(ezi EndpointZoneInfo, zoneRatios map[string]float64) bool {
if len(ezi) == 0 {
return false
}
if len(zoneRatios) == 0 {
return true
}
totalEndpoints := 0.0
for _, numEndpoints := range ezi {
totalEndpoints += float64(numEndpoints)
}
for zone, ratio := range zoneRatios {
svcEndpoints, ok := ezi[zone]
if !ok {
return true
}
minEndpoints := math.Ceil(totalEndpoints * ratio * (1 / (1 + OverloadThreshold)))
if svcEndpoints < int(minEndpoints) {
return true
}
}
return false
}
// NodeReady returns true if the Node has a status condition of type "NodeReady"
// with a status of "True".
func NodeReady(nodeStatus v1.NodeStatus) bool {
for _, cond := range nodeStatus.Conditions {
if cond.Type == v1.NodeReady {
return cond.Status == v1.ConditionTrue
}
}
return false
}
// numReadyEndpoints returns the number of Endpoints that are ready from the
// specified list.
func numReadyEndpoints(endpoints []discovery.Endpoint) int {
var total int
for _, ep := range endpoints {
if ep.Conditions.Ready != nil && *ep.Conditions.Ready {
total++
}
}
return total
}