Skip to content

Commit 91b05fb

Browse files
committed
refactor: expose Validate function
Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>
1 parent 6462d1f commit 91b05fb

File tree

2 files changed

+66
-44
lines changed

2 files changed

+66
-44
lines changed

internal/controller/azurevalidator_controller.go

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ import (
3333
"sigs.k8s.io/controller-runtime/pkg/client"
3434

3535
"github.com/validator-labs/validator-plugin-azure/api/v1alpha1"
36-
"github.com/validator-labs/validator-plugin-azure/pkg/azure"
37-
utils "github.com/validator-labs/validator-plugin-azure/pkg/utils/azure"
36+
"github.com/validator-labs/validator-plugin-azure/pkg/validate"
3837
vapi "github.com/validator-labs/validator/api/v1alpha1"
39-
"github.com/validator-labs/validator/pkg/types"
4038
vres "github.com/validator-labs/validator/pkg/validationresult"
4139
)
4240

@@ -105,47 +103,8 @@ func (r *AzureValidatorReconciler) Reconcile(ctx context.Context, req ctrl.Reque
105103
// Always update the expected result count in case the validator's rules have changed
106104
vr.Spec.ExpectedResults = validator.Spec.ResultCount()
107105

108-
resp := types.ValidationResponse{
109-
ValidationRuleResults: make([]*types.ValidationRuleResult, 0, vr.Spec.ExpectedResults),
110-
ValidationRuleErrors: make([]error, 0, vr.Spec.ExpectedResults),
111-
}
112-
113-
azureAPI, err := utils.NewAzureAPI()
114-
if err != nil {
115-
l.Error(err, "failed to create Azure API object")
116-
} else {
117-
azureCtx := context.WithoutCancel(ctx)
118-
if os.Getenv("IS_TEST") == "true" {
119-
var cancel context.CancelFunc
120-
azureCtx, cancel = context.WithDeadline(ctx, time.Now().Add(utils.TestClientTimeout))
121-
defer cancel()
122-
}
123-
124-
daClient := utils.NewDenyAssignmentsClient(azureCtx, azureAPI.DenyAssignmentsClient)
125-
raClient := utils.NewRoleAssignmentsClient(azureCtx, azureAPI.RoleAssignmentsClient)
126-
rdClient := utils.NewRoleDefinitionsClient(azureCtx, azureAPI.RoleDefinitionsClient)
127-
cgiClient := utils.NewCommunityGalleryImagesClient(azureCtx, azureAPI.CommunityGalleryImagesClientProducer)
128-
129-
// RBAC rules
130-
rbacSvc := azure.NewRBACRuleService(daClient, raClient, rdClient)
131-
for _, rule := range validator.Spec.RBACRules {
132-
vrr, err := rbacSvc.ReconcileRBACRule(rule)
133-
if err != nil {
134-
l.Error(err, "failed to reconcile RBAC rule")
135-
}
136-
resp.AddResult(vrr, err)
137-
}
138-
139-
// Community gallery image rules
140-
cgiSvc := azure.NewCommunityGalleryImageRuleService(cgiClient, r.Log)
141-
for _, rule := range validator.Spec.CommunityGalleryImageRules {
142-
vrr, err := cgiSvc.ReconcileCommunityGalleryImageRule(rule)
143-
if err != nil {
144-
l.Error(err, "failed to reconcile community gallery image rule")
145-
}
146-
resp.AddResult(vrr, err)
147-
}
148-
}
106+
// Validate the rules
107+
resp := validate.Validate(validator.Spec, r.Log)
149108

150109
// Patch the ValidationResult with the latest ValidationRuleResults
151110
if err := vres.SafeUpdate(ctx, p, vr, resp, r.Log); err != nil {

pkg/validate/validate.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Package validate defines a Validate function that evaluates an AzureValidatorSpec and returns a ValidationResponse.
2+
package validate
3+
4+
import (
5+
"context"
6+
"os"
7+
"time"
8+
9+
"github.com/go-logr/logr"
10+
"github.com/validator-labs/validator/pkg/types"
11+
12+
"github.com/validator-labs/validator-plugin-azure/api/v1alpha1"
13+
"github.com/validator-labs/validator-plugin-azure/pkg/azure"
14+
utils "github.com/validator-labs/validator-plugin-azure/pkg/utils/azure"
15+
)
16+
17+
// Validate validates the AzureValidatorSpec and returns a ValidationResponse.
18+
func Validate(spec v1alpha1.AzureValidatorSpec, log logr.Logger) types.ValidationResponse {
19+
resp := types.ValidationResponse{
20+
ValidationRuleResults: make([]*types.ValidationRuleResult, 0, spec.ResultCount()),
21+
ValidationRuleErrors: make([]error, 0, spec.ResultCount()),
22+
}
23+
24+
azureAPI, err := utils.NewAzureAPI()
25+
if err != nil {
26+
log.Error(err, "failed to create Azure API object")
27+
return resp
28+
}
29+
30+
ctx := context.Background()
31+
if os.Getenv("IS_TEST") == "true" {
32+
var cancel context.CancelFunc
33+
ctx, cancel = context.WithDeadline(ctx, time.Now().Add(utils.TestClientTimeout))
34+
defer cancel()
35+
}
36+
37+
daClient := utils.NewDenyAssignmentsClient(ctx, azureAPI.DenyAssignmentsClient)
38+
raClient := utils.NewRoleAssignmentsClient(ctx, azureAPI.RoleAssignmentsClient)
39+
rdClient := utils.NewRoleDefinitionsClient(ctx, azureAPI.RoleDefinitionsClient)
40+
cgiClient := utils.NewCommunityGalleryImagesClient(ctx, azureAPI.CommunityGalleryImagesClientProducer)
41+
42+
// RBAC rules
43+
rbacSvc := azure.NewRBACRuleService(daClient, raClient, rdClient)
44+
for _, rule := range spec.RBACRules {
45+
vrr, err := rbacSvc.ReconcileRBACRule(rule)
46+
if err != nil {
47+
log.Error(err, "failed to reconcile RBAC rule")
48+
}
49+
resp.AddResult(vrr, err)
50+
}
51+
52+
// Community gallery image rules
53+
cgiSvc := azure.NewCommunityGalleryImageRuleService(cgiClient, log)
54+
for _, rule := range spec.CommunityGalleryImageRules {
55+
vrr, err := cgiSvc.ReconcileCommunityGalleryImageRule(rule)
56+
if err != nil {
57+
log.Error(err, "failed to reconcile community gallery image rule")
58+
}
59+
resp.AddResult(vrr, err)
60+
}
61+
62+
return resp
63+
}

0 commit comments

Comments
 (0)