Skip to content

Commit

Permalink
fix: fix config map always update
Browse files Browse the repository at this point in the history
Signed-off-by: Rory Z <[email protected]>
  • Loading branch information
Rory-Z committed Jan 9, 2024
1 parent c3cc0d5 commit 6832900
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 13 deletions.
56 changes: 43 additions & 13 deletions controllers/apps/v2beta1/sync_emqx_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"reflect"
"strings"

emperror "emperror.dev/errors"
Expand Down Expand Up @@ -33,7 +32,7 @@ func (s *syncConfig) reconcile(ctx context.Context, logger logr.Logger, instance
}

lastHoconConfig, _ := hocon.ParseString(lastConfigStr)
if !reflect.DeepEqual(hoconConfig, lastHoconConfig) {
if !deepEqualHoconValue(hoconConfig.GetRoot(), lastHoconConfig.GetRoot()) {
_, coreReady := instance.Status.GetCondition(appsv2beta1.CoreNodesReady)
if coreReady == nil || !instance.Status.IsConditionTrue(appsv2beta1.CoreNodesReady) {
return subResult{}
Expand Down Expand Up @@ -76,17 +75,6 @@ func (s *syncConfig) update(ctx context.Context, logger logr.Logger, instance *a
return nil
}

func mergeDefaultConfig(config string) *hocon.Config {
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
}

func generateConfigMap(instance *appsv2beta1.EMQX, data string) *corev1.ConfigMap {
return &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Expand Down Expand Up @@ -118,3 +106,45 @@ func putEMQXConfigsByAPI(r innerReq.RequesterInterface, mode, config string) err
}
return nil
}

func mergeDefaultConfig(config string) *hocon.Config {
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
}

func deepEqualHoconValue(val1, val2 hocon.Value) bool {
switch val1.Type() {
case hocon.ObjectType:
if len(val1.(hocon.Object)) != len(val2.(hocon.Object)) {
return false
}
for key := range val1.(hocon.Object) {
if _, ok := val2.(hocon.Object)[key]; !ok {
return false
}
if !deepEqualHoconValue(val1.(hocon.Object)[key], val2.(hocon.Object)[key]) {
return false
}
}
case hocon.ArrayType:
if len(val1.(hocon.Array)) != len(val2.(hocon.Array)) {
return false
}
for i := range val1.(hocon.Array) {
if !deepEqualHoconValue(val1.(hocon.Array)[i], val2.(hocon.Array)[i]) {
return false
}
}
case hocon.StringType, hocon.NumberType, hocon.BooleanType, hocon.NullType:
return val1.String() == val2.String()
default:
return false
}
return true
}
148 changes: 148 additions & 0 deletions controllers/apps/v2beta1/sync_emqx_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package v2beta1

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"))
})

t.Run("case2", func(t *testing.T) {
config := ""
config += fmt.Sprintln("listeners.tcp.default.bind = 31883")
config += fmt.Sprintln("listeners.ssl.default.bind = 38883")
config += fmt.Sprintln("listeners.ws.default.bind = 38083")
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"))
})
}

func TestDeepEqualHoconValue(t *testing.T) {
t.Run("case1", func(t *testing.T) {
v1 := hocon.String("a")
v2 := hocon.String("a")
assert.True(t, deepEqualHoconValue(v1, v2))
})

t.Run("case2", func(t *testing.T) {
v1 := hocon.String("a")
v2 := hocon.String("b")
assert.False(t, deepEqualHoconValue(v1, v2))
})

t.Run("case3", func(t *testing.T) {
v1 := hocon.Int(1)
v2 := hocon.Int(1)
assert.True(t, deepEqualHoconValue(v1, v2))
})

t.Run("case4", func(t *testing.T) {
v1 := hocon.Int(1)
v2 := hocon.Int(2)
assert.False(t, deepEqualHoconValue(v1, v2))
})

t.Run("case5", func(t *testing.T) {
v1 := hocon.Boolean(true)
v2 := hocon.Boolean(true)
assert.True(t, deepEqualHoconValue(v1, v2))
})

t.Run("case6", func(t *testing.T) {
v1 := hocon.Boolean(true)
v2 := hocon.Boolean(false)
assert.False(t, deepEqualHoconValue(v1, v2))
})

t.Run("case7", func(t *testing.T) {
v1 := hocon.Null("null")
v2 := hocon.Null("null")
assert.True(t, deepEqualHoconValue(v1, v2))
})

t.Run("case8", func(t *testing.T) {
v1 := hocon.Null("fake")
v2 := hocon.Null("")
assert.True(t, deepEqualHoconValue(v1, v2))
})

t.Run("case9", func(t *testing.T) {
v1 := hocon.Array{hocon.String("a"), hocon.String("b")}
v2 := hocon.Array{hocon.String("a"), hocon.String("b")}
assert.True(t, deepEqualHoconValue(v1, v2))
})

t.Run("case10", func(t *testing.T) {
v1 := hocon.Array{hocon.String("a"), hocon.String("b")}
v2 := hocon.Array{hocon.String("a"), hocon.String("c")}
assert.False(t, deepEqualHoconValue(v1, v2))
})

t.Run("case11", func(t *testing.T) {
v1 := hocon.Array{hocon.String("a"), hocon.String("b")}
v2 := hocon.Array{hocon.String("a"), hocon.String("b"), hocon.String("c")}
assert.False(t, deepEqualHoconValue(v1, v2))
})

t.Run("case12", func(t *testing.T) {
v1 := hocon.Object{"a": hocon.Int(1), "b": hocon.Int(2)}
v2 := hocon.Object{"b": hocon.Int(2), "a": hocon.Int(1)}
assert.True(t, deepEqualHoconValue(v1, v2))
})

t.Run("case13", func(t *testing.T) {
v1 := hocon.Object{"a": hocon.Int(1), "b": hocon.Int(2)}
v2 := hocon.Object{"a": hocon.Int(1), "c": hocon.Int(3)}
assert.False(t, deepEqualHoconValue(v1, v2))
})

t.Run("case13", func(t *testing.T) {
v1 := hocon.Object{"a": hocon.Int(1), "b": hocon.Int(2)}
v2 := hocon.Object{"a": hocon.Int(1), "b": hocon.Int(2), "c": hocon.Int(3)}
assert.False(t, deepEqualHoconValue(v1, v2))
})

t.Run("case14", func(t *testing.T) {
v1 := hocon.Object{
"a": hocon.String("a1"),
"b": hocon.Object{"b1": hocon.String("b1"), "b2": hocon.String("b2")},
"c": hocon.Array{hocon.String("c1"), hocon.String("c2")},
}
v2 := hocon.Object{
"c": hocon.Array{hocon.String("c1"), hocon.String("c2")},
"b": hocon.Object{"b2": hocon.String("b2"), "b1": hocon.String("b1")},
"a": hocon.String("a1"),
}
assert.True(t, deepEqualHoconValue(v1, v2))
})

t.Run("case15", func(t *testing.T) {
v1 := hocon.Object{
"a": hocon.String("a1"),
"b": hocon.Object{"b1": hocon.String("b1"), "b2": hocon.String("b2")},
"c": hocon.Array{hocon.String("c1"), hocon.String("c2")},
}
v2 := hocon.Object{
"c": hocon.Array{hocon.String("c2"), hocon.String("c1")},
"b": hocon.Object{"b2": hocon.String("b2"), "b1": hocon.String("b1")},
"a": hocon.String("a1"),
}
assert.False(t, deepEqualHoconValue(v1, v2))
})
}

0 comments on commit 6832900

Please sign in to comment.