Skip to content

Commit 8d0b54e

Browse files
committed
Make proper use of errgroup context
Signed-off-by: Hidde Beydals <[email protected]>
1 parent bc89087 commit 8d0b54e

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

controllers/helmchart_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context,
589589
Chart: helmChart,
590590
Dependencies: dwr,
591591
}
592-
err = dm.Build()
592+
err = dm.Build(ctx)
593593
if err != nil {
594594
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
595595
}

internal/helm/dependency_manager.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,20 @@ type DependencyManager struct {
4646
}
4747

4848
// Build compiles and builds the chart dependencies
49-
func (dm *DependencyManager) Build() error {
49+
func (dm *DependencyManager) Build(ctx context.Context) error {
5050
if len(dm.Dependencies) == 0 {
5151
return nil
5252
}
5353

54-
ctx := context.Background()
5554
errs, ctx := errgroup.WithContext(ctx)
56-
5755
for _, item := range dm.Dependencies {
5856
errs.Go(func() error {
57+
select {
58+
case <-ctx.Done():
59+
return ctx.Err()
60+
default:
61+
}
62+
5963
var (
6064
ch *helmchart.Chart
6165
err error

internal/helm/dependency_manager_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package helm
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"io/ioutil"
2223
"strings"
@@ -43,7 +44,7 @@ func TestBuild_WithEmptyDependencies(t *testing.T) {
4344
dm := DependencyManager{
4445
Dependencies: nil,
4546
}
46-
if err := dm.Build(); err != nil {
47+
if err := dm.Build(context.TODO()); err != nil {
4748
t.Errorf("Build() should return nil")
4849
}
4950
}
@@ -119,7 +120,7 @@ func TestBuild_WithLocalChart(t *testing.T) {
119120
},
120121
}
121122

122-
err := dm.Build()
123+
err := dm.Build(context.TODO())
123124
deps := dm.Chart.Dependencies()
124125

125126
if (err != nil) && tt.wantErr {
@@ -172,7 +173,7 @@ func TestBuild_WithRemoteChart(t *testing.T) {
172173
},
173174
}
174175

175-
if err := dm.Build(); err != nil {
176+
if err := dm.Build(context.TODO()); err != nil {
176177
t.Errorf("Build() expected to not return error: %s", err)
177178
}
178179

@@ -189,7 +190,7 @@ func TestBuild_WithRemoteChart(t *testing.T) {
189190

190191
// When repo is not set
191192
dm.Dependencies[0].Repo = nil
192-
if err := dm.Build(); err == nil {
193+
if err := dm.Build(context.TODO()); err == nil {
193194
t.Errorf("Build() expected to return error")
194195
} else if !strings.Contains(err.Error(), "chartrepo should not be nil") {
195196
t.Errorf("Build() expected to return different error, got: %s", err)

0 commit comments

Comments
 (0)