|
| 1 | +/* |
| 2 | +Copyright 2025 The KubeFleet Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package e2e |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "math" |
| 21 | + "time" |
| 22 | + |
| 23 | + . "github.com/onsi/ginkgo/v2" |
| 24 | + . "github.com/onsi/gomega" |
| 25 | + corev1 "k8s.io/api/core/v1" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + "k8s.io/utils/ptr" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + |
| 31 | + placementv1beta1 "github.com/kubefleet-dev/kubefleet/apis/placement/v1beta1" |
| 32 | +) |
| 33 | + |
| 34 | +var _ = Describe("validating CRP when using customized resourceSnapshotCreationInterval", Label("custom"), Ordered, func() { |
| 35 | + // skip entire suite if interval is zero |
| 36 | + BeforeAll(func() { |
| 37 | + if resourceSnapshotCreationInterval == 0 { |
| 38 | + Skip("Skipping customized-config placement test when RESOURCE_SNAPSHOT_CREATION_INTERVAL=0m") |
| 39 | + } |
| 40 | + }) |
| 41 | + |
| 42 | + crpName := fmt.Sprintf(crpNameTemplate, GinkgoParallelProcess()) |
| 43 | + |
| 44 | + BeforeAll(func() { |
| 45 | + By("creating work resources") |
| 46 | + createWorkResources() |
| 47 | + |
| 48 | + // Create the CRP. |
| 49 | + crp := &placementv1beta1.ClusterResourcePlacement{ |
| 50 | + ObjectMeta: metav1.ObjectMeta{ |
| 51 | + Name: crpName, |
| 52 | + // Add a custom finalizer; this would allow us to better observe |
| 53 | + // the behavior of the controllers. |
| 54 | + Finalizers: []string{customDeletionBlockerFinalizer}, |
| 55 | + }, |
| 56 | + Spec: placementv1beta1.PlacementSpec{ |
| 57 | + ResourceSelectors: []placementv1beta1.ClusterResourceSelector{ |
| 58 | + { |
| 59 | + Group: "", |
| 60 | + Kind: "Namespace", |
| 61 | + Version: "v1", |
| 62 | + LabelSelector: &metav1.LabelSelector{ |
| 63 | + MatchLabels: map[string]string{ |
| 64 | + workNamespaceLabelName: fmt.Sprintf("test-%d", GinkgoParallelProcess()), |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + Strategy: placementv1beta1.RolloutStrategy{ |
| 70 | + RollingUpdate: &placementv1beta1.RollingUpdateConfig{ |
| 71 | + UnavailablePeriodSeconds: ptr.To(5), |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + } |
| 76 | + By(fmt.Sprintf("creating placement %s", crpName)) |
| 77 | + Expect(hubClient.Create(ctx, crp)).To(Succeed(), "Failed to create CRP %s", crpName) |
| 78 | + }) |
| 79 | + |
| 80 | + AfterAll(func() { |
| 81 | + By(fmt.Sprintf("garbage all things related to placement %s", crpName)) |
| 82 | + ensureCRPAndRelatedResourcesDeleted(crpName, allMemberClusters) |
| 83 | + }) |
| 84 | + |
| 85 | + It("should update CRP status as expected", func() { |
| 86 | + crpStatusUpdatedActual := crpStatusUpdatedActual([]placementv1beta1.ResourceIdentifier{}, allMemberClusterNames, nil, "0") |
| 87 | + Eventually(crpStatusUpdatedActual, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to update CRP %s status as expected", crpName) |
| 88 | + }) |
| 89 | + |
| 90 | + It("should not place work resources on member clusters", checkIfRemovedWorkResourcesFromAllMemberClusters) |
| 91 | + |
| 92 | + It("updating the resources on the hub and the namespace becomes selected", func() { |
| 93 | + workNamespaceName := fmt.Sprintf(workNamespaceNameTemplate, GinkgoParallelProcess()) |
| 94 | + ns := &corev1.Namespace{} |
| 95 | + Expect(hubClient.Get(ctx, types.NamespacedName{Name: workNamespaceName}, ns)).Should(Succeed(), "Failed to get the namespace %s", workNamespaceName) |
| 96 | + ns.Labels = map[string]string{ |
| 97 | + workNamespaceLabelName: fmt.Sprintf("test-%d", GinkgoParallelProcess()), |
| 98 | + } |
| 99 | + Expect(hubClient.Update(ctx, ns)).Should(Succeed(), "Failed to update namespace %s", workNamespaceName) |
| 100 | + }) |
| 101 | + |
| 102 | + It("should not update CRP status immeidately", func() { |
| 103 | + crpStatusUpdatedActual := crpStatusUpdatedActual([]placementv1beta1.ResourceIdentifier{}, allMemberClusterNames, nil, "0") |
| 104 | + Consistently(crpStatusUpdatedActual, resourceSnapshotCreationInterval-3*time.Second, consistentlyInterval).Should(Succeed(), "CRP %s status should be unchanged", crpName) |
| 105 | + }) |
| 106 | + |
| 107 | + It("should update CRP status as expected", func() { |
| 108 | + crpStatusUpdatedActual := crpStatusUpdatedActual(workResourceIdentifiers(), allMemberClusterNames, nil, "1") |
| 109 | + Eventually(crpStatusUpdatedActual, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to update CRP %s status as expected", crpName) |
| 110 | + }) |
| 111 | + |
| 112 | + It("should place the selected resources on member clusters", checkIfPlacedWorkResourcesOnAllMemberClusters) |
| 113 | + |
| 114 | + It("validating the clusterResourceSnapshots are created", func() { |
| 115 | + var resourceSnapshotList placementv1beta1.ClusterResourceSnapshotList |
| 116 | + masterResourceSnapshotLabels := client.MatchingLabels{ |
| 117 | + placementv1beta1.CRPTrackingLabel: crpName, |
| 118 | + } |
| 119 | + Expect(hubClient.List(ctx, &resourceSnapshotList, masterResourceSnapshotLabels)).Should(Succeed(), "Failed to list ClusterResourceSnapshots for CRP %s", crpName) |
| 120 | + Expect(len(resourceSnapshotList.Items)).Should(Equal(2), "Expected 2 ClusterResourceSnapshots for CRP %s, got %d", crpName, len(resourceSnapshotList.Items)) |
| 121 | + // Use math.Abs to get the absolute value of the time difference in seconds. |
| 122 | + snapshotDiffInSeconds := resourceSnapshotList.Items[0].CreationTimestamp.Time.Sub(resourceSnapshotList.Items[1].CreationTimestamp.Time).Seconds() |
| 123 | + diff := math.Abs(snapshotDiffInSeconds) |
| 124 | + Expect(time.Duration(diff)*time.Second >= resourceSnapshotCreationInterval).To(BeTrue(), "The time difference between ClusterResourceSnapshots should be more than resourceSnapshotCreationInterval") |
| 125 | + }) |
| 126 | + |
| 127 | + It("can delete the CRP", func() { |
| 128 | + // Delete the CRP. |
| 129 | + crp := &placementv1beta1.ClusterResourcePlacement{ |
| 130 | + ObjectMeta: metav1.ObjectMeta{ |
| 131 | + Name: crpName, |
| 132 | + }, |
| 133 | + } |
| 134 | + Expect(hubClient.Delete(ctx, crp)).To(Succeed(), "Failed to delete CRP %s", crpName) |
| 135 | + }) |
| 136 | + |
| 137 | + It("should remove placed resources from all member clusters", checkIfRemovedWorkResourcesFromAllMemberClusters) |
| 138 | + |
| 139 | + It("should remove controller finalizers from CRP", func() { |
| 140 | + finalizerRemovedActual := allFinalizersExceptForCustomDeletionBlockerRemovedFromCRPActual(crpName) |
| 141 | + Eventually(finalizerRemovedActual, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to remove controller finalizers from CRP %s", crpName) |
| 142 | + }) |
| 143 | +}) |
0 commit comments