loader.go 4.7 KB

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