loader.go 5.5 KB

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