Make proper use of errgroup context

Signed-off-by: Hidde Beydals <hello@hidde.co>
This commit is contained in:
Hidde Beydals 2020-12-15 00:55:48 +01:00
parent bc890874e1
commit 8d0b54e431
3 changed files with 13 additions and 8 deletions

View File

@ -589,7 +589,7 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context,
Chart: helmChart, Chart: helmChart,
Dependencies: dwr, Dependencies: dwr,
} }
err = dm.Build() err = dm.Build(ctx)
if err != nil { if err != nil {
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
} }

View File

@ -46,16 +46,20 @@ type DependencyManager struct {
} }
// Build compiles and builds the chart dependencies // Build compiles and builds the chart dependencies
func (dm *DependencyManager) Build() error { func (dm *DependencyManager) Build(ctx context.Context) error {
if len(dm.Dependencies) == 0 { if len(dm.Dependencies) == 0 {
return nil return nil
} }
ctx := context.Background()
errs, ctx := errgroup.WithContext(ctx) errs, ctx := errgroup.WithContext(ctx)
for _, item := range dm.Dependencies { for _, item := range dm.Dependencies {
errs.Go(func() error { errs.Go(func() error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
var ( var (
ch *helmchart.Chart ch *helmchart.Chart
err error err error

View File

@ -17,6 +17,7 @@ limitations under the License.
package helm package helm
import ( import (
"context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"strings" "strings"
@ -43,7 +44,7 @@ func TestBuild_WithEmptyDependencies(t *testing.T) {
dm := DependencyManager{ dm := DependencyManager{
Dependencies: nil, Dependencies: nil,
} }
if err := dm.Build(); err != nil { if err := dm.Build(context.TODO()); err != nil {
t.Errorf("Build() should return nil") t.Errorf("Build() should return nil")
} }
} }
@ -119,7 +120,7 @@ func TestBuild_WithLocalChart(t *testing.T) {
}, },
} }
err := dm.Build() err := dm.Build(context.TODO())
deps := dm.Chart.Dependencies() deps := dm.Chart.Dependencies()
if (err != nil) && tt.wantErr { if (err != nil) && tt.wantErr {
@ -172,7 +173,7 @@ func TestBuild_WithRemoteChart(t *testing.T) {
}, },
} }
if err := dm.Build(); err != nil { if err := dm.Build(context.TODO()); err != nil {
t.Errorf("Build() expected to not return error: %s", err) t.Errorf("Build() expected to not return error: %s", err)
} }
@ -189,7 +190,7 @@ func TestBuild_WithRemoteChart(t *testing.T) {
// When repo is not set // When repo is not set
dm.Dependencies[0].Repo = nil dm.Dependencies[0].Repo = nil
if err := dm.Build(); err == nil { if err := dm.Build(context.TODO()); err == nil {
t.Errorf("Build() expected to return error") t.Errorf("Build() expected to return error")
} else if !strings.Contains(err.Error(), "chartrepo should not be nil") { } else if !strings.Contains(err.Error(), "chartrepo should not be nil") {
t.Errorf("Build() expected to return different error, got: %s", err) t.Errorf("Build() expected to return different error, got: %s", err)