Skip to content

Commit

Permalink
handle multiple CRB
Browse files Browse the repository at this point in the history
  • Loading branch information
Arvindthiru committed Nov 13, 2024
1 parent 8c739d6 commit e7acdc2
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 7 deletions.
17 changes: 12 additions & 5 deletions pkg/controllers/clusterresourceplacementeviction/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ const (
reasonClusterResourcePlacementEvictionExecuted = "ClusterResourcePlacementEvictionExecuted"
reasonClusterResourcePlacementEvictionNotExecuted = "ClusterResourcePlacementEvictionNotExecuted"

evictionInvalidMissingCRP = "Failed to find cluster resource placement targeted by eviction"
evictionInvalidMissingCRB = "Failed to find cluster resource binding for cluster targeted by eviction"
evictionValid = "Eviction is valid"
evictionAllowedNoPDB = "Eviction Allowed, no ClusterResourcePlacementDisruptionBudget specified"
evictionInvalidMissingCRP = "Failed to find cluster resource placement targeted by eviction"
evictionInvalidMissingCRB = "Failed to find cluster resource binding for cluster targeted by eviction"
evictionInvalidMultipleCRB = "Found more than one ClusterResourceBinding for cluster targeted by eviction"
evictionValid = "Eviction is valid"
evictionAllowedNoPDB = "Eviction Allowed, no ClusterResourcePlacementDisruptionBudget specified"

evictionAllowedPDBSpecified = "Eviction is allowed by specified ClusterResourcePlacementDisruptionBudget, disruptionsAllowed: %d, availableBindings: %d, desiredBindings: %d, totalBindings: %d"
evictionBlockedPDBSpecified = "Eviction is blocked by specified ClusterResourcePlacementDisruptionBudget, disruptionsAllowed: %d, availableBindings: %d, desiredBindings: %d, totalBindings: %d"
Expand Down Expand Up @@ -95,7 +96,13 @@ func (r *Reconciler) Reconcile(ctx context.Context, req runtime.Request) (runtim
var evictionTargetBinding *placementv1beta1.ClusterResourceBinding
for i := range crbList.Items {
if crbList.Items[i].Spec.TargetCluster == eviction.Spec.ClusterName {
evictionTargetBinding = &crbList.Items[i]
if evictionTargetBinding == nil {
evictionTargetBinding = &crbList.Items[i]
} else {
klog.V(2).InfoS(evictionInvalidMultipleCRB, "clusterResourcePlacementEviction", evictionName, "clusterResourcePlacement", eviction.Spec.PlacementName)
markEvictionInvalid(&eviction, evictionInvalidMultipleCRB)
return runtime.Result{}, r.updateEvictionStatus(ctx, &eviction)
}
}
}
if evictionTargetBinding == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
const (
eventuallyDuration = time.Minute * 2
eventuallyInterval = time.Millisecond * 250
consistentlyDuration = time.Second * 15
consistentlyDuration = time.Second * 10
consistentlyInterval = time.Millisecond * 500
)

Expand Down Expand Up @@ -82,7 +82,6 @@ var _ = Describe("Test ClusterResourcePlacementEviction Controller", Ordered, fu
It("Create ClusterResourcePlacementEviction", func() {
eviction := buildTestEviction(evictionName, crpName, "test-cluster")
Expect(k8sClient.Create(ctx, &eviction)).Should(Succeed())

})

It("Check eviction status", func() {
Expand All @@ -96,6 +95,93 @@ var _ = Describe("Test ClusterResourcePlacementEviction Controller", Ordered, fu
})
})

Context("Invalid Eviction - Multiple ClusterResourceBindings for one cluster", func() {
crpName := fmt.Sprintf(crpNameTemplate, GinkgoParallelProcess())
evictionName := fmt.Sprintf(evictionNameTemplate, GinkgoParallelProcess())
crbName := fmt.Sprintf(crbNameTemplate, GinkgoParallelProcess())
anotherCRBName := fmt.Sprintf("another-crb-%d", GinkgoParallelProcess())

It("Create ClusterResourcePlacement", func() {
crp := buildTestCRP(crpName)
Expect(k8sClient.Create(ctx, &crp)).Should(Succeed())
// ensure CRP exists.
Eventually(func() error {
return k8sClient.Get(ctx, types.NamespacedName{Name: crp.Name}, &crp)
}, eventuallyDuration, eventuallyInterval).Should(Succeed())
})

It("Create ClusterResourceBinding", func() {
// Create CRB.
crb := placementv1beta1.ClusterResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: crbName,
Labels: map[string]string{placementv1beta1.CRPTrackingLabel: crpName},
},
Spec: placementv1beta1.ResourceBindingSpec{
State: placementv1beta1.BindingStateScheduled,
ResourceSnapshotName: "test-resource-snapshot",
SchedulingPolicySnapshotName: "test-scheduling-policy-snapshot",
TargetCluster: "test-cluster",
},
}
Expect(k8sClient.Create(ctx, &crb)).Should(Succeed())
// ensure CRB exists.
Eventually(func() error {
return k8sClient.Get(ctx, types.NamespacedName{Name: crb.Name}, &crb)
}, eventuallyDuration, eventuallyInterval).Should(Succeed())
})

It("Create another ClusterResourceBinding", func() {
// Create anotherCRB.
anotherCRB := placementv1beta1.ClusterResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: anotherCRBName,
Labels: map[string]string{placementv1beta1.CRPTrackingLabel: crpName},
},
Spec: placementv1beta1.ResourceBindingSpec{
State: placementv1beta1.BindingStateScheduled,
ResourceSnapshotName: "test-resource-snapshot",
SchedulingPolicySnapshotName: "test-scheduling-policy-snapshot",
TargetCluster: "test-cluster",
},
}
Expect(k8sClient.Create(ctx, &anotherCRB)).Should(Succeed())
// ensure another CRB exists.
Eventually(func() error {
return k8sClient.Get(ctx, types.NamespacedName{Name: anotherCRB.Name}, &anotherCRB)
}, eventuallyDuration, eventuallyInterval).Should(Succeed())
})

It("Create ClusterResourcePlacementEviction", func() {
eviction := buildTestEviction(evictionName, crpName, "test-cluster")
Expect(k8sClient.Create(ctx, &eviction)).Should(Succeed())
})

It("Check eviction status", func() {
evictionStatusUpdatedActual := evictionStatusUpdatedActual(&isValidEviction{bool: false, msg: evictionInvalidMultipleCRB}, nil)
Eventually(evictionStatusUpdatedActual, eventuallyDuration, eventuallyInterval).Should(Succeed())
})

It("Ensure eviction was not successful", func() {
var crb, anotherCRB placementv1beta1.ClusterResourceBinding
// check to see CRB was not deleted.
Consistently(func() bool {
return !k8serrors.IsNotFound(k8sClient.Get(ctx, types.NamespacedName{Name: crbName}, &crb))
}, consistentlyDuration, consistentlyInterval).Should(BeTrue())
// check to see another CRB was not deleted.
Consistently(func() bool {
return !k8serrors.IsNotFound(k8sClient.Get(ctx, types.NamespacedName{Name: anotherCRBName}, &anotherCRB))
}, consistentlyDuration, consistentlyInterval).Should(BeTrue())
})

It("Clean up resources", func() {
ensureEvictionRemoved(evictionName)
ensureCRBRemoved(anotherCRBName)
ensureCRBRemoved(crbName)
ensureCRPRemoved(crpName)
})
})

Context("Eviction Allowed - ClusterResourcePlacementDisruptionBudget is not present", func() {
evictionName := fmt.Sprintf(evictionNameTemplate, GinkgoParallelProcess())
crpName := fmt.Sprintf(crpNameTemplate, GinkgoParallelProcess())
Expand Down

0 comments on commit e7acdc2

Please sign in to comment.