repo.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package repo
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/porter-dev/porter/api/types"
  6. "github.com/porter-dev/porter/internal/helm/loader"
  7. "github.com/porter-dev/porter/internal/models"
  8. "github.com/stefanmcshane/helm/pkg/chart"
  9. "github.com/porter-dev/porter/internal/repository"
  10. )
  11. // HelmRepo wraps the gorm HelmRepo model
  12. type HelmRepo models.HelmRepo
  13. // ListCharts lists Porter charts for a given helm repo
  14. func (hr *HelmRepo) ListCharts(repo repository.Repository) (types.ListTemplatesResponse, error) {
  15. if hr.BasicAuthIntegrationID != 0 {
  16. return hr.listChartsBasic(repo)
  17. }
  18. return nil, fmt.Errorf("error listing charts")
  19. }
  20. // GetChart retrieves a Porter chart for a given helm repo
  21. func (hr *HelmRepo) GetChart(
  22. repo repository.Repository,
  23. chartName, chartVersion string,
  24. ) (*chart.Chart, error) {
  25. if hr.BasicAuthIntegrationID != 0 {
  26. return hr.getChartBasic(repo, chartName, chartVersion)
  27. }
  28. return nil, fmt.Errorf("error listing charts")
  29. }
  30. func (hr *HelmRepo) listChartsBasic(
  31. repo repository.Repository,
  32. ) (types.ListTemplatesResponse, error) {
  33. // get the basic auth integration
  34. basic, err := repo.BasicIntegration().ReadBasicIntegration(
  35. hr.ProjectID,
  36. hr.BasicAuthIntegrationID,
  37. )
  38. if err != nil {
  39. return nil, err
  40. }
  41. client := &loader.BasicAuthClient{
  42. Username: string(basic.Username),
  43. Password: string(basic.Password),
  44. }
  45. repoIndex, err := loader.LoadRepoIndex(client, hr.RepoURL)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return loader.RepoIndexToPorterChartList(repoIndex, hr.RepoURL), nil
  50. }
  51. func (hr *HelmRepo) getChartBasic(
  52. repo repository.Repository,
  53. chartName, chartVersion string,
  54. ) (*chart.Chart, error) {
  55. // get the basic auth integration
  56. basic, err := repo.BasicIntegration().ReadBasicIntegration(
  57. hr.ProjectID,
  58. hr.BasicAuthIntegrationID,
  59. )
  60. if err != nil {
  61. return nil, err
  62. }
  63. client := &loader.BasicAuthClient{
  64. Username: string(basic.Username),
  65. Password: string(basic.Password),
  66. }
  67. return loader.LoadChart(context.Background(), client, hr.RepoURL, chartName, chartVersion)
  68. }
  69. func ValidateRepoURL(
  70. defaultAddonRepoURL, defaultAppRepoURL string,
  71. hrs []*models.HelmRepo,
  72. repo_url string,
  73. ) bool {
  74. if repo_url == defaultAddonRepoURL || repo_url == defaultAppRepoURL {
  75. return true
  76. }
  77. // otherwise, iterate through helm repos
  78. for _, hr := range hrs {
  79. if hr.RepoURL == repo_url {
  80. return true
  81. }
  82. }
  83. return false
  84. }