mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-14 11:57:48 +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:
parent
75e300799a
commit
68c35b2f2e
13 changed files with 37 additions and 59 deletions
|
@ -59,7 +59,7 @@ type ClusterReportChangeRequest struct {
|
|||
|
||||
// PolicyReportResult provides result details
|
||||
// +optional
|
||||
Results []*report.PolicyReportResult `json:"results,omitempty"`
|
||||
Results []report.PolicyReportResult `json:"results,omitempty"`
|
||||
}
|
||||
|
||||
// ClusterReportChangeRequestList contains a list of ClusterReportChangeRequest
|
||||
|
|
|
@ -58,7 +58,7 @@ type ReportChangeRequest struct {
|
|||
|
||||
// PolicyReportResult provides result details
|
||||
// +optional
|
||||
Results []*report.PolicyReportResult `json:"results,omitempty"`
|
||||
Results []report.PolicyReportResult `json:"results,omitempty"`
|
||||
}
|
||||
|
||||
// ReportChangeRequestList contains a list of ReportChangeRequest
|
||||
|
|
|
@ -44,13 +44,9 @@ func (in *ClusterReportChangeRequest) DeepCopyInto(out *ClusterReportChangeReque
|
|||
out.Summary = in.Summary
|
||||
if in.Results != nil {
|
||||
in, out := &in.Results, &out.Results
|
||||
*out = make([]*policyreportv1alpha2.PolicyReportResult, len(*in))
|
||||
*out = make([]policyreportv1alpha2.PolicyReportResult, len(*in))
|
||||
for i := range *in {
|
||||
if (*in)[i] != nil {
|
||||
in, out := &(*in)[i], &(*out)[i]
|
||||
*out = new(policyreportv1alpha2.PolicyReportResult)
|
||||
(*in).DeepCopyInto(*out)
|
||||
}
|
||||
(*in)[i].DeepCopyInto(&(*out)[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -123,13 +119,9 @@ func (in *ReportChangeRequest) DeepCopyInto(out *ReportChangeRequest) {
|
|||
out.Summary = in.Summary
|
||||
if in.Results != nil {
|
||||
in, out := &in.Results, &out.Results
|
||||
*out = make([]*policyreportv1alpha2.PolicyReportResult, len(*in))
|
||||
*out = make([]policyreportv1alpha2.PolicyReportResult, len(*in))
|
||||
for i := range *in {
|
||||
if (*in)[i] != nil {
|
||||
in, out := &(*in)[i], &(*out)[i]
|
||||
*out = new(policyreportv1alpha2.PolicyReportResult)
|
||||
(*in).DeepCopyInto(*out)
|
||||
}
|
||||
(*in)[i].DeepCopyInto(&(*out)[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ type ClusterPolicyReport struct {
|
|||
|
||||
// PolicyReportResult provides result details
|
||||
// +optional
|
||||
Results []*PolicyReportResult `json:"results,omitempty"`
|
||||
Results []PolicyReportResult `json:"results,omitempty"`
|
||||
}
|
||||
|
||||
// ClusterPolicyReportList contains a list of ClusterPolicyReport
|
||||
|
|
|
@ -103,7 +103,7 @@ type PolicyReportResult struct {
|
|||
|
||||
// Resources is an optional reference to the resource checked by the policy and rule
|
||||
// +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.
|
||||
// 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
|
||||
// +optional
|
||||
Results []*PolicyReportResult `json:"results,omitempty"`
|
||||
Results []PolicyReportResult `json:"results,omitempty"`
|
||||
}
|
||||
|
||||
// PolicyReportList contains a list of PolicyReport
|
||||
|
|
|
@ -43,13 +43,9 @@ func (in *ClusterPolicyReport) DeepCopyInto(out *ClusterPolicyReport) {
|
|||
out.Summary = in.Summary
|
||||
if in.Results != nil {
|
||||
in, out := &in.Results, &out.Results
|
||||
*out = make([]*PolicyReportResult, len(*in))
|
||||
*out = make([]PolicyReportResult, len(*in))
|
||||
for i := range *in {
|
||||
if (*in)[i] != nil {
|
||||
in, out := &(*in)[i], &(*out)[i]
|
||||
*out = new(PolicyReportResult)
|
||||
(*in).DeepCopyInto(*out)
|
||||
}
|
||||
(*in)[i].DeepCopyInto(&(*out)[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -122,13 +118,9 @@ func (in *PolicyReport) DeepCopyInto(out *PolicyReport) {
|
|||
out.Summary = in.Summary
|
||||
if in.Results != nil {
|
||||
in, out := &in.Results, &out.Results
|
||||
*out = make([]*PolicyReportResult, len(*in))
|
||||
*out = make([]PolicyReportResult, len(*in))
|
||||
for i := range *in {
|
||||
if (*in)[i] != nil {
|
||||
in, out := &(*in)[i], &(*out)[i]
|
||||
*out = new(PolicyReportResult)
|
||||
(*in).DeepCopyInto(*out)
|
||||
}
|
||||
(*in)[i].DeepCopyInto(&(*out)[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -188,14 +180,8 @@ func (in *PolicyReportResult) DeepCopyInto(out *PolicyReportResult) {
|
|||
*out = *in
|
||||
if in.Resources != nil {
|
||||
in, out := &in.Resources, &out.Resources
|
||||
*out = make([]*v1.ObjectReference, len(*in))
|
||||
for i := range *in {
|
||||
if (*in)[i] != nil {
|
||||
in, out := &(*in)[i], &(*out)[i]
|
||||
*out = new(v1.ObjectReference)
|
||||
**out = **in
|
||||
}
|
||||
}
|
||||
*out = make([]v1.ObjectReference, len(*in))
|
||||
copy(*out, *in)
|
||||
}
|
||||
if in.ResourceSelector != nil {
|
||||
in, out := &in.ResourceSelector, &out.ResourceSelector
|
||||
|
|
|
@ -73,8 +73,8 @@ func buildPolicyReports(pvInfos []policyreport.Info) (res []*unstructured.Unstru
|
|||
|
||||
// buildPolicyResults returns a string-PolicyReportResult map
|
||||
// the key of the map is one of "clusterpolicyreport", "policyreport-ns-<namespace>"
|
||||
func buildPolicyResults(infos []policyreport.Info) map[string][]*report.PolicyReportResult {
|
||||
results := make(map[string][]*report.PolicyReportResult)
|
||||
func buildPolicyResults(infos []policyreport.Info) map[string][]report.PolicyReportResult {
|
||||
results := make(map[string][]report.PolicyReportResult)
|
||||
now := metav1.Timestamp{Seconds: time.Now().Unix()}
|
||||
|
||||
for _, info := range infos {
|
||||
|
@ -94,7 +94,7 @@ func buildPolicyResults(infos []policyreport.Info) map[string][]*report.PolicyRe
|
|||
|
||||
result := report.PolicyReportResult{
|
||||
Policy: info.PolicyName,
|
||||
Resources: []*corev1.ObjectReference{
|
||||
Resources: []corev1.ObjectReference{
|
||||
{
|
||||
Kind: infoResult.Resource.Kind,
|
||||
Namespace: infoResult.Resource.Namespace,
|
||||
|
@ -111,7 +111,7 @@ func buildPolicyResults(infos []policyreport.Info) map[string][]*report.PolicyRe
|
|||
result.Result = report.PolicyResult(rule.Status)
|
||||
result.Source = policyreport.SourceValue
|
||||
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
|
||||
}
|
||||
|
||||
func calculateSummary(results []*report.PolicyReportResult) (summary report.PolicyReportSummary) {
|
||||
func calculateSummary(results []report.PolicyReportResult) (summary report.PolicyReportSummary) {
|
||||
for _, res := range results {
|
||||
switch string(res.Result) {
|
||||
case report.StatusPass:
|
||||
|
|
|
@ -151,19 +151,19 @@ func Test_buildPolicyResults(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.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)},
|
||||
{
|
||||
Resources: make([]*v1.ObjectReference, 4),
|
||||
Resources: make([]v1.ObjectReference, 4),
|
||||
Result: preport.PolicyResult(preport.StatusPass),
|
||||
},
|
||||
}
|
||||
|
|
|
@ -502,7 +502,7 @@ func buildPolicyResults(engineResponses []*response.EngineResponse, testResults
|
|||
|
||||
result := report.PolicyReportResult{
|
||||
Policy: policyName,
|
||||
Resources: []*corev1.ObjectReference{
|
||||
Resources: []corev1.ObjectReference{
|
||||
{
|
||||
Name: resourceName,
|
||||
},
|
||||
|
|
|
@ -101,7 +101,7 @@ github.com/kyverno/kyverno/api/policyreport/v1alpha2.PolicyReportSummary
|
|||
<td>
|
||||
<code>results</code></br>
|
||||
<em>
|
||||
[]*github.com/kyverno/kyverno/api/policyreport/v1alpha2.PolicyReportResult
|
||||
[]github.com/kyverno/kyverno/api/policyreport/v1alpha2.PolicyReportResult
|
||||
</em>
|
||||
</td>
|
||||
<td>
|
||||
|
@ -184,7 +184,7 @@ github.com/kyverno/kyverno/api/policyreport/v1alpha2.PolicyReportSummary
|
|||
<td>
|
||||
<code>results</code></br>
|
||||
<em>
|
||||
[]*github.com/kyverno/kyverno/api/policyreport/v1alpha2.PolicyReportResult
|
||||
[]github.com/kyverno/kyverno/api/policyreport/v1alpha2.PolicyReportResult
|
||||
</em>
|
||||
</td>
|
||||
<td>
|
||||
|
|
|
@ -113,7 +113,7 @@ func eraseResultsEntries(pclient *kyvernoclient.Clientset, reportLister policyre
|
|||
errors = append(errors, err.Error())
|
||||
} else {
|
||||
for _, polr := range polrs {
|
||||
polr.Results = []*v1alpha2.PolicyReportResult{}
|
||||
polr.Results = []v1alpha2.PolicyReportResult{}
|
||||
polr.Summary = v1alpha2.PolicyReportSummary{}
|
||||
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))
|
||||
|
@ -125,7 +125,7 @@ func eraseResultsEntries(pclient *kyvernoclient.Clientset, reportLister policyre
|
|||
errors = append(errors, err.Error())
|
||||
} else {
|
||||
for _, cpolr := range cpolrs {
|
||||
cpolr.Results = []*v1alpha2.PolicyReportResult{}
|
||||
cpolr.Results = []v1alpha2.PolicyReportResult{}
|
||||
cpolr.Summary = v1alpha2.PolicyReportSummary{}
|
||||
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))
|
||||
|
|
|
@ -94,7 +94,7 @@ func NewBuilder(cpolLister kyvernolister.ClusterPolicyLister, polLister kyvernol
|
|||
}
|
||||
|
||||
func (builder *requestBuilder) build(info Info) (req *unstructured.Unstructured, err error) {
|
||||
results := []*report.PolicyReportResult{}
|
||||
results := []report.PolicyReportResult{}
|
||||
req = new(unstructured.Unstructured)
|
||||
for _, infoResult := range info.Results {
|
||||
for _, rule := range infoResult.Rules {
|
||||
|
@ -160,12 +160,12 @@ func (builder *requestBuilder) build(info Info) (req *unstructured.Unstructured,
|
|||
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)
|
||||
|
||||
result := &report.PolicyReportResult{
|
||||
result := report.PolicyReportResult{
|
||||
Policy: policy,
|
||||
Resources: []*v1.ObjectReference{
|
||||
Resources: []v1.ObjectReference{
|
||||
{
|
||||
Kind: resource.Kind,
|
||||
Namespace: resource.Namespace,
|
||||
|
@ -246,7 +246,7 @@ func setRequestDeletionLabels(req *unstructured.Unstructured, info Info) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func calculateSummary(results []*report.PolicyReportResult) (summary report.PolicyReportSummary) {
|
||||
func calculateSummary(results []report.PolicyReportResult) (summary report.PolicyReportSummary) {
|
||||
for _, res := range results {
|
||||
switch string(res.Result) {
|
||||
case report.StatusPass:
|
||||
|
|
|
@ -457,7 +457,7 @@ func (g *ReportGenerator) removeFromClusterPolicyReport(policyName, ruleName str
|
|||
}
|
||||
|
||||
for _, cpolr := range cpolrs {
|
||||
newRes := []*report.PolicyReportResult{}
|
||||
newRes := []report.PolicyReportResult{}
|
||||
for _, result := range cpolr.Results {
|
||||
if ruleName != "" && result.Rule == ruleName && result.Policy == policyName {
|
||||
continue
|
||||
|
@ -498,7 +498,7 @@ func (g *ReportGenerator) removeFromPolicyReport(policyName, ruleName string) er
|
|||
}
|
||||
|
||||
for _, r := range policyReports {
|
||||
newRes := []*report.PolicyReportResult{}
|
||||
newRes := []report.PolicyReportResult{}
|
||||
for _, result := range r.Results {
|
||||
if ruleName != "" && result.Rule == ruleName && result.Policy == policyName {
|
||||
continue
|
||||
|
@ -567,7 +567,7 @@ func (g *ReportGenerator) aggregateReports(namespace string) (
|
|||
}
|
||||
|
||||
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 {
|
||||
aggregatedRequests := []*changerequest.ClusterReportChangeRequest{}
|
||||
|
|
Loading…
Reference in a new issue