diff --git a/pkg/collector/corechecks/cluster/helm/map_utils.go b/pkg/collector/corechecks/cluster/helm/map_utils.go index d60e4b69098d1f..813b6e18aef7eb 100644 --- a/pkg/collector/corechecks/cluster/helm/map_utils.go +++ b/pkg/collector/corechecks/cluster/helm/map_utils.go @@ -31,11 +31,15 @@ import ( // "agents.image.tag" and "agents.image.pullPolicy" are valid keys, but // "agents.image" is not. func getValue(m map[string]interface{}, dotSeparatedKey string) (string, error) { + if dotSeparatedKey == "" { + return "", fmt.Errorf("not found") + } + keys := strings.Split(dotSeparatedKey, ".") var obj interface{} = m for _, key := range keys { - if reflect.TypeOf(obj).Kind() != reflect.Map { + if obj == nil || reflect.TypeOf(obj).Kind() != reflect.Map { return "", fmt.Errorf("not found") } @@ -50,7 +54,7 @@ func getValue(m map[string]interface{}, dotSeparatedKey string) (string, error) obj = objValue.Interface() } - if reflect.TypeOf(obj).Kind() == reflect.Map { + if obj == nil || reflect.TypeOf(obj).Kind() == reflect.Map { return "", fmt.Errorf("not found") } diff --git a/pkg/collector/corechecks/cluster/helm/map_utils_test.go b/pkg/collector/corechecks/cluster/helm/map_utils_test.go index 5ed3f3138ea5f9..34355df7486321 100644 --- a/pkg/collector/corechecks/cluster/helm/map_utils_test.go +++ b/pkg/collector/corechecks/cluster/helm/map_utils_test.go @@ -98,6 +98,40 @@ func TestGetValue(t *testing.T) { dotSeparatedKey: "agents.image.abc", expectsError: true, }, + { + name: "nil value for final part of the key", + inputMap: map[string]interface{}{ + "agents": map[string]interface{}{ + "image": map[string]interface{}{ + "tag": nil, + }, + }, + }, + dotSeparatedKey: "agents.image.tag", + expectsError: true, + }, + { + name: "nil value for middle part of the key", + inputMap: map[string]interface{}{ + "agents": map[string]interface{}{ + "image": nil, + }, + }, + dotSeparatedKey: "agents.image.tag", + expectsError: true, + }, + { + name: "empty key", + inputMap: map[string]interface{}{ + "agents": map[string]interface{}{ + "image": map[string]string{ + "tag": "7.39.0", + }, + }, + }, + dotSeparatedKey: "", + expectsError: true, + }, } for _, test := range tests {