Skip to content

Commit

Permalink
loader: log HTTP errors to provide faster feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Dec 13, 2023
1 parent 259b8f8 commit 05c1c42
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
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
58 changes: 58 additions & 0 deletions internal/loader/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
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 = errorLogger(ctrl.LoggerFrom(ctx))
return httpClient
}

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

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

0 comments on commit 05c1c42

Please sign in to comment.