@@ -21,6 +21,7 @@ import (
21
21
"bytes"
22
22
"context"
23
23
"encoding/json"
24
+ "fmt"
24
25
"io"
25
26
"net"
26
27
"net/http"
@@ -40,6 +41,7 @@ import (
40
41
"k8s.io/apimachinery/pkg/util/validation"
41
42
"k8s.io/client-go/transport"
42
43
"k8s.io/utils/pointer"
44
+ ctrl "sigs.k8s.io/controller-runtime"
43
45
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
44
46
45
47
runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
@@ -117,6 +119,9 @@ func (c *client) IsReady() bool {
117
119
}
118
120
119
121
func (c * client ) Discover (ctx context.Context , extensionConfig * runtimev1.ExtensionConfig ) (* runtimev1.ExtensionConfig , error ) {
122
+ log := ctrl .LoggerFrom (ctx )
123
+ log .Info ("Performing discovery for ExtensionConfig" )
124
+
120
125
hookGVH , err := c .catalog .GroupVersionHook (runtimehooksv1 .Discovery )
121
126
if err != nil {
122
127
return nil , errors .Wrapf (err , "failed to discover extension %q: failed to compute GVH of hook" , extensionConfig .Name )
@@ -187,9 +192,12 @@ func (c *client) Unregister(extensionConfig *runtimev1.ExtensionConfig) error {
187
192
// See CallExtension for more details on when an ExtensionHandler returns an error.
188
193
// The aggregated result of the ExtensionHandlers is updated into the response object passed to the function.
189
194
func (c * client ) CallAllExtensions (ctx context.Context , hook runtimecatalog.Hook , forObject metav1.Object , request runtime.Object , response runtimehooksv1.ResponseObject ) error {
195
+ hookName := runtimecatalog .HookName (hook )
196
+ log := ctrl .LoggerFrom (ctx ).WithValues ("hook" , hookName )
197
+ ctx = ctrl .LoggerInto (ctx , log )
190
198
gvh , err := c .catalog .GroupVersionHook (hook )
191
199
if err != nil {
192
- return errors .Wrapf (err , "failed to call extension handlers for hook %q: failed to compute GroupVersionHook" , runtimecatalog . HookName ( hook ) )
200
+ return errors .Wrapf (err , "failed to call extension handlers for hook %q: failed to compute GroupVersionHook" , hookName )
193
201
}
194
202
// Make sure the request is compatible with the hook.
195
203
if err := c .catalog .ValidateRequest (gvh , request ); err != nil {
@@ -205,6 +213,7 @@ func (c *client) CallAllExtensions(ctx context.Context, hook runtimecatalog.Hook
205
213
return errors .Wrapf (err , "failed to call extension handlers for hook %q" , gvh .GroupHook ())
206
214
}
207
215
216
+ log .Info (fmt .Sprintf ("Calling all extensions of hook %q" , hookName ))
208
217
responses := []runtimehooksv1.ResponseObject {}
209
218
for _ , registration := range registrations {
210
219
// Creates a new instance of the response parameter.
@@ -221,12 +230,14 @@ func (c *client) CallAllExtensions(ctx context.Context, hook runtimecatalog.Hook
221
230
}
222
231
// If the object namespace isn't matched by the registration NamespaceSelector skip the call.
223
232
if ! namespaceMatches {
233
+ log .V (5 ).Info (fmt .Sprintf ("skipping extension handler %q as object '%s/%s' does not match selector %q of ExtensionConfig" , registration .Name , forObject .GetNamespace (), forObject .GetName (), registration .NamespaceSelector ))
224
234
continue
225
235
}
226
236
227
237
err = c .CallExtension (ctx , hook , forObject , registration .Name , request , tmpResponse )
228
238
// If one of the extension handlers fails lets short-circuit here and return early.
229
239
if err != nil {
240
+ log .Error (err , "failed to call extension handlers" )
230
241
return errors .Wrapf (err , "failed to call extension handlers for hook %q" , gvh .GroupHook ())
231
242
}
232
243
responses = append (responses , tmpResponse )
@@ -289,6 +300,8 @@ func lowestNonZeroRetryAfterSeconds(i, j int32) int32 {
289
300
// - Internal errors. Examples: hooks is incompatible with ExtensionHandler, ExtensionHandler information is missing.
290
301
// - Error when ExtensionHandler returns a response with `Status` set to `Failure`.
291
302
func (c * client ) CallExtension (ctx context.Context , hook runtimecatalog.Hook , forObject metav1.Object , name string , request runtime.Object , response runtimehooksv1.ResponseObject ) error {
303
+ log := ctrl .LoggerFrom (ctx ).WithValues ("extensionHandler" , name , "hook" , runtimecatalog .HookName (hook ))
304
+ ctx = ctrl .LoggerInto (ctx , log )
292
305
hookGVH , err := c .catalog .GroupVersionHook (hook )
293
306
if err != nil {
294
307
return errors .Wrapf (err , "failed to call extension handler %q: failed to compute GroupVersionHook" , name )
@@ -320,6 +333,7 @@ func (c *client) CallExtension(ctx context.Context, hook runtimecatalog.Hook, fo
320
333
return errors .Errorf ("failed to call extension handler %q: namespaceSelector did not match object %s" , name , util .ObjectKey (forObject ))
321
334
}
322
335
336
+ log .Info (fmt .Sprintf ("Calling extension handler %q" , name ))
323
337
var timeoutDuration time.Duration
324
338
if registration .TimeoutSeconds != nil {
325
339
timeoutDuration = time .Duration (* registration .TimeoutSeconds ) * time .Second
@@ -339,18 +353,27 @@ func (c *client) CallExtension(ctx context.Context, hook runtimecatalog.Hook, fo
339
353
ignore := * registration .FailurePolicy == runtimev1 .FailurePolicyIgnore
340
354
if _ , ok := err .(errCallingExtensionHandler ); ok && ignore {
341
355
// Update the response to a default success response and return.
356
+ log .Info (fmt .Sprintf ("ignoring error calling extension handler because of FailurePolicy %q" , * registration .FailurePolicy ))
342
357
response .SetStatus (runtimehooksv1 .ResponseStatusSuccess )
343
358
response .SetMessage ("" )
344
359
return nil
345
360
}
361
+ log .Error (err , "failed to call extension handler" )
346
362
return errors .Wrapf (err , "failed to call extension handler %q" , name )
347
363
}
348
364
349
365
// If the received response is a failure then return an error.
350
366
if response .GetStatus () == runtimehooksv1 .ResponseStatusFailure {
367
+ log .Error (err , "extension handler returned a failure response" )
351
368
return errors .Errorf ("failed to call extension handler %q: got failure response with message %q" , name , response .GetMessage ())
352
369
}
353
370
371
+ if retryResponse , ok := response .(runtimehooksv1.RetryResponseObject ); ok && retryResponse .GetRetryAfterSeconds () != 0 {
372
+ log .Info (fmt .Sprintf ("extension handler returned blocking response with retryAfterSeconds of %q" , retryResponse .GetRetryAfterSeconds ()))
373
+ } else {
374
+ log .Info ("extension handler returned success response" )
375
+ }
376
+
354
377
// Received a successful response from the extension handler. The `response` object
355
378
// has been populated with the result. Return no error.
356
379
return nil
@@ -366,6 +389,7 @@ type httpCallOptions struct {
366
389
}
367
390
368
391
func httpCall (ctx context.Context , request , response runtime.Object , opts * httpCallOptions ) error {
392
+ log := ctrl .LoggerFrom (ctx )
369
393
if opts == nil || request == nil || response == nil {
370
394
return errors .New ("http call failed: opts, request and response cannot be nil" )
371
395
}
@@ -389,6 +413,7 @@ func httpCall(ctx context.Context, request, response runtime.Object, opts *httpC
389
413
responseLocal := response
390
414
391
415
if requireConversion {
416
+ log .V (5 ).Info (fmt .Sprintf ("Hook version of supported request is %s. Converting request from %s" , opts .registrationGVH , opts .hookGVH ))
392
417
// The request and response objects need to be converted to match the version supported by
393
418
// the ExtensionHandler.
394
419
var err error
@@ -485,6 +510,7 @@ func httpCall(ctx context.Context, request, response runtime.Object, opts *httpC
485
510
}
486
511
487
512
if requireConversion {
513
+ log .V (5 ).Info (fmt .Sprintf ("Hook version of received response is %s. Converting response to %s" , opts .registrationGVH , opts .hookGVH ))
488
514
// Convert the received response to the original version of the response object.
489
515
if err := opts .catalog .Convert (responseLocal , response , ctx ); err != nil {
490
516
return errors .Wrapf (err , "http call failed: failed to convert response from %T to %T" , requestLocal , response )
0 commit comments