1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-15 17:51:20 +00:00

refactor: remove some api unnecessary pointers (2) (#3705)

* refactor: remove some api unnecessary pointers

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>

* refactor: remove some api unnecessary pointers (2)

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
This commit is contained in:
Charles-Edouard Brétéché 2022-04-28 11:11:14 +02:00 committed by GitHub
parent 75e300799a
commit 68c35b2f2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 37 additions and 59 deletions

View file

@ -59,7 +59,7 @@ type ClusterReportChangeRequest struct {
// PolicyReportResult provides result details // PolicyReportResult provides result details
// +optional // +optional
Results []*report.PolicyReportResult `json:"results,omitempty"` Results []report.PolicyReportResult `json:"results,omitempty"`
} }
// ClusterReportChangeRequestList contains a list of ClusterReportChangeRequest // ClusterReportChangeRequestList contains a list of ClusterReportChangeRequest

View file

@ -58,7 +58,7 @@ type ReportChangeRequest struct {
// PolicyReportResult provides result details // PolicyReportResult provides result details
// +optional // +optional
Results []*report.PolicyReportResult `json:"results,omitempty"` Results []report.PolicyReportResult `json:"results,omitempty"`
} }
// ReportChangeRequestList contains a list of ReportChangeRequest // ReportChangeRequestList contains a list of ReportChangeRequest

View file

@ -44,13 +44,9 @@ func (in *ClusterReportChangeRequest) DeepCopyInto(out *ClusterReportChangeReque
out.Summary = in.Summary out.Summary = in.Summary
if in.Results != nil { if in.Results != nil {
in, out := &in.Results, &out.Results in, out := &in.Results, &out.Results
*out = make([]*policyreportv1alpha2.PolicyReportResult, len(*in)) *out = make([]policyreportv1alpha2.PolicyReportResult, len(*in))
for i := range *in { for i := range *in {
if (*in)[i] != nil { (*in)[i].DeepCopyInto(&(*out)[i])
in, out := &(*in)[i], &(*out)[i]
*out = new(policyreportv1alpha2.PolicyReportResult)
(*in).DeepCopyInto(*out)
}
} }
} }
} }
@ -123,13 +119,9 @@ func (in *ReportChangeRequest) DeepCopyInto(out *ReportChangeRequest) {
out.Summary = in.Summary out.Summary = in.Summary
if in.Results != nil { if in.Results != nil {
in, out := &in.Results, &out.Results in, out := &in.Results, &out.Results
*out = make([]*policyreportv1alpha2.PolicyReportResult, len(*in)) *out = make([]policyreportv1alpha2.PolicyReportResult, len(*in))
for i := range *in { for i := range *in {
if (*in)[i] != nil { (*in)[i].DeepCopyInto(&(*out)[i])
in, out := &(*in)[i], &(*out)[i]
*out = new(policyreportv1alpha2.PolicyReportResult)
(*in).DeepCopyInto(*out)
}
} }
} }
} }

View file

@ -59,7 +59,7 @@ type ClusterPolicyReport struct {
// PolicyReportResult provides result details // PolicyReportResult provides result details
// +optional // +optional
Results []*PolicyReportResult `json:"results,omitempty"` Results []PolicyReportResult `json:"results,omitempty"`
} }
// ClusterPolicyReportList contains a list of ClusterPolicyReport // ClusterPolicyReportList contains a list of ClusterPolicyReport

View file

@ -103,7 +103,7 @@ type PolicyReportResult struct {
// Resources is an optional reference to the resource checked by the policy and rule // Resources is an optional reference to the resource checked by the policy and rule
// +optional // +optional
Resources []*corev1.ObjectReference `json:"resources,omitempty"` Resources []corev1.ObjectReference `json:"resources,omitempty"`
// ResourceSelector is an optional selector for policy results that apply to multiple resources. // ResourceSelector is an optional selector for policy results that apply to multiple resources.
// For example, a policy result may apply to all pods that match a label. // For example, a policy result may apply to all pods that match a label.
@ -170,7 +170,7 @@ type PolicyReport struct {
// PolicyReportResult provides result details // PolicyReportResult provides result details
// +optional // +optional
Results []*PolicyReportResult `json:"results,omitempty"` Results []PolicyReportResult `json:"results,omitempty"`
} }
// PolicyReportList contains a list of PolicyReport // PolicyReportList contains a list of PolicyReport

View file

@ -43,13 +43,9 @@ func (in *ClusterPolicyReport) DeepCopyInto(out *ClusterPolicyReport) {
out.Summary = in.Summary out.Summary = in.Summary
if in.Results != nil { if in.Results != nil {
in, out := &in.Results, &out.Results in, out := &in.Results, &out.Results
*out = make([]*PolicyReportResult, len(*in)) *out = make([]PolicyReportResult, len(*in))
for i := range *in { for i := range *in {
if (*in)[i] != nil { (*in)[i].DeepCopyInto(&(*out)[i])
in, out := &(*in)[i], &(*out)[i]
*out = new(PolicyReportResult)
(*in).DeepCopyInto(*out)
}
} }
} }
} }
@ -122,13 +118,9 @@ func (in *PolicyReport) DeepCopyInto(out *PolicyReport) {
out.Summary = in.Summary out.Summary = in.Summary
if in.Results != nil { if in.Results != nil {
in, out := &in.Results, &out.Results in, out := &in.Results, &out.Results
*out = make([]*PolicyReportResult, len(*in)) *out = make([]PolicyReportResult, len(*in))
for i := range *in { for i := range *in {
if (*in)[i] != nil { (*in)[i].DeepCopyInto(&(*out)[i])
in, out := &(*in)[i], &(*out)[i]
*out = new(PolicyReportResult)
(*in).DeepCopyInto(*out)
}
} }
} }
} }
@ -188,14 +180,8 @@ func (in *PolicyReportResult) DeepCopyInto(out *PolicyReportResult) {
*out = *in *out = *in
if in.Resources != nil { if in.Resources != nil {
in, out := &in.Resources, &out.Resources in, out := &in.Resources, &out.Resources
*out = make([]*v1.ObjectReference, len(*in)) *out = make([]v1.ObjectReference, len(*in))
for i := range *in { copy(*out, *in)
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = new(v1.ObjectReference)
**out = **in
}
}
} }
if in.ResourceSelector != nil { if in.ResourceSelector != nil {
in, out := &in.ResourceSelector, &out.ResourceSelector in, out := &in.ResourceSelector, &out.ResourceSelector

View file

@ -73,8 +73,8 @@ func buildPolicyReports(pvInfos []policyreport.Info) (res []*unstructured.Unstru
// buildPolicyResults returns a string-PolicyReportResult map // buildPolicyResults returns a string-PolicyReportResult map
// the key of the map is one of "clusterpolicyreport", "policyreport-ns-<namespace>" // the key of the map is one of "clusterpolicyreport", "policyreport-ns-<namespace>"
func buildPolicyResults(infos []policyreport.Info) map[string][]*report.PolicyReportResult { func buildPolicyResults(infos []policyreport.Info) map[string][]report.PolicyReportResult {
results := make(map[string][]*report.PolicyReportResult) results := make(map[string][]report.PolicyReportResult)
now := metav1.Timestamp{Seconds: time.Now().Unix()} now := metav1.Timestamp{Seconds: time.Now().Unix()}
for _, info := range infos { for _, info := range infos {
@ -94,7 +94,7 @@ func buildPolicyResults(infos []policyreport.Info) map[string][]*report.PolicyRe
result := report.PolicyReportResult{ result := report.PolicyReportResult{
Policy: info.PolicyName, Policy: info.PolicyName,
Resources: []*corev1.ObjectReference{ Resources: []corev1.ObjectReference{
{ {
Kind: infoResult.Resource.Kind, Kind: infoResult.Resource.Kind,
Namespace: infoResult.Resource.Namespace, Namespace: infoResult.Resource.Namespace,
@ -111,7 +111,7 @@ func buildPolicyResults(infos []policyreport.Info) map[string][]*report.PolicyRe
result.Result = report.PolicyResult(rule.Status) result.Result = report.PolicyResult(rule.Status)
result.Source = policyreport.SourceValue result.Source = policyreport.SourceValue
result.Timestamp = now result.Timestamp = now
results[appname] = append(results[appname], &result) results[appname] = append(results[appname], result)
} }
} }
} }
@ -160,7 +160,7 @@ func mergeSucceededResults(results map[string][]*report.PolicyReportResult) map[
return resultsNew return resultsNew
} }
func calculateSummary(results []*report.PolicyReportResult) (summary report.PolicyReportSummary) { func calculateSummary(results []report.PolicyReportResult) (summary report.PolicyReportSummary) {
for _, res := range results { for _, res := range results {
switch string(res.Result) { switch string(res.Result) {
case report.StatusPass: case report.StatusPass:

View file

@ -151,19 +151,19 @@ func Test_buildPolicyResults(t *testing.T) {
} }
func Test_calculateSummary(t *testing.T) { func Test_calculateSummary(t *testing.T) {
results := []*preport.PolicyReportResult{ results := []preport.PolicyReportResult{
{ {
Resources: make([]*v1.ObjectReference, 5), Resources: make([]v1.ObjectReference, 5),
Result: preport.PolicyResult(preport.StatusPass), Result: preport.PolicyResult(preport.StatusPass),
}, },
{Result: preport.PolicyResult(preport.StatusFail)}, {Result: preport.PolicyResult(preport.StatusFail)},
{Result: preport.PolicyResult(preport.StatusFail)}, {Result: preport.PolicyResult(preport.StatusFail)},
{Result: preport.PolicyResult(preport.StatusFail)}, {Result: preport.PolicyResult(preport.StatusFail)},
{ {
Resources: make([]*v1.ObjectReference, 1), Resources: make([]v1.ObjectReference, 1),
Result: preport.PolicyResult(preport.StatusPass)}, Result: preport.PolicyResult(preport.StatusPass)},
{ {
Resources: make([]*v1.ObjectReference, 4), Resources: make([]v1.ObjectReference, 4),
Result: preport.PolicyResult(preport.StatusPass), Result: preport.PolicyResult(preport.StatusPass),
}, },
} }

View file

@ -502,7 +502,7 @@ func buildPolicyResults(engineResponses []*response.EngineResponse, testResults
result := report.PolicyReportResult{ result := report.PolicyReportResult{
Policy: policyName, Policy: policyName,
Resources: []*corev1.ObjectReference{ Resources: []corev1.ObjectReference{
{ {
Name: resourceName, Name: resourceName,
}, },

View file

@ -101,7 +101,7 @@ github.com/kyverno/kyverno/api/policyreport/v1alpha2.PolicyReportSummary
<td> <td>
<code>results</code></br> <code>results</code></br>
<em> <em>
[]*github.com/kyverno/kyverno/api/policyreport/v1alpha2.PolicyReportResult []github.com/kyverno/kyverno/api/policyreport/v1alpha2.PolicyReportResult
</em> </em>
</td> </td>
<td> <td>
@ -184,7 +184,7 @@ github.com/kyverno/kyverno/api/policyreport/v1alpha2.PolicyReportSummary
<td> <td>
<code>results</code></br> <code>results</code></br>
<em> <em>
[]*github.com/kyverno/kyverno/api/policyreport/v1alpha2.PolicyReportResult []github.com/kyverno/kyverno/api/policyreport/v1alpha2.PolicyReportResult
</em> </em>
</td> </td>
<td> <td>

View file

@ -113,7 +113,7 @@ func eraseResultsEntries(pclient *kyvernoclient.Clientset, reportLister policyre
errors = append(errors, err.Error()) errors = append(errors, err.Error())
} else { } else {
for _, polr := range polrs { for _, polr := range polrs {
polr.Results = []*v1alpha2.PolicyReportResult{} polr.Results = []v1alpha2.PolicyReportResult{}
polr.Summary = v1alpha2.PolicyReportSummary{} polr.Summary = v1alpha2.PolicyReportSummary{}
if _, err = pclient.Wgpolicyk8sV1alpha2().PolicyReports(polr.GetNamespace()).Update(context.TODO(), polr, metav1.UpdateOptions{}); err != nil { if _, err = pclient.Wgpolicyk8sV1alpha2().PolicyReports(polr.GetNamespace()).Update(context.TODO(), polr, metav1.UpdateOptions{}); err != nil {
errors = append(errors, fmt.Sprintf("%s/%s/%s: %v", polr.Kind, polr.Namespace, polr.Name, err)) errors = append(errors, fmt.Sprintf("%s/%s/%s: %v", polr.Kind, polr.Namespace, polr.Name, err))
@ -125,7 +125,7 @@ func eraseResultsEntries(pclient *kyvernoclient.Clientset, reportLister policyre
errors = append(errors, err.Error()) errors = append(errors, err.Error())
} else { } else {
for _, cpolr := range cpolrs { for _, cpolr := range cpolrs {
cpolr.Results = []*v1alpha2.PolicyReportResult{} cpolr.Results = []v1alpha2.PolicyReportResult{}
cpolr.Summary = v1alpha2.PolicyReportSummary{} cpolr.Summary = v1alpha2.PolicyReportSummary{}
if _, err = pclient.Wgpolicyk8sV1alpha2().ClusterPolicyReports().Update(context.TODO(), cpolr, metav1.UpdateOptions{}); err != nil { if _, err = pclient.Wgpolicyk8sV1alpha2().ClusterPolicyReports().Update(context.TODO(), cpolr, metav1.UpdateOptions{}); err != nil {
errors = append(errors, fmt.Sprintf("%s/%s: %v", cpolr.Kind, cpolr.Name, err)) errors = append(errors, fmt.Sprintf("%s/%s: %v", cpolr.Kind, cpolr.Name, err))

View file

@ -94,7 +94,7 @@ func NewBuilder(cpolLister kyvernolister.ClusterPolicyLister, polLister kyvernol
} }
func (builder *requestBuilder) build(info Info) (req *unstructured.Unstructured, err error) { func (builder *requestBuilder) build(info Info) (req *unstructured.Unstructured, err error) {
results := []*report.PolicyReportResult{} results := []report.PolicyReportResult{}
req = new(unstructured.Unstructured) req = new(unstructured.Unstructured)
for _, infoResult := range info.Results { for _, infoResult := range info.Results {
for _, rule := range infoResult.Rules { for _, rule := range infoResult.Rules {
@ -160,12 +160,12 @@ func (builder *requestBuilder) build(info Info) (req *unstructured.Unstructured,
return req, nil return req, nil
} }
func (builder *requestBuilder) buildRCRResult(policy string, resource response.ResourceSpec, rule kyverno.ViolatedRule) *report.PolicyReportResult { func (builder *requestBuilder) buildRCRResult(policy string, resource response.ResourceSpec, rule kyverno.ViolatedRule) report.PolicyReportResult {
av := builder.fetchAnnotationValues(policy, resource.Namespace) av := builder.fetchAnnotationValues(policy, resource.Namespace)
result := &report.PolicyReportResult{ result := report.PolicyReportResult{
Policy: policy, Policy: policy,
Resources: []*v1.ObjectReference{ Resources: []v1.ObjectReference{
{ {
Kind: resource.Kind, Kind: resource.Kind,
Namespace: resource.Namespace, Namespace: resource.Namespace,
@ -246,7 +246,7 @@ func setRequestDeletionLabels(req *unstructured.Unstructured, info Info) bool {
return false return false
} }
func calculateSummary(results []*report.PolicyReportResult) (summary report.PolicyReportSummary) { func calculateSummary(results []report.PolicyReportResult) (summary report.PolicyReportSummary) {
for _, res := range results { for _, res := range results {
switch string(res.Result) { switch string(res.Result) {
case report.StatusPass: case report.StatusPass:

View file

@ -457,7 +457,7 @@ func (g *ReportGenerator) removeFromClusterPolicyReport(policyName, ruleName str
} }
for _, cpolr := range cpolrs { for _, cpolr := range cpolrs {
newRes := []*report.PolicyReportResult{} newRes := []report.PolicyReportResult{}
for _, result := range cpolr.Results { for _, result := range cpolr.Results {
if ruleName != "" && result.Rule == ruleName && result.Policy == policyName { if ruleName != "" && result.Rule == ruleName && result.Policy == policyName {
continue continue
@ -498,7 +498,7 @@ func (g *ReportGenerator) removeFromPolicyReport(policyName, ruleName string) er
} }
for _, r := range policyReports { for _, r := range policyReports {
newRes := []*report.PolicyReportResult{} newRes := []report.PolicyReportResult{}
for _, result := range r.Results { for _, result := range r.Results {
if ruleName != "" && result.Rule == ruleName && result.Policy == policyName { if ruleName != "" && result.Rule == ruleName && result.Policy == policyName {
continue continue
@ -567,7 +567,7 @@ func (g *ReportGenerator) aggregateReports(namespace string) (
} }
func mergeRequests(ns, kyvernoNs *v1.Namespace, requestsGeneral interface{}) (*unstructured.Unstructured, interface{}, error) { func mergeRequests(ns, kyvernoNs *v1.Namespace, requestsGeneral interface{}) (*unstructured.Unstructured, interface{}, error) {
results := []*report.PolicyReportResult{} results := []report.PolicyReportResult{}
if requests, ok := requestsGeneral.([]*changerequest.ClusterReportChangeRequest); ok { if requests, ok := requestsGeneral.([]*changerequest.ClusterReportChangeRequest); ok {
aggregatedRequests := []*changerequest.ClusterReportChangeRequest{} aggregatedRequests := []*changerequest.ClusterReportChangeRequest{}