repo.go 2.3 KB

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