Skip to content

Commit

Permalink
fix: ensure State always remains failed if any conditions fail
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>
  • Loading branch information
TylerGillson committed Nov 10, 2023
1 parent 2471411 commit 67e8462
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
10 changes: 0 additions & 10 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 9 additions & 11 deletions pkg/validationresult/validation_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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 {
Expand Down

0 comments on commit 67e8462

Please sign in to comment.