-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
loader: log HTTP errors to provide faster feedback
Signed-off-by: Hidde Beydals <[email protected]>
- Loading branch information
Showing
2 changed files
with
48 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package loader | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/hashicorp/go-retryablehttp" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
) | ||
|
||
// NewRetryableHTTPClient returns a new retrying HTTP client for loading | ||
// artifacts. The client will retry up to the given number of times before | ||
// giving up. The context is used to log errors. | ||
func NewRetryableHTTPClient(ctx context.Context, retries int) *retryablehttp.Client { | ||
httpClient := retryablehttp.NewClient() | ||
httpClient.RetryWaitMin = 5 * time.Second | ||
httpClient.RetryWaitMax = 30 * time.Second | ||
httpClient.RetryMax = retries | ||
httpClient.Logger = errorLogger{logger: ctrl.LoggerFrom(ctx)} | ||
return httpClient | ||
} | ||
|
||
// errorLogger is a wrapper around logr.Logger that implements the | ||
// retryablehttp.LeveledLogger interface while only logging errors. | ||
type errorLogger struct { | ||
logger logr.Logger | ||
} | ||
|
||
func (l *errorLogger) Error(msg string, keysAndValues ...interface{}) { | ||
l.logger.Info(msg, keysAndValues...) | ||
} | ||
|
||
func (l *errorLogger) Info(msg string, keysAndValues ...interface{}) { | ||
// Do nothing. | ||
} | ||
|
||
func (l *errorLogger) Debug(msg string, keysAndValues ...interface{}) { | ||
// Do nothing. | ||
} | ||
|
||
func (l *errorLogger) Warn(msg string, keysAndValues ...interface{}) { | ||
// Do nothing. | ||
} |