2020-10-16 16:26:45 -07:00
|
|
|
package apply
|
|
|
|
|
|
|
|
import (
|
2022-05-17 13:12:43 +02:00
|
|
|
policyreportv1alpha2 "github.com/kyverno/kyverno/api/policyreport/v1alpha2"
|
2022-04-14 17:50:18 +05:30
|
|
|
sanitizederror "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/sanitizedError"
|
2020-10-16 16:26:45 -07:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
|
|
|
)
|
|
|
|
|
2021-07-09 18:01:46 -07:00
|
|
|
// generateCLIRaw merges all policy reports to a singe cluster policy report
|
|
|
|
func generateCLIRaw(reports []*unstructured.Unstructured) (*unstructured.Unstructured, error) {
|
2020-10-16 16:26:45 -07:00
|
|
|
for _, report := range reports {
|
|
|
|
if report.GetNamespace() != "" {
|
|
|
|
report.SetNamespace("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mergeClusterReport(reports)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeClusterReport(reports []*unstructured.Unstructured) (*unstructured.Unstructured, error) {
|
|
|
|
var resultsEntry []interface{}
|
|
|
|
res := &unstructured.Unstructured{}
|
|
|
|
res.SetName(clusterpolicyreport)
|
|
|
|
res.SetKind("ClusterPolicyReport")
|
2022-05-17 13:12:43 +02:00
|
|
|
res.SetAPIVersion(policyreportv1alpha2.SchemeGroupVersion.String())
|
2020-10-16 16:26:45 -07:00
|
|
|
|
|
|
|
for _, report := range reports {
|
|
|
|
if report.GetNamespace() != "" {
|
|
|
|
// skip namespace report
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
mergeResults(report, &resultsEntry)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := unstructured.SetNestedSlice(res.Object, resultsEntry, "results"); err != nil {
|
2020-11-17 13:07:30 -08:00
|
|
|
return nil, sanitizederror.NewWithError("failed to set results entry", err)
|
2020-10-16 16:26:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
summary := updateSummary(resultsEntry)
|
|
|
|
if err := unstructured.SetNestedField(res.Object, summary, "summary"); err != nil {
|
2020-11-17 13:07:30 -08:00
|
|
|
return nil, sanitizederror.NewWithError("failed to set summary", err)
|
2020-10-16 16:26:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeResults(report *unstructured.Unstructured, results *[]interface{}) {
|
|
|
|
entries, ok, err := unstructured.NestedSlice(report.UnstructuredContent(), "results")
|
|
|
|
if err != nil {
|
2020-11-19 15:56:14 +05:30
|
|
|
log.Log.V(3).Info("failed to get results entry", "report", report.GetName(), "error", err)
|
2020-10-16 16:26:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
2020-10-19 15:07:28 -07:00
|
|
|
*results = append(*results, entries...)
|
2020-10-16 16:26:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 15:07:28 -07:00
|
|
|
func updateSummary(results []interface{}) map[string]interface{} {
|
|
|
|
summary := make(map[string]interface{})
|
2022-05-17 13:12:43 +02:00
|
|
|
status := []string{policyreportv1alpha2.StatusPass, policyreportv1alpha2.StatusFail, policyreportv1alpha2.StatusError, policyreportv1alpha2.StatusSkip, policyreportv1alpha2.StatusWarn}
|
2020-11-13 00:48:40 +05:30
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
if _, ok := summary[status[i]].(int64); !ok {
|
|
|
|
summary[status[i]] = int64(0)
|
|
|
|
}
|
|
|
|
}
|
2020-10-16 16:26:45 -07:00
|
|
|
for _, result := range results {
|
|
|
|
typedResult, ok := result.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-21 19:35:17 +02:00
|
|
|
switch typedResult["result"].(string) {
|
2022-05-17 13:12:43 +02:00
|
|
|
case policyreportv1alpha2.StatusPass:
|
|
|
|
pass, _ := summary[policyreportv1alpha2.StatusPass].(int64)
|
2020-10-31 01:55:05 +05:30
|
|
|
pass++
|
2022-05-17 13:12:43 +02:00
|
|
|
summary[policyreportv1alpha2.StatusPass] = pass
|
|
|
|
case policyreportv1alpha2.StatusFail:
|
|
|
|
fail, _ := summary[policyreportv1alpha2.StatusFail].(int64)
|
2020-10-19 15:07:28 -07:00
|
|
|
fail++
|
2022-05-17 13:12:43 +02:00
|
|
|
summary[policyreportv1alpha2.StatusFail] = fail
|
|
|
|
case policyreportv1alpha2.StatusWarn:
|
|
|
|
warn, _ := summary[policyreportv1alpha2.StatusWarn].(int64)
|
2020-10-19 15:07:28 -07:00
|
|
|
warn++
|
2022-05-17 13:12:43 +02:00
|
|
|
summary[policyreportv1alpha2.StatusWarn] = warn
|
|
|
|
case policyreportv1alpha2.StatusError:
|
|
|
|
e, _ := summary[policyreportv1alpha2.StatusError].(int64)
|
2020-10-19 15:07:28 -07:00
|
|
|
e++
|
2022-05-17 13:12:43 +02:00
|
|
|
summary[policyreportv1alpha2.StatusError] = e
|
|
|
|
case policyreportv1alpha2.StatusSkip:
|
|
|
|
skip, _ := summary[policyreportv1alpha2.StatusSkip].(int64)
|
2020-10-19 15:07:28 -07:00
|
|
|
skip++
|
2022-05-17 13:12:43 +02:00
|
|
|
summary[policyreportv1alpha2.StatusSkip] = skip
|
2020-10-16 16:26:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return summary
|
|
|
|
}
|