Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: just check config str #1018

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 1 addition & 33 deletions controllers/apps/v2beta1/sync_emqx_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ func (s *syncConfig) reconcile(ctx context.Context, logger logr.Logger, instance
return subResult{}
}

lastHoconConfig, _ := hocon.ParseString(lastConfigStr)
if !deepEqualHoconValue(hoconConfig.GetRoot(), lastHoconConfig.GetRoot()) {
if lastConfigStr != instance.Spec.Config.Data {
_, coreReady := instance.Status.GetCondition(appsv2beta1.CoreNodesReady)
if coreReady == nil || !instance.Status.IsConditionTrue(appsv2beta1.CoreNodesReady) {
return subResult{}
Expand Down Expand Up @@ -144,34 +143,3 @@ func mergeDefaultConfig(config string) *hocon.Config {
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
}
115 changes: 0 additions & 115 deletions controllers/apps/v2beta1/sync_emqx_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"testing"

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

Expand Down Expand Up @@ -32,117 +31,3 @@ func TestMergeDefaultConfig(t *testing.T) {
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))
})
}
Loading