Skip to content

Commit 1aa1d99

Browse files
Refactor: Use GetAllExtensions in CallAllExtensions to eliminate code duplication
Previously, CallAllExtensions duplicated the extension filtering logic (namespace matching, registry listing, GVK lookup) that already existed in GetAllExtensions. This refactoring makes CallAllExtensions call GetAllExtensions to get the list of matching handlers. Signed-off-by: Furkat Gofurov <[email protected]>
1 parent fbdba20 commit 1aa1d99

File tree

1 file changed

+5
-20
lines changed

1 file changed

+5
-20
lines changed

internal/runtime/client/client.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,6 @@ func (c *client) CallAllExtensions(ctx context.Context, hook runtimecatalog.Hook
229229
if err != nil {
230230
return errors.Wrapf(err, "failed to call extension handlers for hook %q: failed to compute GroupVersionHook", hookName)
231231
}
232-
forObjectGVK, err := apiutil.GVKForObject(forObject, c.client.Scheme())
233-
if err != nil {
234-
return errors.Wrapf(err, "failed to call extension handlers for hook %q: failed to get GroupVersionKind for the object the hook is executed for", hookName)
235-
}
236232
// Make sure the request is compatible with the hook.
237233
if err := c.catalog.ValidateRequest(gvh, request); err != nil {
238234
return errors.Wrapf(err, "failed to call extension handlers for hook %q: request object is invalid for hook", gvh.GroupHook())
@@ -242,33 +238,22 @@ func (c *client) CallAllExtensions(ctx context.Context, hook runtimecatalog.Hook
242238
return errors.Wrapf(err, "failed to call extension handlers for hook %q: response object is invalid for hook", gvh.GroupHook())
243239
}
244240

245-
registrations, err := c.registry.List(gvh.GroupHook())
241+
// Get all matching extension handlers for this hook and object.
242+
matchingHandlers, err := c.GetAllExtensions(ctx, hook, forObject)
246243
if err != nil {
247244
return errors.Wrapf(err, "failed to call extension handlers for hook %q", gvh.GroupHook())
248245
}
249246

250-
log.V(4).Info(fmt.Sprintf("Calling all extensions of hook %q for %s %s", hookName, forObjectGVK.Kind, klog.KObj(forObject)))
251247
responses := []runtimehooksv1.ResponseObject{}
252-
for _, registration := range registrations {
248+
for _, handlerName := range matchingHandlers {
253249
// Creates a new instance of the response parameter.
254250
responseObject, err := c.catalog.NewResponse(gvh)
255251
if err != nil {
256-
return errors.Wrapf(err, "failed to call extension handlers for hook %q: failed to call extension handler %q", gvh.GroupHook(), registration.Name)
252+
return errors.Wrapf(err, "failed to call extension handlers for hook %q: failed to call extension handler %q", gvh.GroupHook(), handlerName)
257253
}
258254
tmpResponse := responseObject.(runtimehooksv1.ResponseObject)
259255

260-
// Compute whether the object the call is being made for matches the namespaceSelector
261-
namespaceMatches, err := c.matchNamespace(ctx, registration.NamespaceSelector, forObject.GetNamespace())
262-
if err != nil {
263-
return errors.Wrapf(err, "failed to call extension handlers for hook %q: failed to call extension handler %q", gvh.GroupHook(), registration.Name)
264-
}
265-
// If the object namespace isn't matched by the registration NamespaceSelector skip the call.
266-
if !namespaceMatches {
267-
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))
268-
continue
269-
}
270-
271-
err = c.CallExtension(ctx, hook, forObject, registration.Name, request, tmpResponse)
256+
err = c.CallExtension(ctx, hook, forObject, handlerName, request, tmpResponse)
272257
// If one of the extension handlers fails lets short-circuit here and return early.
273258
if err != nil {
274259
log.Error(err, "failed to call extension handlers")

0 commit comments

Comments
 (0)