-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathnode.go
More file actions
396 lines (352 loc) · 9.32 KB
/
node.go
File metadata and controls
396 lines (352 loc) · 9.32 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
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 model
import (
"fmt"
"k8s.io/apimachinery/pkg/api/resource"
"regexp"
"sync"
"time"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/duration"
karpv1 "sigs.k8s.io/karpenter/pkg/apis/v1"
)
var (
instanceIDRegex = regexp.MustCompile(`aws:///(?P<AZ>.*)/(?P<InstanceID>.*)`)
)
type objectKey struct {
namespace string
name string
}
type Node struct {
mu sync.RWMutex
visible bool
node v1.Node
pods map[objectKey]*Pod
used v1.ResourceList
Price float64
nodeclaimCreationTime time.Time
}
func NewNode(n *v1.Node) *Node {
node := &Node{
node: *n,
pods: map[objectKey]*Pod{},
used: v1.ResourceList{},
}
return node
}
func NewNodeFromNodeClaim(nc *karpv1.NodeClaim) *Node {
node := NewNode(&v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: nc.Status.NodeName,
CreationTimestamp: nc.CreationTimestamp,
Labels: nc.Labels,
Annotations: nc.Annotations,
},
Spec: v1.NodeSpec{
Taints: nc.Spec.Taints,
ProviderID: nc.Status.ProviderID,
},
Status: v1.NodeStatus{
Capacity: nc.Status.Capacity,
Allocatable: nc.Status.Allocatable,
},
})
node.nodeclaimCreationTime = nc.CreationTimestamp.Time
return node
}
func (n *Node) IsOnDemand() bool {
return n.node.Labels["karpenter.sh/capacity-type"] == "on-demand" ||
n.node.Labels["eks.amazonaws.com/capacityType"] == "ON_DEMAND"
}
func (n *Node) IsSpot() bool {
return n.node.Labels["karpenter.sh/capacity-type"] == "spot" ||
n.node.Labels["eks.amazonaws.com/capacityType"] == "SPOT"
}
func (n *Node) IsFargate() bool {
return n.node.Labels["eks.amazonaws.com/compute-type"] == "fargate"
}
func (n *Node) IsAuto() bool {
return n.node.Labels["eks.amazonaws.com/compute-type"] == "auto"
}
func (n *Node) Labels() map[string]string {
return n.node.Labels
}
func (n *Node) Update(node *v1.Node) {
n.mu.Lock()
defer n.mu.Unlock()
n.node = *node
}
func (n *Node) Name() string {
n.mu.RLock()
defer n.mu.RUnlock()
if n.node.Name == "" {
return n.InstanceID()
}
return n.node.Name
}
func (n *Node) ProviderID() string {
n.mu.RLock()
defer n.mu.RUnlock()
return n.node.Spec.ProviderID
}
func (n *Node) InstanceID() string {
providerID := n.ProviderID()
matches := instanceIDRegex.FindStringSubmatch(providerID)
if matches == nil {
return providerID
}
for i, name := range instanceIDRegex.SubexpNames() {
if name == "InstanceID" {
return matches[i]
}
}
return providerID
}
func (n *Node) BindPod(pod *Pod) {
n.mu.Lock()
defer n.mu.Unlock()
key := objectKey{
namespace: pod.Namespace(),
name: pod.Name(),
}
_, alreadyBound := n.pods[key]
n.pods[key] = pod
if !alreadyBound {
for rn, q := range pod.Requested() {
existing := n.used[rn]
existing.Add(q)
n.used[rn] = existing
}
}
}
func (n *Node) DeletePod(namespace string, name string) {
n.mu.Lock()
defer n.mu.Unlock()
key := objectKey{namespace: namespace, name: name}
if p, ok := n.pods[key]; ok {
// subtract the pod requests
for rn, q := range p.Requested() {
existing := n.used[rn]
existing.Sub(q)
n.used[rn] = existing
}
delete(n.pods, key)
}
}
func (n *Node) Allocatable() v1.ResourceList {
n.mu.RLock()
defer n.mu.RUnlock()
// shouldn't be modified so it's safe to return
return n.node.Status.Allocatable
}
func (n *Node) Used() v1.ResourceList {
n.mu.RLock()
defer n.mu.RUnlock()
used := v1.ResourceList{}
for rn, q := range n.used {
used[rn] = q.DeepCopy()
}
return used
}
func (n *Node) UsedNormalized(normalizedAllocation bool) v1.ResourceList {
used := n.Used()
if !normalizedAllocation {
return used
}
allocatable := n.Allocatable()
pctCpu := n.UsedPct(v1.ResourceCPU, false)
pctMem := n.UsedPct(v1.ResourceMemory, false)
if pctCpu > pctMem {
allocatableRes := allocatable[v1.ResourceMemory]
newMem := allocatableRes.AsApproximateFloat64() * pctCpu
used[v1.ResourceMemory] = resource.NewMilliQuantity(int64(newMem*1000), resource.DecimalSI).DeepCopy()
} else if pctMem > pctCpu {
allocatableRes := allocatable[v1.ResourceCPU]
newCpu := allocatableRes.AsApproximateFloat64() * pctMem
used[v1.ResourceCPU] = resource.NewMilliQuantity(int64(newCpu*1000), resource.DecimalSI).DeepCopy()
}
return used
}
func (n *Node) UsedPct(res v1.ResourceName, normalizedAllocation bool) float64 {
used := n.Used()
allocatable := n.Allocatable()
usedRes := used[res]
allocatableRes := allocatable[res]
pct := usedRes.AsApproximateFloat64() / allocatableRes.AsApproximateFloat64()
if allocatableRes.AsApproximateFloat64() == 0 {
pct = 0
} else if normalizedAllocation {
var resRev v1.ResourceName
switch res {
case v1.ResourceCPU:
resRev = v1.ResourceMemory
case v1.ResourceMemory:
resRev = v1.ResourceCPU
}
if resRev != "" {
pctRev := n.UsedPct(resRev, false)
if pctRev > pct {
newUsedRes := allocatableRes.AsApproximateFloat64() * pctRev
pct = newUsedRes / allocatableRes.AsApproximateFloat64()
}
}
}
return pct
}
func (n *Node) Cordoned() bool {
n.mu.RLock()
defer n.mu.RUnlock()
if n.node.Spec.Unschedulable {
return true
}
for _, taint := range n.node.Spec.Taints {
if taint.Key == "karpenter.sh/disruption" && taint.Effect == v1.TaintEffectNoSchedule {
return true
}
}
return false
}
func (n *Node) Ready() bool {
ready := false
n.mu.RLock()
for _, c := range n.node.Status.Conditions {
if c.Status == v1.ConditionTrue && c.Type == v1.NodeReady {
ready = true
break
}
}
n.mu.RUnlock()
// when the node goes ready, remove the nodeclaim creation ts, if any
if ready {
n.mu.Lock()
n.nodeclaimCreationTime = time.Time{}
n.mu.Unlock()
}
return ready
}
func (n *Node) Created() time.Time {
n.mu.RLock()
defer n.mu.RUnlock()
if !n.nodeclaimCreationTime.IsZero() {
return n.nodeclaimCreationTime
}
return n.node.CreationTimestamp.Time
}
func (n *Node) InstanceType() ec2types.InstanceType {
n.mu.RLock()
defer n.mu.RUnlock()
if n.IsFargate() {
if len(n.Pods()) == 1 {
cpu, mem, ok := n.Pods()[0].FargateCapacityProvisioned()
if ok {
return ec2types.InstanceType(fmt.Sprintf("%gvCPU-%gGB", cpu, mem))
}
}
return "Fargate"
}
return ec2types.InstanceType(n.node.Labels[v1.LabelInstanceTypeStable])
}
func (n *Node) Zone() string {
n.mu.RLock()
defer n.mu.RUnlock()
return n.node.Labels[v1.LabelTopologyZone]
}
func (n *Node) NumPods() int {
n.mu.RLock()
defer n.mu.RUnlock()
return len(n.pods)
}
func (n *Node) Hide() {
n.mu.Lock()
defer n.mu.Unlock()
n.visible = false
}
func (n *Node) Visible() bool {
n.mu.RLock()
defer n.mu.RUnlock()
return n.visible
}
func (n *Node) Show() {
n.mu.Lock()
defer n.mu.Unlock()
n.visible = true
}
func (n *Node) Deleting() bool {
n.mu.Lock()
defer n.mu.Unlock()
return !n.node.DeletionTimestamp.IsZero()
}
func (n *Node) Pods() []*Pod {
var pods []*Pod
for _, p := range n.pods {
pods = append(pods, p)
}
return pods
}
func (n *Node) HasPrice() bool {
// we use NaN for an unknown price, so if this is true the price is known
return n.Price == n.Price
}
var resourceLabelRe = regexp.MustCompile("eks-node-viewer/node-(.*?)-usage")
// ComputeLabel computes dynamic labels
func (n *Node) ComputeLabel(labelName string) string {
switch labelName {
case "eks-node-viewer/node-age":
return duration.HumanDuration(time.Since(n.Created()))
}
// resource based custom labels
if match := resourceLabelRe.FindStringSubmatch(labelName); len(match) > 0 {
return pctUsage(n.Allocatable(), n.Used(), match[1])
}
return "-"
}
// NotReadyTime is the time that the node went NotReady, or when it was created if it hasn't been marked as NotReady.
func (n *Node) NotReadyTime() time.Time {
n.mu.RLock()
var notReadyTransitionTime time.Time
for _, c := range n.node.Status.Conditions {
if c.Type == v1.NodeReady && (c.Status == v1.ConditionFalse || c.Status == v1.ConditionUnknown) {
notReadyTransitionTime = c.LastTransitionTime.Time
break
}
}
n.mu.RUnlock()
if !notReadyTransitionTime.IsZero() {
// if there's a nodeclaim creation ts, use it if the node has never been Ready before
if !n.nodeclaimCreationTime.IsZero() {
return n.nodeclaimCreationTime
}
return notReadyTransitionTime
}
return n.Created()
}
func (n *Node) SetPrice(price float64) {
n.Price = price
}
func pctUsage(allocatable v1.ResourceList, used v1.ResourceList, resource string) string {
allocRes, hasAlloc := allocatable[v1.ResourceName(resource)]
if !hasAlloc {
return "N/A"
}
usedRes, hasUsed := used[v1.ResourceName(resource)]
if !hasUsed || usedRes.AsApproximateFloat64() == 0 {
return "0%"
}
pctUsed := 0.0
if allocRes.AsApproximateFloat64() != 0 {
pctUsed = 100 * (usedRes.AsApproximateFloat64() / allocRes.AsApproximateFloat64())
}
return fmt.Sprintf("%.0f%%", pctUsed)
}