2
0

loader.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. Description: indexChart.Description,
  22. Icon: indexChart.Icon,
  23. }
  24. porterCharts = append(porterCharts, porterChart)
  25. }
  26. return porterCharts
  27. }
  28. // BasicAuthClient is just a username/password to set on requests
  29. type BasicAuthClient struct {
  30. Username string
  31. Password string
  32. }
  33. // LoadRepoIndex uses an http request to get the index file and loads it
  34. func LoadRepoIndex(client *BasicAuthClient, repoURL string) (*repo.IndexFile, error) {
  35. trimmedRepoURL := strings.TrimSuffix(strings.TrimSpace(repoURL), "/")
  36. indexURL := trimmedRepoURL + "/index.yaml"
  37. req, err := http.NewRequest("GET", indexURL, nil)
  38. if err != nil {
  39. return nil, err
  40. }
  41. if client.Username != "" {
  42. req.SetBasicAuth(client.Username, client.Password)
  43. }
  44. resp, err := http.DefaultClient.Do(req)
  45. if err != nil {
  46. return nil, err
  47. }
  48. defer resp.Body.Close()
  49. data, err := ioutil.ReadAll(resp.Body)
  50. if err != nil {
  51. return nil, err
  52. }
  53. // index not found in the cache, parse it
  54. index := &repo.IndexFile{}
  55. err = yaml.Unmarshal(data, index)
  56. if err != nil {
  57. return index, err
  58. }
  59. index.SortEntries()
  60. return index, nil
  61. }
  62. // LoadRepoIndexPublic loads an index file from a remote public Helm repo
  63. func LoadRepoIndexPublic(repoURL string) (*repo.IndexFile, error) {
  64. return LoadRepoIndex(&BasicAuthClient{}, repoURL)
  65. }
  66. // LoadChart uses an http request to fetch a chart from a remote Helm repo
  67. func LoadChart(client *BasicAuthClient, repoURL, chartName, chartVersion string) (*chart.Chart, error) {
  68. repoIndex, err := LoadRepoIndex(client, repoURL)
  69. if err != nil {
  70. return nil, err
  71. }
  72. cv, err := repoIndex.Get(chartName, chartVersion)
  73. if err != nil {
  74. return nil, err
  75. } else if len(cv.URLs) == 0 {
  76. return nil, fmt.Errorf("%s:%s no valid download urls", chartName, chartVersion)
  77. }
  78. trimmedRepoURL := strings.TrimSuffix(strings.TrimSpace(repoURL), "/")
  79. chartURL := trimmedRepoURL + "/" + strings.TrimPrefix(cv.URLs[0], "/")
  80. // download tgz
  81. req, err := http.NewRequest("GET", chartURL, nil)
  82. if err != nil {
  83. return nil, err
  84. }
  85. if client.Username != "" {
  86. req.SetBasicAuth(client.Username, client.Password)
  87. }
  88. resp, err := http.DefaultClient.Do(req)
  89. if err != nil {
  90. return nil, err
  91. }
  92. defer resp.Body.Close()
  93. data, err := ioutil.ReadAll(resp.Body)
  94. if err != nil {
  95. return nil, err
  96. }
  97. return chartloader.LoadArchive(bytes.NewReader(data))
  98. }
  99. // LoadChartPublic returns a Helm3 (v2) chart from a remote public repo.
  100. // If chartVersion is an empty string, the most stable latest version is found.
  101. //
  102. // TODO: this is an expensive operation, so after retrieving the digest from the
  103. // repo index, this should check the digest in the cache
  104. func LoadChartPublic(repoURL, chartName, chartVersion string) (*chart.Chart, error) {
  105. return LoadChart(&BasicAuthClient{}, repoURL, chartName, chartVersion)
  106. }