forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodessyncer.go
651 lines (583 loc) · 21.4 KB
/
nodessyncer.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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package nodesyncer
import (
"context"
"fmt"
"reflect"
"sort"
"github.com/pkg/errors"
"github.com/rancher/rancher/pkg/controllers/management/compose/common"
nodehelper "github.com/rancher/rancher/pkg/node"
"github.com/rancher/rancher/pkg/systemaccount"
v1 "github.com/rancher/types/apis/core/v1"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/rancher/types/user"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
const (
AllNodeKey = "_machine_all_"
annotationName = "management.cattle.io/nodesyncer"
apiUpdate = "management.cattle.io/apiUpdate"
)
var apiUpdateMap = map[string]string{apiUpdate: "true"}
type nodeSyncer struct {
machines v3.NodeInterface
clusterNamespace string
nodesSyncer *nodesSyncer
}
type nodesSyncer struct {
machines v3.NodeInterface
machineLister v3.NodeLister
nodeLister v1.NodeLister
nodeClient v1.NodeInterface
podLister v1.PodLister
clusterNamespace string
clusterLister v3.ClusterLister
}
type nodeDrain struct {
userManager user.Manager
tokenClient v3.TokenInterface
userClient v3.UserInterface
kubeConfigGetter common.KubeConfigGetter
clusterName string
systemAccountManager *systemaccount.Manager
clusterLister v3.ClusterLister
machines v3.NodeInterface
ctx context.Context
nodesToContext map[string]context.CancelFunc
}
func Register(ctx context.Context, cluster *config.UserContext, kubeConfigGetter common.KubeConfigGetter) {
m := &nodesSyncer{
clusterNamespace: cluster.ClusterName,
machines: cluster.Management.Management.Nodes(cluster.ClusterName),
machineLister: cluster.Management.Management.Nodes(cluster.ClusterName).Controller().Lister(),
nodeLister: cluster.Core.Nodes("").Controller().Lister(),
nodeClient: cluster.Core.Nodes(""),
podLister: cluster.Core.Pods("").Controller().Lister(),
clusterLister: cluster.Management.Management.Clusters("").Controller().Lister(),
}
n := &nodeSyncer{
clusterNamespace: cluster.ClusterName,
machines: cluster.Management.Management.Nodes(cluster.ClusterName),
nodesSyncer: m,
}
d := &nodeDrain{
userManager: cluster.Management.UserManager,
tokenClient: cluster.Management.Management.Tokens(""),
userClient: cluster.Management.Management.Users(""),
kubeConfigGetter: kubeConfigGetter,
clusterName: cluster.ClusterName,
systemAccountManager: systemaccount.NewManager(cluster.Management),
clusterLister: cluster.Management.Management.Clusters("").Controller().Lister(),
machines: cluster.Management.Management.Nodes(cluster.ClusterName),
ctx: ctx,
nodesToContext: map[string]context.CancelFunc{},
}
cluster.Core.Nodes("").Controller().AddHandler(ctx, "nodesSyncer", n.sync)
cluster.Management.Management.Nodes(cluster.ClusterName).Controller().AddHandler(ctx, "machinesSyncer", m.sync)
cluster.Management.Management.Nodes(cluster.ClusterName).Controller().AddHandler(ctx, "machinesLabelSyncer", m.syncLabels)
cluster.Management.Management.Nodes(cluster.ClusterName).Controller().AddHandler(ctx, "cordonFieldsSyncer", m.syncCordonFields)
cluster.Management.Management.Nodes(cluster.ClusterName).Controller().AddHandler(ctx, "drainNodeSyncer", d.drainNode)
}
func (n *nodeSyncer) sync(key string, node *corev1.Node) (runtime.Object, error) {
needUpdate, err := n.needUpdate(key, node)
if err != nil {
return nil, err
}
if needUpdate {
n.machines.Controller().Enqueue(n.clusterNamespace, AllNodeKey)
}
return nil, nil
}
func (n *nodeSyncer) needUpdate(key string, node *corev1.Node) (bool, error) {
if node == nil || node.DeletionTimestamp != nil {
return true, nil
}
existing, err := n.nodesSyncer.getMachineForNode(node, true)
if err != nil {
return false, err
}
if existing == nil {
return true, nil
}
nodeToPodMap, err := n.nodesSyncer.getNonTerminatedPods()
if err != nil {
return false, err
}
toUpdate, err := n.nodesSyncer.convertNodeToMachine(node, existing, nodeToPodMap)
if err != nil {
return false, err
}
// update only when nothing changed
if objectsAreEqual(existing, toUpdate) {
return false, nil
}
return true, nil
}
func (m *nodesSyncer) sync(key string, machine *v3.Node) (runtime.Object, error) {
if key == fmt.Sprintf("%s/%s", m.clusterNamespace, AllNodeKey) {
return nil, m.reconcileAll()
}
return nil, nil
}
func (m *nodesSyncer) syncLabels(key string, obj *v3.Node) (runtime.Object, error) {
if obj == nil {
return nil, nil
}
if obj.Spec.DesiredNodeAnnotations == nil && obj.Spec.DesiredNodeLabels == nil {
return nil, nil
}
node, err := nodehelper.GetNodeForMachine(obj, m.nodeLister)
if err != nil || node == nil {
return nil, err
}
updateLabels := false
updateAnnotations := false
// set annotations
if obj.Spec.DesiredNodeAnnotations != nil && !reflect.DeepEqual(node.Annotations, obj.Spec.DesiredNodeAnnotations) {
// check sync from node controller and labels.go has finished if not set by api
if reflect.DeepEqual(obj.Spec.CurrentNodeAnnotations, apiUpdateMap) {
updateAnnotations = true
} else {
updateAnnotations = reflect.DeepEqual(node.Annotations, obj.Status.NodeAnnotations) && reflect.DeepEqual(node.Annotations, obj.Spec.CurrentNodeAnnotations)
}
}
// set labels
if obj.Spec.DesiredNodeLabels != nil && !reflect.DeepEqual(node.Labels, obj.Spec.DesiredNodeLabels) {
// check sync from node controller and labels.go has finished if not set by api
if reflect.DeepEqual(obj.Spec.CurrentNodeLabels, apiUpdateMap) {
updateLabels = true
} else {
updateLabels = reflect.DeepEqual(node.Labels, obj.Status.NodeLabels) && reflect.DeepEqual(node.Labels, obj.Spec.CurrentNodeLabels)
}
}
if updateLabels || updateAnnotations {
toUpdate := node.DeepCopy()
if updateLabels {
toUpdate.Labels = obj.Spec.DesiredNodeLabels
}
if updateAnnotations {
toUpdate.Annotations = obj.Spec.DesiredNodeAnnotations
}
logrus.Infof("Updating node %v with labels %v and annotations %v", toUpdate.Name, toUpdate.Labels, toUpdate.Annotations)
if _, err = m.nodeClient.Update(toUpdate); err != nil {
return nil, err
}
}
// in the end we reset all desired fields
if obj.Spec.DesiredNodeAnnotations != nil || obj.Spec.DesiredNodeLabels != nil {
machine := obj.DeepCopy()
machine.Spec.DesiredNodeAnnotations = nil
machine.Spec.DesiredNodeLabels = nil
machine.Spec.CurrentNodeAnnotations = nil
machine.Spec.CurrentNodeLabels = nil
return m.machines.Update(machine)
}
return nil, nil
}
func (m *nodesSyncer) reconcileAll() error {
nodes, err := m.nodeLister.List("", labels.NewSelector())
if err != nil {
return err
}
nodeMap := make(map[string]*corev1.Node)
for _, node := range nodes {
nodeMap[node.Name] = node
}
machines, err := m.machineLister.List(m.clusterNamespace, labels.NewSelector())
if err != nil {
return err
}
machineMap := make(map[string]*v3.Node)
toDelete := make(map[string]*v3.Node)
for _, machine := range machines {
node, err := nodehelper.GetNodeForMachine(machine, m.nodeLister)
if err != nil {
return err
}
if node == nil {
logrus.Debugf("Failed to get node for machine [%s]", machine.Name)
toDelete[machine.Name] = machine
continue
}
machineMap[node.Name] = machine
}
nodeToPodMap, err := m.getNonTerminatedPods()
if err != nil {
return err
}
// reconcile machines for existing nodes
for name, node := range nodeMap {
machine := machineMap[name]
err = m.reconcileNodeForNode(machine, node, nodeToPodMap)
if err != nil {
return err
}
}
// run the logic for machine to remove
for name, machine := range machineMap {
if _, ok := nodeMap[name]; !ok {
if err = m.removeNode(machine); err != nil {
return err
}
}
}
for _, machine := range toDelete {
if err = m.removeNode(machine); err != nil {
return err
}
}
return nil
}
func (m *nodesSyncer) reconcileNodeForNode(machine *v3.Node, node *corev1.Node, pods map[string][]*corev1.Pod) error {
if machine == nil {
return m.createNode(node, pods)
}
return m.updateNode(machine, node, pods)
}
func (m *nodesSyncer) removeNode(machine *v3.Node) error {
if machine.Annotations == nil {
return nil
}
if _, ok := machine.Annotations[annotationName]; !ok {
return nil
}
err := m.machines.Delete(machine.ObjectMeta.Name, nil)
if err != nil {
return errors.Wrapf(err, "Failed to delete machine [%s]", machine.Name)
}
logrus.Infof("Deleted cluster node [%s]", machine.Name)
return nil
}
func (m *nodesSyncer) updateNode(existing *v3.Node, node *corev1.Node, pods map[string][]*corev1.Pod) error {
toUpdate, err := m.convertNodeToMachine(node, existing, pods)
if err != nil {
return err
}
// update only when nothing changed
if objectsAreEqual(existing, toUpdate) {
return nil
}
logrus.Debugf("Updating machine for node [%s]", node.Name)
_, err = m.machines.Update(toUpdate)
if err != nil {
return errors.Wrapf(err, "Failed to update machine for node [%s]", node.Name)
}
logrus.Debugf("Updated machine for node [%s]", node.Name)
return nil
}
func (m *nodesSyncer) createNode(node *corev1.Node, pods map[string][]*corev1.Pod) error {
// do not create machine for rke cluster
cluster, err := m.clusterLister.Get("", m.clusterNamespace)
if err != nil {
return err
}
if cluster.Spec.RancherKubernetesEngineConfig != nil {
return nil
}
// try to get machine from api, in case cache didn't get the update
existing, err := m.getMachineForNode(node, false)
if err != nil {
return err
}
if existing != nil {
return nil
}
machine, err := m.convertNodeToMachine(node, existing, pods)
if err != nil {
return err
}
if machine.Annotations == nil {
machine.Annotations = make(map[string]string)
}
machine.Annotations[annotationName] = "true"
_, err = m.machines.Create(machine)
if err != nil {
return errors.Wrapf(err, "Failed to create machine for node [%s]", node.Name)
}
logrus.Infof("Created machine for node [%s]", node.Name)
return nil
}
func (m *nodesSyncer) getMachineForNode(node *corev1.Node, cache bool) (*v3.Node, error) {
if cache {
return nodehelper.GetMachineForNode(node, m.clusterNamespace, m.machineLister)
}
labelsSearchSet := labels.Set{nodehelper.LabelNodeName: node.Name}
machines, err := m.machines.List(metav1.ListOptions{LabelSelector: labelsSearchSet.AsSelector().String()})
if err != nil {
return nil, err
}
if len(machines.Items) == 0 {
machines, err = m.machines.List(metav1.ListOptions{})
if err != nil {
return nil, err
}
}
for _, machine := range machines.Items {
if machine.Namespace == m.clusterNamespace {
if nodehelper.IsNodeForNode(node, &machine) {
return &machine, nil
}
}
}
return nil, nil
}
func resetConditions(machine *v3.Node) *v3.Node {
if machine.Status.InternalNodeStatus.Conditions == nil {
return machine
}
updated := machine.DeepCopy()
var toUpdateConds []corev1.NodeCondition
for _, cond := range machine.Status.InternalNodeStatus.Conditions {
toUpdateCond := cond.DeepCopy()
toUpdateCond.LastHeartbeatTime = metav1.Time{}
toUpdateCond.LastTransitionTime = metav1.Time{}
toUpdateConds = append(toUpdateConds, *toUpdateCond)
}
sort.Slice(toUpdateConds, func(i, j int) bool {
return toUpdateConds[i].Type < toUpdateConds[j].Type
})
updated.Status.InternalNodeStatus.Conditions = toUpdateConds
return updated
}
func objectsAreEqual(existing *v3.Node, toUpdate *v3.Node) bool {
// we are updating spec and status only, so compare them
toUpdateToCompare := resetConditions(toUpdate)
existingToCompare := resetConditions(existing)
statusEqual := statusEqualTest(toUpdateToCompare.Status.InternalNodeStatus, existingToCompare.Status.InternalNodeStatus)
labelsEqual := reflect.DeepEqual(toUpdateToCompare.Status.NodeLabels, existing.Status.NodeLabels) && reflect.DeepEqual(toUpdateToCompare.Labels, existing.Labels)
annotationsEqual := reflect.DeepEqual(toUpdateToCompare.Status.NodeAnnotations, existing.Status.NodeAnnotations)
specEqual := reflect.DeepEqual(toUpdateToCompare.Spec.InternalNodeSpec, existingToCompare.Spec.InternalNodeSpec)
nodeNameEqual := toUpdateToCompare.Status.NodeName == existingToCompare.Status.NodeName
requestsEqual := isEqual(toUpdateToCompare.Status.Requested, existingToCompare.Status.Requested)
limitsEqual := isEqual(toUpdateToCompare.Status.Limits, existingToCompare.Status.Limits)
rolesEqual := toUpdateToCompare.Spec.Worker == existingToCompare.Spec.Worker && toUpdateToCompare.Spec.Etcd == existingToCompare.Spec.Etcd &&
toUpdateToCompare.Spec.ControlPlane == existingToCompare.Spec.ControlPlane
retVal := statusEqual && specEqual && nodeNameEqual && labelsEqual && annotationsEqual && requestsEqual && limitsEqual && rolesEqual
if !retVal {
logrus.Debugf("ObjectsAreEqualResults for %s: statusEqual: %t specEqual: %t"+
" nodeNameEqual: %t labelsEqual: %t annotationsEqual: %t requestsEqual: %t limitsEqual: %t rolesEqual: %t",
toUpdate.Name, statusEqual, specEqual, nodeNameEqual, labelsEqual, annotationsEqual, requestsEqual, limitsEqual, rolesEqual)
}
return retVal
}
func statusEqualTest(proposed, existing corev1.NodeStatus) bool {
// Tests here should validate that fields of the corev1.NodeStatus type are equal for Rancher's purposes.
// The Images field lists would be equal if they contain the same data regardless of order. Using reflect.DeepEqual
// does not see lists with the same content but different order as equal, and would cause
// Rancher to update the resource unnecessarily. So if Images becomes a field we need to validate we need to add
// a custom method to validate the equality.
//
// Rancher doesn't use the following NodeStatus data, so for time savings we are skipping, but in the future these tests
// should be added here.
//
// SKIP:
// - Images # Do not use reflect.DeepEquals on this field for testing.
// - NodeInfo
// - DaemonEndpoints
// - Phase
// Capacity
if !reflect.DeepEqual(proposed.Capacity, existing.Capacity) {
logrus.Debugf("Changes in Capacity, proposed %#v, existing: %#v", proposed.Capacity, existing.Capacity)
return false
}
// Allocatable
if !reflect.DeepEqual(proposed.Allocatable, existing.Allocatable) {
logrus.Debugf("Changes in Allocatable, proposed %#v, existing: %#v", proposed.Allocatable, existing.Allocatable)
return false
}
// Conditions
if !reflect.DeepEqual(proposed.Conditions, existing.Conditions) {
logrus.Debugf("Changes in Conditions, proposed %#v, existing: %#v", proposed.Conditions, existing.Conditions)
return false
}
// Addresses
if !reflect.DeepEqual(proposed.Addresses, existing.Addresses) {
logrus.Debugf("Changes in Addresses, proposed %#v, existing: %#v", proposed.Addresses, existing.Addresses)
return false
}
// Volumes in use (This test might prove to be an issue if order is not returned consistently.)
if !reflect.DeepEqual(proposed.VolumesInUse, existing.VolumesInUse) {
logrus.Debugf("Changes in VolumesInUse, proposed %#v, existing: %#v", proposed.VolumesInUse, existing.VolumesInUse)
return false
}
// VolumesAttached (This test might prove to cause excessive updates if order is not returned consistently.)
if !reflect.DeepEqual(proposed.VolumesAttached, existing.VolumesAttached) {
logrus.Debugf("Changes in VolumesAttached, proposed %#v, existing: %#v", proposed.VolumesAttached, existing.VolumesAttached)
return false
}
// Compare Node's Kubernetes versions
if proposed.NodeInfo.KubeletVersion != existing.NodeInfo.KubeletVersion ||
proposed.NodeInfo.KubeProxyVersion != existing.NodeInfo.KubeProxyVersion ||
proposed.NodeInfo.ContainerRuntimeVersion != existing.NodeInfo.ContainerRuntimeVersion {
logrus.Debugf("Changes in KubernetesInfo, "+
"KubeletVersion proposed %#v, existing: %#v"+
"KubeProxyVersion proposed %#v, existing: %#v"+
"ContainerRuntimeVersion proposed %#v, existing: %#v",
proposed.NodeInfo.KubeletVersion, existing.NodeInfo.KubeletVersion,
proposed.NodeInfo.KubeProxyVersion, existing.NodeInfo.KubeProxyVersion,
proposed.NodeInfo.ContainerRuntimeVersion, existing.NodeInfo.ContainerRuntimeVersion)
return false
}
return true
}
func (m *nodesSyncer) convertNodeToMachine(node *corev1.Node, existing *v3.Node, pods map[string][]*corev1.Pod) (*v3.Node, error) {
var machine *v3.Node
if existing == nil {
machine = &v3.Node{
Spec: v3.NodeSpec{},
Status: v3.NodeStatus{},
ObjectMeta: metav1.ObjectMeta{
GenerateName: "machine-"},
}
machine.Namespace = m.clusterNamespace
machine.Status.Requested = make(map[corev1.ResourceName]resource.Quantity)
machine.Status.Limits = make(map[corev1.ResourceName]resource.Quantity)
machine.Spec.InternalNodeSpec = *node.Spec.DeepCopy()
machine.Status.InternalNodeStatus = *node.Status.DeepCopy()
machine.Spec.RequestedHostname = node.Name
} else {
machine = existing.DeepCopy()
machine.Spec.InternalNodeSpec = *node.Spec.DeepCopy()
machine.Status.InternalNodeStatus = *node.Status.DeepCopy()
}
requests, limits := aggregateRequestAndLimitsForNode(pods[node.Name])
if machine.Status.Requested == nil {
machine.Status.Requested = corev1.ResourceList{}
}
if machine.Status.Limits == nil {
machine.Status.Limits = corev1.ResourceList{}
}
for name, quantity := range requests {
machine.Status.Requested[name] = quantity
}
for name, quantity := range limits {
machine.Status.Limits[name] = quantity
}
if node.Labels != nil {
_, etcd := node.Labels["node-role.kubernetes.io/etcd"]
machine.Spec.Etcd = etcd
_, control := node.Labels["node-role.kubernetes.io/controlplane"]
_, master := node.Labels["node-role.kubernetes.io/master"]
if control || master {
machine.Spec.ControlPlane = true
}
_, worker := node.Labels["node-role.kubernetes.io/worker"]
machine.Spec.Worker = worker
if !machine.Spec.Worker && !machine.Spec.ControlPlane && !machine.Spec.Etcd {
machine.Spec.Worker = true
}
}
machine.Status.NodeAnnotations = node.Annotations
machine.Status.NodeLabels = node.Labels
machine.Status.NodeName = node.Name
machine.APIVersion = "management.cattle.io/v3"
machine.Kind = "Node"
if machine.Labels == nil {
machine.Labels = map[string]string{}
}
machine.Labels[nodehelper.LabelNodeName] = node.Name
v3.NodeConditionRegistered.True(machine)
v3.NodeConditionRegistered.Message(machine, "registered with kubernetes")
return machine, nil
}
func (m *nodesSyncer) getNonTerminatedPods() (map[string][]*corev1.Pod, error) {
pods := make(map[string][]*corev1.Pod)
fromCache, err := m.podLister.List("", labels.NewSelector())
if err != nil {
return pods, err
}
for _, pod := range fromCache {
if pod.Spec.NodeName == "" {
continue
}
// kubectl uses this cache to filter out the pods
if pod.Status.Phase == "Succeeded" || pod.Status.Phase == "Failed" {
continue
}
var nodePods []*corev1.Pod
if fromMap, ok := pods[pod.Spec.NodeName]; ok {
nodePods = fromMap
}
nodePods = append(nodePods, pod)
pods[pod.Spec.NodeName] = nodePods
}
return pods, nil
}
func aggregateRequestAndLimitsForNode(pods []*corev1.Pod) (map[corev1.ResourceName]resource.Quantity, map[corev1.ResourceName]resource.Quantity) {
requests, limits := map[corev1.ResourceName]resource.Quantity{}, map[corev1.ResourceName]resource.Quantity{}
podsData := make(map[string]map[string]map[corev1.ResourceName]resource.Quantity)
if pods != nil {
//podName -> req/limit -> data
for _, pod := range pods {
podsData[pod.Name] = make(map[string]map[corev1.ResourceName]resource.Quantity)
requests, limits := getPodData(pod)
podsData[pod.Name]["requests"] = requests
podsData[pod.Name]["limits"] = limits
}
requests[corev1.ResourcePods] = *resource.NewQuantity(int64(len(pods)), resource.DecimalSI)
}
for _, podData := range podsData {
podRequests, podLimits := podData["requests"], podData["limits"]
addMap(podRequests, requests)
addMap(podLimits, limits)
}
return requests, limits
}
func isEqual(data1 map[corev1.ResourceName]resource.Quantity, data2 map[corev1.ResourceName]resource.Quantity) bool {
if len(data1) == 0 && len(data2) == 0 {
return true
}
if data1 == nil || data2 == nil {
return false
}
for key, value := range data1 {
if _, exists := data2[key]; !exists {
return false
}
value2 := data2[key]
if value.Value() != value2.Value() {
return false
}
}
return true
}
func getPodData(pod *corev1.Pod) (map[corev1.ResourceName]resource.Quantity, map[corev1.ResourceName]resource.Quantity) {
requests, limits := map[corev1.ResourceName]resource.Quantity{}, map[corev1.ResourceName]resource.Quantity{}
for _, container := range pod.Spec.Containers {
addMap(container.Resources.Requests, requests)
addMap(container.Resources.Limits, limits)
}
for _, container := range pod.Spec.InitContainers {
addMapForInit(container.Resources.Requests, requests)
addMapForInit(container.Resources.Limits, limits)
}
return requests, limits
}
func addMap(data1 map[corev1.ResourceName]resource.Quantity, data2 map[corev1.ResourceName]resource.Quantity) {
for name, quantity := range data1 {
if value, ok := data2[name]; !ok {
data2[name] = *quantity.Copy()
} else {
value.Add(quantity)
data2[name] = value
}
}
}
func addMapForInit(data1 map[corev1.ResourceName]resource.Quantity, data2 map[corev1.ResourceName]resource.Quantity) {
for name, quantity := range data1 {
value, ok := data2[name]
if !ok {
data2[name] = *quantity.Copy()
continue
}
if quantity.Cmp(value) > 0 {
data2[name] = *quantity.Copy()
}
}
}