Merge pull request #987 from fluxcd/chart-version-build-err

helm: typed errors from GetChartVersion()
This commit is contained in:
Sunny 2022-12-19 16:39:49 +05:30 committed by GitHub
commit 9d776414af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 2 deletions

View File

@ -128,8 +128,17 @@ func (b *remoteChartBuilder) downloadFromRepository(ctx context.Context, remote
// Get the current version for the RemoteReference
cv, err := remote.GetChartVersion(remoteRef.Name, remoteRef.Version)
if err != nil {
var reason BuildErrorReason
switch err.(type) {
case *repository.ErrReference:
reason = ErrChartReference
case *repository.ErrExternal:
reason = ErrChartPull
default:
reason = ErrUnknown
}
err = fmt.Errorf("failed to get chart version for remote reference: %w", err)
return nil, nil, &BuildError{Reason: ErrChartReference, Err: err}
return nil, nil, &BuildError{Reason: reason, Err: err}
}
// Verify the chart if necessary

View File

@ -158,9 +158,17 @@ func newChartRepository() *ChartRepository {
func (r *ChartRepository) GetChartVersion(name, ver string) (*repo.ChartVersion, error) {
// See if we already have the index in cache or try to load it.
if err := r.StrategicallyLoadIndex(); err != nil {
return nil, err
return nil, &ErrExternal{Err: err}
}
cv, err := r.getChartVersion(name, ver)
if err != nil {
return nil, &ErrReference{Err: err}
}
return cv, nil
}
func (r *ChartRepository) getChartVersion(name, ver string) (*repo.ChartVersion, error) {
r.RLock()
defer r.RUnlock()

View File

@ -0,0 +1,47 @@
/*
Copyright 2022 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 repository
// ErrReference indicate invalid chart reference.
type ErrReference struct {
Err error
}
// Error implements the error interface.
func (er *ErrReference) Error() string {
return er.Err.Error()
}
// Unwrap returns the underlying error.
func (er *ErrReference) Unwrap() error {
return er.Err
}
// ErrExternal is a generic error for errors related to external API calls.
type ErrExternal struct {
Err error
}
// Error implements the error interface.
func (ee *ErrExternal) Error() string {
return ee.Err.Error()
}
// Unwrap returns the underlying error.
func (ee *ErrExternal) Unwrap() error {
return ee.Err
}

View File

@ -146,7 +146,14 @@ func NewOCIChartRepository(repositoryURL string, chartRepoOpts ...OCIChartReposi
// stable version will be returned and prerelease versions will be ignored.
// adapted from https://github.com/helm/helm/blob/49819b4ef782e80b0c7f78c30bd76b51ebb56dc8/pkg/downloader/chart_downloader.go#L162
func (r *OCIChartRepository) GetChartVersion(name, ver string) (*repo.ChartVersion, error) {
cv, err := r.getChartVersion(name, ver)
if err != nil {
return nil, &ErrExternal{Err: err}
}
return cv, nil
}
func (r *OCIChartRepository) getChartVersion(name, ver string) (*repo.ChartVersion, error) {
cpURL := r.URL
cpURL.Path = path.Join(cpURL.Path, name)