1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-04-08 18:15:48 +00:00

code improvement (#1567)

* code improvement

Signed-off-by: NoSkillGirl <singhpooja240393@gmail.com>

* added if conditions

Signed-off-by: NoSkillGirl <singhpooja240393@gmail.com>

* fixed unit test cases

Signed-off-by: NoSkillGirl <singhpooja240393@gmail.com>
This commit is contained in:
Pooja Singh 2021-02-10 23:58:50 +05:30 committed by GitHub
parent 6d2d2e3c1b
commit 0de83ebe17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 13 deletions

View file

@ -188,28 +188,28 @@ func (ws *WebhookServer) handleUpdateTargetResource(request *v1beta1.AdmissionRe
//stripNonPolicyFields - remove feilds which get updated with each request by kyverno and are non policy fields
func stripNonPolicyFields(obj, newRes map[string]interface{}, logger logr.Logger) (map[string]interface{}, map[string]interface{}) {
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")
if _, found := obj["metadata"]; found {
if metadata, 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"]
if annotations, found := metadata.(map[string]interface{})["annotations"]; found {
delete(annotations.(map[string]interface{}), "kubectl.kubernetes.io/last-applied-configuration")
requiredMetadataInObj["annotations"] = annotations
}
if _, found := newRes["metadata"].(map[string]interface{})["labels"]; found {
requiredMetadataInObj["labels"] = obj["metadata"].(map[string]interface{})["labels"]
if labels, found := metadata.(map[string]interface{})["labels"]; found {
delete(labels.(map[string]interface{}), "generate.kyverno.io/clone-policy-name")
requiredMetadataInObj["labels"] = labels
}
obj["metadata"] = requiredMetadataInObj
}
if _, found := newRes["metadata"]; found {
if metadata, 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 annotations, found := metadata.(map[string]interface{})["annotations"]; found {
requiredMetadataInNewRes["annotations"] = annotations
}
if _, found := newRes["metadata"].(map[string]interface{})["labels"]; found {
requiredMetadataInNewRes["labels"] = newRes["metadata"].(map[string]interface{})["labels"]
if labels, found := metadata.(map[string]interface{})["labels"]; found {
requiredMetadataInNewRes["labels"] = labels
}
newRes["metadata"] = requiredMetadataInNewRes
}

View file

@ -62,6 +62,7 @@ func Test_updateFeildsInSourceAndUpdatedResource(t *testing.T) {
"annotations": map[string]interface{}{
"imageregistry": "https://hub.docker.com/",
},
"labels": map[string]interface{}{},
},
},
@ -269,4 +270,5 @@ func Test_updateFeildsInSourceAndUpdatedResource(t *testing.T) {
assert.Assert(t, reflect.DeepEqual(tc.expectedObj, o))
assert.Assert(t, reflect.DeepEqual(tc.expectedNewRes, n))
}
}