From 67e846293ce26e8f416fbb24d4b247d38f2a15aa Mon Sep 17 00:00:00 2001 From: Tyler Gillson Date: Thu, 9 Nov 2023 21:52:23 -0700 Subject: [PATCH] fix: ensure State always remains failed if any conditions fail Signed-off-by: Tyler Gillson --- pkg/types/types.go | 10 ---------- pkg/validationresult/validation_result.go | 20 +++++++++----------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/pkg/types/types.go b/pkg/types/types.go index 960f885a..cd7f5236 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -7,13 +7,3 @@ type ValidationResult struct { Condition *v1alpha1.ValidationCondition State *v1alpha1.ValidationState } - -// MonotonicBool starts off false and remains true permanently if updated to true -type MonotonicBool struct { - Ok bool -} - -// Update updates the status of a monotonic bool. If the monotonic bool is already true, Update() is a noop. -func (m *MonotonicBool) Update(ok bool) { - m.Ok = ok || m.Ok -} diff --git a/pkg/validationresult/validation_result.go b/pkg/validationresult/validation_result.go index ab705fa8..b68cbb2e 100644 --- a/pkg/validationresult/validation_result.go +++ b/pkg/validationresult/validation_result.go @@ -76,23 +76,13 @@ func HandleNewValidationResult(c client.Client, plugin string, expectedResults i // SafeUpdateValidationResult updates the overall validation result, ensuring // that the overall validation status remains failed if a single rule fails -func SafeUpdateValidationResult( - c client.Client, nn ktypes.NamespacedName, res *types.ValidationResult, - failed *types.MonotonicBool, err error, l logr.Logger, -) { +func SafeUpdateValidationResult(c client.Client, nn ktypes.NamespacedName, res *types.ValidationResult, err error, l logr.Logger) { if err != nil { res.State = ptr.Ptr(v1alpha1.ValidationFailed) res.Condition.Status = corev1.ConditionFalse res.Condition.Message = "Validation failed with an unexpected error" res.Condition.Failures = append(res.Condition.Failures, err.Error()) } - - didFail := *res.State == v1alpha1.ValidationFailed - failed.Update(didFail) - if failed.Ok && !didFail { - res.State = ptr.Ptr(v1alpha1.ValidationFailed) - } - if err := updateValidationResult(c, nn, res, l); err != nil { l.V(0).Error(err, "failed to update ValidationResult") } @@ -104,7 +94,15 @@ func updateValidationResult(c client.Client, nn ktypes.NamespacedName, res *type if err := c.Get(context.Background(), nn, vr); err != nil { return fmt.Errorf("failed to get ValidationResult %s in namespace %s: %v", nn.Name, nn.Namespace, err) } + + // reset to State to ValidationFailed if any conditions failed vr.Status.State = *res.State + for _, c := range vr.Status.Conditions { + if c.Status == corev1.ConditionFalse { + vr.Status.State = v1alpha1.ValidationFailed + break + } + } idx := getConditionIndexByValidationRule(vr.Status.Conditions, res.Condition.ValidationRule) if idx == -1 {