/
helper.go
355 lines (290 loc) · 11.3 KB
/
helper.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 helper
import (
"context"
"reflect"
kubermaticv1 "k8c.io/kubermatic/v2/pkg/apis/kubermatic/v1"
"k8c.io/kubermatic/v2/pkg/version/kubermatic"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/util/retry"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
type ClusterPatchFunc func(cluster *kubermaticv1.Cluster)
// UpdateClusterStatus will attempt to patch the cluster status
// of the given cluster.
func UpdateClusterStatus(ctx context.Context, client ctrlruntimeclient.Client, cluster *kubermaticv1.Cluster, patch ClusterPatchFunc) error {
key := ctrlruntimeclient.ObjectKeyFromObject(cluster)
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
// fetch the current state of the cluster
if err := client.Get(ctx, key, cluster); err != nil {
return err
}
// modify it
original := cluster.DeepCopy()
patch(cluster)
// save some work
if reflect.DeepEqual(original.Status, cluster.Status) {
return nil
}
// update the status
return client.Status().Patch(ctx, cluster, ctrlruntimeclient.MergeFrom(original))
})
}
// ClusterReconcileWrapper is a wrapper that should be used around
// any cluster reconciliaton. It:
// * Checks if the cluster is paused
// * Checks if the worker-name matches
// * Sets the ReconcileSuccess condition for the controller by fetching
// the current Cluster object and patching its status.
func ClusterReconcileWrapper(
ctx context.Context,
client ctrlruntimeclient.Client,
workerName string,
cluster *kubermaticv1.Cluster,
versions kubermatic.Versions,
conditionType kubermaticv1.ClusterConditionType,
reconcile func() (*reconcile.Result, error),
) (*reconcile.Result, error) {
if cluster.Labels[kubermaticv1.WorkerNameLabelKey] != workerName {
return nil, nil
}
if cluster.Spec.Pause {
return nil, nil
}
reconcilingStatus := corev1.ConditionFalse
result, err := reconcile()
// Only set to true if we had no error and don't want to reqeue the cluster
if err == nil && (result == nil || (!result.Requeue && result.RequeueAfter == 0)) {
reconcilingStatus = corev1.ConditionTrue
}
errs := []error{err}
if conditionType != kubermaticv1.ClusterConditionNone {
err = UpdateClusterStatus(ctx, client, cluster, func(c *kubermaticv1.Cluster) {
SetClusterCondition(c, versions, conditionType, reconcilingStatus, "", "")
// In KKP 2.21, the ClusterConditionCloudControllerReconcilingSuccess was renamed
// due to a typo; this code ensures that we remove the old condition so that in
// KKP 2.22, we can removed the misspelling from the ENUM in the CRD.
if conditionType == kubermaticv1.ClusterConditionCloudControllerReconcilingSuccess {
delete(c.Status.Conditions, "CloudControllerReconcilledSuccessfully")
}
})
if ctrlruntimeclient.IgnoreNotFound(err) != nil {
errs = append(errs, err)
}
}
return result, kerrors.NewAggregate(errs)
}
// SetClusterCondition sets a condition on the given cluster using the provided type, status,
// reason and message. It also adds the Kubermatic version and timestamps.
func SetClusterCondition(
c *kubermaticv1.Cluster,
v kubermatic.Versions,
conditionType kubermaticv1.ClusterConditionType,
status corev1.ConditionStatus,
reason string,
message string,
) {
newCondition := kubermaticv1.ClusterCondition{
Status: status,
KubermaticVersion: uniqueVersion(v),
Reason: reason,
Message: message,
}
oldCondition, hadCondition := c.Status.Conditions[conditionType]
if hadCondition {
conditionCopy := oldCondition.DeepCopy()
// Reset the times before comparing
conditionCopy.LastHeartbeatTime.Reset()
conditionCopy.LastTransitionTime.Reset()
if apiequality.Semantic.DeepEqual(*conditionCopy, newCondition) {
return
}
}
now := metav1.Now()
newCondition.LastHeartbeatTime = now
newCondition.LastTransitionTime = oldCondition.LastTransitionTime
if hadCondition && oldCondition.Status != status {
newCondition.LastTransitionTime = now
}
if c.Status.Conditions == nil {
c.Status.Conditions = map[kubermaticv1.ClusterConditionType]kubermaticv1.ClusterCondition{}
}
c.Status.Conditions[conditionType] = newCondition
}
// ClusterReconciliationSuccessful checks if cluster has all conditions that are
// required for it to be healthy. ignoreKubermaticVersion should only be set in tests.
func ClusterReconciliationSuccessful(cluster *kubermaticv1.Cluster, versions kubermatic.Versions, ignoreKubermaticVersion bool) (missingConditions []kubermaticv1.ClusterConditionType, success bool) {
conditionsToExclude := []kubermaticv1.ClusterConditionType{kubermaticv1.ClusterConditionSeedResourcesUpToDate}
for _, conditionType := range kubermaticv1.AllClusterConditionTypes {
if conditionTypeListHasConditionType(conditionsToExclude, conditionType) {
continue
}
if !clusterHasCurrentSuccessfulConditionType(cluster, versions, conditionType, ignoreKubermaticVersion) {
missingConditions = append(missingConditions, conditionType)
}
}
return missingConditions, len(missingConditions) == 0
}
func conditionTypeListHasConditionType(
list []kubermaticv1.ClusterConditionType,
t kubermaticv1.ClusterConditionType,
) bool {
for _, item := range list {
if item == t {
return true
}
}
return false
}
func clusterHasCurrentSuccessfulConditionType(
cluster *kubermaticv1.Cluster,
versions kubermatic.Versions,
conditionType kubermaticv1.ClusterConditionType,
ignoreKubermaticVersion bool,
) bool {
condition, exists := cluster.Status.Conditions[conditionType]
if !exists {
return false
}
if condition.Status != corev1.ConditionTrue {
return false
}
if !ignoreKubermaticVersion && (condition.KubermaticVersion != uniqueVersion(versions)) {
return false
}
return true
}
func IsClusterInitialized(cluster *kubermaticv1.Cluster, versions kubermatic.Versions) bool {
isInitialized := cluster.Status.HasConditionValue(kubermaticv1.ClusterConditionClusterInitialized, corev1.ConditionTrue)
// If was set to true at least once just return true
if isInitialized {
return true
}
_, success := ClusterReconciliationSuccessful(cluster, versions, false)
upToDate := cluster.Status.HasConditionValue(kubermaticv1.ClusterConditionSeedResourcesUpToDate, corev1.ConditionTrue)
return success && upToDate && cluster.Status.ExtendedHealth.AllHealthy()
}
// We assume that the cluster is still provisioning if it was not initialized fully at least once.
func GetHealthStatus(status kubermaticv1.HealthStatus, cluster *kubermaticv1.Cluster, versions kubermatic.Versions) kubermaticv1.HealthStatus {
if status == kubermaticv1.HealthStatusDown && !IsClusterInitialized(cluster, versions) {
return kubermaticv1.HealthStatusProvisioning
}
return status
}
func uniqueVersion(v kubermatic.Versions) string {
return v.KubermaticCommit
}
func NeedCCMMigration(cluster *kubermaticv1.Cluster) bool {
_, ccmOk := cluster.Annotations[kubermaticv1.CCMMigrationNeededAnnotation]
_, csiOk := cluster.Annotations[kubermaticv1.CSIMigrationNeededAnnotation]
return ccmOk && csiOk && !CCMMigrationCompleted(cluster)
}
func CCMMigrationCompleted(cluster *kubermaticv1.Cluster) bool {
return cluster.Status.HasConditionValue(kubermaticv1.ClusterConditionCSIKubeletMigrationCompleted, corev1.ConditionTrue)
}
type SeedPatchFunc func(seed *kubermaticv1.Seed)
func UpdateSeedStatus(ctx context.Context, client ctrlruntimeclient.Client, seed *kubermaticv1.Seed, patch SeedPatchFunc) error {
key := ctrlruntimeclient.ObjectKeyFromObject(seed)
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
// fetch the current state of the seed
if err := client.Get(ctx, key, seed); err != nil {
return err
}
// modify it
original := seed.DeepCopy()
patch(seed)
// save some work
if reflect.DeepEqual(original.Status, seed.Status) {
return nil
}
// update the status
return client.Status().Patch(ctx, seed, ctrlruntimeclient.MergeFrom(original))
})
}
type KubermaticConfigurationPatchFunc func(kc *kubermaticv1.KubermaticConfiguration)
func UpdateKubermaticConfigurationStatus(ctx context.Context,
client ctrlruntimeclient.Client,
kc *kubermaticv1.KubermaticConfiguration,
patch KubermaticConfigurationPatchFunc,
) error {
key := ctrlruntimeclient.ObjectKeyFromObject(kc)
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
// fetch the current state of the Kubermatic Configuration
if err := client.Get(ctx, key, kc); err != nil {
return err
}
// modify it
original := kc.DeepCopy()
patch(kc)
if reflect.DeepEqual(original.Status, kc.Status) {
return nil
}
// update the status
return client.Patch(ctx, kc, ctrlruntimeclient.MergeFrom(original))
})
}
// SetSeedCondition sets a condition on the given seed using the provided type, status,
// reason and message.
func SetSeedCondition(seed *kubermaticv1.Seed, conditionType kubermaticv1.SeedConditionType, status corev1.ConditionStatus, reason string, message string) {
newCondition := kubermaticv1.SeedCondition{
Status: status,
Reason: reason,
Message: message,
}
oldCondition, hadCondition := seed.Status.Conditions[conditionType]
if hadCondition {
conditionCopy := oldCondition.DeepCopy()
// Reset the times before comparing
conditionCopy.LastHeartbeatTime.Reset()
conditionCopy.LastTransitionTime.Reset()
if apiequality.Semantic.DeepEqual(*conditionCopy, newCondition) {
return
}
}
now := metav1.Now()
newCondition.LastHeartbeatTime = now
newCondition.LastTransitionTime = oldCondition.LastTransitionTime
if hadCondition && oldCondition.Status != status {
newCondition.LastTransitionTime = now
}
if seed.Status.Conditions == nil {
seed.Status.Conditions = map[kubermaticv1.SeedConditionType]kubermaticv1.SeedCondition{}
}
seed.Status.Conditions[conditionType] = newCondition
}
type ResourceQuotaPatchFunc func(resourceQuota *kubermaticv1.ResourceQuota)
// UpdateResourceQuotaStatus will attempt to patch the resource quota status
// of the given resource quota.
func UpdateResourceQuotaStatus(ctx context.Context, client ctrlruntimeclient.Client, resourceQuota *kubermaticv1.ResourceQuota, patch ResourceQuotaPatchFunc) error {
key := ctrlruntimeclient.ObjectKeyFromObject(resourceQuota)
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
// fetch the current state of the resourceQuota
if err := client.Get(ctx, key, resourceQuota); err != nil {
return err
}
// modify it
original := resourceQuota.DeepCopy()
patch(resourceQuota)
// save some work
if reflect.DeepEqual(original.Status, resourceQuota.Status) {
return nil
}
// update the status
return client.Status().Patch(ctx, resourceQuota, ctrlruntimeclient.MergeFrom(original))
})
}