Skip to content

Commit

Permalink
✨ add analysis insights (#618)
Browse files Browse the repository at this point in the history
*What creates Insights?*

_Rules that do not have an effort value defined or have an effort value
of 0, will generate insights._

Depending on what rule actions they define, the output will look
different. See following table for all scenarios:

| <rule>.effort | <rule>.tag | <rule>.message |  Description |
| --- | --- | --- | --- |
| 0 | Defined | Not Defined | Rule creates _tags_ and an _insight_.
Insight does not contain _message_. |
| 0 | Defined | Defined | Rule creates _tags_ and an _insight_. Insight
contains _message_. |
| 0 | Not Defined | Defined | Rule creates an _insight_. Insight
contains _message_. |
| 1 | Defined | Defined | Rule creates _tags_ and a _violation_. No
insights here as effort is non-zero. |

See
https://github.com/konveyor/enhancements/edit/master/enhancements/analysis-insights/README.md

Signed-off-by: Pranav Gaikwad <[email protected]>
  • Loading branch information
pranavgaikwad authored Jun 12, 2024
1 parent 24e5b22 commit e2284ce
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 32 deletions.
184 changes: 160 additions & 24 deletions demo-output.yaml

Large diffs are not rendered by default.

30 changes: 25 additions & 5 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (r *ruleEngine) createRuleSet(ruleSet RuleSet) *konveyor.RuleSet {
Description: ruleSet.Description,
Tags: []string{},
Violations: map[string]konveyor.Violation{},
Insights: map[string]konveyor.Violation{},
Errors: map[string]string{},
Unmatched: []string{},
Skipped: []string{},
Expand Down Expand Up @@ -204,12 +205,17 @@ func (r *ruleEngine) RunRules(ctx context.Context, ruleSets []RuleSet, selectors
}
} else {
atomic.AddInt32(&matchedRules, 1)

rs, ok := mapRuleSets[response.RuleSetName]
if !ok {
r.logger.Info("this should never happen that we don't find the ruleset")
return
}
// when a rule has 0 effort, we should create an insight instead
if response.Rule.Effort == nil || *response.Rule.Effort == 0 {
rs.Insights[response.Rule.RuleID] = violation
} else {
rs.Violations[response.Rule.RuleID] = violation
}
rs.Violations[response.Rule.RuleID] = violation
}
} else {
atomic.AddInt32(&unmatchedRules, 1)
Expand Down Expand Up @@ -291,9 +297,9 @@ func (r *ruleEngine) filterRules(ruleSets []RuleSet, selectors ...RuleSelector)
rule: rule,
ruleSetName: ruleSet.Name,
})
// if both message and tag are set
// split message part into a new rule
if rule.Perform.Message.Text != nil {
// if both message and tag are set, split message part into a new rule if effort is non-zero
// if effort is zero, we do not want to create a violation but only tag and an insight
if rule.Perform.Message.Text != nil && rule.Effort != nil && *rule.Effort != 0 {
rule.Perform.Tag = nil
otherRules = append(
otherRules,
Expand Down Expand Up @@ -376,6 +382,20 @@ func (r *ruleEngine) runTaggingRules(ctx context.Context, infoRules []ruleMessag
}
mapRuleSets[ruleMessage.ruleSetName] = rs
}
// create an insight for this tag
violation, err := r.createViolation(ctx, response, rule)
if err != nil {
r.logger.Error(err, "unable to create violation from response", "ruleID", rule.RuleID)
}
if rs, ok := mapRuleSets[ruleMessage.ruleSetName]; ok {
violation.Effort = nil
violation.Category = nil
// we need to tie these incidents back to tags that created them
for tag := range tags {
violation.Labels = append(violation.Labels, fmt.Sprintf("tag=%s", tag))
}
rs.Insights[rule.RuleID] = violation
}
} else {
r.logger.Info("info rule not matched", "rule", rule.RuleID)
if rs, ok := mapRuleSets[ruleMessage.ruleSetName]; ok {
Expand Down
5 changes: 5 additions & 0 deletions output/v1/konveyor/violations.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type RuleSet struct {
// their respective generated violations.
Violations map[string]Violation `yaml:"violations,omitempty" json:"violations,omitempty"`

// Insights is a map containing violations generated for informational rules
// in a ruleset. These rules do not have an effort. They exist to provide
// additional information about a tag.
Insights map[string]Violation `yaml:"insights,omitempty" json:"insights,omitempty"`

// Errors is a map containing errors generated during evaluation
// of rules in this ruleset. Keys are rule IDs, values are
// their respective generated errors.
Expand Down
1 change: 0 additions & 1 deletion provider_container_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@
"name": "builtin",
"initConfig": [
{"location": "examples/java/"},
{"location": "examples/python/"},
{"location": "examples/golang/"},
{"location": "examples/customers-tomcat-legacy/"},
{
Expand Down
1 change: 0 additions & 1 deletion provider_local_external_images.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
"initConfig": [
{"location": "external-providers/java-external-provider/examples/java"},
{"location": "external-providers/java-external-provider/examples/customers-tomcat-legacy"},
{"location": "examples/python/"},
{"location": "examples/golang/"},
{
"location": "examples/builtin/",
Expand Down
1 change: 0 additions & 1 deletion provider_pod_local_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@
"name": "builtin",
"initConfig": [
{"location": "examples/java/"},
{"location": "examples/python/"},
{"location": "examples/golang/"},
{"location": "examples/customers-tomcat-legacy/"},
{
Expand Down
21 changes: 21 additions & 0 deletions rule-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@
pattern: "*.go"
- message: not any go files
ruleID: file-002
effort: 1
when:
builtin.file:
pattern: "*.go"
not: true
- message: POM XML dependencies - '{{{matchingXML}}}'
ruleID: xml-pom-001
effort: 1
when:
builtin.xml:
xpath: "//dependencies/dependency"
- message: '{{{matchingXML}}}'
ruleID: chain-pom-001
effort: 1
when:
or:
- builtin.xml:
Expand All @@ -37,6 +40,7 @@
ignore: true
- message: apiextensions/v1beta1/customresourcedefinitions is deprecated, apiextensions/v1/customresourcedefinitions should be used instead
ruleID: lang-ref-001
effort: 1
when:
or:
- java.referenced:
Expand All @@ -46,11 +50,13 @@
pattern: "v1beta1.CustomResourceDefinition"
- message: 'golang apiextensions/v1/customresourcedefinitions found {{file}}:{{lineNumber}}'
ruleID: go-lang-ref-001
effort: 1
when:
go.referenced:
pattern: "v1beta1.CustomResourceDefinition"
- message: testing nested conditions
ruleID: lang-ref-002
effort: 1
when:
# This is purposfully failing, the golang reference will not
# find anything. testing that `and` will work correctly
Expand All @@ -62,6 +68,7 @@
location: TYPE
- message: 'java found apiextensions/v1/customresourcedefinitions found {{file}}:{{lineNumber}}'
ruleID: lang-ref-003
effort: 1
when:
java.referenced:
pattern: "*apiextensions.v1beta1.CustomResourceDefinition*"
Expand Down Expand Up @@ -109,6 +116,7 @@
- Java
- message: "dependency {{name}} with {{version}} is bad and you should feel bad for using it"
ruleID: golang-gomod-dependencies
effort: 1
when:
and:
- go.dependency:
Expand All @@ -123,6 +131,7 @@
upperbound: v4.2.2
- message: "dependency {{name}} with {{version}} is bad and you should feel bad for using it"
ruleID: java-pomxml-dependencies
effort: 1
when:
and:
- java.dependency:
Expand All @@ -134,6 +143,7 @@
lowerbound: 5.0.100
- message: "found generic call"
ruleID: lang-ref-004
effort: 1
customVariables:
- pattern: '([A-z]+)\.get\(\)'
name: VariableName
Expand All @@ -143,6 +153,7 @@
pattern: com.example.apps.GenericClass.get
- message: condition entries should evaluate out of order
ruleID: singleton-sessionbean-00001
effort: 1
when:
or:
- as: sessionbean
Expand All @@ -156,6 +167,7 @@
pattern: javax.ejb.Singleton
- message: condition entries should evaluate in order
ruleID: singleton-sessionbean-00002
effort: 1
when:
or:
- as: singleton
Expand All @@ -169,12 +181,14 @@
pattern: javax.ejb.SessionBean
- message: "error test"
ruleID: error-rule-001
effort: 1
when:
builtin.xml:
xpath:
invalid-query: "test"
- message: "JBoss 5.x EAR descriptor (jboss-app.xml) was found with public-id"
ruleID: jboss-eap5-7-xml-02000
effort: 1
when:
builtin.xmlPublicID:
regex: .*JBoss.+DTD Java EE.+5.*
Expand All @@ -187,23 +201,27 @@
- Golang
- message: "Found usage of openjdk base image"
ruleID: filecontent-codesnip-test
effort: 1
when:
builtin.filecontent:
pattern: "^FROM.*openjdk-11.*"
filePattern: "Dockerfile"

- message: python sample rule 001
ruleID: python-sample-rule-001
effort: 1
when:
python.referenced:
pattern: "hello_world"
- message: python sample rule 002
ruleID: python-sample-rule-002
effort: 1
when:
python.referenced:
pattern: "speak"
- message: python sample rule 003
ruleID: python-sample-rule-003
effort: 1
when:
python.referenced:
pattern: "create_custom_resource_definition"
Expand Down Expand Up @@ -235,6 +253,7 @@
description: "Test code snippets when match is a key of a XML node"
message: "The code snippet should point to <beans> in the beans.xml file"
ruleID: xml-test-key-match
effort: 1
when:
builtin.xml:
filepaths:
Expand Down Expand Up @@ -269,6 +288,7 @@
We are filtering some out using includedPaths setting.
message: Only incidents in dir-0/test.json should be found
ruleID: builtin-inclusion-test-json
effort: 1
when:
and:
- builtin.json:
Expand All @@ -283,6 +303,7 @@
We are filtering some out using includedPaths setting.
message: Only incidents in dir-0/test.xml should be found
ruleID: builtin-inclusion-test-xml
effort: 1
when:
and:
- builtin.xml:
Expand Down

0 comments on commit e2284ce

Please sign in to comment.