-
Notifications
You must be signed in to change notification settings - Fork 1
/
machine_filters.go
273 lines (241 loc) · 9.67 KB
/
machine_filters.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
/*
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 machinefilters
import (
bootstrapv1 "github.com/zawachte-msft/cluster-api-bootstrap-provider-k3s/api/v1alpha3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
controlplanev1 "github.com/zawachte-msft/cluster-api-controlplane-provider-k3s/api/v1alpha3"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
type Func func(machine *clusterv1.Machine) bool
// And returns a filter that returns true if all of the given filters returns true.
func And(filters ...Func) Func {
return func(machine *clusterv1.Machine) bool {
for _, f := range filters {
if !f(machine) {
return false
}
}
return true
}
}
// Or returns a filter that returns true if any of the given filters returns true.
func Or(filters ...Func) Func {
return func(machine *clusterv1.Machine) bool {
for _, f := range filters {
if f(machine) {
return true
}
}
return false
}
}
// Not returns a filter that returns the opposite of the given filter.
func Not(mf Func) Func {
return func(machine *clusterv1.Machine) bool {
return !mf(machine)
}
}
// HasControllerRef is a filter that returns true if the machine has a controller ref
func HasControllerRef(machine *clusterv1.Machine) bool {
if machine == nil {
return false
}
return metav1.GetControllerOf(machine) != nil
}
// InFailureDomains returns a filter to find all machines
// in any of the given failure domains
func InFailureDomains(failureDomains ...*string) Func {
return func(machine *clusterv1.Machine) bool {
if machine == nil {
return false
}
for i := range failureDomains {
fd := failureDomains[i]
if fd == nil {
if fd == machine.Spec.FailureDomain {
return true
}
continue
}
if machine.Spec.FailureDomain == nil {
continue
}
if *fd == *machine.Spec.FailureDomain {
return true
}
}
return false
}
}
// OwnedMachines returns a filter to find all owned control plane machines.
// Usage: managementCluster.GetMachinesForCluster(ctx, cluster, machinefilters.OwnedMachines(controlPlane))
func OwnedMachines(owner controllerutil.Object) func(machine *clusterv1.Machine) bool {
return func(machine *clusterv1.Machine) bool {
if machine == nil {
return false
}
return util.IsOwnedByObject(machine, owner)
}
}
// ControlPlaneMachines returns a filter to find all control plane machines for a cluster, regardless of ownership.
// Usage: managementCluster.GetMachinesForCluster(ctx, cluster, machinefilters.ControlPlaneMachines(cluster.Name))
func ControlPlaneMachines(clusterName string) func(machine *clusterv1.Machine) bool {
selector := ControlPlaneSelectorForCluster(clusterName)
return func(machine *clusterv1.Machine) bool {
if machine == nil {
return false
}
return selector.Matches(labels.Set(machine.Labels))
}
}
// AdoptableControlPlaneMachines returns a filter to find all un-controlled control plane machines.
// Usage: managementCluster.GetMachinesForCluster(ctx, cluster, AdoptableControlPlaneMachines(cluster.Name, controlPlane))
func AdoptableControlPlaneMachines(clusterName string) func(machine *clusterv1.Machine) bool {
return And(
ControlPlaneMachines(clusterName),
Not(HasControllerRef),
)
}
// HasDeletionTimestamp returns a filter to find all machines that have a deletion timestamp.
func HasDeletionTimestamp(machine *clusterv1.Machine) bool {
if machine == nil {
return false
}
return !machine.DeletionTimestamp.IsZero()
}
// HasUnhealthyCondition returns a filter to find all machines that have a MachineHealthCheckSucceeded condition set to False,
// indicating a problem was detected on the machine, and the MachineOwnerRemediated condition set, indicating that KCP is
// responsible of performing remediation as owner of the machine.
func HasUnhealthyCondition(machine *clusterv1.Machine) bool {
if machine == nil {
return false
}
return conditions.IsFalse(machine, clusterv1.MachineHealthCheckSuccededCondition) && conditions.IsFalse(machine, clusterv1.MachineOwnerRemediatedCondition)
}
// IsReady returns a filter to find all machines with the ReadyCondition equals to True.
func IsReady() Func {
return func(machine *clusterv1.Machine) bool {
if machine == nil {
return false
}
return conditions.IsTrue(machine, clusterv1.ReadyCondition)
}
}
// ShouldRolloutAfter returns a filter to find all machines where
// CreationTimestamp < rolloutAfter < reconciliationTIme
func ShouldRolloutAfter(reconciliationTime, rolloutAfter *metav1.Time) Func {
return func(machine *clusterv1.Machine) bool {
if machine == nil {
return false
}
return machine.CreationTimestamp.Before(rolloutAfter) && rolloutAfter.Before(reconciliationTime)
}
}
// HasAnnotationKey returns a filter to find all machines that have the
// specified Annotation key present
func HasAnnotationKey(key string) Func {
return func(machine *clusterv1.Machine) bool {
if machine == nil || machine.Annotations == nil {
return false
}
if _, ok := machine.Annotations[key]; ok {
return true
}
return false
}
}
// ControlPlaneSelectorForCluster returns the label selector necessary to get control plane machines for a given cluster.
func ControlPlaneSelectorForCluster(clusterName string) labels.Selector {
must := func(r *labels.Requirement, err error) labels.Requirement {
if err != nil {
panic(err)
}
return *r
}
return labels.NewSelector().Add(
must(labels.NewRequirement(clusterv1.ClusterLabelName, selection.Equals, []string{clusterName})),
must(labels.NewRequirement(clusterv1.MachineControlPlaneLabelName, selection.Exists, []string{})),
)
}
// MatchesKCPConfiguration returns a filter to find all machines that matches with KCP config and do not require any rollout.
// Kubernetes version, infrastructure template, and KThreesConfig field need to be equivalent.
func MatchesKCPConfiguration(infraConfigs map[string]*unstructured.Unstructured, machineConfigs map[string]*bootstrapv1.KThreesConfig, kcp *controlplanev1.KThreesControlPlane) func(machine *clusterv1.Machine) bool {
return And(
MatchesKubernetesVersion(kcp.Spec.Version),
MatchesKThreesBootstrapConfig(machineConfigs, kcp),
MatchesTemplateClonedFrom(infraConfigs, kcp),
)
}
// MatchesTemplateClonedFrom returns a filter to find all machines that match a given KCP infra template.
func MatchesTemplateClonedFrom(infraConfigs map[string]*unstructured.Unstructured, kcp *controlplanev1.KThreesControlPlane) Func {
return func(machine *clusterv1.Machine) bool {
if machine == nil {
return false
}
infraObj, found := infraConfigs[machine.Name]
if !found {
// Return true here because failing to get infrastructure machine should not be considered as unmatching.
return true
}
clonedFromName, ok1 := infraObj.GetAnnotations()[clusterv1.TemplateClonedFromNameAnnotation]
clonedFromGroupKind, ok2 := infraObj.GetAnnotations()[clusterv1.TemplateClonedFromGroupKindAnnotation]
if !ok1 || !ok2 {
// All kcp cloned infra machines should have this annotation.
// Missing the annotation may be due to older version machines or adopted machines.
// Should not be considered as mismatch.
return true
}
// Check if the machine's infrastructure reference has been created from the current KCP infrastructure template.
if clonedFromName != kcp.Spec.InfrastructureTemplate.Name ||
clonedFromGroupKind != kcp.Spec.InfrastructureTemplate.GroupVersionKind().GroupKind().String() {
return false
}
return true
}
}
// MatchesKubernetesVersion returns a filter to find all machines that match a given Kubernetes version.
func MatchesKubernetesVersion(kubernetesVersion string) Func {
return func(machine *clusterv1.Machine) bool {
if machine == nil {
return false
}
if machine.Spec.Version == nil {
return false
}
return *machine.Spec.Version == kubernetesVersion
}
}
// MatchesKThreesBootstrapConfig checks if machine's KThreesConfigSpec is equivalent with KCP's KThreesConfigSpec.
func MatchesKThreesBootstrapConfig(machineConfigs map[string]*bootstrapv1.KThreesConfig, kcp *controlplanev1.KThreesControlPlane) Func {
return func(machine *clusterv1.Machine) bool {
return true
}
}
// getAdjustedKcpConfig takes the KThreesConfigSpec from KCP and applies the transformations required
// to allow a comparison with the KThreesConfig referenced from the machine.
// NOTE: The KCP controller applies a set of transformations when creating a KThreesConfig referenced from the machine,
// mostly depending on the fact that the machine was the initial control plane node or a joining control plane node.
// In this function we don't have such information, so we are making the KThreesConfigSpec similar to the KThreesConfig.
func getAdjustedKcpConfig(kcp *controlplanev1.KThreesControlPlane, machineConfig *bootstrapv1.KThreesConfig) *bootstrapv1.KThreesConfigSpec {
return &kcp.Spec.KThreesConfigSpec
}
// cleanupConfigFields cleanups all the fields that are not relevant for the comparison.
func cleanupConfigFields(kcpConfig *bootstrapv1.KThreesConfigSpec, machineConfig *bootstrapv1.KThreesConfig) {
}