Skip to content

Commit

Permalink
feat: use original config to PUT EMQX configs api
Browse files Browse the repository at this point in the history
Signed-off-by: Rory Z <[email protected]>
  • Loading branch information
Rory-Z committed Sep 23, 2024
1 parent 90791ce commit cbf6416
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
52 changes: 28 additions & 24 deletions controllers/apps/v2beta1/sync_emqx_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

emperror "emperror.dev/errors"
semver "github.com/Masterminds/semver/v3"
appsv2beta1 "github.com/emqx/emqx-operator/apis/apps/v2beta1"
innerReq "github.com/emqx/emqx-operator/internal/requester"
"github.com/go-logr/logr"
Expand All @@ -23,8 +24,7 @@ type syncConfig struct {
}

func (s *syncConfig) reconcile(ctx context.Context, logger logr.Logger, instance *appsv2beta1.EMQX, r innerReq.RequesterInterface) subResult {
hoconConfig := mergeDefaultConfig(instance.Spec.Config.Data)
confStr := hoconConfig.String()
confStr := mergeDefaultConfig(instance.Spec.Config.Data)

// Make sure the config map exists
configMap := &corev1.ConfigMap{}
Expand Down Expand Up @@ -63,26 +63,31 @@ func (s *syncConfig) reconcile(ctx context.Context, logger logr.Logger, instance
return subResult{}
}

// Delete readonly configs
hoconConfigObj := hoconConfig.GetRoot().(hocon.Object)
if _, ok := hoconConfigObj["node"]; ok {
s.EventRecorder.Event(instance, corev1.EventTypeNormal, "WontUpdateReadOnlyConfig", "Won't update `node` config, because it's readonly config")
delete(hoconConfigObj, "node")
}
if _, ok := hoconConfigObj["cluster"]; ok {
s.EventRecorder.Event(instance, corev1.EventTypeNormal, "WontUpdateReadOnlyConfig", "Won't update `cluster` config, because it's readonly config")
delete(hoconConfigObj, "cluster")
}
if _, ok := hoconConfigObj["dashboard"]; ok {
s.EventRecorder.Event(instance, corev1.EventTypeNormal, "WontUpdateReadOnlyConfig", "Won't update `dashboard` config, because it's readonly config")
delete(hoconConfigObj, "dashboard")
}
if _, ok := hoconConfigObj["rpc"]; ok {
s.EventRecorder.Event(instance, corev1.EventTypeNormal, "WontUpdateReadOnlyConfig", "Won't update `rpc` config, because it's readonly config")
delete(hoconConfigObj, "rpc")
v, _ := semver.NewVersion(instance.Status.CoreNodes[0].Version)
if v.LessThan(semver.MustParse("5.7.0")) {
// Delete readonly configs
hoconConfig, _ := hocon.ParseString(confStr)
hoconConfigObj := hoconConfig.GetRoot().(hocon.Object)
if _, ok := hoconConfigObj["node"]; ok {
s.EventRecorder.Event(instance, corev1.EventTypeNormal, "WontUpdateReadOnlyConfig", "Won't update `node` config, because it's readonly config")
delete(hoconConfigObj, "node")
}
if _, ok := hoconConfigObj["cluster"]; ok {
s.EventRecorder.Event(instance, corev1.EventTypeNormal, "WontUpdateReadOnlyConfig", "Won't update `cluster` config, because it's readonly config")
delete(hoconConfigObj, "cluster")
}
if _, ok := hoconConfigObj["dashboard"]; ok {
s.EventRecorder.Event(instance, corev1.EventTypeNormal, "WontUpdateReadOnlyConfig", "Won't update `dashboard` config, because it's readonly config")
delete(hoconConfigObj, "dashboard")
}
if _, ok := hoconConfigObj["rpc"]; ok {
s.EventRecorder.Event(instance, corev1.EventTypeNormal, "WontUpdateReadOnlyConfig", "Won't update `rpc` config, because it's readonly config")
delete(hoconConfigObj, "rpc")
}
confStr = hoconConfig.String()
}

if err := putEMQXConfigsByAPI(r, instance.Spec.Config.Mode, hoconConfigObj.String()); err != nil {
if err := putEMQXConfigsByAPI(r, instance.Spec.Config.Mode, confStr); err != nil {
return subResult{err: emperror.Wrap(err, "failed to put emqx config")}
}

Expand Down Expand Up @@ -119,7 +124,7 @@ func generateConfigMap(instance *appsv2beta1.EMQX, data string) *corev1.ConfigMa
}

func putEMQXConfigsByAPI(r innerReq.RequesterInterface, mode, config string) error {
url := r.GetURL("api/v5/configs", "mode="+strings.ToLower(mode))
url := r.GetURL("api/v5/configs", "mode="+strings.ToLower(mode), "ignore_readonly=true")

resp, body, err := r.Request("PUT", url, []byte(config), http.Header{
"Content-Type": []string{"text/plain"},
Expand All @@ -133,13 +138,12 @@ func putEMQXConfigsByAPI(r innerReq.RequesterInterface, mode, config string) err
return nil
}

func mergeDefaultConfig(config string) *hocon.Config {
func mergeDefaultConfig(config string) string {
defaultListenerConfig := ""
defaultListenerConfig += fmt.Sprintln("listeners.tcp.default.bind = 1883")
defaultListenerConfig += fmt.Sprintln("listeners.ssl.default.bind = 8883")
defaultListenerConfig += fmt.Sprintln("listeners.ws.default.bind = 8083")
defaultListenerConfig += fmt.Sprintln("listeners.wss.default.bind = 8084")

hoconConfig, _ := hocon.ParseString(defaultListenerConfig + config)
return hoconConfig
return fmt.Sprintf("%s\n%s", defaultListenerConfig, config)
}
21 changes: 13 additions & 8 deletions controllers/apps/v2beta1/sync_emqx_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import (
"fmt"
"testing"

"github.com/rory-z/go-hocon"
"github.com/stretchr/testify/assert"
)

func TestMergeDefaultConfig(t *testing.T) {
t.Run("case1", func(t *testing.T) {
config := ""
got := mergeDefaultConfig(config)
assert.Equal(t, "1883", got.GetString("listeners.tcp.default.bind"))
assert.Equal(t, "8883", got.GetString("listeners.ssl.default.bind"))
assert.Equal(t, "8083", got.GetString("listeners.ws.default.bind"))
assert.Equal(t, "8084", got.GetString("listeners.wss.default.bind"))
hoconConfig, err := hocon.ParseString(got)
assert.Nil(t, err)
assert.Equal(t, "1883", hoconConfig.GetString("listeners.tcp.default.bind"))
assert.Equal(t, "8883", hoconConfig.GetString("listeners.ssl.default.bind"))
assert.Equal(t, "8083", hoconConfig.GetString("listeners.ws.default.bind"))
assert.Equal(t, "8084", hoconConfig.GetString("listeners.wss.default.bind"))
})

t.Run("case2", func(t *testing.T) {
Expand All @@ -25,9 +28,11 @@ func TestMergeDefaultConfig(t *testing.T) {
config += fmt.Sprintln("listeners.wss.default.bind = 38084")

got := mergeDefaultConfig(config)
assert.Equal(t, "31883", got.GetString("listeners.tcp.default.bind"))
assert.Equal(t, "38883", got.GetString("listeners.ssl.default.bind"))
assert.Equal(t, "38083", got.GetString("listeners.ws.default.bind"))
assert.Equal(t, "38084", got.GetString("listeners.wss.default.bind"))
hoconConfig, err := hocon.ParseString(got)
assert.Nil(t, err)
assert.Equal(t, "31883", hoconConfig.GetString("listeners.tcp.default.bind"))
assert.Equal(t, "38883", hoconConfig.GetString("listeners.ssl.default.bind"))
assert.Equal(t, "38083", hoconConfig.GetString("listeners.ws.default.bind"))
assert.Equal(t, "38084", hoconConfig.GetString("listeners.wss.default.bind"))
})
}

0 comments on commit cbf6416

Please sign in to comment.