Skip to content

Commit 8018b45

Browse files
committed
helm: typed errors from GetChartVersion()
Update the implementations of the helm repository downloaders to return implementation specific typed error from GetChartVersion(). This is needed to distinguish between persistent build error and transient build error. In the case of OCI charts, a transient network failure shouldn't be considered a persistent build failure of the chart and should be retried. Two repository errors, ErrReference and ErrExternal are introduced for the repository downloader implementations to provide enough context about the failure which can be used by the caller to add appropriate context as per the needs. In case of chart builder, it adds the build error context based on the repository error value. Signed-off-by: Sunny <[email protected]>
1 parent 0f7a263 commit 8018b45

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

internal/helm/chart/builder_remote.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,17 @@ func (b *remoteChartBuilder) downloadFromRepository(ctx context.Context, remote
128128
// Get the current version for the RemoteReference
129129
cv, err := remote.GetChartVersion(remoteRef.Name, remoteRef.Version)
130130
if err != nil {
131+
var reason BuildErrorReason
132+
switch err.(type) {
133+
case *repository.ErrReference:
134+
reason = ErrChartReference
135+
case *repository.ErrExternal:
136+
reason = ErrChartPull
137+
default:
138+
reason = ErrUnknown
139+
}
131140
err = fmt.Errorf("failed to get chart version for remote reference: %w", err)
132-
return nil, nil, &BuildError{Reason: ErrChartReference, Err: err}
141+
return nil, nil, &BuildError{Reason: reason, Err: err}
133142
}
134143

135144
// Verify the chart if necessary

internal/helm/repository/chart_repository.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ func newChartRepository() *ChartRepository {
156156
// to be a semver.Constraints compatible string. If version is empty, the latest
157157
// stable version will be returned and prerelease versions will be ignored.
158158
func (r *ChartRepository) GetChartVersion(name, ver string) (*repo.ChartVersion, error) {
159+
cv, err := r.getChartVersion(name, ver)
160+
if err != nil {
161+
return nil, &ErrReference{Err: err}
162+
}
163+
return cv, nil
164+
}
165+
166+
func (r *ChartRepository) getChartVersion(name, ver string) (*repo.ChartVersion, error) {
159167
// See if we already have the index in cache or try to load it.
160168
if err := r.StrategicallyLoadIndex(); err != nil {
161169
return nil, err

internal/helm/repository/errors.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2022 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package repository
18+
19+
// ErrReference indicate invalid chart reference.
20+
type ErrReference struct {
21+
Err error
22+
}
23+
24+
// Error implements the error interface.
25+
func (er *ErrReference) Error() string {
26+
return er.Err.Error()
27+
}
28+
29+
// Unwrap returns the underlying error.
30+
func (er *ErrReference) Unwrap() error {
31+
return er.Err
32+
}
33+
34+
// ErrExternal is a generic error for errors related to external API calls.
35+
type ErrExternal struct {
36+
Err error
37+
}
38+
39+
// Error implements the error interface.
40+
func (ee *ErrExternal) Error() string {
41+
return ee.Err.Error()
42+
}
43+
44+
// Unwrap returns the underlying error.
45+
func (ee *ErrExternal) Unwrap() error {
46+
return ee.Err
47+
}

internal/helm/repository/oci_chart_repository.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,14 @@ func NewOCIChartRepository(repositoryURL string, chartRepoOpts ...OCIChartReposi
146146
// stable version will be returned and prerelease versions will be ignored.
147147
// adapted from https://github.com/helm/helm/blob/49819b4ef782e80b0c7f78c30bd76b51ebb56dc8/pkg/downloader/chart_downloader.go#L162
148148
func (r *OCIChartRepository) GetChartVersion(name, ver string) (*repo.ChartVersion, error) {
149+
cv, err := r.getChartVersion(name, ver)
150+
if err != nil {
151+
return nil, &ErrExternal{Err: err}
152+
}
153+
return cv, nil
154+
}
149155

156+
func (r *OCIChartRepository) getChartVersion(name, ver string) (*repo.ChartVersion, error) {
150157
cpURL := r.URL
151158
cpURL.Path = path.Join(cpURL.Path, name)
152159

0 commit comments

Comments
 (0)