create_addon.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package release
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "github.com/porter-dev/porter/api/server/authz"
  7. "github.com/porter-dev/porter/api/server/handlers"
  8. "github.com/porter-dev/porter/api/server/shared"
  9. "github.com/porter-dev/porter/api/server/shared/apierrors"
  10. "github.com/porter-dev/porter/api/server/shared/config"
  11. "github.com/porter-dev/porter/api/types"
  12. "github.com/porter-dev/porter/internal/analytics"
  13. "github.com/porter-dev/porter/internal/helm"
  14. "github.com/porter-dev/porter/internal/helm/loader"
  15. "github.com/porter-dev/porter/internal/models"
  16. "github.com/porter-dev/porter/internal/oauth"
  17. "github.com/stefanmcshane/helm/pkg/chart"
  18. )
  19. type CreateAddonHandler struct {
  20. handlers.PorterHandlerReadWriter
  21. authz.KubernetesAgentGetter
  22. }
  23. func NewCreateAddonHandler(
  24. config *config.Config,
  25. decoderValidator shared.RequestDecoderValidator,
  26. writer shared.ResultWriter,
  27. ) *CreateAddonHandler {
  28. return &CreateAddonHandler{
  29. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  30. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  31. }
  32. }
  33. func (c *CreateAddonHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  34. user, _ := r.Context().Value(types.UserScope).(*models.User)
  35. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  36. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  37. namespace := r.Context().Value(types.NamespaceScope).(string)
  38. operationID := oauth.CreateRandomState()
  39. c.Config().AnalyticsClient.Track(analytics.ApplicationLaunchStartTrack(
  40. &analytics.ApplicationLaunchStartTrackOpts{
  41. ClusterScopedTrackOpts: analytics.GetClusterScopedTrackOpts(user.ID, cluster.ProjectID, cluster.ID),
  42. FlowID: operationID,
  43. },
  44. ))
  45. helmAgent, err := c.GetHelmAgent(r.Context(), r, cluster, "")
  46. if err != nil {
  47. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  48. return
  49. }
  50. request := &types.CreateAddonRequest{}
  51. if ok := c.DecodeAndValidate(w, r, request); !ok {
  52. return
  53. }
  54. if request.TemplateVersion == "latest" {
  55. request.TemplateVersion = ""
  56. }
  57. chart, err := LoadChart(c.Config(), &LoadAddonChartOpts{
  58. ProjectID: proj.ID,
  59. RepoURL: request.RepoURL,
  60. TemplateName: request.TemplateName,
  61. TemplateVersion: request.TemplateVersion,
  62. })
  63. if err != nil {
  64. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  65. return
  66. }
  67. registries, err := c.Repo().Registry().ListRegistriesByProjectID(cluster.ProjectID)
  68. if err != nil {
  69. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  70. return
  71. }
  72. conf := &helm.InstallChartConfig{
  73. Chart: chart,
  74. Name: request.Name,
  75. Namespace: namespace,
  76. Values: request.Values,
  77. Cluster: cluster,
  78. Repo: c.Repo(),
  79. Registries: registries,
  80. }
  81. helmRelease, err := helmAgent.InstallChart(context.Background(), conf, c.Config().DOConf, c.Config().ServerConf.DisablePullSecretsInjection)
  82. if err != nil {
  83. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  84. fmt.Errorf("error installing a new chart: %s", err.Error()),
  85. http.StatusBadRequest,
  86. ))
  87. return
  88. }
  89. c.Config().AnalyticsClient.Track(analytics.ApplicationLaunchSuccessTrack(
  90. &analytics.ApplicationLaunchSuccessTrackOpts{
  91. ApplicationScopedTrackOpts: analytics.GetApplicationScopedTrackOpts(
  92. user.ID,
  93. cluster.ProjectID,
  94. cluster.ID,
  95. helmRelease.Name,
  96. helmRelease.Namespace,
  97. chart.Metadata.Name,
  98. ),
  99. FlowID: operationID,
  100. },
  101. ))
  102. }
  103. type LoadAddonChartOpts struct {
  104. ProjectID uint
  105. RepoURL, TemplateName, TemplateVersion string
  106. }
  107. func LoadChart(config *config.Config, opts *LoadAddonChartOpts) (*chart.Chart, error) {
  108. // if the chart repo url is one of the specified application/addon charts, just load public
  109. if opts.RepoURL == config.ServerConf.DefaultAddonHelmRepoURL || opts.RepoURL == config.ServerConf.DefaultApplicationHelmRepoURL {
  110. return loader.LoadChartPublic(context.Background(), opts.RepoURL, opts.TemplateName, opts.TemplateVersion)
  111. } else {
  112. // load the helm repos in the project
  113. hrs, err := config.Repo.HelmRepo().ListHelmReposByProjectID(opts.ProjectID)
  114. if err != nil {
  115. return nil, err
  116. }
  117. for _, hr := range hrs {
  118. if hr.RepoURL == opts.RepoURL {
  119. if hr.BasicAuthIntegrationID != 0 {
  120. // read the basic integration id
  121. basic, err := config.Repo.BasicIntegration().ReadBasicIntegration(opts.ProjectID, hr.BasicAuthIntegrationID)
  122. if err != nil {
  123. return nil, err
  124. }
  125. return loader.LoadChart(context.Background(),
  126. &loader.BasicAuthClient{
  127. Username: string(basic.Username),
  128. Password: string(basic.Password),
  129. }, hr.RepoURL, opts.TemplateName, opts.TemplateVersion)
  130. } else {
  131. return loader.LoadChartPublic(context.Background(), hr.RepoURL, opts.TemplateName, opts.TemplateVersion)
  132. }
  133. }
  134. }
  135. }
  136. return nil, fmt.Errorf("chart repo not found")
  137. }