@@ -3,6 +3,7 @@ package validationresult
3
3
4
4
import (
5
5
"context"
6
+ "fmt"
6
7
"strings"
7
8
"time"
8
9
@@ -25,8 +26,44 @@ type Patcher interface {
25
26
Patch (ctx context.Context , obj client.Object , opts ... patch.Option ) error
26
27
}
27
28
28
- // HandleExistingValidationResult processes a preexisting validation result for the active validator.
29
- func HandleExistingValidationResult (vr * v1alpha1.ValidationResult , l logr.Logger ) {
29
+ // ValidationRule is an interface for validation rules.
30
+ type ValidationRule interface {
31
+ client.Object
32
+ PluginCode () string
33
+ ResultCount () int
34
+ }
35
+
36
+ // Build creates a new ValidationResult for a specific ValidationRule.
37
+ func Build (r ValidationRule ) * v1alpha1.ValidationResult {
38
+ return & v1alpha1.ValidationResult {
39
+ ObjectMeta : metav1.ObjectMeta {
40
+ Name : Name (r ),
41
+ Namespace : r .GetNamespace (),
42
+ OwnerReferences : []metav1.OwnerReference {
43
+ {
44
+ APIVersion : r .GetObjectKind ().GroupVersionKind ().Version ,
45
+ Kind : r .GetObjectKind ().GroupVersionKind ().Kind ,
46
+ Name : r .GetName (),
47
+ UID : r .GetUID (),
48
+ Controller : util .Ptr (true ),
49
+ },
50
+ },
51
+ },
52
+ Spec : v1alpha1.ValidationResultSpec {
53
+ Plugin : r .PluginCode (),
54
+ ExpectedResults : r .ResultCount (),
55
+ },
56
+ }
57
+ }
58
+
59
+ // Name returns the name of a ValidationResult for a specific ValidationRule.
60
+ func Name (r ValidationRule ) string {
61
+ name := fmt .Sprintf ("validator-plugin-%s-%s" , r .PluginCode (), r .GetName ())
62
+ return util .Sanitize (name )
63
+ }
64
+
65
+ // HandleExisting processes a preexisting validation result for the active validator.
66
+ func HandleExisting (vr * v1alpha1.ValidationResult , l logr.Logger ) {
30
67
l = l .WithValues ("name" , vr .Name , "namespace" , vr .Namespace , "state" , vr .Status .State )
31
68
32
69
switch vr .Status .State {
@@ -49,8 +86,8 @@ func HandleExistingValidationResult(vr *v1alpha1.ValidationResult, l logr.Logger
49
86
}
50
87
}
51
88
52
- // HandleNewValidationResult creates a new validation result for the active validator.
53
- func HandleNewValidationResult (ctx context.Context , c client.Client , p Patcher , vr * v1alpha1.ValidationResult , l logr.Logger ) error {
89
+ // HandleNew creates a new validation result for the active validator.
90
+ func HandleNew (ctx context.Context , c client.Client , p Patcher , vr * v1alpha1.ValidationResult , l logr.Logger ) error {
54
91
l = l .WithValues ("name" , vr .Name , "namespace" , vr .Namespace )
55
92
56
93
// Create the ValidationResult
@@ -75,11 +112,26 @@ func HandleNewValidationResult(ctx context.Context, c client.Client, p Patcher,
75
112
return nil
76
113
}
77
114
78
- // SafeUpdateValidationResult updates a ValidationResult, ensuring
79
- // that the overall validation status remains failed if a single rule fails.
80
- func SafeUpdateValidationResult (ctx context.Context , p Patcher , vr * v1alpha1.ValidationResult , vrr types.ValidationResponse , l logr.Logger ) error {
115
+ // SafeUpdate processes and patches a ValidationResult with retry logic.
116
+ func SafeUpdate (ctx context.Context , p Patcher , vr * v1alpha1.ValidationResult , vrr types.ValidationResponse , l logr.Logger ) error {
81
117
l = l .WithValues ("name" , vr .Name , "namespace" , vr .Namespace )
82
118
119
+ if err := Finalize (vr , vrr , l ); err != nil {
120
+ l .Error (err , "failed to finalize ValidationResult" )
121
+ return err
122
+ }
123
+ if err := patchValidationResult (ctx , p , vr ); err != nil {
124
+ l .Error (err , "failed to patch ValidationResult" )
125
+ return err
126
+ }
127
+
128
+ l .V (0 ).Info ("Successfully patched ValidationResult" , "state" , vr .Status .State )
129
+ return nil
130
+ }
131
+
132
+ // Finalize finalizes a ValidationResult, ensuring
133
+ // that the overall validation status remains failed if a single rule fails.
134
+ func Finalize (vr * v1alpha1.ValidationResult , vrr types.ValidationResponse , l logr.Logger ) error {
83
135
for i , r := range vrr .ValidationRuleResults {
84
136
// Handle nil ValidationRuleResult
85
137
if r == nil {
@@ -90,21 +142,13 @@ func SafeUpdateValidationResult(ctx context.Context, p Patcher, vr *v1alpha1.Val
90
142
}
91
143
}
92
144
// Update overall ValidationResult status
93
- updateValidationResultStatus (vr , r , vrr .ValidationRuleErrors [i ], l )
94
- }
95
-
96
- l .V (0 ).Info ("Preparing to patch ValidationResult" )
97
- if err := patchValidationResult (ctx , p , vr ); err != nil {
98
- l .Error (err , "failed to patch ValidationResult" )
99
- return err
145
+ updateStatus (vr , r , vrr .ValidationRuleErrors [i ], l )
100
146
}
101
-
102
- l .V (0 ).Info ("Successfully patched ValidationResult" , "state" , vr .Status .State )
103
147
return nil
104
148
}
105
149
106
- // updateValidationResultStatus updates a ValidationResult's status with the result of a single validation rule.
107
- func updateValidationResultStatus (vr * v1alpha1.ValidationResult , vrr * types.ValidationRuleResult , vrrErr error , l logr.Logger ) {
150
+ // updateStatus updates a ValidationResult's status with the result of a single validation rule.
151
+ func updateStatus (vr * v1alpha1.ValidationResult , vrr * types.ValidationRuleResult , vrrErr error , l logr.Logger ) {
108
152
109
153
// Finalize result State and Condition in the event of an unexpected error
110
154
if vrrErr != nil {
0 commit comments