Skip to content

Commit 51cb5e7

Browse files
committed
skip creation of empty work object
Signed-off-by: Britania Rodriguez Reyes <britaniar@microsoft.com>
1 parent d92c2d5 commit 51cb5e7

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

pkg/controllers/workgenerator/controller.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,13 +520,11 @@ func (r *Reconciler) syncAllWork(ctx context.Context, resourceBinding *fleetv1be
520520
}
521521
if len(simpleManifests) == 0 {
522522
klog.V(2).InfoS("the snapshot contains no resource to apply either because of override or enveloped resources", "snapshot", klog.KObj(snapshot))
523+
} else {
524+
work := generateSnapshotWorkObj(workNamePrefix, resourceBinding, snapshot, simpleManifests, resourceOverrideSnapshotHash, clusterResourceOverrideSnapshotHash)
525+
activeWork[work.Name] = work
526+
newWork = append(newWork, work)
523527
}
524-
// generate a work object for the manifests even if there is nothing to place
525-
// to allow CRP to collect the status of the placement
526-
// TODO (RZ): revisit to see if we need this hack
527-
work := generateSnapshotWorkObj(workNamePrefix, resourceBinding, snapshot, simpleManifests, resourceOverrideSnapshotHash, clusterResourceOverrideSnapshotHash)
528-
activeWork[work.Name] = work
529-
newWork = append(newWork, work)
530528

531529
// issue all the create/update requests for the corresponding works for each snapshot in parallel
532530
for ni := range newWork {

pkg/controllers/workgenerator/controller_integration_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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{
@@ -4143,6 +4176,10 @@ func generateResourceSnapshot(resourceIndex, numberResource, subIndex int, rawCo
41434176
clusterResourceSnapshot.Annotations[placementv1beta1.SubindexOfResourceSnapshotAnnotation] = strconv.Itoa(subIndex)
41444177
}
41454178
clusterResourceSnapshot.Name = snapshotName
4179+
if len(rawContents) == 0 {
4180+
clusterResourceSnapshot.Spec.SelectedResources = []placementv1beta1.ResourceContent{}
4181+
return clusterResourceSnapshot
4182+
}
41464183
for _, rawContent := range rawContents {
41474184
clusterResourceSnapshot.Spec.SelectedResources = append(clusterResourceSnapshot.Spec.SelectedResources, placementv1beta1.ResourceContent{
41484185
RawExtension: runtime.RawExtension{Raw: rawContent},

0 commit comments

Comments
 (0)