Skip to content

Commit

Permalink
enhancement: ignore field support slice type (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
howieyuen committed Feb 13, 2023
1 parent 32c3547 commit 7a6e482
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
25 changes: 21 additions & 4 deletions pkg/engine/operation/graph/resource_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"reflect"
"strings"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"kusionstack.io/kusion/pkg/engine/models"
opsmodels "kusionstack.io/kusion/pkg/engine/operation/models"
"kusionstack.io/kusion/pkg/engine/runtime"
Expand Down Expand Up @@ -108,8 +106,8 @@ func (rn *ResourceNode) Execute(operation *opsmodels.Operation) status.Status {
// Ignore differences of target fields
for _, field := range operation.IgnoreFields {
splits := strings.Split(field, ".")
unstructured.RemoveNestedField(liveState.Attributes, splits...)
unstructured.RemoveNestedField(predictableState.Attributes, splits...)
removeNestedField(liveState.Attributes, splits...)
removeNestedField(predictableState.Attributes, splits...)
}
report, err := diff.ToReport(liveState, predictableState)
if err != nil {
Expand Down Expand Up @@ -140,6 +138,25 @@ func (rn *ResourceNode) Execute(operation *opsmodels.Operation) status.Status {
return nil
}

func removeNestedField(obj interface{}, fields ...string) {
m := obj
switch next := m.(type) {
case map[string]interface{}:
if len(fields) == 1 {
delete(next, fields[0])
return
} else {
removeNestedField(next[fields[0]], fields[1:]...)
}
case []interface{}:
for _, n := range next {
removeNestedField(n, fields...)
}
default:
return
}
}

func (rn *ResourceNode) applyResource(operation *opsmodels.Operation, priorState, planedState, live *models.Resource) status.Status {
log.Infof("operation:%v, prior:%v, plan:%v, live:%v", rn.Action, jsonutil.Marshal2String(priorState),
jsonutil.Marshal2String(planedState), jsonutil.Marshal2String(live))
Expand Down
72 changes: 72 additions & 0 deletions pkg/engine/operation/graph/resource_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,75 @@ func TestResourceNode_Execute(t *testing.T) {
})
}
}

func Test_removeNestedField(t *testing.T) {
t.Run("remove nested field", func(t *testing.T) {
e1 := []interface{}{
map[string]interface{}{"f": "f1", "g": "g1"},
}
e2 := []interface{}{
map[string]interface{}{"f": "f2", "g": "g2"},
}

c := []interface{}{
map[string]interface{}{"d": "d1", "e": e1},
map[string]interface{}{"d": "d2", "e": e2},
}

a := map[string]interface{}{
"b": 1,
"c": c,
}

obj := map[string]interface{}{
"a": a,
}

removeNestedField(obj, "a", "c", "e", "f")
assert.Len(t, e1[0], 1)
assert.Len(t, e2[0], 1)

removeNestedField(obj, "a", "c", "e", "g")
assert.Empty(t, e1[0])
assert.Empty(t, e2[0])

removeNestedField(obj, "a", "c", "e")
assert.Len(t, c[0], 1)
assert.Len(t, c[1], 1)

removeNestedField(obj, "a", "c", "d")
assert.Len(t, c[0], 0)
assert.Len(t, c[1], 0)

removeNestedField(obj, "a", "c")
assert.Len(t, a, 1)

removeNestedField(obj, "a", "b")
assert.Len(t, a, 0)

removeNestedField(obj, "a")
assert.Empty(t, obj)
})

t.Run("remove spec.ports.targetPort", func(t *testing.T) {
ports := []interface{}{
map[string]interface{}{
"port": 80,
"protocol": "TCP",
"targetPort": 80,
},
}

spec := map[string]interface{}{
"clusterIP": "172.16.128.40",
"ports": ports,
}

obj := map[string]interface{}{
"spec": spec,
}

removeNestedField(obj, "spec", "ports", "targetPort")
assert.Len(t, ports[0], 2)
})
}

0 comments on commit 7a6e482

Please sign in to comment.