-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathhelpers.go
567 lines (491 loc) · 17.7 KB
/
helpers.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
package v1helpers
import (
"context"
"errors"
"fmt"
"os"
"sort"
"strings"
"time"
"github.com/google/go-cmp/cmp"
configv1 "github.com/openshift/api/config/v1"
operatorv1 "github.com/openshift/api/operator/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
"sigs.k8s.io/yaml"
)
const (
progressingConditionTimeout = 15 * time.Minute
)
// SetOperandVersion sets the new version and returns the previous value.
func SetOperandVersion(versions *[]configv1.OperandVersion, operandVersion configv1.OperandVersion) string {
if versions == nil {
versions = &[]configv1.OperandVersion{}
}
existingVersion := FindOperandVersion(*versions, operandVersion.Name)
if existingVersion == nil {
*versions = append(*versions, operandVersion)
return ""
}
previous := existingVersion.Version
existingVersion.Version = operandVersion.Version
return previous
}
func FindOperandVersion(versions []configv1.OperandVersion, name string) *configv1.OperandVersion {
if versions == nil {
return nil
}
for i := range versions {
if versions[i].Name == name {
return &versions[i]
}
}
return nil
}
func SetOperatorCondition(conditions *[]operatorv1.OperatorCondition, newCondition operatorv1.OperatorCondition) {
if conditions == nil {
conditions = &[]operatorv1.OperatorCondition{}
}
existingCondition := FindOperatorCondition(*conditions, newCondition.Type)
if existingCondition == nil {
newCondition.LastTransitionTime = metav1.NewTime(time.Now())
*conditions = append(*conditions, newCondition)
return
}
if existingCondition.Status != newCondition.Status {
existingCondition.Status = newCondition.Status
existingCondition.LastTransitionTime = metav1.NewTime(time.Now())
}
existingCondition.Reason = newCondition.Reason
existingCondition.Message = newCondition.Message
}
func RemoveOperatorCondition(conditions *[]operatorv1.OperatorCondition, conditionType string) {
if conditions == nil {
conditions = &[]operatorv1.OperatorCondition{}
}
newConditions := []operatorv1.OperatorCondition{}
for _, condition := range *conditions {
if condition.Type != conditionType {
newConditions = append(newConditions, condition)
}
}
*conditions = newConditions
}
func FindOperatorCondition(conditions []operatorv1.OperatorCondition, conditionType string) *operatorv1.OperatorCondition {
for i := range conditions {
if conditions[i].Type == conditionType {
return &conditions[i]
}
}
return nil
}
func IsOperatorConditionTrue(conditions []operatorv1.OperatorCondition, conditionType string) bool {
return IsOperatorConditionPresentAndEqual(conditions, conditionType, operatorv1.ConditionTrue)
}
func IsOperatorConditionFalse(conditions []operatorv1.OperatorCondition, conditionType string) bool {
return IsOperatorConditionPresentAndEqual(conditions, conditionType, operatorv1.ConditionFalse)
}
func IsOperatorConditionPresentAndEqual(conditions []operatorv1.OperatorCondition, conditionType string, status operatorv1.ConditionStatus) bool {
for _, condition := range conditions {
if condition.Type == conditionType {
return condition.Status == status
}
}
return false
}
// UpdateOperatorSpecFunc is a func that mutates an operator spec.
type UpdateOperatorSpecFunc func(spec *operatorv1.OperatorSpec) error
// UpdateSpec applies the update funcs to the oldStatus and tries to update via the client.
func UpdateSpec(ctx context.Context, client OperatorClient, updateFuncs ...UpdateOperatorSpecFunc) (*operatorv1.OperatorSpec, bool, error) {
updated := false
var operatorSpec *operatorv1.OperatorSpec
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
oldSpec, _, resourceVersion, err := client.GetOperatorState()
if err != nil {
return err
}
newSpec := oldSpec.DeepCopy()
for _, update := range updateFuncs {
if err := update(newSpec); err != nil {
return err
}
}
if equality.Semantic.DeepEqual(oldSpec, newSpec) {
return nil
}
operatorSpec, _, err = client.UpdateOperatorSpec(ctx, resourceVersion, newSpec)
updated = err == nil
return err
})
return operatorSpec, updated, err
}
// UpdateObservedConfigFn returns a func to update the config.
func UpdateObservedConfigFn(config map[string]interface{}) UpdateOperatorSpecFunc {
return func(oldSpec *operatorv1.OperatorSpec) error {
oldSpec.ObservedConfig = runtime.RawExtension{Object: &unstructured.Unstructured{Object: config}}
return nil
}
}
// UpdateStatusFunc is a func that mutates an operator status.
type UpdateStatusFunc func(status *operatorv1.OperatorStatus) error
// UpdateStatus applies the update funcs to the oldStatus and tries to update via the client.
func UpdateStatus(ctx context.Context, client OperatorClient, updateFuncs ...UpdateStatusFunc) (*operatorv1.OperatorStatus, bool, error) {
updated := false
var updatedOperatorStatus *operatorv1.OperatorStatus
numberOfAttempts := 0
previousResourceVersion := ""
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
defer func() {
numberOfAttempts++
}()
var oldStatus *operatorv1.OperatorStatus
var resourceVersion string
var err error
// prefer lister if we haven't already failed.
_, oldStatus, resourceVersion, err = client.GetOperatorState()
if err != nil {
return err
}
if resourceVersion == previousResourceVersion {
listerResourceVersion := resourceVersion
// this indicates that we've had a conflict and the lister has not caught up, so do a live GET
_, oldStatus, resourceVersion, err = client.GetOperatorStateWithQuorum(ctx)
if err != nil {
return err
}
klog.V(2).Infof("lister was stale at resourceVersion=%v, live get showed resourceVersion=%v", listerResourceVersion, resourceVersion)
}
previousResourceVersion = resourceVersion
newStatus := oldStatus.DeepCopy()
for _, update := range updateFuncs {
if err := update(newStatus); err != nil {
return err
}
}
if equality.Semantic.DeepEqual(oldStatus, newStatus) {
// We return the newStatus which is a deep copy of oldStatus but with all update funcs applied.
updatedOperatorStatus = newStatus
return nil
}
if klog.V(4).Enabled() {
klog.Infof("Operator status changed: %v", operatorStatusJSONPatchNoError(oldStatus, newStatus))
}
updatedOperatorStatus, err = client.UpdateOperatorStatus(ctx, resourceVersion, newStatus)
updated = err == nil
return err
})
return updatedOperatorStatus, updated, err
}
func operatorStatusJSONPatchNoError(original, modified *operatorv1.OperatorStatus) string {
if original == nil {
return "original object is nil"
}
if modified == nil {
return "modified object is nil"
}
return cmp.Diff(original, modified)
}
// UpdateConditionFn returns a func to update a condition.
func UpdateConditionFn(cond operatorv1.OperatorCondition) UpdateStatusFunc {
return func(oldStatus *operatorv1.OperatorStatus) error {
SetOperatorCondition(&oldStatus.Conditions, cond)
return nil
}
}
// UpdateStaticPodStatusFunc is a func that mutates an operator status.
type UpdateStaticPodStatusFunc func(status *operatorv1.StaticPodOperatorStatus) error
// UpdateStaticPodStatus applies the update funcs to the oldStatus abd tries to update via the client.
func UpdateStaticPodStatus(ctx context.Context, client StaticPodOperatorClient, updateFuncs ...UpdateStaticPodStatusFunc) (*operatorv1.StaticPodOperatorStatus, bool, error) {
updated := false
var updatedOperatorStatus *operatorv1.StaticPodOperatorStatus
numberOfAttempts := 0
previousResourceVersion := ""
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
defer func() {
numberOfAttempts++
}()
var oldStatus *operatorv1.StaticPodOperatorStatus
var resourceVersion string
var err error
// prefer lister if we haven't already failed.
_, oldStatus, resourceVersion, err = client.GetStaticPodOperatorState()
if err != nil {
return err
}
if resourceVersion == previousResourceVersion {
listerResourceVersion := resourceVersion
// this indicates that we've had a conflict and the lister has not caught up, so do a live GET
_, oldStatus, resourceVersion, err = client.GetStaticPodOperatorStateWithQuorum(ctx)
if err != nil {
return err
}
klog.V(2).Infof("lister was stale at resourceVersion=%v, live get showed resourceVersion=%v", listerResourceVersion, resourceVersion)
}
previousResourceVersion = resourceVersion
newStatus := oldStatus.DeepCopy()
for _, update := range updateFuncs {
if err := update(newStatus); err != nil {
return err
}
}
if equality.Semantic.DeepEqual(oldStatus, newStatus) {
// We return the newStatus which is a deep copy of oldStatus but with all update funcs applied.
updatedOperatorStatus = newStatus
return nil
}
if klog.V(4).Enabled() {
klog.Infof("Operator status changed: %v", staticPodOperatorStatusJSONPatchNoError(oldStatus, newStatus))
}
updatedOperatorStatus, err = client.UpdateStaticPodOperatorStatus(ctx, resourceVersion, newStatus)
updated = err == nil
return err
})
return updatedOperatorStatus, updated, err
}
func staticPodOperatorStatusJSONPatchNoError(original, modified *operatorv1.StaticPodOperatorStatus) string {
if original == nil {
return "original object is nil"
}
if modified == nil {
return "modified object is nil"
}
return cmp.Diff(original, modified)
}
// UpdateStaticPodConditionFn returns a func to update a condition.
func UpdateStaticPodConditionFn(cond operatorv1.OperatorCondition) UpdateStaticPodStatusFunc {
return func(oldStatus *operatorv1.StaticPodOperatorStatus) error {
SetOperatorCondition(&oldStatus.Conditions, cond)
return nil
}
}
// EnsureFinalizer adds a new finalizer to the operator CR, if it does not exists. No-op otherwise.
// The finalizer name is computed from the controller name and operator name ($OPERATOR_NAME or os.Args[0])
// It re-tries on conflicts.
func EnsureFinalizer(ctx context.Context, client OperatorClientWithFinalizers, controllerName string) error {
finalizer := getFinalizerName(controllerName)
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
return client.EnsureFinalizer(ctx, finalizer)
})
return err
}
// RemoveFinalizer removes a finalizer from the operator CR, if it is there. No-op otherwise.
// The finalizer name is computed from the controller name and operator name ($OPERATOR_NAME or os.Args[0])
// It re-tries on conflicts.
func RemoveFinalizer(ctx context.Context, client OperatorClientWithFinalizers, controllerName string) error {
finalizer := getFinalizerName(controllerName)
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
return client.RemoveFinalizer(ctx, finalizer)
})
return err
}
// getFinalizerName computes a nice finalizer name from controllerName and the operator name ($OPERATOR_NAME or os.Args[0]).
func getFinalizerName(controllerName string) string {
return fmt.Sprintf("%s.operator.openshift.io/%s", getOperatorName(), controllerName)
}
func getOperatorName() string {
if name := os.Getenv("OPERATOR_NAME"); name != "" {
return name
}
return os.Args[0]
}
type aggregate []error
var _ utilerrors.Aggregate = aggregate{}
// NewMultiLineAggregate returns an aggregate error with multi-line output
func NewMultiLineAggregate(errList []error) error {
var errs []error
for _, e := range errList {
if e != nil {
errs = append(errs, e)
}
}
if len(errs) == 0 {
return nil
}
// We sort errors to allow for consistent output for testing.
sort.SliceStable(errs, func(i, j int) bool {
return errs[i].Error() < errs[j].Error()
})
return aggregate(errs)
}
// Error is part of the error interface.
func (agg aggregate) Error() string {
msgs := make([]string, len(agg))
for i := range agg {
msgs[i] = agg[i].Error()
}
return strings.Join(msgs, "\n")
}
// Errors is part of the Aggregate interface.
func (agg aggregate) Errors() []error {
return []error(agg)
}
// Is is part of the Aggregate interface
func (agg aggregate) Is(target error) bool {
return agg.visit(func(err error) bool {
return errors.Is(err, target)
})
}
func (agg aggregate) visit(f func(err error) bool) bool {
for _, err := range agg {
switch err := err.(type) {
case aggregate:
if match := err.visit(f); match {
return match
}
case utilerrors.Aggregate:
for _, nestedErr := range err.Errors() {
if match := f(nestedErr); match {
return match
}
}
default:
if match := f(err); match {
return match
}
}
}
return false
}
// MapToEnvVars converts a string-string map to a slice of corev1.EnvVar-s
func MapToEnvVars(mapEnvVars map[string]string) []corev1.EnvVar {
if mapEnvVars == nil {
return nil
}
envVars := make([]corev1.EnvVar, len(mapEnvVars))
i := 0
for k, v := range mapEnvVars {
envVars[i] = corev1.EnvVar{Name: k, Value: v}
i++
}
// need to sort the slice so that kube-controller-manager-pod configmap does not change all the time
sort.Slice(envVars, func(i, j int) bool { return envVars[i].Name < envVars[j].Name })
return envVars
}
// InjectObservedProxyIntoContainers injects proxy environment variables in containers specified in containerNames.
func InjectObservedProxyIntoContainers(podSpec *corev1.PodSpec, containerNames []string, observedConfig []byte, fields ...string) error {
var config map[string]interface{}
if err := yaml.Unmarshal(observedConfig, &config); err != nil {
return fmt.Errorf("failed to unmarshal the observedConfig: %w", err)
}
proxyConfig, found, err := unstructured.NestedStringMap(config, fields...)
if err != nil {
return fmt.Errorf("couldn't get the proxy config from observedConfig: %w", err)
}
proxyEnvVars := MapToEnvVars(proxyConfig)
if !found || len(proxyEnvVars) < 1 {
// There's no observed proxy config, we should tolerate that
return nil
}
for _, containerName := range containerNames {
for i := range podSpec.InitContainers {
if podSpec.InitContainers[i].Name == containerName {
podSpec.InitContainers[i].Env = append(podSpec.InitContainers[i].Env, proxyEnvVars...)
}
}
for i := range podSpec.Containers {
if podSpec.Containers[i].Name == containerName {
podSpec.Containers[i].Env = append(podSpec.Containers[i].Env, proxyEnvVars...)
}
}
}
return nil
}
func InjectTrustedCAIntoContainers(podSpec *corev1.PodSpec, configMapName string, containerNames []string) error {
podSpec.Volumes = append(podSpec.Volumes, corev1.Volume{
Name: "non-standard-root-system-trust-ca-bundle",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: configMapName,
},
Items: []corev1.KeyToPath{
{Key: "ca-bundle.crt", Path: "tls-ca-bundle.pem"},
},
},
},
})
for _, containerName := range containerNames {
for i := range podSpec.InitContainers {
if podSpec.InitContainers[i].Name == containerName {
podSpec.InitContainers[i].VolumeMounts = append(podSpec.InitContainers[i].VolumeMounts, corev1.VolumeMount{
Name: "non-standard-root-system-trust-ca-bundle",
MountPath: "/etc/pki/ca-trust/extracted/pem",
ReadOnly: true,
})
}
}
for i := range podSpec.Containers {
if podSpec.Containers[i].Name == containerName {
podSpec.Containers[i].VolumeMounts = append(podSpec.Containers[i].VolumeMounts, corev1.VolumeMount{
Name: "non-standard-root-system-trust-ca-bundle",
MountPath: "/etc/pki/ca-trust/extracted/pem",
ReadOnly: true,
})
}
}
}
return nil
}
func SetCondition(conditions *[]metav1.Condition, newCondition metav1.Condition) {
if conditions == nil {
conditions = &[]metav1.Condition{}
}
existingCondition := FindCondition(*conditions, newCondition.Type)
if existingCondition == nil {
newCondition.LastTransitionTime = metav1.NewTime(time.Now())
*conditions = append(*conditions, newCondition)
return
}
if existingCondition.Status != newCondition.Status {
existingCondition.Status = newCondition.Status
existingCondition.LastTransitionTime = metav1.NewTime(time.Now())
}
existingCondition.Reason = newCondition.Reason
existingCondition.Message = newCondition.Message
}
func RemoveCondition(conditions *[]metav1.Condition, conditionType string) {
if conditions == nil {
conditions = &[]metav1.Condition{}
}
newConditions := []metav1.Condition{}
for _, condition := range *conditions {
if condition.Type != conditionType {
newConditions = append(newConditions, condition)
}
}
*conditions = newConditions
}
func FindCondition(conditions []metav1.Condition, conditionType string) *metav1.Condition {
for i := range conditions {
if conditions[i].Type == conditionType {
return &conditions[i]
}
}
return nil
}
func IsConditionTrue(conditions []metav1.Condition, conditionType string) bool {
return IsConditionPresentAndEqual(conditions, conditionType, metav1.ConditionTrue)
}
func IsConditionFalse(conditions []metav1.Condition, conditionType string) bool {
return IsConditionPresentAndEqual(conditions, conditionType, metav1.ConditionFalse)
}
func IsConditionPresentAndEqual(conditions []metav1.Condition, conditionType string, status metav1.ConditionStatus) bool {
for _, condition := range conditions {
if condition.Type == conditionType {
return condition.Status == status
}
}
return false
}
// IsUpdatingTooLong determines if updating operands condition takes too long.
// It returns true if the given condition was found and has been set to True longer than progressingConditionTimeout.
func IsUpdatingTooLong(operatorStatus *operatorv1.OperatorStatus, progressingConditionType string) bool {
progressing := FindOperatorCondition(operatorStatus.Conditions, progressingConditionType)
return progressing != nil && progressing.Status == operatorv1.ConditionTrue && time.Now().After(progressing.LastTransitionTime.Add(progressingConditionTimeout))
}