forked from operator-framework/operator-lifecycle-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclusteroperator_controller_test.go
329 lines (285 loc) · 10.5 KB
/
clusteroperator_controller_test.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
package openshift
import (
"fmt"
semver "github.com/blang/semver/v4"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
configv1 "github.com/openshift/api/config/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/projection"
"github.com/operator-framework/operator-registry/pkg/api"
)
var _ = Describe("ClusterOperator controller", func() {
var (
clusterOperatorName types.NamespacedName
cv *configv1.ClusterVersion
csv *operatorsv1alpha1.ClusterServiceVersion
)
BeforeEach(func() {
clusterOperatorName = types.NamespacedName{Name: clusterOperator}
csv = &operatorsv1alpha1.ClusterServiceVersion{
Spec: operatorsv1alpha1.ClusterServiceVersionSpec{
InstallStrategy: operatorsv1alpha1.NamedInstallStrategy{
StrategyName: operatorsv1alpha1.InstallStrategyNameDeployment,
StrategySpec: operatorsv1alpha1.StrategyDetailsDeployment{
DeploymentSpecs: []operatorsv1alpha1.StrategyDeploymentSpec{},
},
},
},
}
csv.SetName("olm")
csv.SetNamespace(controllerNamespace)
Eventually(func() error {
return k8sClient.Create(ctx, csv)
}).Should(Succeed())
})
AfterEach(func() {
Eventually(func() error {
err := k8sClient.Delete(ctx, csv)
if err != nil && apierrors.IsNotFound(err) {
err = nil
}
return err
}).Should(Succeed())
})
BeforeEach(func() {
// "version" singleton is available in OpenShift by default
cv = &configv1.ClusterVersion{}
cv.SetName("version")
Eventually(func() error {
return k8sClient.Create(ctx, cv)
}).Should(Succeed())
cv.Status = configv1.ClusterVersionStatus{
Desired: configv1.Release{
Version: clusterVersion,
},
}
Eventually(func() error {
return k8sClient.Status().Update(ctx, cv)
}).Should(Succeed())
})
AfterEach(func() {
Eventually(func() error {
err := k8sClient.Delete(ctx, cv)
if err != nil && apierrors.IsNotFound(err) {
err = nil
}
return err
}).Should(Succeed())
})
It("should ensure the ClusterOperator always exists", func() {
By("initially creating it")
co := &configv1.ClusterOperator{}
Eventually(func() error {
return k8sClient.Get(ctx, clusterOperatorName, co)
}, timeout).Should(Succeed())
By("recreating it when deleted")
Eventually(func() error {
return k8sClient.Delete(ctx, co)
}).Should(Succeed())
Eventually(func() error {
return k8sClient.Get(ctx, clusterOperatorName, co)
}, timeout).Should(Succeed())
})
It("should track related ClusterServiceVersions with the RelatedObjects field", func() {
co := &configv1.ClusterOperator{}
Eventually(func() ([]configv1.ObjectReference, error) {
err := k8sClient.Get(ctx, clusterOperatorName, co)
return co.Status.RelatedObjects, err
}, timeout).Should(ConsistOf([]configv1.ObjectReference{
{
Group: operatorsv1alpha1.GroupName,
Resource: "clusterserviceversions",
Namespace: csv.GetNamespace(),
Name: csv.GetName(),
},
}))
})
It("should gate OpenShift upgrades", func() {
By("setting upgradeable=false before OLM successfully syncs")
co := &configv1.ClusterOperator{}
Eventually(func() ([]configv1.ClusterOperatorStatusCondition, error) {
err := k8sClient.Get(ctx, clusterOperatorName, co)
return co.Status.Conditions, err
}, timeout).Should(ContainElement(configv1.ClusterOperatorStatusCondition{
Type: configv1.OperatorUpgradeable,
Status: configv1.ConditionFalse,
Message: "Waiting for updates to take effect",
LastTransitionTime: fixedNow(),
}))
By("setting upgradeable=true after OLM successfully syncs")
// Signal a successful sync
syncCh <- nil
Eventually(func() ([]configv1.ClusterOperatorStatusCondition, error) {
err := k8sClient.Get(ctx, clusterOperatorName, co)
return co.Status.Conditions, err
}, timeout).Should(ContainElement(configv1.ClusterOperatorStatusCondition{
Type: configv1.OperatorUpgradeable,
Status: configv1.ConditionTrue,
LastTransitionTime: fixedNow(),
}))
By("setting upgradeable=false when there's an error determining compatibility")
cv.Status = configv1.ClusterVersionStatus{}
Eventually(func() error {
return k8sClient.Status().Update(ctx, cv)
}).Should(Succeed())
Eventually(func() ([]configv1.ClusterOperatorStatusCondition, error) {
err := k8sClient.Get(ctx, clusterOperatorName, co)
return co.Status.Conditions, err
}, timeout).Should(ContainElement(configv1.ClusterOperatorStatusCondition{
Type: configv1.OperatorUpgradeable,
Status: configv1.ConditionFalse,
Reason: ErrorCheckingOperatorCompatibility,
Message: "Encountered errors while checking compatibility with the next minor version of OpenShift: desired release version missing from ClusterVersion",
LastTransitionTime: fixedNow(),
}))
cv.Status = configv1.ClusterVersionStatus{
Desired: configv1.Release{
Version: clusterVersion,
},
}
Eventually(func() error {
return k8sClient.Status().Update(ctx, cv)
}).Should(Succeed())
By("setting upgradeable=false when incompatible operators exist")
ns := &corev1.Namespace{}
ns.SetName("nostromo")
Eventually(func() error {
return k8sClient.Create(ctx, ns)
}).Should(Succeed())
defer func() {
Eventually(func() error {
return k8sClient.Delete(ctx, ns)
}).Should(Succeed())
}()
incompatible := &operatorsv1alpha1.ClusterServiceVersion{Spec: csv.Spec}
incompatible.SetName("xenomorph")
incompatible.SetNamespace(ns.GetName())
withMax := func(versions ...string) map[string]string {
var properties []*api.Property
for _, v := range versions {
properties = append(properties, &api.Property{
Type: MaxOpenShiftVersionProperty,
Value: v,
})
}
value, err := projection.PropertiesAnnotationFromPropertyList(properties)
Expect(err).ToNot(HaveOccurred())
return map[string]string{
projection.PropertiesAnnotationKey: value,
}
}
incompatible.SetAnnotations(withMax(fmt.Sprintf(`"%s"`, clusterVersion))) // Wrap in quotes so we don't break property marshaling
Eventually(func() error {
return k8sClient.Create(ctx, incompatible)
}).Should(Succeed())
defer func() {
Eventually(func() error {
return k8sClient.Delete(ctx, incompatible)
}).Should(Succeed())
}()
parsedVersion := semver.MustParse(clusterVersion)
Eventually(func() ([]configv1.ClusterOperatorStatusCondition, error) {
err := k8sClient.Get(ctx, clusterOperatorName, co)
return co.Status.Conditions, err
}, timeout).Should(ContainElement(configv1.ClusterOperatorStatusCondition{
Type: configv1.OperatorUpgradeable,
Status: configv1.ConditionFalse,
Reason: IncompatibleOperatorsInstalled,
Message: skews{
{
namespace: ns.GetName(),
name: incompatible.GetName(),
maxOpenShiftVersion: fmt.Sprintf("%d.%d", parsedVersion.Major, parsedVersion.Minor),
},
}.String(),
LastTransitionTime: fixedNow(),
}))
By("setting upgradeable=true when incompatible operators become compatible")
// Set compatibility to the next minor version
next := semver.MustParse(clusterVersion)
Expect(next.IncrementMinor()).To(Succeed())
incompatible.SetAnnotations(withMax(fmt.Sprintf(`"%s"`, next.String())))
Eventually(func() error {
return k8sClient.Update(ctx, incompatible)
}).Should(Succeed())
Eventually(func() ([]configv1.ClusterOperatorStatusCondition, error) {
err := k8sClient.Get(ctx, clusterOperatorName, co)
return co.Status.Conditions, err
}, timeout).Should(ContainElement(configv1.ClusterOperatorStatusCondition{
Type: configv1.OperatorUpgradeable,
Status: configv1.ConditionTrue,
LastTransitionTime: fixedNow(),
}))
By("understanding unquoted short max versions; e.g. X.Y")
// Mimic common pipeline shorthand
v := semver.MustParse(clusterVersion)
short := fmt.Sprintf("%d.%d", v.Major, v.Minor)
incompatible.SetAnnotations(withMax(short))
Eventually(func() error {
return k8sClient.Update(ctx, incompatible)
}).Should(Succeed())
Eventually(func() ([]configv1.ClusterOperatorStatusCondition, error) {
err := k8sClient.Get(ctx, clusterOperatorName, co)
return co.Status.Conditions, err
}, timeout).Should(ContainElement(configv1.ClusterOperatorStatusCondition{
Type: configv1.OperatorUpgradeable,
Status: configv1.ConditionFalse,
Reason: IncompatibleOperatorsInstalled,
Message: skews{
{
namespace: ns.GetName(),
name: incompatible.GetName(),
maxOpenShiftVersion: short,
},
}.String(),
LastTransitionTime: fixedNow(),
}))
By("setting upgradeable=false when invalid max versions are found")
incompatible.SetAnnotations(withMax(`"garbage"`))
Eventually(func() error {
return k8sClient.Update(ctx, incompatible)
}).Should(Succeed())
_, parseErr := semver.ParseTolerant("garbage")
Eventually(func() ([]configv1.ClusterOperatorStatusCondition, error) {
err := k8sClient.Get(ctx, clusterOperatorName, co)
return co.Status.Conditions, err
}, timeout).Should(ContainElement(configv1.ClusterOperatorStatusCondition{
Type: configv1.OperatorUpgradeable,
Status: configv1.ConditionFalse,
Reason: IncompatibleOperatorsInstalled,
Message: skews{
{
namespace: ns.GetName(),
name: incompatible.GetName(),
err: fmt.Errorf(`failed to parse "garbage" as semver: %w`, parseErr),
},
}.String(),
LastTransitionTime: fixedNow(),
}))
By("setting upgradeable=false when more than one max version property is defined")
incompatible.SetAnnotations(withMax(fmt.Sprintf(`"%s"`, clusterVersion), fmt.Sprintf(`"%s"`, next.String())))
Eventually(func() error {
return k8sClient.Update(ctx, incompatible)
}).Should(Succeed())
Eventually(func() ([]configv1.ClusterOperatorStatusCondition, error) {
err := k8sClient.Get(ctx, clusterOperatorName, co)
return co.Status.Conditions, err
}, timeout).Should(ContainElement(configv1.ClusterOperatorStatusCondition{
Type: configv1.OperatorUpgradeable,
Status: configv1.ConditionFalse,
Reason: IncompatibleOperatorsInstalled,
Message: skews{
{
namespace: ns.GetName(),
name: incompatible.GetName(),
err: fmt.Errorf(`defining more than one "%s" property is not allowed`, MaxOpenShiftVersionProperty),
},
}.String(),
LastTransitionTime: fixedNow(),
}))
})
})