loader.go 4.8 KB

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