From a8f9752e9ea97a763eb25d5960362393c2f5fd60 Mon Sep 17 00:00:00 2001 From: Frank Jogeleit Date: Sun, 27 Mar 2022 13:34:13 +0200 Subject: [PATCH 1/2] Add APIVersion to Result APIs Signed-off-by: Frank Jogeleit --- CHANGELOG.md | 4 ++++ charts/policy-reporter/Chart.yaml | 4 ++-- charts/policy-reporter/values.yaml | 2 +- pkg/api/v1/model.go | 1 + pkg/sqlite3/store.go | 8 ++++---- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bebdc58..a186d443 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +# 2.7.1 +* Policy Reporter + * Add Resource APIVersion to the Results REST APIs + # 2.7.0 * Policy Reporter * PolicyReport Filter: diff --git a/charts/policy-reporter/Chart.yaml b/charts/policy-reporter/Chart.yaml index df97f8c2..4d74bd64 100644 --- a/charts/policy-reporter/Chart.yaml +++ b/charts/policy-reporter/Chart.yaml @@ -5,8 +5,8 @@ description: | It creates Prometheus Metrics and can send rule validation events to different targets like Loki, Elasticsearch, Slack or Discord type: application -version: 2.7.0 -appVersion: 2.4.0 +version: 2.7.1 +appVersion: 2.4.1 icon: https://github.com/kyverno/kyverno/raw/main/img/logo.png home: https://kyverno.github.io/policy-reporter diff --git a/charts/policy-reporter/values.yaml b/charts/policy-reporter/values.yaml index 5ad5322d..ba916868 100644 --- a/charts/policy-reporter/values.yaml +++ b/charts/policy-reporter/values.yaml @@ -2,7 +2,7 @@ image: registry: ghcr.io repository: kyverno/policy-reporter pullPolicy: IfNotPresent - tag: 2.4.0 + tag: 2.4.1 imagePullSecrets: [] diff --git a/pkg/api/v1/model.go b/pkg/api/v1/model.go index 6fe3caf1..c45d3775 100644 --- a/pkg/api/v1/model.go +++ b/pkg/api/v1/model.go @@ -29,6 +29,7 @@ type ListResult struct { ID string `json:"id"` Namespace string `json:"namespace,omitempty"` Kind string `json:"kind"` + APIVersion string `json:"apiVersion"` Name string `json:"name"` Message string `json:"message"` Category string `json:"category,omitempty"` diff --git a/pkg/sqlite3/store.go b/pkg/sqlite3/store.go index 522fc7bf..2cb4f2c9 100644 --- a/pkg/sqlite3/store.go +++ b/pkg/sqlite3/store.go @@ -607,7 +607,7 @@ func (s *policyReportStore) FetchNamespacedResults(filter api.Filter) ([]*api.Li } rows, err := s.db.Query(` - SELECT id, resource_namespace, resource_kind, resource_name, message, policy, rule, severity, properties, status, category + SELECT id, resource_namespace, resource_kind, resource_api_version, resource_name, message, policy, rule, severity, properties, status, category FROM policy_report_result WHERE resource_namespace != ""`+where+` ORDER BY resource_namespace, resource_name, resource_uid ASC`, args...) @@ -619,7 +619,7 @@ func (s *policyReportStore) FetchNamespacedResults(filter api.Filter) ([]*api.Li result := api.ListResult{} var props []byte - err := rows.Scan(&result.ID, &result.Namespace, &result.Kind, &result.Name, &result.Message, &result.Policy, &result.Rule, &result.Severity, &props, &result.Status, &result.Category) + err := rows.Scan(&result.ID, &result.Namespace, &result.Kind, &result.APIVersion, &result.Name, &result.Message, &result.Policy, &result.Rule, &result.Severity, &props, &result.Status, &result.Category) if err != nil { return list, err } @@ -641,7 +641,7 @@ func (s *policyReportStore) FetchClusterResults(filter api.Filter) ([]*api.ListR } rows, err := s.db.Query(` - SELECT id, resource_namespace, resource_kind, resource_name, message, policy, rule, severity, properties, status, category + SELECT id, resource_namespace, resource_kind, resource_api_version, resource_name, message, policy, rule, severity, properties, status, category FROM policy_report_result WHERE resource_namespace =""`+where+` ORDER BY resource_namespace, resource_name, resource_uid ASC`, args...) @@ -653,7 +653,7 @@ func (s *policyReportStore) FetchClusterResults(filter api.Filter) ([]*api.ListR result := api.ListResult{} var props []byte - err := rows.Scan(&result.ID, &result.Namespace, &result.Kind, &result.Name, &result.Message, &result.Policy, &result.Rule, &result.Severity, &props, &result.Status, &result.Category) + err := rows.Scan(&result.ID, &result.Namespace, &result.Kind, &result.APIVersion, &result.Name, &result.Message, &result.Policy, &result.Rule, &result.Severity, &props, &result.Status, &result.Category) if err != nil { return list, err } From d03e8e0fa8ee1c3c56f20e2dbc8bc9f5866c88c1 Mon Sep 17 00:00:00 2001 From: Frank Jogeleit Date: Sun, 27 Mar 2022 13:38:03 +0200 Subject: [PATCH 2/2] Update Tests Signed-off-by: Frank Jogeleit --- pkg/api/v1/handler_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/api/v1/handler_test.go b/pkg/api/v1/handler_test.go index 4a56c465..9342e762 100644 --- a/pkg/api/v1/handler_test.go +++ b/pkg/api/v1/handler_test.go @@ -396,7 +396,7 @@ func Test_V1_API(t *testing.T) { t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) } - expected := `[{"id":"123","namespace":"test","kind":"Deployment","name":"nginx","message":"validation error: requests and limits required. Rule autogen-check-for-requests-and-limits failed at path /spec/template/spec/containers/0/resources/requests/","category":"Best Practices","policy":"require-requests-and-limits-required","rule":"autogen-check-for-requests-and-limits","status":"fail","severity":"high"},{"id":"124","namespace":"test","kind":"Pod","name":"nginx","message":"validation error: requests and limits required. Rule autogen-check-for-requests-and-limits failed at path /spec/template/spec/containers/0/resources/requests/","category":"Best Practices","policy":"require-requests-and-limits-required","rule":"autogen-check-for-requests-and-limits","status":"pass"}]` + expected := `[{"id":"123","namespace":"test","kind":"Deployment","apiVersion":"v1","name":"nginx","message":"validation error: requests and limits required. Rule autogen-check-for-requests-and-limits failed at path /spec/template/spec/containers/0/resources/requests/","category":"Best Practices","policy":"require-requests-and-limits-required","rule":"autogen-check-for-requests-and-limits","status":"fail","severity":"high"},{"id":"124","namespace":"test","kind":"Pod","apiVersion":"v1","name":"nginx","message":"validation error: requests and limits required. Rule autogen-check-for-requests-and-limits failed at path /spec/template/spec/containers/0/resources/requests/","category":"Best Practices","policy":"require-requests-and-limits-required","rule":"autogen-check-for-requests-and-limits","status":"pass"}]` if !strings.Contains(rr.Body.String(), expected) { t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected) } @@ -416,7 +416,7 @@ func Test_V1_API(t *testing.T) { t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) } - expected := "{\"id\":\"125\",\"kind\":\"Namespace\",\"name\":\"test\",\"message\":\"validation error: The label `test` is required. Rule check-for-labels-on-namespace\",\"category\":\"Convention\",\"policy\":\"require-ns-labels\",\"rule\":\"check-for-labels-on-namespace\",\"status\":\"pass\",\"severity\":\"medium\"}" + expected := "{\"id\":\"125\",\"kind\":\"Namespace\",\"apiVersion\":\"v1\",\"name\":\"test\",\"message\":\"validation error: The label `test` is required. Rule check-for-labels-on-namespace\",\"category\":\"Convention\",\"policy\":\"require-ns-labels\",\"rule\":\"check-for-labels-on-namespace\",\"status\":\"pass\",\"severity\":\"medium\"}" if !strings.Contains(rr.Body.String(), expected) { t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected) }