Skip to content

Commit

Permalink
refactor: Add BuildDefault validation rule result util code for all p…
Browse files Browse the repository at this point in the history
…lugins to use (#419)

## Description
Moves the code in `pkg/validators/common.go` of
https://github.com/validator-labs/validator-plugin-aws to this repo so
it can be used in all plugins, as function `BuildDefault`. Sometimes the
default result doesn't use the `<prefix>-<sanitized_rule_name>` format,
so there is a param that can be used to override this and use a
particular value.

---------

Signed-off-by: Matt Welke <matt.welke@spectrocloud.com>
  • Loading branch information
mattwelke committed Sep 11, 2024
1 parent 638c5d2 commit a01093e
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build
Submodule build updated 1 files
+1 −1 makelib/common.mk
2 changes: 1 addition & 1 deletion chart/validator/README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions chart/validator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ plugins:
- chart:
name: validator-plugin-vsphere
repository: validator-plugin-vsphere
version: v0.0.34
version: v0.1.2
values: |-
controllerManager:
kubeRbacProxy:
Expand Down Expand Up @@ -623,7 +623,7 @@ plugins:
- ALL
image:
repository: quay.io/validator-labs/validator-plugin-vsphere
tag: v0.0.34
tag: v0.1.2
resources:
limits:
cpu: 500m
Expand Down
4 changes: 2 additions & 2 deletions hauler-manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spec:
- name: quay.io/validator-labs/validator-plugin-maas:v0.0.12
- name: quay.io/validator-labs/validator-plugin-network:v0.1.0
- name: quay.io/validator-labs/validator-plugin-oci:v0.3.3
- name: quay.io/validator-labs/validator-plugin-vsphere:v0.0.34
- name: quay.io/validator-labs/validator-plugin-vsphere:v0.1.2
- name: quay.io/validator-labs/validator-certs-init:1.0.0
- name: gcr.io/spectro-images-public/release/spectro-cleanup:1.2.0
- name: kindest/node:v1.30.2
Expand Down Expand Up @@ -48,7 +48,7 @@ spec:
version: 0.3.3
- name: validator-plugin-vsphere
repoURL: https://validator-labs.github.io/validator-plugin-vsphere
version: 0.0.34
version: 0.1.2
---
apiVersion: content.hauler.cattle.io/v1alpha1
kind: Files
Expand Down
39 changes: 39 additions & 0 deletions pkg/validationruleresult/validation_rule_result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Package validationruleresult includes code to help work with ValidationRuleResults.
package validationruleresult

import (
"fmt"

"github.com/validator-labs/validator/api/v1alpha1"
"github.com/validator-labs/validator/pkg/constants"
"github.com/validator-labs/validator/pkg/types"
"github.com/validator-labs/validator/pkg/util"
)

// BuildDefault builds a default ValidationResult for a given validation type.
//
// The latest condition message param should be one that conveys that validation succeeded. The
// validation type param should be unique for each combination of plugin and rule (e.g.
// "aws-iam-role-policy").
//
// One of the validation rule or rule name params must be provided. If validation rule is provided,
// it is used as the validation rule description of the condition. If it isn't, a description is
// generated based on the rule name and used instead.
func BuildDefault(latestConditionMsg, validationType, validationRule, ruleName string) *types.ValidationRuleResult {
state := v1alpha1.ValidationSucceeded
latestCondition := v1alpha1.DefaultValidationCondition()
latestCondition.Message = latestConditionMsg
if validationRule != "" {
latestCondition.ValidationRule = validationRule
} else {
latestCondition.ValidationRule = fmt.Sprintf(
"%s-%s",
constants.ValidationRulePrefix, util.Sanitize(ruleName),
)
}
latestCondition.ValidationType = validationType
return &types.ValidationRuleResult{
Condition: &latestCondition,
State: &state,
}
}
81 changes: 81 additions & 0 deletions pkg/validationruleresult/validation_rule_result_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Package validationruleresult includes code to help work with ValidationRuleResults.
package validationruleresult

import (
"encoding/json"
"reflect"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/validator-labs/validator/api/v1alpha1"
"github.com/validator-labs/validator/pkg/types"
"github.com/validator-labs/validator/pkg/util"
)

func TestBuildDefault(t *testing.T) {
type args struct {
latestConditionMsg string
validationType string
validationRule string
ruleName string
}
tests := []struct {
name string
args args
want *types.ValidationRuleResult
}{
{
name: "uses validation rule description if provided",
args: args{
latestConditionMsg: "a",
validationType: "b",
validationRule: "c",
ruleName: "",
},
want: &types.ValidationRuleResult{
Condition: &v1alpha1.ValidationCondition{
ValidationType: "b",
ValidationRule: "c",
Message: "a",
Status: corev1.ConditionTrue,
},
State: util.Ptr(v1alpha1.ValidationSucceeded),
},
},
{
name: "correctly generates a validation rule description based on rule name if validation rule not provided",
args: args{
latestConditionMsg: "a",
validationType: "b",
validationRule: "",
ruleName: "d",
},
want: &types.ValidationRuleResult{
Condition: &v1alpha1.ValidationCondition{
ValidationType: "b",
ValidationRule: "validation-d",
Message: "a",
Status: corev1.ConditionTrue,
},
State: util.Ptr(v1alpha1.ValidationSucceeded),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := BuildDefault(tt.args.latestConditionMsg, tt.args.validationType, tt.args.validationRule, tt.args.ruleName)

// Remove the time. It will be different each run.
got.Condition.LastValidationTime = metav1.Time{}

gotJSON, _ := json.MarshalIndent(got, "", " ")
wantJSON, _ := json.MarshalIndent(tt.want, "", " ")

if !reflect.DeepEqual(gotJSON, wantJSON) {
t.Errorf("BuildDefault() = %v, want %v", string(gotJSON), string(wantJSON))
}
})
}
}

0 comments on commit a01093e

Please sign in to comment.