diff --git a/util/diff/diff.go b/util/diff/diff.go index 59da25320192c..8f9d9c10860ff 100644 --- a/util/diff/diff.go +++ b/util/diff/diff.go @@ -137,8 +137,16 @@ func removeNamespaceAnnotation(orig *unstructured.Unstructured) *unstructured.Un metadata := metadataIf.(map[string]interface{}) delete(metadata, "namespace") if annotationsIf, ok := metadata["annotations"]; ok { - annotation := annotationsIf.(map[string]interface{}) - if len(annotation) == 0 { + shouldDelete := false + if annotationsIf == nil { + shouldDelete = true + } else { + annotation := annotationsIf.(map[string]interface{}) + if len(annotation) == 0 { + shouldDelete = true + } + } + if shouldDelete { delete(metadata, "annotations") } } diff --git a/util/diff/diff_test.go b/util/diff/diff_test.go index 136f6a48aadc8..06aa6589d555c 100644 --- a/util/diff/diff_test.go +++ b/util/diff/diff_test.go @@ -297,3 +297,23 @@ func TestThreeWayDiffExplicitNamespace(t *testing.T) { assert.Nil(t, err) log.Println(ascii) } + +func TestRemoveNamespaceAnnotation(t *testing.T) { + obj := removeNamespaceAnnotation(&unstructured.Unstructured{Object: map[string]interface{}{ + "metadata": map[string]interface{}{ + "name": "test", + "namespace": "default", + }, + }}) + assert.Equal(t, "", obj.GetNamespace()) + + obj = removeNamespaceAnnotation(&unstructured.Unstructured{Object: map[string]interface{}{ + "metadata": map[string]interface{}{ + "name": "test", + "namespace": "default", + "annotations": make(map[string]interface{}), + }, + }}) + assert.Equal(t, "", obj.GetNamespace()) + assert.Nil(t, obj.GetAnnotations()) +}