From 2efcfc956576a45641ea32ee0ba5d11efc6697fe Mon Sep 17 00:00:00 2001 From: Patrick Decat Date: Mon, 12 Feb 2018 22:59:47 +0100 Subject: [PATCH] Fix adding labels and annotations to a resource that didn't have any --- kubernetes/patch_operations.go | 9 +++++++++ kubernetes/patch_operations_test.go | 11 +++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/kubernetes/patch_operations.go b/kubernetes/patch_operations.go index ea552105fe..12e8aff89c 100644 --- a/kubernetes/patch_operations.go +++ b/kubernetes/patch_operations.go @@ -12,6 +12,15 @@ func diffStringMap(pathPrefix string, oldV, newV map[string]interface{}) PatchOp pathPrefix = strings.TrimRight(pathPrefix, "/") + // If old value was empty, just create the object + if len(oldV) == 0 { + ops = append(ops, &AddOperation{ + Path: pathPrefix, + Value: newV, + }) + return ops + } + // This is suboptimal for adding whole new map from scratch // or deleting the whole map, but it's actually intention. // There may be some other map items managed outside of TF diff --git a/kubernetes/patch_operations_test.go b/kubernetes/patch_operations_test.go index 523579ac01..de30839e16 100644 --- a/kubernetes/patch_operations_test.go +++ b/kubernetes/patch_operations_test.go @@ -103,12 +103,11 @@ func TestDiffStringMap(t *testing.T) { }, ExpectedOps: []PatchOperation{ &AddOperation{ - Path: "/parent/one", - Value: "111", - }, - &AddOperation{ - Path: "/parent/two", - Value: "222", + Path: "/parent", + Value: map[string]interface{}{ + "one": "111", + "two": "222", + }, }, }, },