loader.go 4.3 KB

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