From 546d071f4f4f15eac93f6779a1463594b62e38d9 Mon Sep 17 00:00:00 2001 From: Seyed Mohammad Mahdi Hatami Date: Fri, 16 Jun 2023 15:52:01 +0330 Subject: [PATCH] added json supporting to DownloadIndexFile Signed-off-by: Seyed Mohammad Mahdi Hatami --- pkg/repo/chartrepo.go | 33 ++++++++++++++++++++++++++++----- pkg/repo/index.go | 12 +++++++++--- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/pkg/repo/chartrepo.go b/pkg/repo/chartrepo.go index d9022ee6e..e57b93704 100644 --- a/pkg/repo/chartrepo.go +++ b/pkg/repo/chartrepo.go @@ -98,7 +98,7 @@ func (r *ChartRepository) Load() error { // what repos to use? filepath.Walk(r.Config.Name, func(path string, f os.FileInfo, err error) error { if !f.IsDir() { - if strings.Contains(f.Name(), "-index.yaml") { + if strings.Contains(f.Name(), "-index.json") || strings.Contains(f.Name(), "-index.yaml") { i, err := LoadIndexFile(path) if err != nil { return err @@ -115,12 +115,16 @@ func (r *ChartRepository) Load() error { // DownloadIndexFile fetches the index from a repository. func (r *ChartRepository) DownloadIndexFile() (string, error) { - indexURL, err := ResolveReferenceURL(r.Config.URL, "index.yaml") + jsonIndexURL, err := ResolveReferenceURL(r.Config.URL, "index.json") + if err != nil { + return "", err + } + yamlIndexURL, err := ResolveReferenceURL(r.Config.URL, "index.yaml") if err != nil { return "", err } - resp, err := r.Client.Get(indexURL, + resp, err := r.Client.Get(jsonIndexURL, getter.WithURL(r.Config.URL), getter.WithInsecureSkipVerifyTLS(r.Config.InsecureSkipTLSverify), getter.WithTLSClientConfig(r.Config.CertFile, r.Config.KeyFile, r.Config.CAFile), @@ -136,9 +140,28 @@ func (r *ChartRepository) DownloadIndexFile() (string, error) { return "", err } - indexFile, err := loadIndex(index, r.Config.URL) + indexFile, err := loadIndex(index, jsonIndexURL) if err != nil { - return "", err + resp, err = r.Client.Get(yamlIndexURL, + getter.WithURL(r.Config.URL), + getter.WithInsecureSkipVerifyTLS(r.Config.InsecureSkipTLSverify), + getter.WithTLSClientConfig(r.Config.CertFile, r.Config.KeyFile, r.Config.CAFile), + getter.WithBasicAuth(r.Config.Username, r.Config.Password), + getter.WithPassCredentialsAll(r.Config.PassCredentialsAll), + ) + if err != nil { + return "", err + } + + index, err = io.ReadAll(resp) + if err != nil { + return "", err + } + + indexFile, err = loadIndex(index, yamlIndexURL) + if err != nil { + return "", err + } } // Create the chart list file in the cache directory diff --git a/pkg/repo/index.go b/pkg/repo/index.go index 2050dd575..85783e4a3 100644 --- a/pkg/repo/index.go +++ b/pkg/repo/index.go @@ -52,6 +52,8 @@ var ( ErrNoChartName = errors.New("no chart name found") // ErrEmptyIndexYaml indicates that the content of index.yaml is empty. ErrEmptyIndexYaml = errors.New("empty index.yaml file") + // ErrEmptyIndexJson indicates that the content of index.json is empty. + ErrEmptyIndexJson = errors.New("empty index.json file") ) // ChartVersions is a list of versioned chart references. @@ -332,14 +334,18 @@ func IndexDirectory(dir, baseURL string) (*IndexFile, error) { // This will fail if API Version is not set (ErrNoAPIVersion) or if the unmarshal fails. func loadIndex(data []byte, source string) (*IndexFile, error) { i := &IndexFile{} + isjson := strings.HasSuffix(source, ".json") // TODO : add error for empty json if len(data) == 0 { - return i, ErrEmptyIndexYaml + if isjson { + return i, ErrEmptyIndexJson + } else { + return i, ErrEmptyIndexYaml + } } - // TODO : check file type, if json, unmarshal with json. - if strings.HasSuffix(source, ".json") { + if isjson { if err := json.Unmarshal(data, i); err != nil { return i, err }