forked from operator-framework/operator-lifecycle-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadoption_controller_test.go
383 lines (329 loc) · 11.1 KB
/
adoption_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
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
package operators
import (
"context"
"fmt"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/reference"
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/decorators"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/testobj"
)
var _ = Describe("Adoption Controller", func() {
var (
ctx context.Context
)
BeforeEach(func() {
ctx = context.Background()
})
Describe("Component label generation", func() {
var (
created []client.Object
)
BeforeEach(func() {
created = []client.Object{}
})
JustAfterEach(func() {
for _, obj := range created {
Eventually(func() error {
err := k8sClient.Delete(ctx, obj)
if apierrors.IsNotFound(err) {
return nil
}
return err
}).Should(Succeed())
}
})
Context("a subscription", func() {
var (
ns *corev1.Namespace
sub *operatorsv1alpha1.Subscription
)
BeforeEach(func() {
ns = &corev1.Namespace{}
ns.SetName(genName("operators-"))
Eventually(func() error {
return k8sClient.Create(ctx, ns)
}, timeout, interval).Should(Succeed())
created = append(created, ns)
})
Context("with a package", func() {
var (
componentLabelKey string
installed *operatorsv1alpha1.ClusterServiceVersion
)
BeforeEach(func() {
sub = &operatorsv1alpha1.Subscription{
Spec: &operatorsv1alpha1.SubscriptionSpec{
Package: "poultry",
},
}
sub.SetNamespace(ns.GetName())
sub.SetName(sub.Spec.Package)
Eventually(func() error {
return k8sClient.Create(ctx, sub)
}, timeout, interval).Should(Succeed())
created = append(created, sub)
// Set the Subscription's status separately
Eventually(func() error {
if err := k8sClient.Get(ctx, testobj.NamespacedName(sub), sub); err != nil {
return err
}
sub.Status = operatorsv1alpha1.SubscriptionStatus{
InstalledCSV: "turkey",
LastUpdated: metav1.Now(),
}
return k8sClient.Status().Update(ctx, sub)
}, timeout, interval).Should(Succeed())
componentLabelKey = fmt.Sprintf("%s%s.%s", decorators.ComponentLabelKeyPrefix, sub.Spec.Package, sub.GetNamespace())
})
Context("that references an existing installplan", func() {
var (
ip *operatorsv1alpha1.InstallPlan
)
BeforeEach(func() {
ip = fixtures.Fill(&operatorsv1alpha1.InstallPlan{}).(*operatorsv1alpha1.InstallPlan)
ip.SetNamespace(sub.GetNamespace())
ip.SetName(genName("poultry-"))
Eventually(func() error {
owned := testobj.WithOwner(sub, ip)
return k8sClient.Create(ctx, owned)
}).Should(Succeed())
created = append(created, ip)
ref, err := reference.GetReference(scheme, ip)
Expect(err).ToNot(HaveOccurred())
// Set the Subscription's status separately
status := sub.DeepCopy().Status
status.InstallPlanRef = ref
Eventually(func() error {
if err := k8sClient.Get(ctx, testobj.NamespacedName(sub), sub); err != nil {
return err
}
sub.Status = status
return k8sClient.Status().Update(ctx, sub)
}).Should(Succeed())
})
Context("and has other, non-latest, adopted installplans", func() {
var (
ips []*operatorsv1alpha1.InstallPlan
)
BeforeEach(func() {
for i := 0; i < 4; i++ {
ip := fixtures.Fill(&operatorsv1alpha1.InstallPlan{}).(*operatorsv1alpha1.InstallPlan)
ip.SetNamespace(sub.GetNamespace())
ip.SetName(genName(""))
ip.SetLabels(map[string]string{
componentLabelKey: "",
})
Eventually(func() error {
return k8sClient.Create(ctx, ip)
}).Should(Succeed())
created = append(created, ip)
ips = append(ips, ip)
}
})
Specify("correct component labels", func() {
installPlan := ip.DeepCopy()
Eventually(func() (map[string]string, error) {
err := k8sClient.Get(ctx, testobj.NamespacedName(ip), installPlan)
return installPlan.GetLabels(), err
}).Should(HaveKey(componentLabelKey))
for _, ip := range ips {
Eventually(func() (map[string]string, error) {
err := k8sClient.Get(ctx, testobj.NamespacedName(ip), ip)
return ip.GetLabels(), err
}, timeout, interval).ShouldNot(HaveKey(componentLabelKey))
}
})
})
})
Context("that has an existing installed csv", func() {
var (
providedCRD *apiextensionsv1.CustomResourceDefinition
)
BeforeEach(func() {
providedCRD = fixtures.Fill(&apiextensionsv1.CustomResourceDefinition{}).(*apiextensionsv1.CustomResourceDefinition)
installed = &operatorsv1alpha1.ClusterServiceVersion{
Spec: operatorsv1alpha1.ClusterServiceVersionSpec{
CustomResourceDefinitions: operatorsv1alpha1.CustomResourceDefinitions{
Owned: []operatorsv1alpha1.CRDDescription{
{
Name: providedCRD.GetName(),
Kind: providedCRD.Spec.Names.Kind,
Version: providedCRD.Spec.Versions[0].Name,
},
},
},
InstallStrategy: operatorsv1alpha1.NamedInstallStrategy{
StrategyName: operatorsv1alpha1.InstallStrategyNameDeployment,
StrategySpec: operatorsv1alpha1.StrategyDetailsDeployment{
DeploymentSpecs: []operatorsv1alpha1.StrategyDeploymentSpec{},
},
},
},
}
installed.SetNamespace(ns.GetName())
installed.SetName(sub.Status.InstalledCSV)
Eventually(func() error {
return k8sClient.Create(ctx, installed)
}, timeout, interval).Should(Succeed())
created = append(created, installed)
})
Context("that has no resources owned by the installed csv", func() {
Specify("a component label", func() {
Eventually(func() (map[string]string, error) {
latest := &operatorsv1alpha1.ClusterServiceVersion{}
err := k8sClient.Get(ctx, testobj.NamespacedName(installed), latest)
return latest.GetLabels(), err
}, timeout, interval).Should(HaveKey(componentLabelKey))
})
})
Context("with an existing provided CRD", func() {
BeforeEach(func() {
Eventually(func() error {
return k8sClient.Create(ctx, providedCRD)
}, timeout, interval).Should(Succeed())
created = append(created, providedCRD)
Eventually(func() (map[string]string, error) {
latest := &apiextensionsv1.CustomResourceDefinition{}
err := k8sClient.Get(ctx, testobj.NamespacedName(providedCRD), latest)
return latest.GetLabels(), err
}, timeout, interval).Should(HaveKey(componentLabelKey))
})
Context("when its component label is removed", func() {
BeforeEach(func() {
Eventually(func() error {
latest := &apiextensionsv1.CustomResourceDefinition{}
if err := k8sClient.Get(ctx, testobj.NamespacedName(providedCRD), latest); err != nil {
return err
}
if len(latest.GetLabels()) == 0 {
return nil
}
delete(latest.GetLabels(), componentLabelKey)
return k8sClient.Update(ctx, latest)
}, timeout, interval).Should(Succeed())
})
It("should be relabelled", func() {
Eventually(func() (map[string]string, error) {
latest := &apiextensionsv1.CustomResourceDefinition{}
err := k8sClient.Get(ctx, testobj.NamespacedName(providedCRD), latest)
return latest.GetLabels(), err
}, timeout, interval).Should(HaveKey(componentLabelKey))
})
})
})
Context("that has resources owned by the installed csv", func() {
var (
components []testobj.RuntimeMetaObject
)
BeforeEach(func() {
Eventually(func() error {
return k8sClient.Get(ctx, testobj.NamespacedName(installed), installed)
}, timeout, interval).Should(Succeed())
namespace := installed.GetNamespace()
ownerLabels := map[string]string{
ownerutil.OwnerKind: operatorsv1alpha1.ClusterServiceVersionKind,
ownerutil.OwnerNamespaceKey: namespace,
ownerutil.OwnerKey: installed.GetName(),
}
components = []testobj.RuntimeMetaObject{
testobj.WithOwner(
installed,
testobj.WithNamespace(
namespace,
fixtures.Fill(&appsv1.Deployment{}),
),
),
testobj.WithOwner(
installed,
testobj.WithNamespace(
namespace,
fixtures.Fill(&corev1.Service{}),
),
),
testobj.WithOwner(
installed,
testobj.WithNamespace(
namespace,
fixtures.Fill(&corev1.ServiceAccount{}),
),
),
testobj.WithOwner(
installed,
testobj.WithNamespace(
namespace,
fixtures.Fill(&corev1.Secret{}),
),
),
testobj.WithOwner(
installed,
testobj.WithNamespace(
namespace,
fixtures.Fill(&corev1.ConfigMap{}),
),
),
testobj.WithOwner(
installed,
testobj.WithNamespace(
namespace,
fixtures.Fill(&rbacv1.Role{}),
),
),
testobj.WithOwner(
installed,
testobj.WithNamespace(
namespace,
fixtures.Fill(&rbacv1.RoleBinding{}),
),
),
testobj.WithLabels(
ownerLabels,
fixtures.Fill(&rbacv1.ClusterRole{}),
),
testobj.WithLabels(
ownerLabels,
fixtures.Fill(&rbacv1.ClusterRoleBinding{}),
),
testobj.WithLabels(
ownerLabels,
fixtures.Fill(&apiregistrationv1.APIService{}),
),
}
for _, component := range components {
labels := component.GetLabels()
if labels == nil {
labels = map[string]string{}
}
labels[install.OLMManagedLabelKey] = install.OLMManagedLabelValue
component.SetLabels(labels)
Eventually(func() error {
return k8sClient.Create(ctx, component)
}, timeout, interval).Should(Succeed())
created = append(created, component)
}
})
Specify("a component label", func() {
for _, component := range components {
Eventually(func() (map[string]string, error) {
err := k8sClient.Get(ctx, testobj.NamespacedName(component), component)
return component.GetLabels(), err
}, timeout, interval).Should(HaveKey(componentLabelKey))
}
})
})
})
})
})
})
})