Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loader: log HTTP errors to provide faster feedback #845

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions internal/controller/helmrelease_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"strings"
"time"

"github.com/hashicorp/go-retryablehttp"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -90,8 +89,8 @@ type HelmReleaseReconciler struct {
FieldManager string
DefaultServiceAccount string

httpClient *retryablehttp.Client
requeueDependency time.Duration
requeueDependency time.Duration
artifactFetchRetries int
}

type HelmReleaseReconcilerOptions struct {
Expand Down Expand Up @@ -122,15 +121,7 @@ func (r *HelmReleaseReconciler) SetupWithManager(ctx context.Context, mgr ctrl.M
}

r.requeueDependency = opts.DependencyRequeueInterval

// Configure the retryable http client used for fetching artifacts.
// By default, it retries 10 times within a 3.5 minutes window.
httpClient := retryablehttp.NewClient()
httpClient.RetryWaitMin = 5 * time.Second
httpClient.RetryWaitMax = 30 * time.Second
httpClient.RetryMax = opts.HTTPRetry
httpClient.Logger = nil
r.httpClient = httpClient
r.artifactFetchRetries = opts.HTTPRetry

return ctrl.NewControllerManagedBy(mgr).
For(&v2.HelmRelease{}, builder.WithPredicates(
Expand Down Expand Up @@ -294,7 +285,7 @@ func (r *HelmReleaseReconciler) reconcileRelease(ctx context.Context, patchHelpe
}

// Load chart from artifact.
loadedChart, err := loader.SecureLoadChartFromURL(r.httpClient, hc.GetArtifact().URL, hc.GetArtifact().Digest)
loadedChart, err := loader.SecureLoadChartFromURL(loader.NewRetryableHTTPClient(ctx, r.artifactFetchRetries), hc.GetArtifact().URL, hc.GetArtifact().Digest)
if err != nil {
if errors.Is(err, loader.ErrFileNotFound) {
msg := fmt.Sprintf("Chart not ready: artifact not found. Retrying in %s", r.requeueDependency.String())
Expand Down
6 changes: 0 additions & 6 deletions internal/controller/helmrelease_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"testing"
"time"

"github.com/hashicorp/go-retryablehttp"
. "github.com/onsi/gomega"
"github.com/opencontainers/go-digest"
helmrelease "helm.sh/helm/v3/pkg/release"
Expand Down Expand Up @@ -433,7 +432,6 @@ func TestHelmReleaseReconciler_reconcileRelease(t *testing.T) {
WithStatusSubresource(&v2.HelmRelease{}).
WithObjects(chart, obj).
Build(),
httpClient: retryablehttp.NewClient(),
requeueDependency: 10 * time.Second,
}

Expand Down Expand Up @@ -526,7 +524,6 @@ func TestHelmReleaseReconciler_reconcileRelease(t *testing.T) {
Client: c,
GetClusterConfig: GetTestClusterConfig,
EventRecorder: record.NewFakeRecorder(32),
httpClient: retryablehttp.NewClient(),
}

// Store the Helm release mock in the test namespace.
Expand Down Expand Up @@ -607,7 +604,6 @@ func TestHelmReleaseReconciler_reconcileRelease(t *testing.T) {
Client: c,
GetClusterConfig: GetTestClusterConfig,
EventRecorder: record.NewFakeRecorder(32),
httpClient: retryablehttp.NewClient(),
}

res, err := r.reconcileRelease(context.TODO(), patch.NewSerialPatcher(obj, c), obj)
Expand Down Expand Up @@ -683,7 +679,6 @@ func TestHelmReleaseReconciler_reconcileRelease(t *testing.T) {
Client: c,
GetClusterConfig: GetTestClusterConfig,
EventRecorder: record.NewFakeRecorder(32),
httpClient: retryablehttp.NewClient(),
}

_, err = r.reconcileRelease(context.TODO(), patch.NewSerialPatcher(obj, c), obj)
Expand Down Expand Up @@ -755,7 +750,6 @@ func TestHelmReleaseReconciler_reconcileRelease(t *testing.T) {
Client: c,
GetClusterConfig: GetTestClusterConfig,
EventRecorder: record.NewFakeRecorder(32),
httpClient: retryablehttp.NewClient(),
}

_, err = r.reconcileRelease(context.TODO(), patch.NewSerialPatcher(obj, c), obj)
Expand Down
64 changes: 64 additions & 0 deletions internal/loader/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2023 The Flux authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

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 = newLoggerForContext(ctx)
return httpClient
}

func newLoggerForContext(ctx context.Context) retryablehttp.LeveledLogger {
return &errorLogger{log: ctrl.LoggerFrom(ctx)}
}

// errorLogger is a wrapper around logr.Logger that implements the
// retryablehttp.LeveledLogger interface while only logging errors.
type errorLogger struct {
log logr.Logger
}

func (l *errorLogger) Error(msg string, keysAndValues ...interface{}) {
l.log.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.
}