@@ -19,32 +19,22 @@ package controller
19
19
20
20
import (
21
21
"context"
22
- "fmt"
23
22
"os"
24
23
"time"
25
24
26
25
"github.com/go-logr/logr"
27
26
"github.com/pkg/errors"
28
27
corev1 "k8s.io/api/core/v1"
29
28
apierrs "k8s.io/apimachinery/pkg/api/errors"
30
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31
29
"k8s.io/apimachinery/pkg/runtime"
32
30
ktypes "k8s.io/apimachinery/pkg/types"
33
31
"sigs.k8s.io/cluster-api/util/patch"
34
32
ctrl "sigs.k8s.io/controller-runtime"
35
33
"sigs.k8s.io/controller-runtime/pkg/client"
36
34
37
35
"github.com/validator-labs/validator-plugin-aws/api/v1alpha1"
38
- "github.com/validator-labs/validator-plugin-aws/internal/aws"
39
- "github.com/validator-labs/validator-plugin-aws/internal/constants"
40
- "github.com/validator-labs/validator-plugin-aws/internal/validators"
41
- "github.com/validator-labs/validator-plugin-aws/internal/validators/ami"
42
- "github.com/validator-labs/validator-plugin-aws/internal/validators/iam"
43
- "github.com/validator-labs/validator-plugin-aws/internal/validators/servicequota"
44
- "github.com/validator-labs/validator-plugin-aws/internal/validators/tag"
36
+ "github.com/validator-labs/validator-plugin-aws/pkg/validate"
45
37
vapi "github.com/validator-labs/validator/api/v1alpha1"
46
- "github.com/validator-labs/validator/pkg/types"
47
- "github.com/validator-labs/validator/pkg/util"
48
38
vres "github.com/validator-labs/validator/pkg/validationresult"
49
39
)
50
40
@@ -95,16 +85,16 @@ func (r *AwsValidatorReconciler) Reconcile(ctx context.Context, req ctrl.Request
95
85
return ctrl.Result {}, err
96
86
}
97
87
nn := ktypes.NamespacedName {
98
- Name : validationResultName (validator ),
88
+ Name : vres . Name (validator ),
99
89
Namespace : req .Namespace ,
100
90
}
101
91
if err := r .Get (ctx , nn , vr ); err == nil {
102
- vres .HandleExistingValidationResult (vr , r .Log )
92
+ vres .HandleExisting (vr , r .Log )
103
93
} else {
104
94
if ! apierrs .IsNotFound (err ) {
105
95
l .Error (err , "unexpected error getting ValidationResult" )
106
96
}
107
- if err := vres .HandleNewValidationResult (ctx , r .Client , p , buildValidationResult (validator ), r .Log ); err != nil {
97
+ if err := vres .HandleNew (ctx , r .Client , p , vres . Build (validator ), r .Log ); err != nil {
108
98
return ctrl.Result {}, err
109
99
}
110
100
return ctrl.Result {RequeueAfter : time .Millisecond }, nil
@@ -113,111 +103,11 @@ func (r *AwsValidatorReconciler) Reconcile(ctx context.Context, req ctrl.Request
113
103
// Always update the expected result count in case the validator's rules have changed
114
104
vr .Spec .ExpectedResults = validator .Spec .ResultCount ()
115
105
116
- resp := types.ValidationResponse {
117
- ValidationRuleResults : make ([]* types.ValidationRuleResult , 0 , vr .Spec .ExpectedResults ),
118
- ValidationRuleErrors : make ([]error , 0 , vr .Spec .ExpectedResults ),
119
- }
120
-
121
- // AMI rules
122
- for _ , rule := range validator .Spec .AmiRules {
123
- awsAPI , err := aws .NewAPI (validator .Spec .Auth , rule .Region )
124
- if err != nil {
125
- errMsg := "Failed to reconcile AMI rule"
126
- r .Log .V (0 ).Error (err , errMsg )
127
- vrr := validators .BuildValidationResult (rule .Name , errMsg , constants .ValidationTypeAmi )
128
- resp .AddResult (vrr , err )
129
- continue
130
- }
131
- amiRuleService := ami .NewAmiRuleService (r .Log , awsAPI .EC2 )
132
- vrr , err := amiRuleService .ReconcileAmiRule (rule )
133
- if err != nil {
134
- r .Log .V (0 ).Error (err , "failed to reconcile AMI rule" )
135
- }
136
- resp .AddResult (vrr , err )
137
- }
138
-
139
- // IAM rules
140
- awsAPI , err := aws .NewAPI (validator .Spec .Auth , validator .Spec .DefaultRegion )
141
- if err != nil {
142
- r .Log .V (0 ).Error (err , "failed to get AWS client" )
143
- } else {
144
- iamRuleService := iam .NewIAMRuleService (r .Log , awsAPI .IAM )
145
-
146
- for _ , rule := range validator .Spec .IamRoleRules {
147
- vrr , err := iamRuleService .ReconcileIAMRoleRule (rule )
148
- if err != nil {
149
- r .Log .V (0 ).Error (err , "failed to reconcile IAM role rule" )
150
- }
151
- resp .AddResult (vrr , err )
152
- }
153
- for _ , rule := range validator .Spec .IamUserRules {
154
- vrr , err := iamRuleService .ReconcileIAMUserRule (rule )
155
- if err != nil {
156
- r .Log .V (0 ).Error (err , "failed to reconcile IAM user rule" )
157
- }
158
- resp .AddResult (vrr , err )
159
- }
160
- for _ , rule := range validator .Spec .IamGroupRules {
161
- vrr , err := iamRuleService .ReconcileIAMGroupRule (rule )
162
- if err != nil {
163
- r .Log .V (0 ).Error (err , "failed to reconcile IAM group rule" )
164
- }
165
- resp .AddResult (vrr , err )
166
- }
167
- for _ , rule := range validator .Spec .IamPolicyRules {
168
- vrr , err := iamRuleService .ReconcileIAMPolicyRule (rule )
169
- if err != nil {
170
- r .Log .V (0 ).Error (err , "failed to reconcile IAM policy rule" )
171
- }
172
- resp .AddResult (vrr , err )
173
- }
174
- }
175
-
176
- // Service Quota rules
177
- for _ , rule := range validator .Spec .ServiceQuotaRules {
178
- awsAPI , err := aws .NewAPI (validator .Spec .Auth , rule .Region )
179
- if err != nil {
180
- errMsg := "Failed to reconcile Service Quota rule"
181
- r .Log .V (0 ).Error (err , errMsg )
182
- vrr := validators .BuildValidationResult (rule .Name , errMsg , constants .ValidationTypeServiceQuota )
183
- resp .AddResult (vrr , err )
184
- continue
185
- }
186
- svcQuotaService := servicequota .NewServiceQuotaRuleService (
187
- r .Log ,
188
- awsAPI .EC2 ,
189
- awsAPI .EFS ,
190
- awsAPI .ELB ,
191
- awsAPI .ELBV2 ,
192
- awsAPI .SQ ,
193
- )
194
- vrr , err := svcQuotaService .ReconcileServiceQuotaRule (rule )
195
- if err != nil {
196
- r .Log .V (0 ).Error (err , "failed to reconcile Service Quota rule" )
197
- }
198
- resp .AddResult (vrr , err )
199
- }
200
-
201
- // Tag rules
202
- for _ , rule := range validator .Spec .TagRules {
203
- awsAPI , err := aws .NewAPI (validator .Spec .Auth , rule .Region )
204
- if err != nil {
205
- errMsg := "Failed to reconcile Tag rule"
206
- r .Log .V (0 ).Error (err , errMsg )
207
- vrr := validators .BuildValidationResult (rule .Name , errMsg , constants .ValidationTypeTag )
208
- resp .AddResult (vrr , err )
209
- continue
210
- }
211
- tagRuleService := tag .NewTagRuleService (r .Log , awsAPI .EC2 )
212
- vrr , err := tagRuleService .ReconcileTagRule (rule )
213
- if err != nil {
214
- r .Log .V (0 ).Error (err , "failed to reconcile Tag rule" )
215
- }
216
- resp .AddResult (vrr , err )
217
- }
106
+ // Validate the rules
107
+ resp := validate .Validate (validator .Spec , r .Log )
218
108
219
109
// Patch the ValidationResult with the latest ValidationRuleResults
220
- if err := vres .SafeUpdateValidationResult (ctx , p , vr , resp , r .Log ); err != nil {
110
+ if err := vres .SafeUpdate (ctx , p , vr , resp , r .Log ); err != nil {
221
111
return ctrl.Result {}, err
222
112
}
223
113
@@ -250,29 +140,3 @@ func (r *AwsValidatorReconciler) SetupWithManager(mgr ctrl.Manager) error {
250
140
For (& v1alpha1.AwsValidator {}).
251
141
Complete (r )
252
142
}
253
-
254
- func buildValidationResult (validator * v1alpha1.AwsValidator ) * vapi.ValidationResult {
255
- return & vapi.ValidationResult {
256
- ObjectMeta : metav1.ObjectMeta {
257
- Name : validationResultName (validator ),
258
- Namespace : validator .Namespace ,
259
- OwnerReferences : []metav1.OwnerReference {
260
- {
261
- APIVersion : validator .APIVersion ,
262
- Kind : validator .Kind ,
263
- Name : validator .Name ,
264
- UID : validator .UID ,
265
- Controller : util .Ptr (true ),
266
- },
267
- },
268
- },
269
- Spec : vapi.ValidationResultSpec {
270
- Plugin : constants .PluginCode ,
271
- ExpectedResults : validator .Spec .ResultCount (),
272
- },
273
- }
274
- }
275
-
276
- func validationResultName (validator * v1alpha1.AwsValidator ) string {
277
- return fmt .Sprintf ("validator-plugin-aws-%s" , validator .Name )
278
- }
0 commit comments