-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathhelpers.go
315 lines (284 loc) · 12.3 KB
/
helpers.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
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
/*
Copyright 2023 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 disruption
import (
"context"
"fmt"
"math"
"strconv"
"github.com/samber/lo"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/clock"
"knative.dev/pkg/logging"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/karpenter/pkg/apis/v1beta1"
"sigs.k8s.io/karpenter/pkg/cloudprovider"
disruptionevents "sigs.k8s.io/karpenter/pkg/controllers/disruption/events"
"sigs.k8s.io/karpenter/pkg/controllers/disruption/orchestration"
"sigs.k8s.io/karpenter/pkg/controllers/provisioning"
pscheduling "sigs.k8s.io/karpenter/pkg/controllers/provisioning/scheduling"
"sigs.k8s.io/karpenter/pkg/controllers/state"
"sigs.k8s.io/karpenter/pkg/events"
"sigs.k8s.io/karpenter/pkg/scheduling"
nodeutils "sigs.k8s.io/karpenter/pkg/utils/node"
nodepoolutil "sigs.k8s.io/karpenter/pkg/utils/nodepool"
"sigs.k8s.io/karpenter/pkg/utils/pod"
)
func filterCandidates(ctx context.Context, kubeClient client.Client, recorder events.Recorder, nodes []*Candidate) ([]*Candidate, error) {
pdbs, err := NewPDBLimits(ctx, kubeClient)
if err != nil {
return nil, fmt.Errorf("tracking PodDisruptionBudgets, %w", err)
}
// filter out nodes that can't be terminated
nodes = lo.Filter(nodes, func(cn *Candidate, _ int) bool {
if !cn.Node.DeletionTimestamp.IsZero() {
recorder.Publish(disruptionevents.Blocked(cn.Node, cn.NodeClaim, "Node in the process of deletion")...)
return false
}
if pdb, ok := pdbs.CanEvictPods(cn.pods); !ok {
recorder.Publish(disruptionevents.Blocked(cn.Node, cn.NodeClaim, fmt.Sprintf("PDB %q prevents pod evictions", pdb))...)
return false
}
if p, ok := hasDoNotDisruptPod(cn); ok {
recorder.Publish(disruptionevents.Blocked(cn.Node, cn.NodeClaim, fmt.Sprintf("Pod %q has do not evict annotation", client.ObjectKeyFromObject(p)))...)
return false
}
return true
})
return nodes, nil
}
//nolint:gocyclo
func simulateScheduling(ctx context.Context, kubeClient client.Client, cluster *state.Cluster, provisioner *provisioning.Provisioner,
candidates ...*Candidate) (*pscheduling.Results, error) {
candidateNames := sets.NewString(lo.Map(candidates, func(t *Candidate, i int) string { return t.Name() })...)
nodes := cluster.Nodes()
deletingNodes := nodes.Deleting()
stateNodes := lo.Filter(nodes.Active(), func(n *state.StateNode, _ int) bool {
return !candidateNames.Has(n.Name())
})
// We do one final check to ensure that the node that we are attempting to consolidate isn't
// already handled for deletion by some other controller. This could happen if the node was markedForDeletion
// between returning the candidates and getting the stateNodes above
if _, ok := lo.Find(deletingNodes, func(n *state.StateNode) bool {
return candidateNames.Has(n.Name())
}); ok {
return nil, errCandidateDeleting
}
// We get the pods that are on nodes that are deleting
deletingNodePods, err := deletingNodes.Pods(ctx, kubeClient)
if err != nil {
return nil, fmt.Errorf("failed to get pods from deleting nodes, %w", err)
}
// start by getting all pending pods
pods, err := provisioner.GetPendingPods(ctx)
if err != nil {
return nil, fmt.Errorf("determining pending pods, %w", err)
}
for _, n := range candidates {
pods = append(pods, n.pods...)
}
pods = append(pods, deletingNodePods...)
scheduler, err := provisioner.NewScheduler(ctx, pods, stateNodes, pscheduling.SchedulerOptions{
SimulationMode: true,
})
if err != nil {
return nil, fmt.Errorf("creating scheduler, %w", err)
}
results := scheduler.Solve(ctx, pods)
// check if the scheduling relied on an existing node that isn't ready yet, if so we fail
// to schedule since we want to assume that we can delete a node and its pods will immediately
// move to an existing node which won't occur if that node isn't ready.
for _, n := range results.ExistingNodes {
if !n.Initialized() || nodeutils.GetCondition(n.Node, v1.NodeReady).Status != v1.ConditionTrue {
for _, p := range n.Pods {
results.PodErrors[p] = fmt.Errorf("would schedule against a non-initialized node %s", n.Name())
}
}
}
return results, nil
}
// instanceTypesAreSubset returns true if the lhs slice of instance types are a subset of the rhs.
func instanceTypesAreSubset(lhs []*cloudprovider.InstanceType, rhs []*cloudprovider.InstanceType) bool {
rhsNames := sets.NewString(lo.Map(rhs, func(t *cloudprovider.InstanceType, i int) string { return t.Name })...)
lhsNames := sets.NewString(lo.Map(lhs, func(t *cloudprovider.InstanceType, i int) string { return t.Name })...)
return len(rhsNames.Intersection(lhsNames)) == len(lhsNames)
}
// GetPodEvictionCost returns the disruption cost computed for evicting the given pod.
func GetPodEvictionCost(ctx context.Context, p *v1.Pod) float64 {
cost := 1.0
podDeletionCostStr, ok := p.Annotations[v1.PodDeletionCost]
if ok {
podDeletionCost, err := strconv.ParseFloat(podDeletionCostStr, 64)
if err != nil {
logging.FromContext(ctx).Errorf("parsing %s=%s from pod %s, %s",
v1.PodDeletionCost, podDeletionCostStr, client.ObjectKeyFromObject(p), err)
} else {
// the pod deletion disruptionCost is in [-2147483647, 2147483647]
// the min pod disruptionCost makes one pod ~ -15 pods, and the max pod disruptionCost to ~ 17 pods.
cost += podDeletionCost / math.Pow(2, 27.0)
}
}
// the scheduling priority is in [-2147483648, 1000000000]
if p.Spec.Priority != nil {
cost += float64(*p.Spec.Priority) / math.Pow(2, 25)
}
// overall we clamp the pod cost to the range [-10.0, 10.0] with the default being 1.0
return clamp(-10.0, cost, 10.0)
}
func filterByPrice(options []*cloudprovider.InstanceType, reqs scheduling.Requirements, price float64) []*cloudprovider.InstanceType {
var result []*cloudprovider.InstanceType
for _, it := range options {
launchPrice := worstLaunchPrice(it.Offerings.Available(), reqs)
if launchPrice < price {
result = append(result, it)
}
}
return result
}
func disruptionCost(ctx context.Context, pods []*v1.Pod) float64 {
cost := 0.0
for _, p := range pods {
cost += GetPodEvictionCost(ctx, p)
}
return cost
}
// GetCandidates returns nodes that appear to be currently deprovisionable based off of their nodePool
func GetCandidates(ctx context.Context, cluster *state.Cluster, kubeClient client.Client, recorder events.Recorder, clk clock.Clock,
cloudProvider cloudprovider.CloudProvider, shouldDeprovision CandidateFilter, queue *orchestration.Queue) ([]*Candidate, error) {
nodePoolMap, nodePoolToInstanceTypesMap, err := buildNodePoolMap(ctx, kubeClient, cloudProvider)
if err != nil {
return nil, err
}
candidates := lo.FilterMap(cluster.Nodes(), func(n *state.StateNode, _ int) (*Candidate, bool) {
cn, e := NewCandidate(ctx, kubeClient, recorder, clk, n, nodePoolMap, nodePoolToInstanceTypesMap, queue)
return cn, e == nil
})
// Filter only the valid candidates that we should disrupt
return lo.Filter(candidates, func(c *Candidate, _ int) bool { return shouldDeprovision(ctx, c) }), nil
}
// BuildDisruptionBudgets will return a map for nodePoolName -> numAllowedDisruptions and an error
func BuildDisruptionBudgets(ctx context.Context, cluster *state.Cluster, clk clock.Clock, kubeClient client.Client) (map[string]int, error) {
nodePoolList, err := nodepoolutil.List(ctx, kubeClient)
if err != nil {
return nil, fmt.Errorf("listing node pools, %w", err)
}
numNodes := map[string]int{}
deleting := map[string]int{}
disruptionBudgetMapping := map[string]int{}
// We need to get all the nodes in the cluster
// Get each current active number of nodes per nodePool
// Get the max disruptions for each nodePool
// Get the number of deleting nodes for each of those nodePools
// Find the difference to know how much left we can disrupt
nodes := cluster.Nodes()
for _, node := range nodes {
if !node.Managed() {
continue
}
nodePool := node.Labels()[v1beta1.NodePoolLabelKey]
if node.MarkedForDeletion() {
deleting[nodePool]++
}
numNodes[nodePool]++
}
for i := range nodePoolList.Items {
nodePool := nodePoolList.Items[i]
disruptions := nodePool.MustGetAllowedDisruptions(ctx, clk, numNodes[nodePool.Name])
// Subtract the allowed number of disruptions from the number of already deleting nodes.
// Floor the value since the number of deleting nodes can exceed the number of allowed disruptions.
// Allowing this value to be negative breaks assumptions in the code used to calculate how
// many nodes can be disrupted.
disruptionBudgetMapping[nodePool.Name] = lo.Clamp(disruptions-deleting[nodePool.Name], 0, math.MaxInt32)
}
return disruptionBudgetMapping, nil
}
// buildNodePoolMap builds a provName -> nodePool map and a provName -> instanceName -> instance type map
func buildNodePoolMap(ctx context.Context, kubeClient client.Client, cloudProvider cloudprovider.CloudProvider) (map[string]*v1beta1.NodePool, map[string]map[string]*cloudprovider.InstanceType, error) {
nodePoolMap := map[string]*v1beta1.NodePool{}
nodePoolList, err := nodepoolutil.List(ctx, kubeClient)
if err != nil {
return nil, nil, fmt.Errorf("listing node pools, %w", err)
}
nodePoolToInstanceTypesMap := map[string]map[string]*cloudprovider.InstanceType{}
for i := range nodePoolList.Items {
np := &nodePoolList.Items[i]
nodePoolMap[np.Name] = np
nodePoolInstanceTypes, err := cloudProvider.GetInstanceTypes(ctx, np)
if err != nil {
// don't error out on building the node pool, we just won't be able to handle any nodes that
// were created by it
logging.FromContext(ctx).Errorf("listing instance types for %s, %s", np.Name, err)
continue
}
if len(nodePoolInstanceTypes) == 0 {
continue
}
nodePoolToInstanceTypesMap[np.Name] = map[string]*cloudprovider.InstanceType{}
for _, it := range nodePoolInstanceTypes {
nodePoolToInstanceTypesMap[np.Name][it.Name] = it
}
}
return nodePoolMap, nodePoolToInstanceTypesMap, nil
}
// mapCandidates maps the list of proposed candidates with the current state
func mapCandidates(proposed, current []*Candidate) []*Candidate {
proposedNames := sets.NewString(lo.Map(proposed, func(c *Candidate, i int) string { return c.Name() })...)
return lo.Filter(current, func(c *Candidate, _ int) bool {
return proposedNames.Has(c.Name())
})
}
// worstLaunchPrice gets the worst-case launch price from the offerings that are offered
// on an instance type. If the instance type has a spot offering available, then it uses the spot offering
// to get the launch price; else, it uses the on-demand launch price
func worstLaunchPrice(ofs []cloudprovider.Offering, reqs scheduling.Requirements) float64 {
// We prefer to launch spot offerings, so we will get the worst price based on the node requirements
if reqs.Get(v1beta1.CapacityTypeLabelKey).Has(v1beta1.CapacityTypeSpot) {
spotOfferings := lo.Filter(ofs, func(of cloudprovider.Offering, _ int) bool {
return of.CapacityType == v1beta1.CapacityTypeSpot && reqs.Get(v1.LabelTopologyZone).Has(of.Zone)
})
if len(spotOfferings) > 0 {
return lo.MaxBy(spotOfferings, func(of1, of2 cloudprovider.Offering) bool {
return of1.Price > of2.Price
}).Price
}
}
if reqs.Get(v1beta1.CapacityTypeLabelKey).Has(v1beta1.CapacityTypeOnDemand) {
onDemandOfferings := lo.Filter(ofs, func(of cloudprovider.Offering, _ int) bool {
return of.CapacityType == v1beta1.CapacityTypeOnDemand && reqs.Get(v1.LabelTopologyZone).Has(of.Zone)
})
if len(onDemandOfferings) > 0 {
return lo.MaxBy(onDemandOfferings, func(of1, of2 cloudprovider.Offering) bool {
return of1.Price > of2.Price
}).Price
}
}
return math.MaxFloat64
}
func clamp(min, val, max float64) float64 {
if val < min {
return min
}
if val > max {
return max
}
return val
}
func hasDoNotDisruptPod(c *Candidate) (*v1.Pod, bool) {
return lo.Find(c.pods, func(p *v1.Pod) bool {
if pod.IsTerminating(p) || pod.IsTerminal(p) || pod.IsOwnedByNode(p) {
return false
}
return pod.HasDoNotDisrupt(p)
})
}