From fd4f27fcb03b1792529f856612a462ade93585a1 Mon Sep 17 00:00:00 2001 From: Alexander Matyushentsev Date: Fri, 16 Nov 2018 09:31:09 -0800 Subject: [PATCH] Issue #790 - Fix application controller panic --- util/diff/diff.go | 12 ++++++++++-- util/diff/diff_test.go | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) 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()) +}