loader.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package loader
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. "k8s.io/helm/pkg/repo"
  9. "sigs.k8s.io/yaml"
  10. "helm.sh/helm/v3/pkg/chart"
  11. chartloader "helm.sh/helm/v3/pkg/chart/loader"
  12. )
  13. // LoadRepoIndex loads an index file from a remote Helm repo
  14. func LoadRepoIndex(indexURL string) (*repo.IndexFile, error) {
  15. resp, err := http.Get(indexURL)
  16. if err != nil {
  17. return nil, err
  18. }
  19. defer resp.Body.Close()
  20. data, err := ioutil.ReadAll(resp.Body)
  21. if err != nil {
  22. return nil, err
  23. }
  24. // index not found in the cache, parse it
  25. index := &repo.IndexFile{}
  26. err = yaml.Unmarshal(data, index)
  27. if err != nil {
  28. return index, err
  29. }
  30. index.SortEntries()
  31. return index, nil
  32. }
  33. // LoadChart returns a Helm3 (v2) chart from a remote repo. If chartVersion is an
  34. // empty string, the most stable latest version is found.
  35. //
  36. // TODO: this is an expensive operation, so after retrieving the digest from the
  37. // repo index, this should check the digest in the cache
  38. func LoadChart(repoURL, chartName, chartVersion string) (*chart.Chart, error) {
  39. trimmedRepoURL := strings.TrimSuffix(strings.TrimSpace(repoURL), "/")
  40. repoIndex, err := LoadRepoIndex(trimmedRepoURL + "/index.yaml")
  41. if err != nil {
  42. return nil, err
  43. }
  44. cv, err := repoIndex.Get(chartName, chartVersion)
  45. if err != nil {
  46. return nil, err
  47. } else if len(cv.URLs) == 0 {
  48. return nil, fmt.Errorf("%s:%s no valid download urls", chartName, chartVersion)
  49. }
  50. chartURL := trimmedRepoURL + "/" + strings.TrimPrefix(cv.URLs[0], "/")
  51. fmt.Println(chartURL)
  52. // download tgz
  53. resp, err := http.Get(chartURL)
  54. if err != nil {
  55. return nil, err
  56. }
  57. defer resp.Body.Close()
  58. data, err := ioutil.ReadAll(resp.Body)
  59. if err != nil {
  60. return nil, err
  61. }
  62. return chartloader.LoadArchive(bytes.NewReader(data))
  63. }