Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Friendly message for invalid or unreachable repositories #824

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions internal/helm/repository/chart_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,19 @@ func (r *ChartRepository) LoadFromFile(path string) error {
// The caller is expected to handle the garbage collection of CachePath, and to
// load the Index separately using LoadFromCache if required.
func (r *ChartRepository) CacheIndex() (string, error) {
index, err := r.DownloadIndex()
if err != nil {
return "", err
}

f, err := os.CreateTemp("", "chart-index-*.yaml")
if err != nil {
return "", fmt.Errorf("failed to create temp file to cache index to: %w", err)
}

h := sha256.New()
mw := io.MultiWriter(f, h)
if err = r.DownloadIndex(mw); err != nil {
if _, err = io.Copy(mw, index); err != nil {
f.Close()
os.RemoveAll(f.Name())
return "", fmt.Errorf("failed to cache index to temporary file: %w", err)
Expand Down Expand Up @@ -425,12 +430,12 @@ func (r *ChartRepository) LoadFromCache() error {
}

// DownloadIndex attempts to download the chart repository index using
// the Client and set Options, and writes the index to the given io.Writer.
// It returns an url.Error if the URL failed to parse.
func (r *ChartRepository) DownloadIndex(w io.Writer) (err error) {
// the Client and set Options.
// It returns the downloaded index bytes, or an error if it's unreachable or invalid.
func (r *ChartRepository) DownloadIndex() (*bytes.Buffer, error) {
u, err := url.Parse(r.URL)
if err != nil {
return err
return nil, err
}
u.RawPath = path.Join(u.RawPath, "index.yaml")
u.Path = path.Join(u.Path, "index.yaml")
Expand All @@ -439,15 +444,11 @@ func (r *ChartRepository) DownloadIndex(w io.Writer) (err error) {
clientOpts := append(r.Options, getter.WithTransport(t))
defer transport.Release(t)

var res *bytes.Buffer
res, err = r.Client.Get(u.String(), clientOpts...)
res, err := r.Client.Get(u.String(), clientOpts...)
if err != nil {
return err
}
if _, err = io.Copy(w, res); err != nil {
return err
return nil, fmt.Errorf("looks like %q is not a valid chart repository or cannot be reached: %w", r.URL, err)
}
return nil
return res, nil
}

// HasIndex returns true if the Index is not nil.
Expand Down