Skip to content

Commit

Permalink
add proper details section to the message for testing DVO consumer
Browse files Browse the repository at this point in the history
fix unchecked err (golangci-lint)

fix samples map

fix exported var comment

fix nested structure in test DVO message

fix goconst
  • Loading branch information
Bee-lee committed Feb 25, 2024
1 parent 6abda75 commit 748cf5d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion consumer/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const (
organizationIDNotInAllowList = "organization ID is not in allow list"

testReport = `{"fingerprints": [], "info": [], "skips": [], "system": {}, "analysis_metadata":{"metadata":"some metadata"},"reports":[{"rule_id":"rule_4|RULE_4","component":"ccx_rules_ocp.external.rules.rule_1.report","type":"rule","key":"RULE_4","details":"some details"},{"rule_id":"rule_4|RULE_4","component":"ccx_rules_ocp.external.rules.rule_2.report","type":"rule","key":"RULE_2","details":"some details"},{"rule_id":"rule_5|RULE_5","component":"ccx_rules_ocp.external.rules.rule_5.report","type":"rule","key":"RULE_3","details":"some details"}]}`
testMetrics = `{"system":{"metadata":{},"hostname":null},"fingerprints":[],"version":1,"analysis_metadata":{},"workload_recommendations":[{"response_id":"an_issue|DVO_AN_ISSUE","component":"ccx_rules_ocp.external.dvo.an_issue_pod.recommendation","key":"DVO_AN_ISSUE","details":{},"tags":[],"links":{"jira":["https://issues.redhat.com/browse/AN_ISSUE"],"product_documentation":[]},"workloads":[{"namespace":"namespace-name-A","namespace_uid":"NAMESPACE-UID-A","kind":"DaemonSet","name":"test-name-0099","uid":"UID-0099"}]}]}`
testMetrics = `{"system":{"metadata":{},"hostname":null},"fingerprints":[],"version":1,"analysis_metadata":{},"workload_recommendations":[{"response_id":"an_issue|DVO_AN_ISSUE","component":"ccx_rules_ocp.external.dvo.an_issue_pod.recommendation","key":"DVO_AN_ISSUE","details":{"check_name":"","check_url":"","samples":[{"namespace_uid":"NAMESPACE-UID-A","kind":"DaemonSet","uid":"193a2099-1234-5678-916a-d570c9aac158"}]},"tags":[],"links":{"jira":["https://issues.redhat.com/browse/AN_ISSUE"],"product_documentation":[]},"workloads":[{"namespace":"namespace-name-A","namespace_uid":"NAMESPACE-UID-A","kind":"DaemonSet","name":"test-name-0099","uid":"193a2099-1234-5678-916a-d570c9aac158"}]}]}`
)

var (
Expand Down
9 changes: 5 additions & 4 deletions consumer/dvo_rules_consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,10 @@ func TestParseDVOMessageWithProperMetrics(t *testing.T) {
Details: map[string]interface{}{
"check_name": "",
"check_url": "",
"samples": []map[string]interface{}{
{"namespace_uid": "193a2099-1234-5678-916a-d570c9aac158", "kind": "Deployment", "uid": "0501e150-1234-5678-907f-ee732c25044a"},
{"namespace_uid": "337477af-1234-5678-b258-16f19d8a6289", "kind": "Deployment", "uid": "8c534861-1234-5678-9af5-913de71a545b"},
"samples": []interface{}{
map[string]interface{}{
"namespace_uid": "NAMESPACE-UID-A", "kind": "DaemonSet", "uid": "193a2099-1234-5678-916a-d570c9aac158",
},
},
},
Tags: []string{},
Expand All @@ -363,7 +364,7 @@ func TestParseDVOMessageWithProperMetrics(t *testing.T) {
NamespaceUID: "NAMESPACE-UID-A",
Kind: "DaemonSet",
Name: "test-name-0099",
UID: "UID-0099",
UID: "193a2099-1234-5678-916a-d570c9aac158",
},
},
},
Expand Down
20 changes: 12 additions & 8 deletions server/dvo_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import (
)

const (
namespaceIDParam = "namespace"
namespaceIDParam = "namespace"
// RecommendationSuffix is used to strip a suffix from rule ID
RecommendationSuffix = ".recommendation"
)

Expand Down Expand Up @@ -154,7 +155,7 @@ func (server *HTTPServer) getWorkloads(writer http.ResponseWriter, request *http

processedWorkloads := server.processDVOWorkloads(workloads)

log.Info().Uint32(orgIDStr, uint32(orgID)).Msgf(
log.Debug().Uint32(orgIDStr, uint32(orgID)).Msgf(
"getWorkloads took %s", time.Since(tStart),
)
err = responses.SendOK(writer, responses.BuildOkResponseWithData("workloads", processedWorkloads))
Expand Down Expand Up @@ -220,9 +221,6 @@ func (server *HTTPServer) getWorkloadsForNamespace(writer http.ResponseWriter, r

processedWorkload := server.processSingleDVONamespace(workload)

log.Info().Uint32(orgIDStr, uint32(orgID)).Msgf(
"getWorkloadsForNamespace -\n\n\n\n\n\n\n %v", processedWorkload,
)
log.Info().Uint32(orgIDStr, uint32(orgID)).Msgf(
"getWorkloadsForNamespace took %s", time.Since(tStart),
)
Expand Down Expand Up @@ -256,7 +254,12 @@ func (server *HTTPServer) processSingleDVONamespace(workload types.DVOReport) (
var dvoReport types.DVOMetrics
// remove doubled escape characters due to improper encoding during storage
s := strings.Replace(workload.Report, "\\", "", -1)
json.Unmarshal(json.RawMessage(s), &dvoReport)

err := json.Unmarshal(json.RawMessage(s), &dvoReport)
if err != nil {
log.Error().Err(err).Msg("error unmarshalling full report")
return
}

for _, recommendation := range dvoReport.WorkloadRecommendations {
filteredObjects := make([]DVOObject, 0)
Expand All @@ -273,13 +276,14 @@ func (server *HTTPServer) processSingleDVONamespace(workload types.DVOReport) (
})
}

// recommendation.ResponseID doesn't contain the full rule ID, so smart-proxy was unable to retrieve content, we need to build it
compositeRuleID, err := generators.GenerateCompositeRuleID(
// for some unknown reason, there's a `.recommendation` suffix for each rule hit. no idea why...
// for some unknown reason, there's a `.recommendation` suffix for each rule hit instead of the usual .report
types.RuleFQDN(strings.TrimSuffix(recommendation.Component, RecommendationSuffix)),
types.ErrorKey(recommendation.Key),
)
if err != nil {
log.Error().Err(err).Msgf("error generating composite rule ID for rule [%+v]", recommendation)
log.Error().Err(err).Msg("error generating composite rule ID for rule")
continue
}

Expand Down
12 changes: 8 additions & 4 deletions storage/dvo_recommendations_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ type DVORecommendationsStorage interface {
DeleteReportsForOrg(orgID types.OrgID) error
}

// dvoDBSchema represents the name of the DB schema used by DVO-related queries/migrations
const dvoDBSchema = "dvo"
const (
// dvoDBSchema represents the name of the DB schema used by DVO-related queries/migrations
dvoDBSchema = "dvo"
// orgIDStr used in log messages
orgIDStr = "orgID"
)

// DVORecommendationsDBStorage is an implementation of Storage interface that use selected SQL like database
// like PostgreSQL or RDS etc. That implementation is based on the standard
Expand Down Expand Up @@ -467,7 +471,7 @@ func (storage DVORecommendationsDBStorage) ReadWorkloadsForOrganization(orgID ty
count++
}

log.Debug().Int("org_id", int(orgID)).Msgf("ReadWorkloadsForOrganization processed %d rows in %v", count, time.Since(tStart))
log.Debug().Int(orgIDStr, int(orgID)).Msgf("ReadWorkloadsForOrganization processed %d rows in %v", count, time.Since(tStart))

return workloads, err
}
Expand Down Expand Up @@ -524,7 +528,7 @@ func (storage DVORecommendationsDBStorage) ReadWorkloadsForClusterAndNamespace(
dvoReport.LastCheckedAt = types.Timestamp(lastCheckedAtDB.Time.UTC().Format(time.RFC3339))
dvoReport.ReportedAt = types.Timestamp(reportedAtDB.Time.UTC().Format(time.RFC3339))

log.Debug().Int("org_id", int(orgID)).Msgf("ReadWorkloadsForClusterAndNamespace took %v", time.Since(tStart))
log.Debug().Int(orgIDStr, int(orgID)).Msgf("ReadWorkloadsForClusterAndNamespace took %v", time.Since(tStart))

return dvoReport, err
}
Expand Down
2 changes: 1 addition & 1 deletion storage/rule_disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (storage OCPRecommendationsDBStorage) EnableRuleSystemWide(
ruleID types.RuleID,
errorKey types.ErrorKey,
) error {
log.Info().Int("org_id", int(orgID)).Msgf("re-enabling rule %v|%v", ruleID, errorKey)
log.Info().Int(orgIDStr, int(orgID)).Msgf("re-enabling rule %v|%v", ruleID, errorKey)

const query = `DELETE FROM rule_disable
WHERE org_id = $1
Expand Down

0 comments on commit 748cf5d

Please sign in to comment.