loader.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package loader
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. "github.com/porter-dev/porter/internal/models"
  9. "k8s.io/helm/pkg/repo"
  10. "sigs.k8s.io/yaml"
  11. "helm.sh/helm/v3/pkg/chart"
  12. chartloader "helm.sh/helm/v3/pkg/chart/loader"
  13. )
  14. // RepoIndexToPorterChartList converts an index file to a list of porter charts
  15. func RepoIndexToPorterChartList(index *repo.IndexFile) []*models.PorterChartList {
  16. porterCharts := make([]*models.PorterChartList, 0)
  17. for _, entry := range index.Entries {
  18. indexChart := entry[0]
  19. porterChart := &models.PorterChartList{
  20. Name: indexChart.Name,
  21. Version: indexChart.Version,
  22. Description: indexChart.Description,
  23. Icon: indexChart.Icon,
  24. }
  25. porterCharts = append(porterCharts, porterChart)
  26. }
  27. return porterCharts
  28. }
  29. // BasicAuthClient is just a username/password to set on requests
  30. type BasicAuthClient struct {
  31. Username string
  32. Password string
  33. }
  34. // LoadRepoIndex uses an http request to get the index file and loads it
  35. func LoadRepoIndex(client *BasicAuthClient, repoURL string) (*repo.IndexFile, error) {
  36. trimmedRepoURL := strings.TrimSuffix(strings.TrimSpace(repoURL), "/")
  37. indexURL := trimmedRepoURL + "/index.yaml"
  38. req, err := http.NewRequest("GET", indexURL, nil)
  39. if err != nil {
  40. return nil, err
  41. }
  42. if client.Username != "" {
  43. req.SetBasicAuth(client.Username, client.Password)
  44. }
  45. resp, err := http.DefaultClient.Do(req)
  46. if err != nil {
  47. return nil, err
  48. }
  49. defer resp.Body.Close()
  50. data, err := ioutil.ReadAll(resp.Body)
  51. if err != nil {
  52. return nil, err
  53. }
  54. // index not found in the cache, parse it
  55. index := &repo.IndexFile{}
  56. err = yaml.Unmarshal(data, index)
  57. if err != nil {
  58. return index, err
  59. }
  60. index.SortEntries()
  61. return index, nil
  62. }
  63. // LoadRepoIndexPublic loads an index file from a remote public Helm repo
  64. func LoadRepoIndexPublic(repoURL string) (*repo.IndexFile, error) {
  65. return LoadRepoIndex(&BasicAuthClient{}, repoURL)
  66. }
  67. // LoadChart uses an http request to fetch a chart from a remote Helm repo
  68. func LoadChart(client *BasicAuthClient, repoURL, chartName, chartVersion string) (*chart.Chart, error) {
  69. repoIndex, err := LoadRepoIndex(client, repoURL)
  70. if err != nil {
  71. return nil, err
  72. }
  73. cv, err := repoIndex.Get(chartName, chartVersion)
  74. if err != nil {
  75. return nil, err
  76. } else if len(cv.URLs) == 0 {
  77. return nil, fmt.Errorf("%s:%s no valid download urls", chartName, chartVersion)
  78. }
  79. trimmedRepoURL := strings.TrimSuffix(strings.TrimSpace(repoURL), "/")
  80. chartURL := trimmedRepoURL + "/" + strings.TrimPrefix(cv.URLs[0], "/")
  81. // download tgz
  82. req, err := http.NewRequest("GET", chartURL, nil)
  83. if err != nil {
  84. return nil, err
  85. }
  86. if client.Username != "" {
  87. req.SetBasicAuth(client.Username, client.Password)
  88. }
  89. resp, err := http.DefaultClient.Do(req)
  90. if err != nil {
  91. return nil, err
  92. }
  93. defer resp.Body.Close()
  94. data, err := ioutil.ReadAll(resp.Body)
  95. if err != nil {
  96. return nil, err
  97. }
  98. return chartloader.LoadArchive(bytes.NewReader(data))
  99. }
  100. // LoadChartPublic returns a Helm3 (v2) chart from a remote public repo.
  101. // If chartVersion is an empty string, the most stable latest version is found.
  102. //
  103. // TODO: this is an expensive operation, so after retrieving the digest from the
  104. // repo index, this should check the digest in the cache
  105. func LoadChartPublic(repoURL, chartName, chartVersion string) (*chart.Chart, error) {
  106. return LoadChart(&BasicAuthClient{}, repoURL, chartName, chartVersion)
  107. }