Skip to content

Commit 634bd3b

Browse files
Refactor: Extract response status validation into helper function
Signed-off-by: Furkat Gofurov <furkat.gofurov@suse.com>
1 parent 1aa1d99 commit 634bd3b

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

internal/runtime/client/client.go

Lines changed: 21 additions & 18 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.
@@ -396,15 +390,8 @@ func (c *client) CallExtension(ctx context.Context, hook runtimecatalog.Hook, fo
396390
}
397391

398392
// If the received response is not a success then return an error.
399-
if response.GetStatus() != runtimehooksv1.ResponseStatusSuccess {
400-
if response.GetStatus() == runtimehooksv1.ResponseStatusFailure {
401-
log.Info(fmt.Sprintf("Failed to call extension handler %q: got failure response with message %v", name, response.GetMessage()))
402-
// 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
403-
return errors.Errorf("failed to call extension handler %q: got failure response, please check controller logs for errors", name)
404-
}
405-
// Handle unknown status.
406-
log.Info(fmt.Sprintf("Failed to call extension handler %q: got unknown response status %q with message %v", name, response.GetStatus(), response.GetMessage()))
407-
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
408395
}
409396

410397
if retryResponse, ok := response.(runtimehooksv1.RetryResponseObject); ok && retryResponse.GetRetryAfterSeconds() != 0 {
@@ -733,3 +720,19 @@ func ExtensionNameFromHandlerName(registeredHandlerName string) (string, error)
733720
}
734721
return parts[1], nil
735722
}
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)