@@ -21,6 +21,7 @@ import (
2121 "encoding/json"
2222 "errors"
2323 "fmt"
24+ "math/rand"
2425 "regexp"
2526 "strings"
2627 "time"
@@ -57,6 +58,15 @@ func SetupVirtualServiceTemplateWebhookWithManager(mgr ctrl.Manager, cacheUpdate
5758// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
5859// +kubebuilder:webhook:path=/validate-envoy-kaasops-io-v1alpha1-virtualservicetemplate,mutating=false,failurePolicy=fail,sideEffects=None,groups=envoy.kaasops.io,resources=virtualservicetemplates,verbs=create;update;delete,versions=v1alpha1,name=vvirtualservicetemplate-v1alpha1.envoy.kaasops.io,admissionReviewVersions=v1
5960
61+ type contextKey string
62+
63+ const validationIDKey contextKey = "validationID"
64+
65+ // generateValidationID creates a short unique ID for correlating logs
66+ func generateValidationID () string {
67+ return fmt .Sprintf ("%08x" , rand .Uint32 ())
68+ }
69+
6070// VirtualServiceTemplateCustomValidator struct is responsible for validating the VirtualServiceTemplate resource
6171// when it is created, updated, or deleted.
6272//
@@ -175,11 +185,16 @@ func (v *VirtualServiceTemplateCustomValidator) ValidateUpdate(ctx context.Conte
175185 if ! ok {
176186 return nil , fmt .Errorf ("expected a VirtualServiceTemplate object but got %T" , newObj )
177187 }
178- virtualservicetemplatelog .Info ("Validation for VirtualServiceTemplate upon update" , "name" , virtualservicetemplate .GetName ())
188+
189+ // Generate correlation ID for linking logs
190+ validationID := generateValidationID ()
191+ ctx = context .WithValue (ctx , validationIDKey , validationID )
192+
193+ virtualservicetemplatelog .Info ("Validation for VirtualServiceTemplate upon update" , "name" , virtualservicetemplate .GetName (), "validationID" , validationID )
179194
180195 // Validate that all extraFields are used in the template
181196 if err := validateExtraFieldsUsage (virtualservicetemplate ); err != nil {
182- virtualservicetemplatelog .Error (err , "ExtraFields validation failed" , "name" , virtualservicetemplate .GetName ())
197+ virtualservicetemplatelog .Error (err , "ExtraFields validation failed" , "name" , virtualservicetemplate .GetName (), "validationID" , validationID )
183198 return nil , err
184199 }
185200
@@ -188,27 +203,27 @@ func (v *VirtualServiceTemplateCustomValidator) ValidateUpdate(ctx context.Conte
188203 defer cancelTracing ()
189204 if err := validateTemplateTracing (ctxTracing , v .Client , virtualservicetemplate ); err != nil {
190205 if errors .Is (ctxTracing .Err (), context .DeadlineExceeded ) {
191- virtualservicetemplatelog .Error (err , "Tracing validation timed out" , "name" , virtualservicetemplate .GetName (), "timeout" , v .getDryRunTimeout ())
206+ virtualservicetemplatelog .Error (err , "Tracing validation timed out" , "name" , virtualservicetemplate .GetName (), "timeout" , v .getDryRunTimeout (), "validationID" , validationID )
192207 return nil , fmt .Errorf ("tracing validation timed out after %s; please check Kubernetes API availability" , v .getDryRunTimeout ())
193208 }
194- virtualservicetemplatelog .Error (err , "Tracing validation failed" , "name" , virtualservicetemplate .GetName ())
209+ virtualservicetemplatelog .Error (err , "Tracing validation failed" , "name" , virtualservicetemplate .GetName (), "validationID" , validationID )
195210 return nil , err
196211 }
197212
198213 // Apply timeout for heavy dry-run path when updating template
199- virtualservicetemplatelog .Info ("Starting dry-run validation" , "name" , virtualservicetemplate .GetName (), "timeout" , v .getDryRunTimeout ())
214+ virtualservicetemplatelog .Info ("Starting dry-run validation" , "name" , virtualservicetemplate .GetName (), "timeout" , v .getDryRunTimeout (), "validationID" , validationID )
200215 ctxTO , cancel := context .WithTimeout (ctx , v .getDryRunTimeout ())
201216 defer cancel ()
202217 if err := v .cacheUpdater .DryBuildSnapshotsWithVirtualServiceTemplate (ctxTO , virtualservicetemplate ); err != nil {
203218 if errors .Is (ctxTO .Err (), context .DeadlineExceeded ) {
204- virtualservicetemplatelog .Error (err , "Dry-run validation timed out" , "name" , virtualservicetemplate .GetName (), "timeout" , v .getDryRunTimeout ())
219+ virtualservicetemplatelog .Error (err , "Dry-run validation timed out" , "name" , virtualservicetemplate .GetName (), "timeout" , v .getDryRunTimeout (), "validationID" , validationID )
205220 return nil , fmt .Errorf ("validation timed out after %s; please retry or increase WEBHOOK_DRYRUN_TIMEOUT_MS" , v .getDryRunTimeout ())
206221 }
207- virtualservicetemplatelog .Error (err , "Dry-run validation failed" , "name" , virtualservicetemplate .GetName ())
222+ virtualservicetemplatelog .Error (err , "Dry-run validation failed" , "name" , virtualservicetemplate .GetName (), "validationID" , validationID )
208223 return nil , fmt .Errorf ("failed to apply VirtualServiceTemplate: %w" , err )
209224 }
210225
211- virtualservicetemplatelog .Info ("VirtualServiceTemplate validation completed" , "name" , virtualservicetemplate .GetName ())
226+ virtualservicetemplatelog .Info ("VirtualServiceTemplate validation completed" , "name" , virtualservicetemplate .GetName (), "validationID" , validationID )
212227
213228 return nil , nil
214229}
0 commit comments