Skip to content

Commit 0caa968

Browse files
committed
Split out S3BucketReconcileRequired function
1 parent bb41d6f commit 0caa968

File tree

3 files changed

+110
-9
lines changed

3 files changed

+110
-9
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package v1alpha1
2+
3+
import (
4+
"time"
5+
)
6+
7+
func (i *Velero) S3BucketReconcileRequired(reconcilePeriod time.Duration) bool {
8+
// If any of the following are true, reconcile the S3 bucket:
9+
// - Name is empty
10+
// - Provisioned is false
11+
// - The LastSyncTimestamp is unset
12+
// - It's been longer than 1 hour since last sync
13+
if i.Status.S3Bucket.Name == "" ||
14+
!i.Status.S3Bucket.Provisioned ||
15+
i.Status.S3Bucket.LastSyncTimestamp.IsZero() ||
16+
time.Since(i.Status.S3Bucket.LastSyncTimestamp.Time) > reconcilePeriod {
17+
return true
18+
}
19+
20+
return false
21+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package v1alpha1
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
)
9+
10+
func TestS3BucketReconcileRequired(t *testing.T) {
11+
var testcases = []struct {
12+
testName string
13+
bucketName string
14+
bucketProvisioned bool
15+
shouldReconcile bool
16+
timestamp time.Time
17+
reconcilePeriod time.Duration
18+
}{
19+
{
20+
testName: "default bucket, provisioned 30 mins ago, 60 minute period",
21+
bucketName: "test-bucket",
22+
bucketProvisioned: true,
23+
timestamp: time.Now().Add(-time.Minute * 30),
24+
reconcilePeriod: time.Minute * 60,
25+
shouldReconcile: false,
26+
},
27+
{
28+
testName: "bucket name empty",
29+
bucketName: "",
30+
bucketProvisioned: true,
31+
timestamp: time.Now(),
32+
reconcilePeriod: time.Minute * 60,
33+
shouldReconcile: true,
34+
},
35+
{
36+
testName: "bucket not provioned",
37+
bucketName: "test-bucket",
38+
bucketProvisioned: false,
39+
timestamp: time.Now(),
40+
reconcilePeriod: time.Minute * 60,
41+
shouldReconcile: true,
42+
},
43+
{
44+
testName: "timestamp is epoch",
45+
bucketName: "test-bucket",
46+
bucketProvisioned: true,
47+
timestamp: time.Unix(0, 0),
48+
reconcilePeriod: time.Minute * 60,
49+
shouldReconcile: true,
50+
},
51+
{
52+
testName: "timestamp is unset",
53+
bucketName: "test-bucket",
54+
bucketProvisioned: true,
55+
reconcilePeriod: time.Minute * 60,
56+
shouldReconcile: true,
57+
},
58+
}
59+
60+
for _, tc := range testcases {
61+
t.Logf("Running scenario %q", tc.testName)
62+
63+
instance := &Velero{
64+
Spec: VeleroSpec{},
65+
Status: VeleroStatus{
66+
S3Bucket: S3Bucket{
67+
Name: tc.bucketName,
68+
Provisioned: tc.bucketProvisioned,
69+
},
70+
},
71+
}
72+
73+
if !tc.timestamp.IsZero() {
74+
instance.Status.S3Bucket.LastSyncTimestamp = &metav1.Time{Time: tc.timestamp}
75+
}
76+
77+
reconcile := instance.S3BucketReconcileRequired(tc.reconcilePeriod)
78+
79+
if reconcile != tc.shouldReconcile {
80+
if tc.shouldReconcile {
81+
t.Errorf("did not reconcile when expecting one")
82+
} else {
83+
t.Errorf("reconciled when not expecting one")
84+
}
85+
}
86+
}
87+
}

pkg/controller/velero/controller.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,8 @@ func (r *ReconcileVelero) Reconcile(request reconcile.Request) (reconcile.Result
143143
return reconcile.Result{}, err
144144
}
145145

146-
// If any of the following are true, reconcile the S3 bucket:
147-
// - Name is empty
148-
// - Provisioned is false
149-
// - The LastSyncTimestamp is unset
150-
// - It's been longer than 1 hour since last sync
151-
if instance.Status.S3Bucket.Name == "" ||
152-
!instance.Status.S3Bucket.Provisioned ||
153-
instance.Status.S3Bucket.LastSyncTimestamp.IsZero() ||
154-
time.Since(instance.Status.S3Bucket.LastSyncTimestamp.Time) > s3ReconcilePeriod {
146+
// Check if bucket needs to be reconciled
147+
if instance.S3BucketReconcileRequired(s3ReconcilePeriod) {
155148
// Always directly return from this, as we will either update the
156149
// timestamp when complete, or return an error.
157150
return r.provisionS3(reqLogger, s3Client, instance)

0 commit comments

Comments
 (0)