-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathlinodeobjectstoragebucket_controller_test.go
More file actions
411 lines (379 loc) · 15.9 KB
/
Copy pathlinodeobjectstoragebucket_controller_test.go
File metadata and controls
411 lines (379 loc) · 15.9 KB
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
// /*
// Copyright 2023 Akamai Technologies, Inc.
// 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 controller
import (
"context"
"errors"
"time"
"github.com/linode/linodego"
"go.uber.org/mock/gomock"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/scheme"
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
infrav1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
"github.com/linode/cluster-api-provider-linode/cloud/scope"
"github.com/linode/cluster-api-provider-linode/mock"
"github.com/linode/cluster-api-provider-linode/util"
rec "github.com/linode/cluster-api-provider-linode/util/reconciler"
. "github.com/linode/cluster-api-provider-linode/mock/mocktest"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("lifecycle", Ordered, Label("bucket", "lifecycle"), func() {
suite := NewControllerSuite(GinkgoT(), mock.MockLinodeClient{})
obj := infrav1alpha2.LinodeObjectStorageBucket{
ObjectMeta: metav1.ObjectMeta{
Name: "lifecycle",
Namespace: "default",
Finalizers: []string{
infrav1alpha2.BucketFinalizer,
},
},
Spec: infrav1alpha2.LinodeObjectStorageBucketSpec{
Region: "region",
AccessKeyRef: &corev1.ObjectReference{
Name: "lifecycle-mgmt",
},
},
}
key := infrav1alpha2.LinodeObjectStorageKey{
ObjectMeta: metav1.ObjectMeta{
Name: "lifecycle-mgmt",
Namespace: "default",
},
Spec: infrav1alpha2.LinodeObjectStorageKeySpec{
BucketAccess: []infrav1alpha2.BucketAccessRef{{
BucketName: "lifecycle",
Permissions: "read_write",
Region: "region",
}},
},
}
secret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "lifecycle-mgmt-obj-key",
Namespace: "default",
},
Data: map[string][]byte{
"access": []byte("access-key"),
"secret": []byte("secret-key"),
"endpoint": []byte("https://region-1.linodeobjects.com"),
"bucket": []byte("lifecycle"),
},
}
bScope := scope.ObjectStorageBucketScope{
Bucket: &obj,
}
reconciler := LinodeObjectStorageBucketReconciler{}
BeforeAll(func(ctx SpecContext) {
bScope.Client = k8sClient
Expect(k8sClient.Create(ctx, &key)).To(Succeed())
Expect(k8sClient.Create(ctx, &obj)).To(Succeed())
Expect(k8sClient.Create(ctx, &secret)).To(Succeed())
})
suite.BeforeEach(func(ctx context.Context, mck Mock) {
reconciler.Recorder = mck.Recorder()
bScope.Logger = mck.Logger()
objectKey := client.ObjectKey{Name: "lifecycle", Namespace: "default"}
Expect(k8sClient.Get(ctx, objectKey, &obj)).To(Succeed())
accessKey := client.ObjectKey{Name: "lifecycle-mgmt", Namespace: "default"}
Expect(k8sClient.Get(ctx, accessKey, &key)).To(Succeed())
secretKey := client.ObjectKey{Name: "lifecycle-mgmt-obj-key", Namespace: "default"}
Expect(k8sClient.Get(ctx, secretKey, &secret)).To(Succeed())
// Create patch helper with latest state of resource.
// This is only needed when relying on envtest's k8sClient.
patchHelper, err := patch.NewHelper(&obj, k8sClient)
Expect(err).NotTo(HaveOccurred())
bScope.PatchHelper = patchHelper
})
suite.Run(
OneOf(
Path(
Call("bucket is created", func(ctx context.Context, mck Mock) {
getBucket := mck.LinodeClient.EXPECT().GetObjectStorageBucket(gomock.Any(), obj.Spec.Region, gomock.Any()).Return(nil, nil)
mck.LinodeClient.EXPECT().CreateObjectStorageBucket(gomock.Any(), gomock.Any()).
After(getBucket).
Return(&linodego.ObjectStorageBucket{
Label: "bucket",
Region: obj.Spec.Region,
Created: util.Pointer(time.Now()),
Hostname: "hostname",
}, nil)
}),
Result("resource status is updated", func(ctx context.Context, mck Mock) {
objectKey := client.ObjectKeyFromObject(&obj)
bScope.LinodeClient = mck.LinodeClient
tmpClient := bScope.Client
bScope.Client = k8sClient
_, err := reconciler.reconcile(ctx, &bScope)
Expect(err).NotTo(HaveOccurred())
By("status")
Expect(k8sClient.Get(ctx, objectKey, &obj)).To(Succeed())
Expect(obj.Status.Ready).To(BeTrue())
Expect(obj.Status.FailureMessage).To(BeNil())
Expect(obj.Status.Conditions).To(HaveLen(1))
readyCond := obj.GetCondition(clusterv1.ReadyCondition)
Expect(readyCond).NotTo(BeNil())
Expect(*obj.Status.Hostname).To(Equal("hostname"))
Expect(obj.Status.CreationTime).NotTo(BeNil())
logOutput := mck.Logs()
Expect(logOutput).To(ContainSubstring("Reconciling apply"))
bScope.Client = tmpClient
}),
),
Path(
Call("bucket is not created", func(ctx context.Context, mck Mock) {
getBucket := mck.LinodeClient.EXPECT().GetObjectStorageBucket(gomock.Any(), obj.Spec.Region, gomock.Any()).Return(nil, nil)
mck.LinodeClient.EXPECT().CreateObjectStorageBucket(gomock.Any(), gomock.Any()).After(getBucket).Return(nil, errors.New("create bucket error"))
}),
Result("error", func(ctx context.Context, mck Mock) {
bScope.LinodeClient = mck.LinodeClient
_, err := reconciler.reconcile(ctx, &bScope)
Expect(err.Error()).To(ContainSubstring("create bucket error"))
}),
),
),
Once("resource ACL is modified", func(ctx context.Context, _ Mock) {
obj.Spec.ACL = infrav1alpha2.ACLPublicRead
Expect(k8sClient.Update(ctx, &obj)).To(Succeed())
}),
OneOf(
Path(
Call("bucket is not retrieved on update", func(ctx context.Context, mck Mock) {
mck.LinodeClient.EXPECT().GetObjectStorageBucket(gomock.Any(), obj.Spec.Region, gomock.Any()).Return(nil, errors.New("get bucket error"))
}),
Result("error", func(ctx context.Context, mck Mock) {
bScope.LinodeClient = mck.LinodeClient
_, err := reconciler.reconcile(ctx, &bScope)
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(ContainSubstring("get bucket error"))
}),
),
Path(
Call("bucket access options are not retrieved on update", func(ctx context.Context, mck Mock) {
mck.LinodeClient.EXPECT().GetObjectStorageBucket(gomock.Any(), obj.Spec.Region, gomock.Any()).
Return(&linodego.ObjectStorageBucket{
Label: "bucket",
Region: obj.Spec.Region,
Hostname: "hostname",
Created: util.Pointer(time.Now()),
}, nil)
mck.LinodeClient.EXPECT().GetObjectStorageBucketAccess(gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil, errors.New("bucket access options fetch error"))
}),
Result("error", func(ctx context.Context, mck Mock) {
bScope.LinodeClient = mck.LinodeClient
_, err := reconciler.reconcile(ctx, &bScope)
Expect(err.Error()).To(ContainSubstring("failed to get bucket access details"))
}),
),
Path(
Call("bucket access options are not successfully updated", func(ctx context.Context, mck Mock) {
mck.LinodeClient.EXPECT().GetObjectStorageBucket(gomock.Any(), obj.Spec.Region, gomock.Any()).
Return(&linodego.ObjectStorageBucket{
Label: "bucket",
Region: obj.Spec.Region,
Hostname: "hostname",
Created: util.Pointer(time.Now()),
}, nil)
mck.LinodeClient.EXPECT().GetObjectStorageBucketAccess(gomock.Any(), gomock.Any(), gomock.Any()).
Return(&linodego.ObjectStorageBucketAccess{
ACL: linodego.ACLPrivate,
CorsEnabled: true,
}, nil)
mck.LinodeClient.EXPECT().UpdateObjectStorageBucketAccess(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(errors.New("bucket access options update error"))
}),
Result("error", func(ctx context.Context, mck Mock) {
bScope.LinodeClient = mck.LinodeClient
_, err := reconciler.reconcile(ctx, &bScope)
Expect(err.Error()).To(ContainSubstring("failed to update the bucket access options"))
}),
),
Path(
Call("bucket is retrieved and updated", func(ctx context.Context, mck Mock) {
mck.LinodeClient.EXPECT().GetObjectStorageBucket(gomock.Any(), obj.Spec.Region, gomock.Any()).
Return(&linodego.ObjectStorageBucket{
Label: "bucket",
Region: obj.Spec.Region,
Hostname: "hostname",
Created: util.Pointer(time.Now()),
}, nil)
mck.LinodeClient.EXPECT().GetObjectStorageBucketAccess(gomock.Any(), gomock.Any(), gomock.Any()).
Return(&linodego.ObjectStorageBucketAccess{
ACL: linodego.ACLPrivate,
CorsEnabled: true,
}, nil)
mck.LinodeClient.EXPECT().UpdateObjectStorageBucketAccess(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil)
}),
Result("success", func(ctx context.Context, mck Mock) {
bScope.LinodeClient = mck.LinodeClient
_, err := reconciler.reconcile(ctx, &bScope)
Expect(err).ToNot(HaveOccurred())
}),
),
),
OneOf(
Path(
Call("unable to delete", func(ctx context.Context, mck Mock) {
obj.Spec.ForceDeleteBucket = true
obj.Spec.AccessKeyRef = &corev1.ObjectReference{Name: "lifecycle-mgmt", Namespace: "default"}
obj.DeletionTimestamp = &metav1.Time{Time: time.Now()}
Expect(k8sClient.Delete(ctx, &obj)).To(Succeed())
}),
OneOf(
Path(Result("cannot purge bucket", func(ctx context.Context, mck Mock) {
_, err := reconciler.reconcile(ctx, &bScope)
Expect(err).NotTo(HaveOccurred())
Expect(mck.Logs()).To(ContainSubstring("failed to purge all objects"))
})),
),
),
Path(
Call("able to delete", func(ctx context.Context, _ Mock) {
obj.Spec.ForceDeleteBucket = false
obj.Spec.AccessKeyRef = nil
obj.DeletionTimestamp = &metav1.Time{Time: time.Now()}
Expect(k8sClient.Delete(ctx, &obj)).To(Succeed())
}),
// TODO: Mock smithy operations so we can test bucket deletion
Result("success for preserving bucket", func(ctx context.Context, mck Mock) {
res, err := reconciler.reconcile(ctx, &bScope)
Expect(err).NotTo(HaveOccurred())
Expect(res.RequeueAfter).To(Equal(time.Duration(0)))
}),
),
),
)
})
var _ = Describe("errors", Label("bucket", "errors"), func() {
suite := NewControllerSuite(
GinkgoT(),
mock.MockLinodeClient{},
mock.MockK8sClient{},
)
reconciler := LinodeObjectStorageBucketReconciler{}
bScope := scope.ObjectStorageBucketScope{}
suite.BeforeEach(func(_ context.Context, mck Mock) {
reconciler.Recorder = mck.Recorder()
bScope.Logger = mck.Logger()
// Reset obj to base state to be modified in each test path.
// We can use a consistent name since these tests are stateless.
bScope.Bucket = &infrav1alpha2.LinodeObjectStorageBucket{
ObjectMeta: metav1.ObjectMeta{
Name: "mock",
Namespace: "default",
UID: "12345",
},
Spec: infrav1alpha2.LinodeObjectStorageBucketSpec{
Region: "region",
},
}
})
suite.Run(
OneOf(
Path(Call("resource can be fetched", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
})),
Path(
Call("resource is not found", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(apierrors.NewNotFound(schema.GroupResource{}, "mock"))
}),
Result("no error", func(ctx context.Context, mck Mock) {
reconciler.Client = mck.K8sClient
_, err := rec.AsReconcilerWithTracing(mck.K8sClient, &reconciler).Reconcile(ctx, reconcile.Request{
NamespacedName: client.ObjectKeyFromObject(bScope.Bucket),
})
Expect(err).NotTo(HaveOccurred())
}),
),
Path(
Call("resource can't be fetched", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("non-404 error"))
}),
Result("error", func(ctx context.Context, mck Mock) {
reconciler.Client = mck.K8sClient
reconciler.Logger = bScope.Logger
_, err := rec.AsReconcilerWithTracing(mck.K8sClient, &reconciler).Reconcile(ctx, reconcile.Request{
NamespacedName: client.ObjectKeyFromObject(bScope.Bucket),
})
Expect(err.Error()).To(ContainSubstring("non-404 error"))
}),
),
),
Result("scope params is missing args", func(ctx context.Context, mck Mock) {
reconciler.Client = mck.K8sClient
reconciler.Logger = bScope.Logger
_, err := rec.AsReconcilerWithTracing(mck.K8sClient, &reconciler).Reconcile(ctx, reconcile.Request{
NamespacedName: client.ObjectKeyFromObject(bScope.Bucket),
})
Expect(err.Error()).To(ContainSubstring("failed to create object storage bucket scope"))
Expect(mck.Logs()).To(ContainSubstring("Failed to create object storage bucket scope"))
}),
)
// Reconciler retry: when a finalizer operation fails transiently the reconciler
// should requeue with jitter rather than surface an error.
suite.Run(
OneOf(
// AddFinalizer patches the bucket; if the patch fails it should requeue.
Path(
Call("bucket finalizer patch fails", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Scheme().Return(scheme.Scheme).AnyTimes()
firstPatch := mck.K8sClient.EXPECT().Patch(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("patch error"))
mck.K8sClient.EXPECT().Patch(gomock.Any(), gomock.Any(), gomock.Any()).After(firstPatch).Return(nil)
}),
Result("requeues with jitter", func(ctx context.Context, mck Mock) {
// Base bucket has no BucketFinalizer, so AddFinalizer is triggered.
bScope.Bucket.Spec.AccessKeyRef = &corev1.ObjectReference{Name: "some-key"}
bScope.Client = mck.K8sClient
helper, err := patch.NewHelper(bScope.Bucket, mck.K8sClient)
Expect(err).NotTo(HaveOccurred())
bScope.PatchHelper = helper
res, err := reconciler.reconcile(ctx, &bScope)
Expect(err).NotTo(HaveOccurred())
Expect(res.RequeueAfter).To(BeNumerically(">=", rec.DefaultObjectStorageBucketControllerReconcileDelay))
Expect(res.RequeueAfter).To(BeNumerically("<=", rec.DefaultObjectStorageBucketControllerReconcileDelay+time.Duration(float64(rec.DefaultObjectStorageBucketControllerReconcileDelay)*rec.RetryJitterFraction)))
Expect(mck.Logs()).To(ContainSubstring("failed to update bucket finalizer"))
}),
),
// AddAccessKeyRefFinalizer fetches the key ref; if the Get fails it should requeue.
Path(
Call("access key finalizer Get fails", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Scheme().Return(scheme.Scheme).AnyTimes()
mck.K8sClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("get error"))
}),
Result("requeues with jitter", func(ctx context.Context, mck Mock) {
// BucketFinalizer already present so AddFinalizer is a no-op.
bScope.Bucket.Finalizers = []string{infrav1alpha2.BucketFinalizer}
bScope.Bucket.Spec.AccessKeyRef = &corev1.ObjectReference{Name: "some-key"}
bScope.Client = mck.K8sClient
helper, err := patch.NewHelper(bScope.Bucket, mck.K8sClient)
Expect(err).NotTo(HaveOccurred())
bScope.PatchHelper = helper
res, err := reconciler.reconcile(ctx, &bScope)
Expect(err).NotTo(HaveOccurred())
Expect(res.RequeueAfter).To(BeNumerically(">=", rec.DefaultObjectStorageBucketControllerReconcileDelay))
Expect(res.RequeueAfter).To(BeNumerically("<=", rec.DefaultObjectStorageBucketControllerReconcileDelay+time.Duration(float64(rec.DefaultObjectStorageBucketControllerReconcileDelay)*rec.RetryJitterFraction)))
Expect(mck.Logs()).To(ContainSubstring("failed to update access key finalizer"))
}),
),
),
)
})