From 4788085c4f28a07af1f21f027caaf43aa9972903 Mon Sep 17 00:00:00 2001 From: Pooja Singh <36136335+NoSkillGirl@users.noreply.github.com> Date: Wed, 10 Feb 2021 00:04:57 +0530 Subject: [PATCH] Panic fix in generation.go (#1563) * added if condition Signed-off-by: NoSkillGirl * fixed test condition Signed-off-by: NoSkillGirl --- pkg/webhooks/generation.go | 29 +++++++++++++++++++---------- pkg/webhooks/generation_test.go | 1 - 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/pkg/webhooks/generation.go b/pkg/webhooks/generation.go index e0c5f14a51..856081b52b 100644 --- a/pkg/webhooks/generation.go +++ b/pkg/webhooks/generation.go @@ -191,19 +191,28 @@ func stripNonPolicyFields(obj, newRes map[string]interface{}, logger logr.Logger delete(obj["metadata"].(map[string]interface{})["annotations"].(map[string]interface{}), "kubectl.kubernetes.io/last-applied-configuration") delete(obj["metadata"].(map[string]interface{})["labels"].(map[string]interface{}), "generate.kyverno.io/clone-policy-name") - requiredMetadataInObj := make(map[string]interface{}) - requiredMetadataInObj["annotations"] = obj["metadata"].(map[string]interface{})["annotations"] - requiredMetadataInObj["labels"] = obj["metadata"].(map[string]interface{})["labels"] - obj["metadata"] = requiredMetadataInObj + if _, found := obj["metadata"]; found { + requiredMetadataInObj := make(map[string]interface{}) + if _, found := obj["metadata"].(map[string]interface{})["annotations"]; found { + requiredMetadataInObj["annotations"] = obj["metadata"].(map[string]interface{})["annotations"] + } - requiredMetadataInNewRes := make(map[string]interface{}) - if _, found := newRes["metadata"].(map[string]interface{})["annotations"]; found { - requiredMetadataInNewRes["annotations"] = newRes["metadata"].(map[string]interface{})["annotations"] + if _, found := newRes["metadata"].(map[string]interface{})["labels"]; found { + requiredMetadataInObj["labels"] = obj["metadata"].(map[string]interface{})["labels"] + } + obj["metadata"] = requiredMetadataInObj } - if _, found := newRes["metadata"].(map[string]interface{})["labels"]; found { - requiredMetadataInNewRes["labels"] = newRes["metadata"].(map[string]interface{})["labels"] + + if _, found := newRes["metadata"]; found { + requiredMetadataInNewRes := make(map[string]interface{}) + if _, found := newRes["metadata"].(map[string]interface{})["annotations"]; found { + requiredMetadataInNewRes["annotations"] = newRes["metadata"].(map[string]interface{})["annotations"] + } + if _, found := newRes["metadata"].(map[string]interface{})["labels"]; found { + requiredMetadataInNewRes["labels"] = newRes["metadata"].(map[string]interface{})["labels"] + } + newRes["metadata"] = requiredMetadataInNewRes } - newRes["metadata"] = requiredMetadataInNewRes if _, found := obj["status"]; found { delete(obj, "status") diff --git a/pkg/webhooks/generation_test.go b/pkg/webhooks/generation_test.go index d306b9eac6..6b0952d451 100644 --- a/pkg/webhooks/generation_test.go +++ b/pkg/webhooks/generation_test.go @@ -62,7 +62,6 @@ func Test_updateFeildsInSourceAndUpdatedResource(t *testing.T) { "annotations": map[string]interface{}{ "imageregistry": "https://hub.docker.com/", }, - "labels": map[string]interface{}{}, }, },