@@ -37,10 +37,10 @@ import (
3737
3838 "github.com/spectrocloud-labs/valid8or-plugin-aws/api/v1alpha1"
3939 "github.com/spectrocloud-labs/valid8or-plugin-aws/internal/constants"
40- "github.com/spectrocloud-labs/valid8or-plugin-aws/internal/iam"
41- "github.com/spectrocloud-labs/valid8or-plugin-aws/internal/servicequota"
42- "github.com/spectrocloud-labs/valid8or-plugin-aws/internal/tag"
4340 "github.com/spectrocloud-labs/valid8or-plugin-aws/internal/types"
41+ "github.com/spectrocloud-labs/valid8or-plugin-aws/internal/validators/iam"
42+ "github.com/spectrocloud-labs/valid8or-plugin-aws/internal/validators/servicequota"
43+ "github.com/spectrocloud-labs/valid8or-plugin-aws/internal/validators/tag"
4444 valid8orv1alpha1 "github.com/spectrocloud-labs/valid8or/api/v1alpha1"
4545)
4646
@@ -51,12 +51,14 @@ type AwsValidatorReconciler struct {
5151 Scheme * runtime.Scheme
5252}
5353
54+ // monotonicBool starts off false and remains true permanently if updated to true
5455type monotonicBool struct {
5556 ok bool
5657}
5758
59+ // Update updates the status of a monotonic bool. If the monotonic bool is already true, Update() is a noop.
5860func (m * monotonicBool ) Update (ok bool ) {
59- m .ok = ! ok || m .ok
61+ m .ok = ok || m .ok
6062}
6163
6264//+kubebuilder:rbac:groups=validation.spectrocloud.labs,resources=awsvalidators,verbs=get;list;watch;create;update;patch;delete
@@ -127,7 +129,7 @@ func (r *AwsValidatorReconciler) Reconcile(ctx context.Context, req ctrl.Request
127129 if err != nil {
128130 r .Log .V (0 ).Error (err , "failed to reconcile IAM rule" )
129131 }
130- r .safeUpdateValidationResult (nn , * validationResult , failed )
132+ r .safeUpdateValidationResult (nn , validationResult , failed , err )
131133 }
132134
133135 // Service Quota rules
@@ -136,7 +138,7 @@ func (r *AwsValidatorReconciler) Reconcile(ctx context.Context, req ctrl.Request
136138 if err != nil {
137139 r .Log .V (0 ).Error (err , "failed to reconcile Service Quota rule" )
138140 }
139- r .safeUpdateValidationResult (nn , * validationResult , failed )
141+ r .safeUpdateValidationResult (nn , validationResult , failed , err )
140142 }
141143
142144 // Tag rules
@@ -145,7 +147,7 @@ func (r *AwsValidatorReconciler) Reconcile(ctx context.Context, req ctrl.Request
145147 if err != nil {
146148 r .Log .V (0 ).Error (err , "failed to reconcile Tag rule" )
147149 }
148- r .safeUpdateValidationResult (nn , * validationResult , failed )
150+ r .safeUpdateValidationResult (nn , validationResult , failed , err )
149151 }
150152
151153 r .Log .V (0 ).Info ("Requeuing for re-validation in two minutes." , "name" , req .Name , "namespace" , req .Namespace )
@@ -159,18 +161,6 @@ func (r *AwsValidatorReconciler) SetupWithManager(mgr ctrl.Manager) error {
159161 Complete (r )
160162}
161163
162- // safeUpdateValidationResult updates the overall validation result, ensuring that the overall validation status remains failed if a single rule fails
163- func (r * AwsValidatorReconciler ) safeUpdateValidationResult (nn k8stypes.NamespacedName , validationResult types.ValidationResult , failed * monotonicBool ) {
164- didFail := validationResult .State == valid8orv1alpha1 .ValidationFailed
165- failed .Update (didFail )
166- if failed .ok && ! didFail {
167- validationResult .State = valid8orv1alpha1 .ValidationFailed
168- }
169- if err := r .updateValidationResult (nn , validationResult ); err != nil {
170- r .Log .V (0 ).Error (err , "failed to update ValidationResult" )
171- }
172- }
173-
174164// secretKeyAuth creates AWS credentials from a secret containing an access key id and secret access key
175165func (r * AwsValidatorReconciler ) secretKeyAuth (req ctrl.Request , validator * v1alpha1.AwsValidator ) (* credentials.Credentials , * reconcile.Result ) {
176166 authSecret := & corev1.Secret {}
@@ -259,6 +249,26 @@ func (r *AwsValidatorReconciler) handleNewValidationResult(nn k8stypes.Namespace
259249 return nil , nil
260250}
261251
252+ // safeUpdateValidationResult updates the overall validation result, ensuring that the overall validation status remains failed if a single rule fails
253+ func (r * AwsValidatorReconciler ) safeUpdateValidationResult (nn k8stypes.NamespacedName , validationResult * types.ValidationResult , failed * monotonicBool , err error ) {
254+ if err != nil {
255+ validationResult .State = valid8orv1alpha1 .ValidationFailed
256+ validationResult .Condition .Status = corev1 .ConditionFalse
257+ validationResult .Condition .Message = "Validation failed with an unexpected error"
258+ validationResult .Condition .Failures = append (validationResult .Condition .Failures , err .Error ())
259+ }
260+
261+ didFail := validationResult .State == valid8orv1alpha1 .ValidationFailed
262+ failed .Update (didFail )
263+ if failed .ok && ! didFail {
264+ validationResult .State = valid8orv1alpha1 .ValidationFailed
265+ }
266+
267+ if err := r .updateValidationResult (nn , * validationResult ); err != nil {
268+ r .Log .V (0 ).Error (err , "failed to update ValidationResult" )
269+ }
270+ }
271+
262272// updateValidationResult updates the ValidationResult for the active validation rule
263273func (r * AwsValidatorReconciler ) updateValidationResult (nn k8stypes.NamespacedName , res types.ValidationResult ) error {
264274 vr := & valid8orv1alpha1.ValidationResult {}
@@ -269,9 +279,9 @@ func (r *AwsValidatorReconciler) updateValidationResult(nn k8stypes.NamespacedNa
269279
270280 idx := getConditionIndexByValidationRule (vr .Status .Conditions , res .Condition .ValidationRule )
271281 if idx == - 1 {
272- vr .Status .Conditions = append (vr .Status .Conditions , res .Condition )
282+ vr .Status .Conditions = append (vr .Status .Conditions , * res .Condition )
273283 } else {
274- vr .Status .Conditions [idx ] = res .Condition
284+ vr .Status .Conditions [idx ] = * res .Condition
275285 }
276286
277287 if err := r .Status ().Update (context .Background (), vr ); err != nil {
0 commit comments