-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
machine_collection.go
230 lines (198 loc) · 6.63 KB
/
machine_collection.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
/*
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.
*/
// Modified copy of k8s.io/apimachinery/pkg/util/sets/int64.go
// Modifications
// - int64 became *clusterv1.Machine
// - Empty type is removed
// - Sortable data type is removed in favor of util.MachinesByCreationTimestamp
// - nil checks added to account for the pointer
// - Added Filter, AnyFilter, and Oldest methods
// - Added FromMachineList initializer
// - Updated Has to also check for equality of Machines
// - Removed unused methods
package collections
import (
"sort"
"github.com/blang/semver/v4"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/version"
)
// Machines is a set of Machines.
type Machines map[string]*clusterv1.Machine
// MachinesByVersion sorts the list of Machine by spec.version, using their names as tie breaker.
// machines with no version are placed lower in the order.
type machinesByVersion []*clusterv1.Machine
func (v machinesByVersion) Len() int { return len(v) }
func (v machinesByVersion) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
func (v machinesByVersion) Less(i, j int) bool {
vi, _ := semver.ParseTolerant(*v[i].Spec.Version)
vj, _ := semver.ParseTolerant(*v[j].Spec.Version)
comp := version.Compare(vi, vj, version.WithBuildTags())
if comp == 0 {
return v[i].Name < v[j].Name
}
return comp == -1
}
// machinesByCreationTimestamp sorts a list of Machine by creation timestamp, using their names as a tie breaker.
type machinesByCreationTimestamp []*clusterv1.Machine
func (o machinesByCreationTimestamp) Len() int { return len(o) }
func (o machinesByCreationTimestamp) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o machinesByCreationTimestamp) Less(i, j int) bool {
if o[i].CreationTimestamp.Equal(&o[j].CreationTimestamp) {
return o[i].Name < o[j].Name
}
return o[i].CreationTimestamp.Before(&o[j].CreationTimestamp)
}
// New creates an empty Machines.
func New() Machines {
return make(Machines)
}
// FromMachines creates a Machines from a list of values.
func FromMachines(machines ...*clusterv1.Machine) Machines {
ss := make(Machines, len(machines))
ss.Insert(machines...)
return ss
}
// FromMachineList creates a Machines from the given MachineList.
func FromMachineList(machineList *clusterv1.MachineList) Machines {
ss := make(Machines, len(machineList.Items))
for i := range machineList.Items {
ss.Insert(&machineList.Items[i])
}
return ss
}
// ToMachineList creates a MachineList from the given Machines.
func ToMachineList(machines Machines) clusterv1.MachineList {
ml := clusterv1.MachineList{}
for _, m := range machines {
ml.Items = append(ml.Items, *m)
}
return ml
}
// Insert adds items to the set.
func (s Machines) Insert(machines ...*clusterv1.Machine) {
for i := range machines {
if machines[i] != nil {
m := machines[i]
s[m.Name] = m
}
}
}
// Difference returns a copy without machines that are in the given collection.
func (s Machines) Difference(machines Machines) Machines {
return s.Filter(func(m *clusterv1.Machine) bool {
_, found := machines[m.Name]
return !found
})
}
// SortedByCreationTimestamp returns the machines sorted by creation timestamp.
func (s Machines) SortedByCreationTimestamp() []*clusterv1.Machine {
res := make(machinesByCreationTimestamp, 0, len(s))
for _, value := range s {
res = append(res, value)
}
sort.Sort(res)
return res
}
// UnsortedList returns the slice with contents in random order.
func (s Machines) UnsortedList() []*clusterv1.Machine {
res := make([]*clusterv1.Machine, 0, len(s))
for _, value := range s {
res = append(res, value)
}
return res
}
// Len returns the size of the set.
func (s Machines) Len() int {
return len(s)
}
// newFilteredMachineCollection creates a Machines from a filtered list of values.
func newFilteredMachineCollection(filter Func, machines ...*clusterv1.Machine) Machines {
ss := make(Machines, len(machines))
for i := range machines {
m := machines[i]
if filter(m) {
ss.Insert(m)
}
}
return ss
}
// Filter returns a Machines containing only the Machines that match all of the given MachineFilters.
func (s Machines) Filter(filters ...Func) Machines {
return newFilteredMachineCollection(And(filters...), s.UnsortedList()...)
}
// AnyFilter returns a Machines containing only the Machines that match any of the given MachineFilters.
func (s Machines) AnyFilter(filters ...Func) Machines {
return newFilteredMachineCollection(Or(filters...), s.UnsortedList()...)
}
// Oldest returns the Machine with the oldest CreationTimestamp.
func (s Machines) Oldest() *clusterv1.Machine {
if len(s) == 0 {
return nil
}
return s.SortedByCreationTimestamp()[0]
}
// Newest returns the Machine with the most recent CreationTimestamp.
func (s Machines) Newest() *clusterv1.Machine {
if len(s) == 0 {
return nil
}
return s.SortedByCreationTimestamp()[len(s)-1]
}
// DeepCopy returns a deep copy.
func (s Machines) DeepCopy() Machines {
result := make(Machines, len(s))
for _, m := range s {
result.Insert(m.DeepCopy())
}
return result
}
// ConditionGetters returns the slice with machines converted into conditions.Getter.
func (s Machines) ConditionGetters() []conditions.Getter {
res := make([]conditions.Getter, 0, len(s))
for _, v := range s {
value := *v
res = append(res, &value)
}
return res
}
// Names returns a slice of the names of each machine in the collection.
// Useful for logging and test assertions.
func (s Machines) Names() []string {
names := make([]string, 0, s.Len())
for _, m := range s {
names = append(names, m.Name)
}
return names
}
// SortedByVersion returns the machines sorted by version.
func (s Machines) sortedByVersion() []*clusterv1.Machine {
res := make(machinesByVersion, 0, len(s))
for _, value := range s {
res = append(res, value)
}
sort.Sort(res)
return res
}
// LowestVersion returns the lowest version among all the machine with
// defined versions. If no machine has a defined version it returns an
// empty string.
func (s Machines) LowestVersion() *string {
machines := s.Filter(WithVersion())
if len(machines) == 0 {
return nil
}
m := machines.sortedByVersion()[0]
return m.Spec.Version
}