Skip to content

Commit caddc7d

Browse files
authored
Merge pull request #12905 from furkatgofurov7/dedup-CallAllExtensions-code
🌱 Deduplicate extension filtering and response validation logic
2 parents 76697d5 + 634bd3b commit caddc7d

File tree

1 file changed

+26
-38
lines changed

1 file changed

+26
-38
lines changed

internal/runtime/client/client.go

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"strings"
3333
"time"
3434

35+
"github.com/go-logr/logr"
3536
"github.com/pkg/errors"
3637
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3738
"k8s.io/apimachinery/pkg/labels"
@@ -123,15 +124,8 @@ func (c *client) Discover(ctx context.Context, extensionConfig *runtimev1.Extens
123124
}
124125

125126
// Check to see if the response is not a success and handle the failure accordingly.
126-
if response.GetStatus() != runtimehooksv1.ResponseStatusSuccess {
127-
if response.GetStatus() == runtimehooksv1.ResponseStatusFailure {
128-
log.Info(fmt.Sprintf("Failed to discover extension %q: got failure response with message %v", extensionConfig.Name, response.GetMessage()))
129-
// Don't add the message to the error as it is may be unique causing too many reconciliations. Ref: https://github.com/kubernetes-sigs/cluster-api/issues/6921
130-
return nil, errors.Errorf("failed to discover extension %q: got failure response, please check controller logs for errors", extensionConfig.Name)
131-
}
132-
// Handle unknown status.
133-
log.Info(fmt.Sprintf("Failed to discover extension %q: got unknown response status %q with message %v", extensionConfig.Name, response.GetStatus(), response.GetMessage()))
134-
return nil, errors.Errorf("failed to discover extension %q: got unknown response status %q, please check controller logs for errors", extensionConfig.Name, response.GetStatus())
127+
if err := validateResponseStatus(log, response, "discover extension", extensionConfig.Name); err != nil {
128+
return nil, err
135129
}
136130

137131
// Check to see if the response is valid.
@@ -229,10 +223,6 @@ func (c *client) CallAllExtensions(ctx context.Context, hook runtimecatalog.Hook
229223
if err != nil {
230224
return errors.Wrapf(err, "failed to call extension handlers for hook %q: failed to compute GroupVersionHook", hookName)
231225
}
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-
}
236226
// Make sure the request is compatible with the hook.
237227
if err := c.catalog.ValidateRequest(gvh, request); err != nil {
238228
return errors.Wrapf(err, "failed to call extension handlers for hook %q: request object is invalid for hook", gvh.GroupHook())
@@ -242,33 +232,22 @@ func (c *client) CallAllExtensions(ctx context.Context, hook runtimecatalog.Hook
242232
return errors.Wrapf(err, "failed to call extension handlers for hook %q: response object is invalid for hook", gvh.GroupHook())
243233
}
244234

245-
registrations, err := c.registry.List(gvh.GroupHook())
235+
// Get all matching extension handlers for this hook and object.
236+
matchingHandlers, err := c.GetAllExtensions(ctx, hook, forObject)
246237
if err != nil {
247238
return errors.Wrapf(err, "failed to call extension handlers for hook %q", gvh.GroupHook())
248239
}
249240

250-
log.V(4).Info(fmt.Sprintf("Calling all extensions of hook %q for %s %s", hookName, forObjectGVK.Kind, klog.KObj(forObject)))
251241
responses := []runtimehooksv1.ResponseObject{}
252-
for _, registration := range registrations {
242+
for _, handlerName := range matchingHandlers {
253243
// Creates a new instance of the response parameter.
254244
responseObject, err := c.catalog.NewResponse(gvh)
255245
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)
246+
return errors.Wrapf(err, "failed to call extension handlers for hook %q: failed to call extension handler %q", gvh.GroupHook(), handlerName)
257247
}
258248
tmpResponse := responseObject.(runtimehooksv1.ResponseObject)
259249

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)
250+
err = c.CallExtension(ctx, hook, forObject, handlerName, request, tmpResponse)
272251
// If one of the extension handlers fails lets short-circuit here and return early.
273252
if err != nil {
274253
log.Error(err, "failed to call extension handlers")
@@ -411,15 +390,8 @@ func (c *client) CallExtension(ctx context.Context, hook runtimecatalog.Hook, fo
411390
}
412391

413392
// If the received response is not a success then return an error.
414-
if response.GetStatus() != runtimehooksv1.ResponseStatusSuccess {
415-
if response.GetStatus() == runtimehooksv1.ResponseStatusFailure {
416-
log.Info(fmt.Sprintf("Failed to call extension handler %q: got failure response with message %v", name, response.GetMessage()))
417-
// Don't add the message to the error as it is may be unique causing too many reconciliations. Ref: https://github.com/kubernetes-sigs/cluster-api/issues/6921
418-
return errors.Errorf("failed to call extension handler %q: got failure response, please check controller logs for errors", name)
419-
}
420-
// Handle unknown status.
421-
log.Info(fmt.Sprintf("Failed to call extension handler %q: got unknown response status %q with message %v", name, response.GetStatus(), response.GetMessage()))
422-
return errors.Errorf("failed to call extension handler %q: got unknown response status %q, please check controller logs for errors", name, response.GetStatus())
393+
if err := validateResponseStatus(log, response, "call extension handler", name); err != nil {
394+
return err
423395
}
424396

425397
if retryResponse, ok := response.(runtimehooksv1.RetryResponseObject); ok && retryResponse.GetRetryAfterSeconds() != 0 {
@@ -748,3 +720,19 @@ func ExtensionNameFromHandlerName(registeredHandlerName string) (string, error)
748720
}
749721
return parts[1], nil
750722
}
723+
724+
// validateResponseStatus checks if the response status is successful and returns an error otherwise.
725+
// It logs appropriate messages for failure and unknown statuses.
726+
func validateResponseStatus(log logr.Logger, response runtimehooksv1.ResponseObject, operationName, targetName string) error {
727+
if response.GetStatus() != runtimehooksv1.ResponseStatusSuccess {
728+
if response.GetStatus() == runtimehooksv1.ResponseStatusFailure {
729+
log.Info(fmt.Sprintf("Failed to %s %q: got failure response with message %v", operationName, targetName, response.GetMessage()))
730+
// Don't add the message to the error as it is may be unique causing too many reconciliations. Ref: https://github.com/kubernetes-sigs/cluster-api/issues/6921
731+
return errors.Errorf("failed to %s %q: got failure response, please check controller logs for errors", operationName, targetName)
732+
}
733+
// Handle unknown status.
734+
log.Info(fmt.Sprintf("Failed to %s %q: got unknown response status %q with message %v", operationName, targetName, response.GetStatus(), response.GetMessage()))
735+
return errors.Errorf("failed to %s %q: got unknown response status %q, please check controller logs for errors", operationName, targetName, response.GetStatus())
736+
}
737+
return nil
738+
}

0 commit comments

Comments
 (0)