From cbf64166015f19f49258cd764b2335054b0b1fb8 Mon Sep 17 00:00:00 2001 From: Rory Z <16801068+Rory-Z@users.noreply.github.com> Date: Mon, 23 Sep 2024 14:23:11 +0800 Subject: [PATCH] feat: use original config to PUT EMQX configs api Signed-off-by: Rory Z <16801068+Rory-Z@users.noreply.github.com> --- controllers/apps/v2beta1/sync_emqx_config.go | 52 ++++++++++--------- .../apps/v2beta1/sync_emqx_config_test.go | 21 +++++--- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/controllers/apps/v2beta1/sync_emqx_config.go b/controllers/apps/v2beta1/sync_emqx_config.go index 5cbcbdc1a..698303e58 100644 --- a/controllers/apps/v2beta1/sync_emqx_config.go +++ b/controllers/apps/v2beta1/sync_emqx_config.go @@ -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" @@ -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{} @@ -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")} } @@ -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"}, @@ -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) } diff --git a/controllers/apps/v2beta1/sync_emqx_config_test.go b/controllers/apps/v2beta1/sync_emqx_config_test.go index 7d639f332..81c15cb98 100644 --- a/controllers/apps/v2beta1/sync_emqx_config_test.go +++ b/controllers/apps/v2beta1/sync_emqx_config_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/rory-z/go-hocon" "github.com/stretchr/testify/assert" ) @@ -11,10 +12,12 @@ 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) { @@ -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")) }) }