create.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. package porter_app
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "github.com/google/uuid"
  9. "github.com/porter-dev/porter/internal/telemetry"
  10. "github.com/porter-dev/porter/api/server/authz"
  11. "github.com/porter-dev/porter/api/server/handlers"
  12. "github.com/porter-dev/porter/api/server/shared"
  13. "github.com/porter-dev/porter/api/server/shared/apierrors"
  14. "github.com/porter-dev/porter/api/server/shared/config"
  15. "github.com/porter-dev/porter/api/server/shared/requestutils"
  16. "github.com/porter-dev/porter/api/types"
  17. "github.com/porter-dev/porter/internal/helm"
  18. "github.com/porter-dev/porter/internal/helm/loader"
  19. "github.com/porter-dev/porter/internal/models"
  20. "github.com/porter-dev/porter/internal/repository"
  21. "github.com/stefanmcshane/helm/pkg/chart"
  22. )
  23. type CreatePorterAppHandler struct {
  24. handlers.PorterHandlerReadWriter
  25. authz.KubernetesAgentGetter
  26. }
  27. func NewCreatePorterAppHandler(
  28. config *config.Config,
  29. decoderValidator shared.RequestDecoderValidator,
  30. writer shared.ResultWriter,
  31. ) *CreatePorterAppHandler {
  32. return &CreatePorterAppHandler{
  33. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  34. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  35. }
  36. }
  37. func (c *CreatePorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  38. ctx := r.Context()
  39. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  40. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  41. ctx, span := telemetry.NewSpan(r.Context(), "serve-create-porter-app")
  42. defer span.End()
  43. request := &types.CreatePorterAppRequest{}
  44. if ok := c.DecodeAndValidate(w, r, request); !ok {
  45. err := telemetry.Error(ctx, span, nil, "error decoding request")
  46. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  47. return
  48. }
  49. stackName, reqErr := requestutils.GetURLParamString(r, types.URLParamStackName)
  50. if reqErr != nil {
  51. err := telemetry.Error(ctx, span, reqErr, "error getting stack name from url")
  52. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  53. return
  54. }
  55. namespace := fmt.Sprintf("porter-stack-%s", stackName)
  56. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "application-name", Value: stackName})
  57. helmAgent, err := c.GetHelmAgent(ctx, r, cluster, namespace)
  58. if err != nil {
  59. err = telemetry.Error(ctx, span, err, "error getting helm agent")
  60. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  61. return
  62. }
  63. k8sAgent, err := c.GetAgent(r, cluster, namespace)
  64. if err != nil {
  65. err = telemetry.Error(ctx, span, err, "error getting k8s agent")
  66. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  67. return
  68. }
  69. helmRelease, err := helmAgent.GetRelease(ctx, stackName, 0, false)
  70. shouldCreate := err != nil
  71. porterYamlBase64 := request.PorterYAMLBase64
  72. porterYaml, err := base64.StdEncoding.DecodeString(porterYamlBase64)
  73. if err != nil {
  74. err = telemetry.Error(ctx, span, err, "error decoding porter yaml")
  75. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  76. return
  77. }
  78. imageInfo := request.ImageInfo
  79. registries, err := c.Repo().Registry().ListRegistriesByProjectID(cluster.ProjectID)
  80. if err != nil {
  81. err = telemetry.Error(ctx, span, err, "error listing registries")
  82. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  83. return
  84. }
  85. var releaseValues map[string]interface{}
  86. var releaseDependencies []*chart.Dependency
  87. if shouldCreate || request.OverrideRelease {
  88. releaseValues = nil
  89. releaseDependencies = nil
  90. // this is required because when the front-end sends an update request with overrideRelease=true, it is unable to
  91. // get the image info from the release. unless it is explicitly provided in the request, we avoid overwriting it
  92. // by attempting to get the image info from the release
  93. if helmRelease != nil && (imageInfo.Repository == "" || imageInfo.Tag == "") {
  94. imageInfo = attemptToGetImageInfoFromRelease(helmRelease.Config)
  95. }
  96. } else {
  97. releaseValues = helmRelease.Config
  98. releaseDependencies = helmRelease.Chart.Metadata.Dependencies
  99. }
  100. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "image-repo", Value: imageInfo.Repository}, telemetry.AttributeKV{Key: "image-tag", Value: imageInfo.Tag})
  101. if request.Builder == "" {
  102. // attempt to get builder from db
  103. app, err := c.Repo().PorterApp().ReadPorterAppByName(cluster.ID, stackName)
  104. if err == nil {
  105. request.Builder = app.Builder
  106. }
  107. }
  108. injectLauncher := strings.Contains(request.Builder, "heroku") ||
  109. strings.Contains(request.Builder, "paketo")
  110. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "builder", Value: request.Builder})
  111. chart, values, releaseJobValues, err := parse(
  112. porterYaml,
  113. imageInfo,
  114. c.Config(),
  115. cluster.ProjectID,
  116. releaseValues,
  117. releaseDependencies,
  118. SubdomainCreateOpts{
  119. k8sAgent: k8sAgent,
  120. dnsRepo: c.Repo().DNSRecord(),
  121. powerDnsClient: c.Config().PowerDNSClient,
  122. appRootDomain: c.Config().ServerConf.AppRootDomain,
  123. stackName: stackName,
  124. },
  125. injectLauncher,
  126. shouldCreate,
  127. )
  128. if err != nil {
  129. err = telemetry.Error(ctx, span, err, "error parsing porter yaml into chart and values")
  130. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  131. return
  132. }
  133. if shouldCreate {
  134. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "installing-application", Value: true})
  135. // create the namespace if it does not exist already
  136. _, err = k8sAgent.CreateNamespace(namespace, nil)
  137. if err != nil {
  138. err = telemetry.Error(ctx, span, err, "error creating namespace")
  139. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  140. return
  141. }
  142. // create the release job chart if it does not exist (only done by front-end currently, where we set overrideRelease=true)
  143. if request.OverrideRelease && releaseJobValues != nil {
  144. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "installing-pre-deploy-job", Value: true})
  145. conf, err := createReleaseJobChart(
  146. ctx,
  147. stackName,
  148. releaseJobValues,
  149. c.Config().ServerConf.DefaultApplicationHelmRepoURL,
  150. registries,
  151. cluster,
  152. c.Repo(),
  153. )
  154. if err != nil {
  155. err = telemetry.Error(ctx, span, err, "error making config for pre-deploy job chart")
  156. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  157. return
  158. }
  159. _, err = helmAgent.InstallChart(ctx, conf, c.Config().DOConf, c.Config().ServerConf.DisablePullSecretsInjection)
  160. if err != nil {
  161. err = telemetry.Error(ctx, span, err, "error installing pre-deploy job chart")
  162. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "install-pre-deploy-job-error", Value: err})
  163. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  164. _, uninstallChartErr := helmAgent.UninstallChart(ctx, fmt.Sprintf("%s-r", stackName))
  165. if uninstallChartErr != nil {
  166. uninstallChartErr = telemetry.Error(ctx, span, err, "error uninstalling pre-deploy job chart after failed install")
  167. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(uninstallChartErr, http.StatusInternalServerError))
  168. }
  169. return
  170. }
  171. }
  172. conf := &helm.InstallChartConfig{
  173. Chart: chart,
  174. Name: stackName,
  175. Namespace: namespace,
  176. Values: values,
  177. Cluster: cluster,
  178. Repo: c.Repo(),
  179. Registries: registries,
  180. }
  181. // create the app chart
  182. _, err = helmAgent.InstallChart(ctx, conf, c.Config().DOConf, c.Config().ServerConf.DisablePullSecretsInjection)
  183. if err != nil {
  184. err = telemetry.Error(ctx, span, err, "error installing app chart")
  185. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  186. _, err = helmAgent.UninstallChart(ctx, stackName)
  187. if err != nil {
  188. err = telemetry.Error(ctx, span, err, "error uninstalling app chart after failed install")
  189. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  190. }
  191. return
  192. }
  193. existing, err := c.Repo().PorterApp().ReadPorterAppByName(cluster.ID, stackName)
  194. if err != nil {
  195. err = telemetry.Error(ctx, span, err, "error reading app from DB")
  196. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  197. return
  198. } else if existing.Name != "" {
  199. err = telemetry.Error(ctx, span, err, "app with name already exists in project")
  200. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusForbidden))
  201. return
  202. }
  203. app := &models.PorterApp{
  204. Name: stackName,
  205. ClusterID: cluster.ID,
  206. ProjectID: project.ID,
  207. RepoName: request.RepoName,
  208. GitRepoID: request.GitRepoID,
  209. GitBranch: request.GitBranch,
  210. BuildContext: request.BuildContext,
  211. Builder: request.Builder,
  212. Buildpacks: request.Buildpacks,
  213. Dockerfile: request.Dockerfile,
  214. ImageRepoURI: request.ImageRepoURI,
  215. PullRequestURL: request.PullRequestURL,
  216. PorterYamlPath: request.PorterYamlPath,
  217. }
  218. // create the db entry
  219. porterApp, err := c.Repo().PorterApp().UpdatePorterApp(app)
  220. if err != nil {
  221. err = telemetry.Error(ctx, span, err, "error writing app to DB")
  222. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  223. return
  224. }
  225. _, err = createPorterAppEvent(ctx, "SUCCESS", porterApp.ID, 1, imageInfo.Tag, c.Repo().PorterAppEvent())
  226. if err != nil {
  227. err = telemetry.Error(ctx, span, err, "error creating porter app event")
  228. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  229. return
  230. }
  231. c.WriteResult(w, r, porterApp.ToPorterAppType())
  232. } else {
  233. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "upgrading-application", Value: true})
  234. // create/update the release job chart
  235. if request.OverrideRelease {
  236. if releaseJobValues == nil {
  237. releaseJobName := fmt.Sprintf("%s-r", stackName)
  238. _, err := helmAgent.GetRelease(ctx, releaseJobName, 0, false)
  239. if err == nil {
  240. // handle exception where the user has chosen to delete the release job
  241. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "deleting-pre-deploy-job", Value: true})
  242. _, err = helmAgent.UninstallChart(ctx, releaseJobName)
  243. if err != nil {
  244. err = telemetry.Error(ctx, span, err, "error uninstalling pre-deploy job chart")
  245. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  246. return
  247. }
  248. }
  249. } else {
  250. releaseJobName := fmt.Sprintf("%s-r", stackName)
  251. helmRelease, err := helmAgent.GetRelease(ctx, releaseJobName, 0, false)
  252. if err != nil {
  253. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "creating-pre-deploy-job", Value: true})
  254. conf, err := createReleaseJobChart(
  255. ctx,
  256. stackName,
  257. releaseJobValues,
  258. c.Config().ServerConf.DefaultApplicationHelmRepoURL,
  259. registries,
  260. cluster,
  261. c.Repo(),
  262. )
  263. if err != nil {
  264. err = telemetry.Error(ctx, span, err, "error making config for pre-deploy job chart")
  265. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  266. return
  267. }
  268. _, err = helmAgent.InstallChart(ctx, conf, c.Config().DOConf, c.Config().ServerConf.DisablePullSecretsInjection)
  269. if err != nil {
  270. err = telemetry.Error(ctx, span, err, "error installing pre-deploy job chart")
  271. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "install-pre-deploy-job-error", Value: err})
  272. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  273. _, uninstallChartErr := helmAgent.UninstallChart(ctx, fmt.Sprintf("%s-r", stackName))
  274. if uninstallChartErr != nil {
  275. uninstallChartErr = telemetry.Error(ctx, span, err, "error uninstalling pre-deploy job chart after failed install")
  276. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(uninstallChartErr, http.StatusInternalServerError))
  277. }
  278. return
  279. }
  280. } else {
  281. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "updating-pre-deploy-job", Value: true})
  282. chart, err := loader.LoadChartPublic(ctx, c.Config().Metadata.DefaultAppHelmRepoURL, "job", "")
  283. if err != nil {
  284. err = telemetry.Error(ctx, span, err, "error loading latest job chart")
  285. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  286. return
  287. }
  288. conf := &helm.UpgradeReleaseConfig{
  289. Name: helmRelease.Name,
  290. Cluster: cluster,
  291. Repo: c.Repo(),
  292. Registries: registries,
  293. Values: releaseJobValues,
  294. Chart: chart,
  295. }
  296. _, err = helmAgent.UpgradeReleaseByValues(ctx, conf, c.Config().DOConf, c.Config().ServerConf.DisablePullSecretsInjection, false)
  297. if err != nil {
  298. err = telemetry.Error(ctx, span, err, "error upgrading pre-deploy job chart")
  299. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  300. return
  301. }
  302. }
  303. }
  304. }
  305. // update the app chart
  306. conf := &helm.InstallChartConfig{
  307. Chart: chart,
  308. Name: stackName,
  309. Namespace: namespace,
  310. Values: values,
  311. Cluster: cluster,
  312. Repo: c.Repo(),
  313. Registries: registries,
  314. }
  315. // update the chart
  316. _, err = helmAgent.UpgradeInstallChart(ctx, conf, c.Config().DOConf, c.Config().ServerConf.DisablePullSecretsInjection)
  317. if err != nil {
  318. err = telemetry.Error(ctx, span, err, "error upgrading application")
  319. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  320. return
  321. }
  322. // update the DB entry
  323. app, err := c.Repo().PorterApp().ReadPorterAppByName(cluster.ID, stackName)
  324. if err != nil {
  325. err = telemetry.Error(ctx, span, err, "error reading app from DB")
  326. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  327. return
  328. }
  329. if app == nil {
  330. err = telemetry.Error(ctx, span, nil, "app with name does not exist in project")
  331. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusForbidden))
  332. return
  333. }
  334. if request.RepoName != "" {
  335. app.RepoName = request.RepoName
  336. }
  337. if request.GitBranch != "" {
  338. app.GitBranch = request.GitBranch
  339. }
  340. if request.BuildContext != "" {
  341. app.BuildContext = request.BuildContext
  342. }
  343. // handles deletion of builder,buildpacks, and dockerfile path
  344. if request.Builder != "" {
  345. if request.Builder == "null" {
  346. app.Builder = ""
  347. } else {
  348. app.Builder = request.Builder
  349. }
  350. }
  351. if request.Buildpacks != "" {
  352. if request.Buildpacks == "null" {
  353. app.Buildpacks = ""
  354. } else {
  355. app.Buildpacks = request.Buildpacks
  356. }
  357. }
  358. if request.Dockerfile != "" {
  359. if request.Dockerfile == "null" {
  360. app.Dockerfile = ""
  361. } else {
  362. app.Dockerfile = request.Dockerfile
  363. }
  364. }
  365. if request.ImageRepoURI != "" {
  366. app.ImageRepoURI = request.ImageRepoURI
  367. }
  368. if request.PullRequestURL != "" {
  369. app.PullRequestURL = request.PullRequestURL
  370. }
  371. telemetry.WithAttributes(
  372. span,
  373. telemetry.AttributeKV{Key: "updated-repo-name", Value: app.RepoName},
  374. telemetry.AttributeKV{Key: "updated-git-branch", Value: app.GitBranch},
  375. telemetry.AttributeKV{Key: "updated-build-context", Value: app.BuildContext},
  376. telemetry.AttributeKV{Key: "updated-builder", Value: app.Builder},
  377. telemetry.AttributeKV{Key: "updated-buildpacks", Value: app.Buildpacks},
  378. telemetry.AttributeKV{Key: "updated-dockerfile", Value: app.Dockerfile},
  379. telemetry.AttributeKV{Key: "updated-image-repo-uri", Value: app.ImageRepoURI},
  380. telemetry.AttributeKV{Key: "updated-pull-request-url", Value: app.PullRequestURL},
  381. )
  382. updatedPorterApp, err := c.Repo().PorterApp().UpdatePorterApp(app)
  383. if err != nil {
  384. err = telemetry.Error(ctx, span, err, "error writing updated app to DB")
  385. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  386. return
  387. }
  388. _, err = createPorterAppEvent(ctx, "SUCCESS", updatedPorterApp.ID, helmRelease.Version+1, imageInfo.Tag, c.Repo().PorterAppEvent())
  389. if err != nil {
  390. err = telemetry.Error(ctx, span, err, "error creating porter app event")
  391. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  392. return
  393. }
  394. c.WriteResult(w, r, updatedPorterApp.ToPorterAppType())
  395. }
  396. }
  397. // createPorterAppEvent creates an event for use in the activity feed
  398. func createPorterAppEvent(ctx context.Context, status string, appID uint, revision int, tag string, repo repository.PorterAppEventRepository) (*models.PorterAppEvent, error) {
  399. event := models.PorterAppEvent{
  400. ID: uuid.New(),
  401. Status: status,
  402. Type: "DEPLOY",
  403. TypeExternalSource: "KUBERNETES",
  404. PorterAppID: appID,
  405. Metadata: map[string]any{
  406. "revision": revision,
  407. "image_tag": tag,
  408. },
  409. }
  410. err := repo.CreateEvent(ctx, &event)
  411. if err != nil {
  412. return nil, err
  413. }
  414. if event.ID == uuid.Nil {
  415. return nil, err
  416. }
  417. return &event, nil
  418. }
  419. func createReleaseJobChart(
  420. ctx context.Context,
  421. stackName string,
  422. values map[string]interface{},
  423. repoUrl string,
  424. registries []*models.Registry,
  425. cluster *models.Cluster,
  426. repo repository.Repository,
  427. ) (*helm.InstallChartConfig, error) {
  428. chart, err := loader.LoadChartPublic(ctx, repoUrl, "job", "")
  429. if err != nil {
  430. return nil, err
  431. }
  432. releaseName := fmt.Sprintf("%s-r", stackName)
  433. namespace := fmt.Sprintf("porter-stack-%s", stackName)
  434. return &helm.InstallChartConfig{
  435. Chart: chart,
  436. Name: releaseName,
  437. Namespace: namespace,
  438. Values: values,
  439. Cluster: cluster,
  440. Repo: repo,
  441. Registries: registries,
  442. }, nil
  443. }