Skip to content

Commit 5b1b94e

Browse files
committed
fix the ut
Signed-off-by: Zhiying Lin <[email protected]>
1 parent 698f008 commit 5b1b94e

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

pkg/controllers/clusterresourceplacement/controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,10 @@ func (r *Reconciler) getOrCreateClusterResourceSnapshot(ctx context.Context, crp
591591
// shouldCreateNewResourceSnapshotNow checks whether it is ready to create the new resource snapshot to avoid too frequent creation
592592
// based on the configured ResourceSnapshotCreationInterval.
593593
func (r *Reconciler) shouldCreateNewResourceSnapshotNow(ctx context.Context, latestResourceSnapshot *fleetv1beta1.ClusterResourceSnapshot) (ctrl.Result, error) {
594+
if r.ResourceSnapshotCreationInterval <= 0 {
595+
return ctrl.Result{}, nil
596+
}
597+
594598
// We reserve half of the resourceSnapshotCreationInterval to allow the controller to bundle all the resource changes into one snapshot.
595599
// For example, if the interval is 1m, the first resource change will be captured starting from 30s.
596600
// And then the next 30s will be used to capture all the resource changes into one snapshot.

pkg/controllers/clusterresourceplacement/controller_test.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,8 +1514,9 @@ func TestGetOrCreateClusterResourceSnapshot(t *testing.T) {
15141514
},
15151515
},
15161516
Annotations: map[string]string{
1517-
fleetv1beta1.ResourceGroupHashAnnotation: resourceSnapshotSpecWithServiceResourceHash,
1518-
fleetv1beta1.NumberOfResourceSnapshotsAnnotation: "3",
1517+
fleetv1beta1.ResourceGroupHashAnnotation: resourceSnapshotSpecWithServiceResourceHash,
1518+
fleetv1beta1.NumberOfResourceSnapshotsAnnotation: "3",
1519+
fleetv1beta1.NextResourceSnapshotCandidateDetectionTimeAnnotation: now.Add(-5 * time.Minute).Format(time.RFC3339),
15191520
},
15201521
CreationTimestamp: metav1.NewTime(now.Time.Add(-1 * time.Hour)),
15211522
},
@@ -1629,8 +1630,9 @@ func TestGetOrCreateClusterResourceSnapshot(t *testing.T) {
16291630
},
16301631
},
16311632
Annotations: map[string]string{
1632-
fleetv1beta1.ResourceGroupHashAnnotation: resourceSnapshotSpecWithServiceResourceHash,
1633-
fleetv1beta1.NumberOfResourceSnapshotsAnnotation: "3",
1633+
fleetv1beta1.ResourceGroupHashAnnotation: resourceSnapshotSpecWithServiceResourceHash,
1634+
fleetv1beta1.NumberOfResourceSnapshotsAnnotation: "3",
1635+
fleetv1beta1.NextResourceSnapshotCandidateDetectionTimeAnnotation: now.Add(-5 * time.Minute).Format(time.RFC3339),
16341636
},
16351637
},
16361638
Spec: fleetv1beta1.ResourceSnapshotSpec{SelectedResources: []fleetv1beta1.ResourceContent{serviceResourceContent}},
@@ -1951,9 +1953,10 @@ func TestGetOrCreateClusterResourceSnapshot(t *testing.T) {
19511953
},
19521954
},
19531955
Annotations: map[string]string{
1954-
fleetv1beta1.ResourceGroupHashAnnotation: resourceSnapshotSpecWithMultipleResourcesHash,
1955-
fleetv1beta1.NumberOfResourceSnapshotsAnnotation: "3",
1956-
fleetv1beta1.NumberOfEnvelopedObjectsAnnotation: "0",
1956+
fleetv1beta1.ResourceGroupHashAnnotation: resourceSnapshotSpecWithMultipleResourcesHash,
1957+
fleetv1beta1.NumberOfResourceSnapshotsAnnotation: "3",
1958+
fleetv1beta1.NumberOfEnvelopedObjectsAnnotation: "0",
1959+
fleetv1beta1.NextResourceSnapshotCandidateDetectionTimeAnnotation: now.Add(-5 * time.Minute).Format(time.RFC3339),
19571960
},
19581961
CreationTimestamp: metav1.NewTime(now.Time.Add(-1 * time.Hour)),
19591962
},
@@ -2231,9 +2234,10 @@ func TestGetOrCreateClusterResourceSnapshot(t *testing.T) {
22312234
},
22322235
},
22332236
Annotations: map[string]string{
2234-
fleetv1beta1.ResourceGroupHashAnnotation: resourceSnapshotSpecWithMultipleResourcesHash,
2235-
fleetv1beta1.NumberOfResourceSnapshotsAnnotation: "3",
2236-
fleetv1beta1.NumberOfEnvelopedObjectsAnnotation: "0",
2237+
fleetv1beta1.ResourceGroupHashAnnotation: resourceSnapshotSpecWithMultipleResourcesHash,
2238+
fleetv1beta1.NumberOfResourceSnapshotsAnnotation: "3",
2239+
fleetv1beta1.NumberOfEnvelopedObjectsAnnotation: "0",
2240+
fleetv1beta1.NextResourceSnapshotCandidateDetectionTimeAnnotation: now.Add(-5 * time.Minute).Format(time.RFC3339),
22372241
},
22382242
CreationTimestamp: metav1.NewTime(now.Time.Add(-1 * time.Hour)),
22392243
},
@@ -2315,9 +2319,10 @@ func TestGetOrCreateClusterResourceSnapshot(t *testing.T) {
23152319
},
23162320
},
23172321
Annotations: map[string]string{
2318-
fleetv1beta1.ResourceGroupHashAnnotation: resourceSnapshotSpecWithServiceResourceHash,
2319-
fleetv1beta1.NumberOfResourceSnapshotsAnnotation: "1",
2320-
fleetv1beta1.NumberOfEnvelopedObjectsAnnotation: "0",
2322+
fleetv1beta1.ResourceGroupHashAnnotation: resourceSnapshotSpecWithServiceResourceHash,
2323+
fleetv1beta1.NumberOfResourceSnapshotsAnnotation: "1",
2324+
fleetv1beta1.NumberOfEnvelopedObjectsAnnotation: "0",
2325+
fleetv1beta1.NextResourceSnapshotCandidateDetectionTimeAnnotation: now.Add(-5 * time.Minute).Format(time.RFC3339),
23212326
},
23222327
CreationTimestamp: metav1.NewTime(now.Time.Add(-1 * time.Hour)),
23232328
},
@@ -2343,9 +2348,10 @@ func TestGetOrCreateClusterResourceSnapshot(t *testing.T) {
23432348
},
23442349
},
23452350
Annotations: map[string]string{
2346-
fleetv1beta1.ResourceGroupHashAnnotation: resourceSnapshotSpecWithServiceResourceHash,
2347-
fleetv1beta1.NumberOfResourceSnapshotsAnnotation: "1",
2348-
fleetv1beta1.NumberOfEnvelopedObjectsAnnotation: "0",
2351+
fleetv1beta1.ResourceGroupHashAnnotation: resourceSnapshotSpecWithServiceResourceHash,
2352+
fleetv1beta1.NumberOfResourceSnapshotsAnnotation: "1",
2353+
fleetv1beta1.NumberOfEnvelopedObjectsAnnotation: "0",
2354+
fleetv1beta1.NextResourceSnapshotCandidateDetectionTimeAnnotation: now.Add(-5 * time.Minute).Format(time.RFC3339),
23492355
},
23502356
},
23512357
Spec: fleetv1beta1.ResourceSnapshotSpec{SelectedResources: []fleetv1beta1.ResourceContent{serviceResourceContent}},
@@ -4485,18 +4491,21 @@ func TestShouldCreateNewResourceSnapshotNow(t *testing.T) {
44854491

44864492
cases := []struct {
44874493
name string
4494+
interval time.Duration
44884495
creationTime time.Time
44894496
annotationValue string
44904497
wantAnnoation bool
44914498
wantRequeue bool
44924499
}{
44934500
{
44944501
name: "recently created resource snapshot",
4502+
interval: interval,
44954503
creationTime: now.Add(-half + 5*time.Second),
44964504
wantRequeue: true,
44974505
},
44984506
{
44994507
name: "existing resource snapshot without annotation",
4508+
interval: interval,
45004509
creationTime: now.Add(-2 * half),
45014510
// no annotation → sets it and requeues
45024511
annotationValue: "",
@@ -4505,6 +4514,7 @@ func TestShouldCreateNewResourceSnapshotNow(t *testing.T) {
45054514
},
45064515
{
45074516
name: "existing resource snapshot with expired annotation",
4517+
interval: interval,
45084518
creationTime: now.Add(-2 * half),
45094519
annotationValue: now.Add(-half).Format(time.RFC3339),
45104520
// annotation far in past → no requeue
@@ -4513,6 +4523,7 @@ func TestShouldCreateNewResourceSnapshotNow(t *testing.T) {
45134523
},
45144524
{
45154525
name: "existing resource snapshot with invalid annotation",
4526+
interval: interval,
45164527
creationTime: now.Add(-2 * half),
45174528
annotationValue: "invalid-value",
45184529
// annotation far in past → no requeue
@@ -4521,12 +4532,18 @@ func TestShouldCreateNewResourceSnapshotNow(t *testing.T) {
45214532
},
45224533
{
45234534
name: "existing resource snapshot with recently added annotation",
4535+
interval: interval,
45244536
creationTime: now.Add(-2 * half),
45254537
annotationValue: now.Add(-half + 5*time.Second).Format(time.RFC3339),
45264538
// annotation exists but within half interval → requeue
45274539
wantAnnoation: true,
45284540
wantRequeue: true,
45294541
},
4542+
{
4543+
name: "ResourceSnapshotCreationInterval is 0",
4544+
interval: 0,
4545+
wantRequeue: false,
4546+
},
45304547
}
45314548

45324549
for _, tc := range cases {
@@ -4552,7 +4569,7 @@ func TestShouldCreateNewResourceSnapshotNow(t *testing.T) {
45524569

45534570
r := &Reconciler{
45544571
Client: client,
4555-
ResourceSnapshotCreationInterval: interval,
4572+
ResourceSnapshotCreationInterval: tc.interval,
45564573
}
45574574

45584575
ctx := context.Background()

0 commit comments

Comments
 (0)