Skip to content

Commit

Permalink
fix(v2beta1): fix rebalance error
Browse files Browse the repository at this point in the history
  • Loading branch information
Rory-Z committed Jul 30, 2023
1 parent f143309 commit 2727b6b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
7 changes: 6 additions & 1 deletion controllers/apps/v2beta1/rebalance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,12 @@ func (r *RebalanceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

// check if emqx is enterprise edition
if emqx.Status.CoreNodes[0].Edition != "Enterprise" {
return ctrl.Result{}, emperror.New("only support enterprise edition")
_ = rebalance.Status.SetFailed(appsv2beta1.RebalanceCondition{
Type: appsv2beta1.RebalanceConditionFailed,
Status: corev1.ConditionTrue,
Message: "Only enterprise edition can be rebalanced",
})
return ctrl.Result{}, r.Client.Status().Update(ctx, rebalance)
}

requester, err = newRequester(r.Client, emqx)
Expand Down
82 changes: 80 additions & 2 deletions e2e/v2beta1/e2e_rebalance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var rebalance = appsv2beta1.Rebalance{
},
}

var _ = Describe("EMQX 5 Rebalance Test", func() {
var _ = Describe("EMQX 5 Rebalance Test", Label("rebalance"), func() {
var instance *appsv2beta1.EMQX
var r *appsv2beta1.Rebalance
BeforeEach(func() {
Expand Down Expand Up @@ -108,6 +108,84 @@ var _ = Describe("EMQX 5 Rebalance Test", func() {
})
})

Context("EMQX is not enterprise", func() {
BeforeEach(func() {
instance.Spec.Image = "emqx/emqx:5.1"
r = rebalance.DeepCopy()
r.Namespace = instance.GetNamespace()
r.Spec.InstanceKind = instance.GroupVersionKind().Kind
r.Spec.InstanceName = instance.Name

By("Creating namespace", func() {
// create namespace
Eventually(func() bool {
err := k8sClient.Create(context.TODO(), &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: instance.GetNamespace(),
Labels: map[string]string{
"test": "e2e",
},
},
})
return err == nil || k8sErrors.IsAlreadyExists(err)
}).Should(BeTrue())
})

By("Creating EMQX CR", func() {
// create EMQX CR
instance.Spec.ReplicantTemplate = nil
instance.Spec.CoreTemplate.Spec.Replicas = pointer.Int32Ptr(2)
instance.Default()
Expect(instance.ValidateCreate()).Should(Succeed())
Expect(k8sClient.Create(context.TODO(), instance)).Should(Succeed())

// check EMQX CR if created successfully
Eventually(func() *appsv2beta1.EMQX {
_ = k8sClient.Get(context.TODO(), client.ObjectKeyFromObject(instance), instance)
return instance
}).WithTimeout(timeout).WithPolling(interval).Should(
WithTransform(func(instance *appsv2beta1.EMQX) bool {
return instance.Status.IsConditionTrue(appsv2beta1.Ready)
}, BeTrue()),
)
})

By("Creating Rebalance CR", func() {
Expect(k8sClient.Create(context.TODO(), r)).Should(Succeed())
})
})

AfterEach(func() {
By("Deleting Rebalance CR, can be successful", func() {
Eventually(func() error {
return k8sClient.Delete(context.TODO(), r)
}, timeout, interval).Should(Succeed())
Eventually(func() bool {
return k8sErrors.IsNotFound(k8sClient.Get(context.TODO(), client.ObjectKeyFromObject(r), r))
}).Should(BeTrue())
})
})

It("Rebalance will failed, because the EMQX is not enterprise", func() {
Eventually(func() appsv2beta1.RebalanceStatus {
_ = k8sClient.Get(context.TODO(), client.ObjectKeyFromObject(r), r)
return r.Status
}, timeout, interval).Should(And(
HaveField("Phase", appsv2beta1.RebalancePhaseFailed),
HaveField("RebalanceStates", BeNil()),
HaveField("Conditions", ContainElements(
HaveField("Type", appsv2beta1.RebalanceConditionFailed),
)),
HaveField("Conditions", ContainElements(
HaveField("Status", corev1.ConditionTrue),
)),
HaveField("Conditions", ContainElements(
HaveField("Message", ContainSubstring("Only enterprise edition can be rebalanced"))),
),
))
})
})

Context("EMQX is exist", func() {
BeforeEach(func() {
r = rebalance.DeepCopy()
Expand Down Expand Up @@ -233,7 +311,7 @@ var _ = Describe("EMQX 5 Rebalance Test", func() {
})
})

var _ = Describe("EMQX 4 Rebalance Test", Label("rebalance"), func() {
var _ = Describe("EMQX 4 Rebalance Test", func() {
var instance *appsv1beta4.EmqxEnterprise
var r *appsv2beta1.Rebalance
BeforeEach(func() {
Expand Down

0 comments on commit 2727b6b

Please sign in to comment.