1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-05 15:37:19 +00:00

handling autogen

Signed-off-by: NoSkillGirl <singhpooja240393@gmail.com>
This commit is contained in:
NoSkillGirl 2021-09-07 20:23:03 +05:30
parent 745838ff26
commit 1180ba4a86

View file

@ -76,6 +76,7 @@ type TestResults struct {
Result report.PolicyResult `json:"result"`
Status report.PolicyResult `json:"status"`
Resource string `json:"resource"`
AutoGeneratedRule bool `json:"auto_generated_rule"`
}
type ReportResult struct {
@ -234,9 +235,10 @@ func getLocalDirTestFiles(fs billy.Filesystem, path, fileName, valuesFile string
return errors
}
func buildPolicyResults(resps []*response.EngineResponse, testResults []TestResults, infos []policyreport.Info) map[string]report.PolicyReportResult {
func buildPolicyResults(resps []*response.EngineResponse, testResults []TestResults, infos []policyreport.Info) (map[string]report.PolicyReportResult, []TestResults) {
results := make(map[string]report.PolicyReportResult)
now := metav1.Timestamp{Seconds: time.Now().Unix()}
for _, resp := range resps {
policyName := resp.PolicyResponse.Policy.Name
resourceName := resp.PolicyResponse.Resource.Name
@ -253,10 +255,15 @@ func buildPolicyResults(resps []*response.EngineResponse, testResults []TestResu
},
},
}
for _, test := range testResults {
for i, test := range testResults {
if test.Policy == policyName && test.Resource == resourceName {
if !util.ContainsString(rules, test.Rule) {
if !util.ContainsString(rules, "autogen-"+test.Rule) {
result.Result = report.StatusSkip
} else {
testResults[i].AutoGeneratedRule = true
test.Rule = "autogen-" + test.Rule
}
}
resultsKey := fmt.Sprintf("%s-%s-%s", test.Policy, test.Rule, test.Resource)
if _, ok := results[resultsKey]; !ok {
@ -265,9 +272,11 @@ func buildPolicyResults(resps []*response.EngineResponse, testResults []TestResu
}
}
}
for _, info := range infos {
for _, infoResult := range info.Results {
for _, rule := range infoResult.Rules {
if rule.Type != utils.Validation.String() {
continue
}
@ -280,14 +289,16 @@ func buildPolicyResults(resps []*response.EngineResponse, testResults []TestResu
}
result.Rule = rule.Name
result.Result = report.PolicyResult(rule.Check)
result.Source = policyreport.SourceValue
result.Timestamp = now
results[resultsKey] = result
}
}
}
return results
return results, testResults
}
func getPolicyResourceFullPath(path []string, policyResourcePath string, isGit bool) []string {
@ -404,8 +415,9 @@ func applyPoliciesFromPath(fs billy.Filesystem, policyBytes []byte, valuesFile s
pvInfos = append(pvInfos, info)
}
}
resultsMap := buildPolicyResults(validateEngineResponses, values.Results, pvInfos)
resultErr := printTestResult(resultsMap, values.Results, rc)
resultsMap, testResults := buildPolicyResults(validateEngineResponses, values.Results, pvInfos)
resultErr := printTestResult(resultsMap, testResults, rc)
if resultErr != nil {
return sanitizederror.NewWithError("Unable to genrate result. Error:", resultErr)
}
@ -425,7 +437,16 @@ func printTestResult(resps map[string]report.PolicyReportResult, testResults []T
res.Policy = boldFgCyan.Sprintf(v.Policy)
res.Rule = boldFgCyan.Sprintf(v.Rule)
res.Resource = boldFgCyan.Sprintf(v.Resource)
resultKey := fmt.Sprintf("%s-%s-%s", v.Policy, v.Rule, v.Resource)
var ruleNameInResultKey string
if v.AutoGeneratedRule {
ruleNameInResultKey = fmt.Sprintf("autogen-%s", v.Rule)
} else {
ruleNameInResultKey = v.Rule
}
resultKey := fmt.Sprintf("%s-%s-%s", v.Policy, ruleNameInResultKey, v.Resource)
var testRes report.PolicyReportResult
if val, ok := resps[resultKey]; ok {
testRes = val