Skip to content

Commit

Permalink
fix: prevent extraneous sink emissions
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 a6ce7af commit 22de119
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
3 changes: 3 additions & 0 deletions api/v1alpha1/validationresult_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (
// ValidationResultSpec defines the desired state of ValidationResult
type ValidationResultSpec struct {
Plugin string `json:"plugin"`
// The number of rules in the validator plugin spec, hence the number of expected ValidationResults.
// +kubebuilder:validation:Minimum=1
ExpectedResults int `json:"expectedResults"`
}

type ValidationState string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ spec:
spec:
description: ValidationResultSpec defines the desired state of ValidationResult
properties:
expectedResults:
type: integer
plugin:
type: string
required:
- expectedResults
- plugin
type: object
status:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ spec:
spec:
description: ValidationResultSpec defines the desired state of ValidationResult
properties:
expectedResults:
description: The number of rules in the validator plugin spec, hence
the number of expected ValidationResults.
minimum: 1
type: integer
plugin:
type: string
required:
- expectedResults
- plugin
type: object
status:
Expand Down
7 changes: 4 additions & 3 deletions internal/controller/validationresult_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ func (r *ValidationResultReconciler) Reconcile(ctx context.Context, req ctrl.Req
)
}

// emit ValidationResult to a sink - either on the 1st reconciliation, or if its hash changes

if vc.Spec.Sink != nil && len(vr.Status.Conditions) > 0 && (!ok || prevHash != currHash) {
// Emit ValidationResult to a sink - either upon the completion of the 1st reconciliation, or if its hash changes.
// Do not emit until the number of conditions matches the expected number of results, otherwise N
// emissions will occur during the 1st reconciliation, where N is the number of rules in the validator.
if vc.Spec.Sink != nil && len(vr.Status.Conditions) == vr.Spec.ExpectedResults && (!ok || prevHash != currHash) {
sinkState = v1alpha1.SinkEmitFailed

sink := sinks.NewSink(vc.Spec.Sink.Type, r.Log)
Expand Down
17 changes: 10 additions & 7 deletions pkg/validationresult/validation_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,18 @@ func HandleExistingValidationResult(nn ktypes.NamespacedName, vr *v1alpha1.Valid
}

// HandleNewValidationResult creates a new validation result for the active validator
func HandleNewValidationResult(c client.Client, plugin string, nn ktypes.NamespacedName, vr *v1alpha1.ValidationResult, l logr.Logger) error {
func HandleNewValidationResult(c client.Client, plugin string, expectedResults int, nn ktypes.NamespacedName, l logr.Logger) error {

// Create the ValidationResult
vr.ObjectMeta = metav1.ObjectMeta{
Name: nn.Name,
Namespace: nn.Namespace,
}
vr.Spec = v1alpha1.ValidationResultSpec{
Plugin: plugin,
vr := &v1alpha1.ValidationResult{
ObjectMeta: metav1.ObjectMeta{
Name: nn.Name,
Namespace: nn.Namespace,
},
Spec: v1alpha1.ValidationResultSpec{
Plugin: plugin,
ExpectedResults: expectedResults,
},
}
if err := c.Create(context.Background(), vr, &client.CreateOptions{}); err != nil {
l.V(0).Error(err, "failed to create ValidationResult", "name", nn.Name, "namespace", nn.Namespace)
Expand Down

0 comments on commit 22de119

Please sign in to comment.