Skip to content

Commit 6bc8746

Browse files
committed
fix e2e and integration tests
Signed-off-by: Britania Rodriguez Reyes <britaniar@microsoft.com>
1 parent f72b7ed commit 6bc8746

File tree

5 files changed

+98
-100
lines changed

5 files changed

+98
-100
lines changed

pkg/controllers/workapplier/controller.go

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ func (r *Reconciler) garbageCollectAppliedWork(ctx context.Context, work *fleetv
501501
// If the Work object allows co-ownership, we need to change the blockOwnerDeletion to false
502502
// so that the appliedWork can be deleted without waiting for the object that has another owner to be deleted.
503503
klog.V(2).InfoS("Work object allows co-ownership; updating owner reference to not block deletion", "work", work.Name)
504-
if err := r.updateAppliedWorkOwnerReference(ctx, work); !apierrors.IsNotFound(err) && err != nil {
504+
if err := r.updateOwnerReference(ctx, work); !apierrors.IsNotFound(err) && err != nil {
505505
klog.ErrorS(err, "Failed to update owner reference for the work", "work", work.Name)
506506
return ctrl.Result{}, controller.NewAPIServerError(false, err)
507507
}
@@ -517,18 +517,18 @@ func (r *Reconciler) garbageCollectAppliedWork(ctx context.Context, work *fleetv
517517
}
518518

519519
klog.V(2).InfoS("Deletion for appliedWork is in progress", "appliedWork", work.Name)
520-
return ctrl.Result{RequeueAfter: 5 * time.Second}, nil
520+
return ctrl.Result{RequeueAfter: time.Minute}, nil
521521
}
522522

523-
func (r *Reconciler) updateAppliedWorkOwnerReference(ctx context.Context, work *fleetv1beta1.Work) error {
523+
func (r *Reconciler) updateOwnerReference(ctx context.Context, work *fleetv1beta1.Work) error {
524524
// Get the AppliedWork object
525525
var appliedWork fleetv1beta1.AppliedWork
526526
if err := r.spokeClient.Get(ctx, types.NamespacedName{Name: work.Name}, &appliedWork); err != nil {
527527
if apierrors.IsNotFound(err) {
528-
klog.V(2).InfoS("AppliedWork not found for updateAppliedWorkOwnerReference", "appliedWork", work.Name)
528+
klog.V(2).InfoS("AppliedWork not found for updateOwnerReference", "appliedWork", work.Name)
529529
return err
530530
}
531-
klog.ErrorS(err, "Failed to get AppliedWork for updateAppliedWorkOwnerReference", "appliedWork", work.Name)
531+
klog.ErrorS(err, "Failed to get AppliedWork for updateOwnerReference", "appliedWork", work.Name)
532532
return err
533533
}
534534

@@ -549,36 +549,31 @@ func (r *Reconciler) updateAppliedWorkOwnerReference(ctx context.Context, work *
549549
if apierrors.IsNotFound(err) {
550550
continue
551551
}
552-
klog.ErrorS(err, "Failed to get manifest for updateAppliedWorkOwnerReference", "gvr", gvr, "name", res.Name, "namespace", res.Namespace)
552+
klog.ErrorS(err, "Failed to get manifest for updateOwnerReference", "gvr", gvr, "name", res.Name, "namespace", res.Namespace)
553553
return err
554554
}
555-
// Check if there is more than one owner reference
556-
if len(obj.GetOwnerReferences()) > 1 {
557-
owners := obj.GetOwnerReferences()
558-
updated := false
559-
for i := range owners {
560-
// Check if the owner reference matches the appliedWork UID and has BlockOwnerDeletion set to true
561-
if owners[i].UID == appliedWork.UID && owners[i].BlockOwnerDeletion != nil && *owners[i].BlockOwnerDeletion {
562-
// If we found an owner reference to update, set BlockOwnerDeletion to false
563-
b := false
564-
owners[i].BlockOwnerDeletion = &b
565-
updated = true
566-
break
567-
}
555+
owners := obj.GetOwnerReferences()
556+
// Check if the owner reference already exists
557+
updated := false
558+
for i := range owners {
559+
if owners[i].UID == appliedWork.UID && owners[i].BlockOwnerDeletion != nil && *owners[i].BlockOwnerDeletion {
560+
b := false
561+
owners[i].BlockOwnerDeletion = &b
562+
updated = true
568563
}
569-
if updated {
570-
obj.SetOwnerReferences(owners)
571-
if res.Namespace != "" {
572-
_, err = r.spokeDynamicClient.Resource(gvr).Namespace(res.Namespace).Update(ctx, obj, metav1.UpdateOptions{})
573-
} else {
574-
_, err = r.spokeDynamicClient.Resource(gvr).Update(ctx, obj, metav1.UpdateOptions{})
575-
}
576-
if err != nil {
577-
klog.ErrorS(err, "Failed to update manifest owner reference blockOwnerDeletion", "gvr", gvr, "name", res.Name, "namespace", res.Namespace)
578-
return err
579-
} else {
580-
klog.InfoS("Set blockOwnerDeletion=false for owner reference", "gvr", gvr, "name", res.Name, "namespace", res.Namespace)
581-
}
564+
}
565+
if updated {
566+
obj.SetOwnerReferences(owners)
567+
if res.Namespace != "" {
568+
_, err = r.spokeDynamicClient.Resource(gvr).Namespace(res.Namespace).Update(ctx, obj, metav1.UpdateOptions{})
569+
} else {
570+
_, err = r.spokeDynamicClient.Resource(gvr).Update(ctx, obj, metav1.UpdateOptions{})
571+
}
572+
if err != nil {
573+
klog.ErrorS(err, "Failed to update manifest owner reference blockOwnerDeletion", "gvr", gvr, "name", res.Name, "namespace", res.Namespace)
574+
return err
575+
} else {
576+
klog.InfoS("Set blockOwnerDeletion=false for owner reference", "gvr", gvr, "name", res.Name, "namespace", res.Namespace)
582577
}
583578
}
584579
}

pkg/controllers/workapplier/controller_integration_test.go

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,12 +1374,6 @@ var _ = Describe("applying manifests", func() {
13741374
// The environment prepared by the envtest package does not support namespace
13751375
// deletion; each test case would use a new namespace.
13761376
nsName := fmt.Sprintf(nsNameTemplate, utils.RandStr())
1377-
anotherOwnerReference := metav1.OwnerReference{
1378-
APIVersion: "another-api-version",
1379-
Kind: "another-kind",
1380-
Name: "another-owner",
1381-
UID: "another-uid",
1382-
}
13831377

13841378
var appliedWorkOwnerRef *metav1.OwnerReference
13851379
var regularNS *corev1.Namespace
@@ -1398,7 +1392,7 @@ var _ = Describe("applying manifests", func() {
13981392
regularDeployJSON := marshalK8sObjJSON(regularDeploy)
13991393

14001394
// Create a new Work object with all the manifest JSONs.
1401-
createWorkObject(workName, &fleetv1beta1.ApplyStrategy{AllowCoOwnership: true}, regularNSJSON, regularDeployJSON)
1395+
createWorkObject(workName, nil, regularNSJSON, regularDeployJSON)
14021396
})
14031397

14041398
It("should add cleanup finalizer to the Work object", func() {
@@ -1534,75 +1528,82 @@ var _ = Describe("applying manifests", func() {
15341528
})
15351529

15361530
It("can update object to add another owner reference", func() {
1537-
// Retrieve the Deployment object.
1538-
gotDeploy := &appsv1.Deployment{}
1539-
Expect(memberClient.Get(ctx, client.ObjectKey{Namespace: nsName, Name: deployName}, gotDeploy)).To(Succeed(), "Failed to retrieve the Deployment object")
1540-
1541-
// Add another owner reference to the Deployment object.
1542-
gotDeploy.OwnerReferences = append(gotDeploy.OwnerReferences, anotherOwnerReference)
1543-
Expect(memberClient.Update(ctx, gotDeploy)).To(Succeed(), "Failed to update the Deployment object with another owner reference")
1544-
1545-
// Ensure that the Deployment object has been updated as expected.
1546-
Eventually(func() error {
1547-
// Retrieve the Deployment object again.
1548-
if err := memberClient.Get(ctx, client.ObjectKey{Namespace: nsName, Name: deployName}, gotDeploy); err != nil {
1549-
return fmt.Errorf("failed to retrieve the Deployment object: %w", err)
1550-
}
1551-
1552-
// Check that the Deployment object has been updated as expected.
1553-
if len(gotDeploy.OwnerReferences) != 2 {
1554-
return fmt.Errorf("expected 2 owner references, got %d", len(gotDeploy.OwnerReferences))
1555-
}
1556-
for _, ownerRef := range gotDeploy.OwnerReferences {
1557-
if ownerRef.UID == anotherOwnerReference.UID {
1558-
return nil
1559-
}
1560-
}
1561-
return fmt.Errorf("another owner reference not found in the Deployment object")
1562-
}, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to find another owner reference on Deployment")
15631531
})
15641532

1565-
It("should start deleting the Work object", func() {
1566-
// Start deleting the Work object.
1567-
deleteWorkObject(workName)
1533+
It("should garbage collect removed manifests", func() {
1534+
deployRemovedActual := regularDeployRemovedActual(nsName, deployName)
1535+
Eventually(deployRemovedActual, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to remove the deployment object")
15681536
})
15691537

1570-
It("should start deleting the AppliedWork object", func() {
1571-
// Ensure that the Work object is being deleted.
1572-
Eventually(func() error {
1573-
appliedWork := &fleetv1beta1.AppliedWork{}
1574-
if err := memberClient.Get(ctx, client.ObjectKey{Name: workName}, appliedWork); err != nil {
1575-
return err
1576-
}
1577-
if !appliedWork.DeletionTimestamp.IsZero() {
1578-
return fmt.Errorf("appliedWork object still is not being deleted")
1579-
}
1580-
return nil
1581-
}, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to start deleting the AppliedWork object")
1538+
It("should update the Work object status", func() {
1539+
// Prepare the status information.
1540+
workConds := []metav1.Condition{
1541+
{
1542+
Type: fleetv1beta1.WorkConditionTypeApplied,
1543+
Status: metav1.ConditionTrue,
1544+
Reason: WorkAllManifestsAppliedReason,
1545+
},
1546+
{
1547+
Type: fleetv1beta1.WorkConditionTypeAvailable,
1548+
Status: metav1.ConditionTrue,
1549+
Reason: WorkAllManifestsAvailableReason,
1550+
},
1551+
}
1552+
manifestConds := []fleetv1beta1.ManifestCondition{
1553+
{
1554+
Identifier: fleetv1beta1.WorkResourceIdentifier{
1555+
Ordinal: 0,
1556+
Group: "",
1557+
Version: "v1",
1558+
Kind: "Namespace",
1559+
Resource: "namespaces",
1560+
Name: nsName,
1561+
},
1562+
Conditions: []metav1.Condition{
1563+
{
1564+
Type: fleetv1beta1.WorkConditionTypeApplied,
1565+
Status: metav1.ConditionTrue,
1566+
Reason: string(ManifestProcessingApplyResultTypeApplied),
1567+
ObservedGeneration: 0,
1568+
},
1569+
{
1570+
Type: fleetv1beta1.WorkConditionTypeAvailable,
1571+
Status: metav1.ConditionTrue,
1572+
Reason: string(ManifestProcessingAvailabilityResultTypeAvailable),
1573+
ObservedGeneration: 0,
1574+
},
1575+
},
1576+
},
1577+
}
1578+
1579+
workStatusUpdatedActual := workStatusUpdated(workName, workConds, manifestConds, nil, nil)
1580+
Eventually(workStatusUpdatedActual, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to update work status")
15821581
})
15831582

1584-
It("should update owner reference from the Deployment object", func() {
1585-
// Ensure that the Deployment object has been updated to remove the owner reference.
1586-
Eventually(func() error {
1587-
// Retrieve the Deployment object again.
1588-
gotDeploy := &appsv1.Deployment{}
1589-
if err := memberClient.Get(ctx, client.ObjectKey{Namespace: nsName, Name: deployName}, gotDeploy); err != nil {
1590-
return fmt.Errorf("failed to retrieve the Deployment object: %w", err)
1591-
}
1583+
It("should update the AppliedWork object status", func() {
1584+
// Prepare the status information.
1585+
appliedResourceMeta := []fleetv1beta1.AppliedResourceMeta{
1586+
{
1587+
WorkResourceIdentifier: fleetv1beta1.WorkResourceIdentifier{
1588+
Ordinal: 0,
1589+
Group: "",
1590+
Version: "v1",
1591+
Kind: "Namespace",
1592+
Resource: "namespaces",
1593+
Name: nsName,
1594+
},
1595+
UID: regularNS.UID,
1596+
},
1597+
}
15921598

1593-
// Check that the Deployment object has been updated as expected.
1594-
for _, ownerRef := range gotDeploy.OwnerReferences {
1595-
if ownerRef.UID == appliedWorkOwnerRef.UID {
1596-
if *ownerRef.BlockOwnerDeletion {
1597-
return fmt.Errorf("owner reference from AppliedWork still has BlockOwnerDeletion set to true")
1598-
}
1599-
}
1600-
}
1601-
return nil
1602-
}, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to remove owner reference from Deployment")
1599+
appliedWorkStatusUpdatedActual := appliedWorkStatusUpdated(workName, appliedResourceMeta)
1600+
Eventually(appliedWorkStatusUpdatedActual, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to update appliedWork status")
16031601
})
16041602

16051603
AfterAll(func() {
1604+
// Delete the Work object and related resources.
1605+
deleteWorkObject(workName)
1606+
16061607
// Ensure that the AppliedWork object has been removed.
16071608
appliedWorkRemovedActual := appliedWorkRemovedActual(workName)
16081609
Eventually(appliedWorkRemovedActual, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to remove the AppliedWork object")

test/e2e/enveloped_object_placement_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ var _ = Describe("placing wrapped resources using a CRP", func() {
207207
AfterAll(func() {
208208
By(fmt.Sprintf("deleting envelope %s", testResourceEnvelope.Name))
209209
Expect(hubClient.Delete(ctx, &testResourceEnvelope)).To(Succeed(), "Failed to delete ResourceEnvelope")
210+
By(fmt.Sprintf("deleting envelope %s", testClusterResourceEnvelope.Name))
210211
Expect(hubClient.Delete(ctx, &testClusterResourceEnvelope)).To(Succeed(), "Failed to delete testClusterResourceEnvelope")
211212
By(fmt.Sprintf("deleting placement %s and related resources", crpName))
212213
ensureCRPAndRelatedResourcesDeleted(crpName, allMemberClusters)

test/e2e/rollout_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,8 @@ var _ = Describe("placing wrapped resources using a CRP", Ordered, func() {
718718
})
719719
// job is not trackable, so we need to wait for a bit longer for each roll out
720720
It("should update CRP status as expected", func() {
721-
crpStatusUpdatedActual := customizedCRPStatusUpdatedActual(crpName, wantSelectedResources, allMemberClusterNames, nil, "0", false)
722-
Eventually(crpStatusUpdatedActual, 6*time.Duration(unAvailablePeriodSeconds)*time.Second, eventuallyInterval).Should(Succeed(), "Failed to update CRP status as expected")
721+
crpStatusUpdatedActual := customizedCRPStatusUpdatedActual(crpName, wantSelectedResources, allMemberClusterNames, nil, "1", false)
722+
Eventually(crpStatusUpdatedActual, 5*time.Duration(unAvailablePeriodSeconds)*time.Second, eventuallyInterval).Should(Succeed(), "Failed to update CRP status as expected")
723723
})
724724

725725
AfterAll(func() {

test/e2e/utils_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,7 @@ func readJobTestManifest(testManifest *batchv1.Job) {
12341234

12351235
func readEnvelopeResourceTestManifest(testEnvelopeObj *placementv1beta1.ResourceEnvelope) {
12361236
By("Read testEnvelopConfigMap resource")
1237+
testEnvelopeObj.ResourceVersion = ""
12371238
err := utils.GetObjectFromManifest("resources/test-envelope-object.yaml", testEnvelopeObj)
12381239
Expect(err).Should(Succeed())
12391240
}

0 commit comments

Comments
 (0)