From 0f0c070072f3c678aa33bc0f280bf1b31c13d7e8 Mon Sep 17 00:00:00 2001 From: shuting Date: Mon, 8 Nov 2021 15:53:21 -0800 Subject: [PATCH] Fix memory issue - RCR conversion (#2678) --- go.mod | 4 ++++ go.sum | 6 ++---- pkg/policyreport/builder.go | 27 ++++++++++++++++++++++----- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 8e00e8141d..fbfacbd449 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/cornelk/hashmap v1.0.1 github.com/dchest/siphash v1.2.1 // indirect github.com/distribution/distribution v2.7.1+incompatible + github.com/docker/cli v20.10.10+incompatible // indirect github.com/evanphx/json-patch/v5 v5.5.0 github.com/fatih/color v1.12.0 github.com/gardener/controller-manager-library v0.2.0 @@ -54,6 +55,9 @@ require ( ) replace ( + github.com/docker/cli v0.0.0-20191017083524-a8ff7f821017 => github.com/docker/cli v20.10.10+incompatible + github.com/docker/cli v20.10.7+incompatible => github.com/docker/cli v20.10.10+incompatible + github.com/docker/cli v20.10.8+incompatible => github.com/docker/cli v20.10.10+incompatible github.com/evanphx/json-patch/v5 => github.com/kyverno/json-patch/v5 v5.5.1-0.20210915204938-7578f4ee9c77 github.com/go-logr/logr => github.com/go-logr/logr v0.4.0 github.com/gorilla/rpc v1.2.0+incompatible => github.com/gorilla/rpc v1.2.0 diff --git a/go.sum b/go.sum index bd03ae0616..1fe382f0c1 100644 --- a/go.sum +++ b/go.sum @@ -513,10 +513,8 @@ github.com/dimchansky/utfbom v1.1.1/go.mod h1:SxdoEBH5qIqFocHMyGOXVAybYJdr71b1Q/ github.com/distribution/distribution v2.7.1+incompatible h1:aGFx4EvJWKEh//lHPLwFhFgwFHKH06TzNVPamrMn04M= github.com/distribution/distribution v2.7.1+incompatible/go.mod h1:EgLm2NgWtdKgzF9NpMzUKgzmR7AMmb0VQi2B+ZzDRjc= github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E= -github.com/docker/cli v0.0.0-20191017083524-a8ff7f821017/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8= -github.com/docker/cli v20.10.7+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8= -github.com/docker/cli v20.10.8+incompatible h1:/zO/6y9IOpcehE49yMRTV9ea0nBpb8OeqSskXLNfH1E= -github.com/docker/cli v20.10.8+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8= +github.com/docker/cli v20.10.10+incompatible h1:kcbwdgWbrBOH8QwQzaJmyriHwF7XIl4HT1qh0HTRys4= +github.com/docker/cli v20.10.10+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8= github.com/docker/distribution v0.0.0-20190905152932-14b96e55d84c/go.mod h1:0+TTO4EOBfRPhZXAeF1Vu+W3hHZ8eLp8PgKVZlcvtFY= github.com/docker/distribution v2.7.1-0.20190205005809-0d3efadf0154+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug= diff --git a/pkg/policyreport/builder.go b/pkg/policyreport/builder.go index f8bf7fc17a..495a642bb8 100755 --- a/pkg/policyreport/builder.go +++ b/pkg/policyreport/builder.go @@ -1,6 +1,7 @@ package policyreport import ( + "encoding/json" "fmt" "reflect" "time" @@ -17,7 +18,7 @@ import ( v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" ) @@ -90,6 +91,7 @@ func NewBuilder(cpolLister kyvernolister.ClusterPolicyLister, polLister kyvernol func (builder *requestBuilder) build(info Info) (req *unstructured.Unstructured, err error) { results := []*report.PolicyReportResult{} + req = new(unstructured.Unstructured) for _, infoResult := range info.Results { for _, rule := range infoResult.Rules { if rule.Type != utils.Validation.String() { @@ -107,12 +109,19 @@ func (builder *requestBuilder) build(info Info) (req *unstructured.Unstructured, Results: results, } - obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(rr) + gv := report.SchemeGroupVersion + rr.SetGroupVersionKind(schema.GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: "ReportChangeRequest"}) + + rawRcr, err := json.Marshal(rr) + if err != nil { + return nil, err + } + + err = json.Unmarshal(rawRcr, req) if err != nil { return nil, err } - req = &unstructured.Unstructured{Object: obj} set(req, info) } else { rr := &request.ClusterReportChangeRequest{ @@ -120,11 +129,19 @@ func (builder *requestBuilder) build(info Info) (req *unstructured.Unstructured, Results: results, } - obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(rr) + gv := report.SchemeGroupVersion + rr.SetGroupVersionKind(schema.GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: "ClusterReportChangeRequest"}) + + rawRcr, err := json.Marshal(rr) if err != nil { return nil, err } - req = &unstructured.Unstructured{Object: obj} + + err = json.Unmarshal(rawRcr, req) + if err != nil { + return nil, err + } + set(req, info) }