Skip to content

Commit

Permalink
INSIGHTS-528 cli opa validation: Support multiple rules that return v…
Browse files Browse the repository at this point in the history
…alues in OPA (#221)

* INSIGHTS-528 cli opa validation: Support multiple rules that return values in OPA

* INSIGHTS-528 cli opa validation: Support multiple rules that return values in OPA
  • Loading branch information
jdesouza authored Dec 6, 2024
1 parent e7ab573 commit 701fddf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pkg/opavalidation/opavalidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ func TestRunWithLibs(t *testing.T) {
assert.Len(t, ais, 1)
assert.Equal(t, "Label is missing", ais[0].Title)
}

func TestMultipleRules(t *testing.T) {
ais, err := opavalidation.Run("test/multipleRules.rego", "testdata/pod1.yaml", opavalidation.ExpectActionItemOptions{}, fwrego.InsightsInfo{}, "", "")
assert.NoError(t, err)
assert.Len(t, ais, 0)
}
15 changes: 15 additions & 0 deletions pkg/opavalidation/test/multipleRules.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fairwinds
foo := {"s": "foo"}
envMaxReplicasDeployments[actionItem] {
print(foo.s)
input.kind == "Deployment"
env_suffix := array.reverse(split(input.metadata.namespace, "-"))[0]
replicas := input.spec.replicas
actionItem := {
"title": "Non-production environment replica count exceeds maximum",
"description": sprintf("The Deployment %v in the %v environment replicas exceed the maximum replica count for this environment.", [input.metadata.name, env_suffix]),
"severity": 0.5,
"remediation": "Reduce the number of replicas",
"category": "Reliability"
}
}
8 changes: 6 additions & 2 deletions pkg/opavalidation/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ func arrayFromRegoOutput(results rego.ResultSet) []interface{} {

for _, result := range results {
for _, pack := range result.Bindings["results"].(map[string]interface{}) {
for _, outputArray := range pack.(map[string]interface{}) {
returnSet = append(returnSet, outputArray.([]interface{})...)
if _, ok := pack.(map[string]interface{}); ok {
for _, outputArray := range pack.(map[string]interface{}) {
if _, ok := outputArray.([]interface{}); ok {
returnSet = append(returnSet, outputArray.([]interface{})...)
}
}
}
}
}
Expand Down

0 comments on commit 701fddf

Please sign in to comment.