/
failure_domain.go
122 lines (101 loc) · 3.53 KB
/
failure_domain.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
/*
Copyright 2020 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 k3s
import (
"sort"
"k8s.io/klog/v2/klogr"
"k8s.io/utils/pointer"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)
// Log is the global logger for the internal package.
var Log = klogr.New()
type failureDomainAggregation struct {
id string
count int
}
type failureDomainAggregations []failureDomainAggregation
// Len is the number of elements in the collection.
func (f failureDomainAggregations) Len() int {
return len(f)
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (f failureDomainAggregations) Less(i, j int) bool {
return f[i].count < f[j].count
}
// Swap swaps the elements with indexes i and j.
func (f failureDomainAggregations) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}
// PickMost returns a failure domain that is in machines and has most control-plane machines on.
func PickMost(c *ControlPlane, machines FilterableMachineCollection) *string {
// orderDescending sorts failure domains according to all control plane machines
fds := orderDescending(c.Cluster.Status.FailureDomains.FilterControlPlane(), c.Machines)
for _, fd := range fds {
for _, m := range machines {
if m.Spec.FailureDomain == nil {
continue
}
if *m.Spec.FailureDomain == fd.id {
return &fd.id
}
}
}
return nil
}
// orderDescending returns the sorted failure domains in decreasing order.
func orderDescending(failureDomains clusterv1.FailureDomains, machines FilterableMachineCollection) failureDomainAggregations {
aggregations := pick(failureDomains, machines)
if len(aggregations) == 0 {
return nil
}
sort.Sort(sort.Reverse(aggregations))
return aggregations
}
// PickFewest returns the failure domain with the fewest number of machines.
func PickFewest(failureDomains clusterv1.FailureDomains, machines FilterableMachineCollection) *string {
aggregations := pick(failureDomains, machines)
if len(aggregations) == 0 {
return nil
}
sort.Sort(aggregations)
return pointer.String(aggregations[0].id)
}
func pick(failureDomains clusterv1.FailureDomains, machines FilterableMachineCollection) failureDomainAggregations {
if len(failureDomains) == 0 {
return failureDomainAggregations{}
}
counters := map[string]int{}
// Initialize the known failure domain keys to find out if an existing machine is in an unsupported failure domain.
for fd := range failureDomains {
counters[fd] = 0
}
// Count how many machines are in each failure domain.
for _, m := range machines {
if m.Spec.FailureDomain == nil {
continue
}
id := *m.Spec.FailureDomain
if _, ok := failureDomains[id]; !ok {
Log.Info("unknown failure domain", "machine-name", m.GetName(), "failure-domain-id", id, "known-failure-domains", failureDomains)
continue
}
counters[id]++
}
aggregations := make(failureDomainAggregations, 0)
// Gather up tuples of failure domains ids and counts
for fd, count := range counters {
aggregations = append(aggregations, failureDomainAggregation{id: fd, count: count})
}
return aggregations
}