Skip to content

Commit

Permalink
chore(v2alpha2): the number or core node must be greater than 1 and l…
Browse files Browse the repository at this point in the history
…ess than or equal to 4
  • Loading branch information
Rory-Z committed Jun 20, 2023
1 parent c28e165 commit 3c5be2e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
16 changes: 14 additions & 2 deletions apis/apps/v2alpha2/emqx_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,18 @@ var _ webhook.Validator = &EMQX{}
func (r *EMQX) ValidateCreate() error {
emqxlog.Info("validate create", "name", r.Name)

if *r.Spec.CoreTemplate.Spec.Replicas < 2 {
if *r.Spec.CoreTemplate.Spec.Replicas <= 1 {
err := emperror.New("the number of EMQX core nodes must be greater than 1")
emqxlog.Error(err, "validate create failed")
return err
}

if *r.Spec.CoreTemplate.Spec.Replicas > 4 {
err := emperror.New("the number of EMQX core nodes must be less than or equal to 4")
emqxlog.Error(err, "validate create failed")
return err
}

if _, err := hocon.ParseString(r.Spec.BootstrapConfig); err != nil {
err = emperror.Wrap(err, "failed to parse bootstrap config")
emqxlog.Error(err, "validate create failed")
Expand All @@ -87,12 +93,18 @@ func (r *EMQX) ValidateCreate() error {
func (r *EMQX) ValidateUpdate(old runtime.Object) error {
emqxlog.Info("validate update", "name", r.Name)

if *r.Spec.CoreTemplate.Spec.Replicas < 2 {
if *r.Spec.CoreTemplate.Spec.Replicas <= 1 {
err := emperror.New("the number of EMQX core nodes must be greater than 1")
emqxlog.Error(err, "validate update failed")
return err
}

if *r.Spec.CoreTemplate.Spec.Replicas > 4 {
err := emperror.New("the number of EMQX core nodes must be less than or equal to 4")
emqxlog.Error(err, "validate create failed")
return err
}

oldEMQX := old.(*EMQX)
if !reflect.DeepEqual(oldEMQX.Spec.BootstrapAPIKeys, r.Spec.BootstrapAPIKeys) {
err := emperror.Errorf("bootstrap APIKey cannot be updated")
Expand Down
9 changes: 9 additions & 0 deletions apis/apps/v2alpha2/emqx_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func TestValidateCreate(t *testing.T) {
instance.Spec.CoreTemplate.Spec.Replicas = pointer.Int32Ptr(1)
assert.Error(t, instance.ValidateCreate(), "the number of EMQX core nodes must be greater than 1")

instance.Spec.CoreTemplate.Spec.Replicas = pointer.Int32Ptr(5)
assert.Error(t, instance.ValidateCreate(), "the number of EMQX core nodes must be less than or equal to 4")

instance.Spec.CoreTemplate.Spec.Replicas = pointer.Int32Ptr(2)
assert.Nil(t, instance.ValidateCreate())

Expand Down Expand Up @@ -76,6 +79,12 @@ func TestValidateUpdate(t *testing.T) {
assert.Error(t, new.ValidateUpdate(instance), "the number of EMQX core nodes must be greater than 1")
})

t.Run("should return error if core nodes is greater then 4", func(t *testing.T) {
new := instance.DeepCopy()
new.Spec.CoreTemplate.Spec.Replicas = pointer.Int32Ptr(5)
assert.Error(t, new.ValidateUpdate(instance), "the number of EMQX core nodes must be less than or equal to 4")
})

t.Run("should return error if bootstrap config is invalid", func(t *testing.T) {
new := instance.DeepCopy()
new.Spec.BootstrapConfig = "hello world"
Expand Down

0 comments on commit 3c5be2e

Please sign in to comment.