provision_handler.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "github.com/go-chi/chi"
  7. "github.com/porter-dev/porter/internal/forms"
  8. "github.com/porter-dev/porter/internal/kubernetes"
  9. "github.com/porter-dev/porter/internal/kubernetes/provisioner"
  10. "github.com/porter-dev/porter/internal/models"
  11. "github.com/porter-dev/porter/internal/adapter"
  12. )
  13. // HandleProvisionTest will create a test resource by deploying a provisioner
  14. // container pod
  15. func (app *App) HandleProvisionTest(w http.ResponseWriter, r *http.Request) {
  16. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  17. if err != nil || projID == 0 {
  18. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  19. return
  20. }
  21. // create a new agent
  22. agent, err := kubernetes.GetAgentInClusterConfig()
  23. if err != nil {
  24. app.handleErrorDataRead(err, w)
  25. return
  26. }
  27. _, err = agent.ProvisionTest(
  28. uint(projID),
  29. provisioner.Apply,
  30. &app.DBConf,
  31. app.RedisConf,
  32. app.ServerConf.ProvisionerImageTag,
  33. )
  34. if err != nil {
  35. app.handleErrorInternal(err, w)
  36. return
  37. }
  38. w.WriteHeader(http.StatusOK)
  39. }
  40. // HandleProvisionAWSECRInfra provisions a new aws ECR instance for a project
  41. func (app *App) HandleProvisionAWSECRInfra(w http.ResponseWriter, r *http.Request) {
  42. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  43. if err != nil || projID == 0 {
  44. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  45. return
  46. }
  47. form := &forms.CreateECRInfra{
  48. ProjectID: uint(projID),
  49. }
  50. // decode from JSON to form value
  51. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  52. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  53. return
  54. }
  55. // validate the form
  56. if err := app.validator.Struct(form); err != nil {
  57. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  58. return
  59. }
  60. // convert the form to an aws infra instance
  61. infra, err := form.ToInfra()
  62. if err != nil {
  63. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  64. return
  65. }
  66. // handle write to the database
  67. infra, err = app.Repo.Infra.CreateInfra(infra)
  68. if err != nil {
  69. app.handleErrorDataWrite(err, w)
  70. return
  71. }
  72. awsInt, err := app.Repo.AWSIntegration.ReadAWSIntegration(infra.AWSIntegrationID)
  73. if err != nil {
  74. infra.Status = models.StatusError
  75. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  76. app.handleErrorDataRead(err, w)
  77. return
  78. }
  79. // launch provisioning pod
  80. agent, err := kubernetes.GetAgentInClusterConfig()
  81. if err != nil {
  82. infra.Status = models.StatusError
  83. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  84. app.handleErrorDataRead(err, w)
  85. return
  86. }
  87. _, err = agent.ProvisionECR(
  88. uint(projID),
  89. awsInt,
  90. form.ECRName,
  91. infra,
  92. provisioner.Apply,
  93. &app.DBConf,
  94. app.RedisConf,
  95. app.ServerConf.ProvisionerImageTag,
  96. )
  97. if err != nil {
  98. infra.Status = models.StatusError
  99. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  100. app.handleErrorInternal(err, w)
  101. return
  102. }
  103. app.Logger.Info().Msgf("New aws ecr infra created: %d", infra.ID)
  104. w.WriteHeader(http.StatusCreated)
  105. infraExt := infra.Externalize()
  106. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  107. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  108. return
  109. }
  110. }
  111. // HandleDestroyAWSECRInfra destroys ecr infra
  112. func (app *App) HandleDestroyAWSECRInfra(w http.ResponseWriter, r *http.Request) {
  113. // get path parameters
  114. infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
  115. if err != nil {
  116. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  117. return
  118. }
  119. // read infra to get id
  120. infra, err := app.Repo.Infra.ReadInfra(uint(infraID))
  121. if err != nil {
  122. app.handleErrorDataRead(err, w)
  123. return
  124. }
  125. awsInt, err := app.Repo.AWSIntegration.ReadAWSIntegration(infra.AWSIntegrationID)
  126. form := &forms.DestroyECRInfra{}
  127. // decode from JSON to form value
  128. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  129. infra.Status = models.StatusError
  130. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  131. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  132. return
  133. }
  134. // validate the form
  135. if err := app.validator.Struct(form); err != nil {
  136. infra.Status = models.StatusError
  137. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  138. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  139. return
  140. }
  141. // launch provisioning destruction pod
  142. agent, err := kubernetes.GetAgentInClusterConfig()
  143. if err != nil {
  144. infra.Status = models.StatusError
  145. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  146. app.handleErrorDataRead(err, w)
  147. return
  148. }
  149. // mark infra for deletion
  150. infra.Status = models.StatusDestroying
  151. infra, err = app.Repo.Infra.UpdateInfra(infra)
  152. if err != nil {
  153. app.handleErrorDataWrite(err, w)
  154. return
  155. }
  156. _, err = agent.ProvisionECR(
  157. infra.ProjectID,
  158. awsInt,
  159. form.ECRName,
  160. infra,
  161. provisioner.Destroy,
  162. &app.DBConf,
  163. app.RedisConf,
  164. app.ServerConf.ProvisionerImageTag,
  165. )
  166. if err != nil {
  167. app.handleErrorInternal(err, w)
  168. return
  169. }
  170. app.Logger.Info().Msgf("AWS ECR infra marked for destruction: %d", infra.ID)
  171. w.WriteHeader(http.StatusOK)
  172. }
  173. // HandleProvisionAWSEKSInfra provisions a new aws EKS instance for a project
  174. func (app *App) HandleProvisionAWSEKSInfra(w http.ResponseWriter, r *http.Request) {
  175. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  176. if err != nil || projID == 0 {
  177. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  178. return
  179. }
  180. form := &forms.CreateEKSInfra{
  181. ProjectID: uint(projID),
  182. }
  183. // decode from JSON to form value
  184. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  185. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  186. return
  187. }
  188. // validate the form
  189. if err := app.validator.Struct(form); err != nil {
  190. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  191. return
  192. }
  193. // convert the form to an aws infra instance
  194. infra, err := form.ToInfra()
  195. if err != nil {
  196. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  197. return
  198. }
  199. // handle write to the database
  200. infra, err = app.Repo.Infra.CreateInfra(infra)
  201. if err != nil {
  202. app.handleErrorDataWrite(err, w)
  203. return
  204. }
  205. awsInt, err := app.Repo.AWSIntegration.ReadAWSIntegration(infra.AWSIntegrationID)
  206. if err != nil {
  207. infra.Status = models.StatusError
  208. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  209. app.handleErrorDataRead(err, w)
  210. return
  211. }
  212. // launch provisioning pod
  213. agent, err := kubernetes.GetAgentInClusterConfig()
  214. if err != nil {
  215. infra.Status = models.StatusError
  216. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  217. app.handleErrorDataRead(err, w)
  218. return
  219. }
  220. _, err = agent.ProvisionEKS(
  221. uint(projID),
  222. awsInt,
  223. form.EKSName,
  224. infra,
  225. provisioner.Apply,
  226. &app.DBConf,
  227. app.RedisConf,
  228. app.ServerConf.ProvisionerImageTag,
  229. )
  230. if err != nil {
  231. infra.Status = models.StatusError
  232. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  233. app.handleErrorInternal(err, w)
  234. return
  235. }
  236. app.Logger.Info().Msgf("New aws eks infra created: %d", infra.ID)
  237. w.WriteHeader(http.StatusCreated)
  238. infraExt := infra.Externalize()
  239. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  240. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  241. return
  242. }
  243. }
  244. // HandleDestroyAWSEKSInfra destroys eks infra
  245. func (app *App) HandleDestroyAWSEKSInfra(w http.ResponseWriter, r *http.Request) {
  246. // get path parameters
  247. infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
  248. if err != nil {
  249. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  250. return
  251. }
  252. // read infra to get id
  253. infra, err := app.Repo.Infra.ReadInfra(uint(infraID))
  254. if err != nil {
  255. app.handleErrorDataRead(err, w)
  256. return
  257. }
  258. awsInt, err := app.Repo.AWSIntegration.ReadAWSIntegration(infra.AWSIntegrationID)
  259. form := &forms.DestroyEKSInfra{}
  260. // decode from JSON to form value
  261. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  262. infra.Status = models.StatusError
  263. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  264. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  265. return
  266. }
  267. // validate the form
  268. if err := app.validator.Struct(form); err != nil {
  269. infra.Status = models.StatusError
  270. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  271. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  272. return
  273. }
  274. // launch provisioning destruction pod
  275. agent, err := kubernetes.GetAgentInClusterConfig()
  276. if err != nil {
  277. infra.Status = models.StatusError
  278. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  279. app.handleErrorDataRead(err, w)
  280. return
  281. }
  282. // mark infra for deletion
  283. infra.Status = models.StatusDestroying
  284. infra, err = app.Repo.Infra.UpdateInfra(infra)
  285. if err != nil {
  286. app.handleErrorDataWrite(err, w)
  287. return
  288. }
  289. _, err = agent.ProvisionEKS(
  290. infra.ProjectID,
  291. awsInt,
  292. form.EKSName,
  293. infra,
  294. provisioner.Destroy,
  295. &app.DBConf,
  296. app.RedisConf,
  297. app.ServerConf.ProvisionerImageTag,
  298. )
  299. if err != nil {
  300. app.handleErrorInternal(err, w)
  301. return
  302. }
  303. app.Logger.Info().Msgf("AWS EKS infra marked for destruction: %d", infra.ID)
  304. w.WriteHeader(http.StatusOK)
  305. }
  306. // HandleProvisionGCPGCRInfra enables GCR for a project
  307. func (app *App) HandleProvisionGCPGCRInfra(w http.ResponseWriter, r *http.Request) {
  308. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  309. if err != nil || projID == 0 {
  310. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  311. return
  312. }
  313. form := &forms.CreateGCRInfra{
  314. ProjectID: uint(projID),
  315. }
  316. // decode from JSON to form value
  317. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  318. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  319. return
  320. }
  321. // validate the form
  322. if err := app.validator.Struct(form); err != nil {
  323. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  324. return
  325. }
  326. // convert the form to an aws infra instance
  327. infra, err := form.ToInfra()
  328. if err != nil {
  329. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  330. return
  331. }
  332. // handle write to the database
  333. infra, err = app.Repo.Infra.CreateInfra(infra)
  334. if err != nil {
  335. app.handleErrorDataWrite(err, w)
  336. return
  337. }
  338. gcpInt, err := app.Repo.GCPIntegration.ReadGCPIntegration(infra.GCPIntegrationID)
  339. if err != nil {
  340. infra.Status = models.StatusError
  341. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  342. app.handleErrorDataRead(err, w)
  343. return
  344. }
  345. // launch provisioning pod
  346. agent, err := kubernetes.GetAgentInClusterConfig()
  347. if err != nil {
  348. infra.Status = models.StatusError
  349. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  350. app.handleErrorDataRead(err, w)
  351. return
  352. }
  353. _, err = agent.ProvisionGCR(
  354. uint(projID),
  355. gcpInt,
  356. infra,
  357. provisioner.Apply,
  358. &app.DBConf,
  359. app.RedisConf,
  360. app.ServerConf.ProvisionerImageTag,
  361. )
  362. if err != nil {
  363. infra.Status = models.StatusError
  364. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  365. app.handleErrorInternal(err, w)
  366. return
  367. }
  368. app.Logger.Info().Msgf("New gcp gcr infra created: %d", infra.ID)
  369. w.WriteHeader(http.StatusCreated)
  370. infraExt := infra.Externalize()
  371. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  372. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  373. return
  374. }
  375. }
  376. // HandleProvisionGCPGKEInfra provisions a new GKE instance for a project
  377. func (app *App) HandleProvisionGCPGKEInfra(w http.ResponseWriter, r *http.Request) {
  378. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  379. if err != nil || projID == 0 {
  380. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  381. return
  382. }
  383. form := &forms.CreateGKEInfra{
  384. ProjectID: uint(projID),
  385. }
  386. // decode from JSON to form value
  387. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  388. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  389. return
  390. }
  391. // validate the form
  392. if err := app.validator.Struct(form); err != nil {
  393. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  394. return
  395. }
  396. // convert the form to an aws infra instance
  397. infra, err := form.ToInfra()
  398. if err != nil {
  399. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  400. return
  401. }
  402. // handle write to the database
  403. infra, err = app.Repo.Infra.CreateInfra(infra)
  404. if err != nil {
  405. app.handleErrorDataWrite(err, w)
  406. return
  407. }
  408. gcpInt, err := app.Repo.GCPIntegration.ReadGCPIntegration(infra.GCPIntegrationID)
  409. if err != nil {
  410. infra.Status = models.StatusError
  411. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  412. app.handleErrorDataRead(err, w)
  413. return
  414. }
  415. // launch provisioning pod
  416. agent, err := kubernetes.GetAgentInClusterConfig()
  417. if err != nil {
  418. infra.Status = models.StatusError
  419. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  420. app.handleErrorDataRead(err, w)
  421. return
  422. }
  423. _, err = agent.ProvisionGKE(
  424. uint(projID),
  425. gcpInt,
  426. form.GKEName,
  427. infra,
  428. provisioner.Apply,
  429. &app.DBConf,
  430. app.RedisConf,
  431. app.ServerConf.ProvisionerImageTag,
  432. )
  433. if err != nil {
  434. infra.Status = models.StatusError
  435. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  436. app.handleErrorInternal(err, w)
  437. return
  438. }
  439. app.Logger.Info().Msgf("New gcp gke infra created: %d", infra.ID)
  440. w.WriteHeader(http.StatusCreated)
  441. infraExt := infra.Externalize()
  442. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  443. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  444. return
  445. }
  446. }
  447. // HandleDestroyGCPGKEInfra destroys gke infra
  448. func (app *App) HandleDestroyGCPGKEInfra(w http.ResponseWriter, r *http.Request) {
  449. // get path parameters
  450. infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
  451. if err != nil {
  452. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  453. return
  454. }
  455. // read infra to get id
  456. infra, err := app.Repo.Infra.ReadInfra(uint(infraID))
  457. if err != nil {
  458. app.handleErrorDataRead(err, w)
  459. return
  460. }
  461. gcpInt, err := app.Repo.GCPIntegration.ReadGCPIntegration(infra.GCPIntegrationID)
  462. form := &forms.DestroyGKEInfra{}
  463. // decode from JSON to form value
  464. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  465. infra.Status = models.StatusError
  466. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  467. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  468. return
  469. }
  470. // validate the form
  471. if err := app.validator.Struct(form); err != nil {
  472. infra.Status = models.StatusError
  473. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  474. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  475. return
  476. }
  477. // launch provisioning destruction pod
  478. agent, err := kubernetes.GetAgentInClusterConfig()
  479. if err != nil {
  480. infra.Status = models.StatusError
  481. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  482. app.handleErrorDataRead(err, w)
  483. return
  484. }
  485. // mark infra for deletion
  486. infra.Status = models.StatusDestroying
  487. infra, err = app.Repo.Infra.UpdateInfra(infra)
  488. if err != nil {
  489. app.handleErrorDataWrite(err, w)
  490. return
  491. }
  492. _, err = agent.ProvisionGKE(
  493. infra.ProjectID,
  494. gcpInt,
  495. form.GKEName,
  496. infra,
  497. provisioner.Destroy,
  498. &app.DBConf,
  499. app.RedisConf,
  500. app.ServerConf.ProvisionerImageTag,
  501. )
  502. if err != nil {
  503. app.handleErrorInternal(err, w)
  504. return
  505. }
  506. app.Logger.Info().Msgf("GCP GKE infra marked for destruction: %d", infra.ID)
  507. w.WriteHeader(http.StatusOK)
  508. }
  509. // HandleGetProvisioningLogs returns real-time logs of the provisioning process via websockets
  510. func (app *App) HandleGetProvisioningLogs(w http.ResponseWriter, r *http.Request) {
  511. // get path parameters
  512. infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
  513. if err != nil {
  514. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  515. return
  516. }
  517. // read infra to get id
  518. infra, err := app.Repo.Infra.ReadInfra(uint(infraID))
  519. if err != nil {
  520. app.handleErrorDataRead(err, w)
  521. return
  522. }
  523. streamName := infra.GetID()
  524. upgrader.CheckOrigin = func(r *http.Request) bool { return true }
  525. // upgrade to websocket.
  526. conn, err := upgrader.Upgrade(w, r, nil)
  527. if err != nil {
  528. app.handleErrorUpgradeWebsocket(err, w)
  529. }
  530. client, err := adapter.NewRedisClient(app.RedisConf)
  531. if err != nil {
  532. app.handleErrorInternal(err, w)
  533. return
  534. }
  535. err = provisioner.ResourceStream(client, streamName, conn)
  536. if err != nil {
  537. app.handleErrorWebsocketWrite(err, w)
  538. return
  539. }
  540. }