loader.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package loader
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. "github.com/porter-dev/porter/internal/telemetry"
  11. "k8s.io/helm/pkg/repo"
  12. "sigs.k8s.io/yaml"
  13. "github.com/porter-dev/porter/api/types"
  14. "github.com/stefanmcshane/helm/pkg/chart"
  15. chartloader "github.com/stefanmcshane/helm/pkg/chart/loader"
  16. )
  17. // RepoIndexToPorterChartList converts an index file to a list of porter charts
  18. func RepoIndexToPorterChartList(index *repo.IndexFile, repoURL string) types.ListTemplatesResponse {
  19. // sort the entries before parsing
  20. index.SortEntries()
  21. porterCharts := make(types.ListTemplatesResponse, 0)
  22. for _, entryVersions := range index.Entries {
  23. indexChart := entryVersions[0]
  24. versions := make([]string, 0)
  25. for _, entryVersion := range entryVersions {
  26. versions = append(versions, entryVersion.Version)
  27. }
  28. porterChart := types.PorterTemplateSimple{
  29. Name: indexChart.Name,
  30. Description: indexChart.Description,
  31. Icon: indexChart.Icon,
  32. Versions: versions,
  33. RepoURL: repoURL,
  34. Tags: indexChart.Keywords,
  35. }
  36. porterCharts = append(porterCharts, porterChart)
  37. }
  38. return porterCharts
  39. }
  40. // FindPorterChartInIndexList finds a chart by name given an index file and returns it
  41. func FindPorterChartInIndexList(index *repo.IndexFile, name string) *types.PorterTemplateSimple {
  42. // sort the entries before parsing
  43. index.SortEntries()
  44. for _, entryVersions := range index.Entries {
  45. indexChart := entryVersions[0]
  46. if indexChart.Name == name {
  47. versions := make([]string, 0)
  48. for _, entryVersion := range entryVersions {
  49. versions = append(versions, entryVersion.Version)
  50. }
  51. return &types.PorterTemplateSimple{
  52. Name: indexChart.Name,
  53. Description: indexChart.Description,
  54. Icon: indexChart.Icon,
  55. Versions: versions,
  56. }
  57. }
  58. }
  59. return nil
  60. }
  61. // BasicAuthClient is just a username/password to set on requests
  62. type BasicAuthClient struct {
  63. Username string
  64. Password string
  65. }
  66. // LoadRepoIndex uses an http request to get the index file and loads it
  67. func LoadRepoIndex(client *BasicAuthClient, repoURL string) (*repo.IndexFile, error) {
  68. trimmedRepoURL := strings.TrimSuffix(strings.TrimSpace(repoURL), "/")
  69. indexURL := trimmedRepoURL + "/index.yaml"
  70. req, err := http.NewRequest("GET", indexURL, nil)
  71. if err != nil {
  72. return nil, err
  73. }
  74. if client.Username != "" {
  75. req.SetBasicAuth(client.Username, client.Password)
  76. }
  77. resp, err := http.DefaultClient.Do(req)
  78. if err != nil {
  79. return nil, err
  80. }
  81. defer resp.Body.Close()
  82. data, err := ioutil.ReadAll(resp.Body)
  83. if err != nil {
  84. return nil, err
  85. }
  86. // index not found in the cache, parse it
  87. index := &repo.IndexFile{}
  88. err = yaml.Unmarshal(data, index)
  89. if err != nil {
  90. return index, err
  91. }
  92. index.SortEntries()
  93. return index, nil
  94. }
  95. // LoadRepoIndexPublic loads an index file from a remote public Helm repo
  96. func LoadRepoIndexPublic(repoURL string) (*repo.IndexFile, error) {
  97. return LoadRepoIndex(&BasicAuthClient{}, repoURL)
  98. }
  99. // LoadChart uses an http request to fetch a chart from a remote Helm repo
  100. func LoadChart(ctx context.Context, client *BasicAuthClient, repoURL, chartName, chartVersion string) (*chart.Chart, error) {
  101. ctx, span := telemetry.NewSpan(ctx, "load-chart")
  102. defer span.End()
  103. telemetry.WithAttributes(span,
  104. telemetry.AttributeKV{Key: "repo-url", Value: repoURL},
  105. telemetry.AttributeKV{Key: "chart-name", Value: chartName},
  106. telemetry.AttributeKV{Key: "chart-version", Value: chartVersion},
  107. )
  108. repoIndex, err := LoadRepoIndex(client, repoURL)
  109. if err != nil {
  110. return nil, telemetry.Error(ctx, span, err, "error loading repo index")
  111. }
  112. cv, err := repoIndex.Get(chartName, chartVersion)
  113. if err != nil {
  114. return nil, telemetry.Error(ctx, span, err, "error getting repo index")
  115. } else if len(cv.URLs) == 0 {
  116. return nil, telemetry.Error(ctx, span, nil, fmt.Sprintf("%s:%s no valid download urls", chartName, chartVersion))
  117. }
  118. trimmedRepoURL := strings.TrimSuffix(strings.TrimSpace(repoURL), "/")
  119. chartURL := cv.URLs[0]
  120. if !isValidURL(chartURL) {
  121. chartURL = trimmedRepoURL + "/" + strings.TrimPrefix(cv.URLs[0], "/")
  122. }
  123. // download tgz
  124. req, err := http.NewRequest("GET", chartURL, nil)
  125. if err != nil {
  126. return nil, telemetry.Error(ctx, span, err, "error creating request")
  127. }
  128. if client.Username != "" {
  129. req.SetBasicAuth(client.Username, client.Password)
  130. }
  131. resp, err := http.DefaultClient.Do(req)
  132. if err != nil {
  133. return nil, telemetry.Error(ctx, span, err, "error executing request")
  134. }
  135. defer resp.Body.Close()
  136. data, err := ioutil.ReadAll(resp.Body)
  137. if err != nil {
  138. return nil, telemetry.Error(ctx, span, err, "error reading response body")
  139. }
  140. return chartloader.LoadArchive(bytes.NewReader(data))
  141. }
  142. // LoadChartPublic returns a Helm3 (v2) chart from a remote public repo.
  143. // If chartVersion is an empty string, the most stable latest version is found.
  144. //
  145. // TODO: this is an expensive operation, so after retrieving the digest from the
  146. // repo index, this should check the digest in the cache
  147. func LoadChartPublic(ctx context.Context, repoURL, chartName, chartVersion string) (*chart.Chart, error) {
  148. return LoadChart(ctx, &BasicAuthClient{}, repoURL, chartName, chartVersion)
  149. }
  150. // Helper method to test if chart repo URL is valid, or is a path. Chartmuseum saves URLs
  151. // as paths, other Helm repositories do not.
  152. func isValidURL(testURI string) bool {
  153. _, err := url.ParseRequestURI(testURI)
  154. if err != nil {
  155. return false
  156. }
  157. u, err := url.Parse(testURI)
  158. if err != nil || u.Scheme == "" || u.Host == "" {
  159. return false
  160. }
  161. return true
  162. }