@@ -15,6 +15,7 @@ import (
1515 corev1 "k8s.io/api/core/v1"
1616 rbacv1 "k8s.io/api/rbac/v1"
1717 "k8s.io/apimachinery/pkg/api/errors"
18+ apiResource "k8s.io/apimachinery/pkg/api/resource"
1819 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1920 "k8s.io/apimachinery/pkg/types"
2021 "k8s.io/utils/ptr"
@@ -29,7 +30,7 @@ import (
2930
3031var (
3132 // we are propagating large secrets from hub to member clusters the timeout needs to be large.
32- largeEventuallyDuration = time .Minute * 5
33+ largeEventuallyDuration = time .Second * 15
3334)
3435
3536// Note that this container will run in parallel with other containers.
@@ -1185,6 +1186,138 @@ var _ = Describe("validating CRP when selected resources cross the 1MB limit", O
11851186 })
11861187})
11871188
1189+ // This test verifies that resources are selected and ordered correctly according to the sortResources logic
1190+ var _ = FDescribe ("creating CRP and checking selected resources order" , Ordered , func () {
1191+ crpName := fmt .Sprintf ("test-resource-order-%d" , GinkgoParallelProcess ())
1192+ nsName := fmt .Sprintf ("test-ns-order-%d" , GinkgoParallelProcess ())
1193+ var configMap * corev1.ConfigMap
1194+ var secret * corev1.Secret
1195+ var pvc * corev1.PersistentVolumeClaim
1196+ var role * rbacv1.Role
1197+ BeforeAll (func () {
1198+ By ("creating test resources in specific order for ordering verification" )
1199+ // Create a namespace for our test resources
1200+ ns := & corev1.Namespace {
1201+ ObjectMeta : metav1.ObjectMeta {
1202+ Name : nsName ,
1203+ Labels : map [string ]string {
1204+ "test-label" : "selected" ,
1205+ },
1206+ },
1207+ }
1208+ Expect (hubClient .Create (ctx , ns )).To (Succeed (), "Failed to create test namespace" )
1209+
1210+ // Create ConfigMap
1211+ configMap = & corev1.ConfigMap {
1212+ ObjectMeta : metav1.ObjectMeta {
1213+ Name : fmt .Sprintf ("test-configmap-%d" , GinkgoParallelProcess ()),
1214+ Namespace : nsName ,
1215+ },
1216+ Data : map [string ]string {
1217+ "key1" : "value1" ,
1218+ },
1219+ }
1220+ Expect (hubClient .Create (ctx , configMap )).To (Succeed (), "Failed to create ConfigMap" )
1221+
1222+ // Create Secret
1223+ secret = & corev1.Secret {
1224+ ObjectMeta : metav1.ObjectMeta {
1225+ Name : fmt .Sprintf ("test-secret-%d" , GinkgoParallelProcess ()),
1226+ Namespace : nsName ,
1227+ },
1228+ StringData : map [string ]string {
1229+ "username" : "test-user" ,
1230+ "password" : "test-password" ,
1231+ },
1232+ Type : corev1 .SecretTypeOpaque ,
1233+ }
1234+ Expect (hubClient .Create (ctx , secret )).To (Succeed (), "Failed to create Secret" )
1235+
1236+ // Create PersistentVolumeClaim
1237+ pvc = & corev1.PersistentVolumeClaim {
1238+ ObjectMeta : metav1.ObjectMeta {
1239+ Name : fmt .Sprintf ("test-pvc-%d" , GinkgoParallelProcess ()),
1240+ Namespace : nsName ,
1241+ },
1242+ Spec : corev1.PersistentVolumeClaimSpec {
1243+ AccessModes : []corev1.PersistentVolumeAccessMode {corev1 .ReadWriteOnce },
1244+ StorageClassName : ptr .To ("standard" ),
1245+ Resources : corev1.VolumeResourceRequirements {
1246+ Requests : corev1.ResourceList {
1247+ corev1 .ResourceStorage : apiResource .MustParse ("1Gi" ),
1248+ },
1249+ },
1250+ },
1251+ }
1252+ Expect (hubClient .Create (ctx , pvc )).To (Succeed (), "Failed to create PVC" )
1253+
1254+ // Create Role
1255+ role = & rbacv1.Role {
1256+ ObjectMeta : metav1.ObjectMeta {
1257+ Name : fmt .Sprintf ("test-role-%d" , GinkgoParallelProcess ()),
1258+ Namespace : nsName ,
1259+ },
1260+ Rules : []rbacv1.PolicyRule {
1261+ {
1262+ APIGroups : []string {"" },
1263+ Resources : []string {"configmaps" },
1264+ Verbs : []string {"get" , "list" },
1265+ },
1266+ },
1267+ }
1268+ Expect (hubClient .Create (ctx , role )).To (Succeed (), "Failed to create Role" )
1269+
1270+ // Create the CRP
1271+ crp := & placementv1beta1.ClusterResourcePlacement {
1272+ ObjectMeta : metav1.ObjectMeta {
1273+ Name : crpName ,
1274+ Finalizers : []string {customDeletionBlockerFinalizer },
1275+ },
1276+ Spec : placementv1beta1.ClusterResourcePlacementSpec {
1277+ ResourceSelectors : []placementv1beta1.ClusterResourceSelector {
1278+ {
1279+ Group : "" ,
1280+ Kind : "Namespace" ,
1281+ Version : "v1" ,
1282+ Name : nsName ,
1283+ },
1284+ },
1285+ },
1286+ }
1287+ By (fmt .Sprintf ("creating placement %s" , crpName ))
1288+ Expect (hubClient .Create (ctx , crp )).To (Succeed (), "Failed to create CRP %s" , crpName )
1289+ })
1290+
1291+ AfterAll (func () {
1292+ By (fmt .Sprintf ("garbage collect all things related to placement %s" , crpName ))
1293+ ensureCRPAndRelatedResourcesDeleted (crpName , allMemberClusters )
1294+
1295+ // Delete the namespace which will cascade delete all resources
1296+ ns := & corev1.Namespace {
1297+ ObjectMeta : metav1.ObjectMeta {
1298+ Name : nsName ,
1299+ },
1300+ }
1301+ Expect (client .IgnoreNotFound (hubClient .Delete (ctx , ns ))).To (Succeed (), "Failed to delete test namespace" )
1302+ })
1303+
1304+ It ("should update CRP status with the correct resources and target clusters" , func () {
1305+ // Define the expected resources in order
1306+ expectedResources := []placementv1beta1.ResourceIdentifier {
1307+ {Kind : "Namespace" , Name : nsName , Version : "v1" },
1308+ {Kind : "ConfigMap" , Name : configMap .Name , Namespace : nsName , Version : "v1" },
1309+ {Kind : "Secret" , Name : secret .Name , Namespace : nsName , Version : "v1" },
1310+ {Kind : "PersistentVolumeClaim" , Name : pvc .Name , Namespace : nsName , Version : "v1" },
1311+ {Kind : "Role" , Name : role .Name , Namespace : nsName , Version : "v1" },
1312+ }
1313+
1314+ // Use the common crpStatusUpdatedActual function to verify the status
1315+ crpStatusUpdatedActual := crpStatusUpdatedActual (expectedResources , allMemberClusterNames , nil , "1" )
1316+ Eventually (crpStatusUpdatedActual , eventuallyDuration , eventuallyInterval ).Should (Succeed (),
1317+ "Failed to update CRP %s status as expected" , crpName )
1318+ })
1319+ })
1320+
11881321func createResourcesForMultipleResourceSnapshots () {
11891322 createWorkResources ()
11901323
0 commit comments