Skip to content

Commit

Permalink
Handle value removal using null val
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <tamal@appscode.com>
  • Loading branch information
tamalsaha committed Apr 27, 2024
1 parent 6c466a6 commit 473234b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/values/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func getValuesDiff(original, modified map[string]any, prefix string, diff map[st
diff[k] = d2
}
case []any, string, int8, uint8, int16, uint16, int32, uint32, int64, uint64, int, uint, float32, float64, bool, json.Number, nil:
if !reflect.DeepEqual(original[k], val) {
if origVal, ok := original[k]; !ok || !reflect.DeepEqual(origVal, val) {
diff[k] = val
}
default:
Expand Down Expand Up @@ -184,7 +184,7 @@ func getChangedValues(original, modified map[string]any, prefix string, cmds []s
cmds = append(cmds, fmt.Sprintf("%s=%v", curKey, val))
}
case nil:
if !reflect.DeepEqual(original[k], val) {
if origVal, ok := original[k]; !ok || !reflect.DeepEqual(origVal, val) {
cmds = append(cmds, fmt.Sprintf("%s=null", curKey))
}
default:
Expand Down
63 changes: 63 additions & 0 deletions pkg/values/diff_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package values

import (
"reflect"
"testing"

"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -183,6 +184,23 @@ func TestGetChangedValues(t *testing.T) {
`nodeSelector.'kubernetes\.io/role'=master`,
},
},
{
name: "delete values",
args: args{
original: map[string]any{
"annotations": map[string]any{},
},
modified: map[string]any{
"annotations": map[string]any{
"helm.sh/hook": nil,
},
},
},
want: []string{
`annotations.'helm\.sh/hook'=null`,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -197,3 +215,48 @@ func TestGetChangedValues(t *testing.T) {
})
}
}

func TestGetValuesMapDiff(t *testing.T) {
type args struct {
original map[string]any
modified map[string]any
}
tests := []struct {
name string
args args
want map[string]any
wantErr bool
}{
{
name: "delete values",
args: args{
original: map[string]any{
"annotations": map[string]any{},
},
modified: map[string]any{
"annotations": map[string]any{
"helm.sh/hook": nil,
},
},
},
want: map[string]any{
"annotations": map[string]any{
"helm.sh/hook": nil,
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetValuesMapDiff(tt.args.original, tt.args.modified)
if (err != nil) != tt.wantErr {
t.Errorf("GetValuesMapDiff() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetValuesMapDiff() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 473234b

Please sign in to comment.