@@ -162,6 +162,39 @@ var _ = Describe("Test Work Generator Controller", func() {
162162 }, timeout , interval ).Should (BeTrue (), "Get() member cluster, want not found" )
163163 })
164164
165+ It ("Should not create work when no resources are selected" , func () {
166+ // create master resource snapshot with 0 resources
167+ masterSnapshot := generateResourceSnapshot (1 , 1 , 0 , [][]byte {})
168+ Expect (k8sClient .Create (ctx , masterSnapshot )).Should (Succeed ())
169+ // create a scheduled binding
170+ spec := placementv1beta1.ResourceBindingSpec {
171+ State : placementv1beta1 .BindingStateScheduled ,
172+ ResourceSnapshotName : masterSnapshot .Name ,
173+ TargetCluster : memberClusterName ,
174+ }
175+ createClusterResourceBinding (& binding , spec )
176+ // check the work is not created since the binding state is not bound
177+ work := placementv1beta1.Work {}
178+ Consistently (func () bool {
179+ err := k8sClient .Get (ctx , types.NamespacedName {Name : fmt .Sprintf (placementv1beta1 .FirstWorkNameFmt , testCRPName ), Namespace : memberClusterNamespaceName }, & work )
180+ return errors .IsNotFound (err )
181+ }, duration , interval ).Should (BeTrue (), "controller should not create work in hub cluster until all resources are created" )
182+ // binding should not have any finalizers
183+ Expect (k8sClient .Get (ctx , types.NamespacedName {Name : binding .Name }, binding )).Should (Succeed ())
184+ Expect (len (binding .Finalizers )).Should (Equal (0 ))
185+ // flip the binding state to bound
186+ binding .Spec .State = placementv1beta1 .BindingStateBound
187+ Expect (k8sClient .Update (ctx , binding )).Should (Succeed ())
188+ updateRolloutStartedGeneration (& binding )
189+ // check the work is not created since no resources are selected
190+ Consistently (func () bool {
191+ err := k8sClient .Get (ctx , types.NamespacedName {Name : fmt .Sprintf (placementv1beta1 .FirstWorkNameFmt , testCRPName ), Namespace : memberClusterNamespaceName }, & work )
192+ return errors .IsNotFound (err )
193+ }, duration , interval ).Should (BeTrue (), "controller should not create work in hub cluster until all resources are created" )
194+ // check the binding status is available
195+ verifyBindStatusAvail (binding , false , false )
196+ })
197+
165198 It ("Should not create work for the binding with state scheduled" , func () {
166199 // create master resource snapshot with 2 number of resources
167200 masterSnapshot := generateResourceSnapshot (1 , 1 , 0 , [][]byte {
@@ -4144,6 +4177,10 @@ func generateResourceSnapshot(resourceIndex, numberResource, subIndex int, rawCo
41444177 clusterResourceSnapshot .Annotations [placementv1beta1 .SubindexOfResourceSnapshotAnnotation ] = strconv .Itoa (subIndex )
41454178 }
41464179 clusterResourceSnapshot .Name = snapshotName
4180+ if len (rawContents ) == 0 {
4181+ clusterResourceSnapshot .Spec .SelectedResources = []placementv1beta1.ResourceContent {}
4182+ return clusterResourceSnapshot
4183+ }
41474184 for _ , rawContent := range rawContents {
41484185 clusterResourceSnapshot .Spec .SelectedResources = append (clusterResourceSnapshot .Spec .SelectedResources , placementv1beta1.ResourceContent {
41494186 RawExtension : runtime.RawExtension {Raw : rawContent },
0 commit comments