cluster.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi"
  5. "github.com/porter-dev/porter/api/server/handlers/cluster"
  6. "github.com/porter-dev/porter/api/server/handlers/database"
  7. "github.com/porter-dev/porter/api/server/handlers/environment"
  8. "github.com/porter-dev/porter/api/server/handlers/kube_events"
  9. "github.com/porter-dev/porter/api/server/shared"
  10. "github.com/porter-dev/porter/api/server/shared/config"
  11. "github.com/porter-dev/porter/api/types"
  12. )
  13. func NewClusterScopedRegisterer(children ...*Registerer) *Registerer {
  14. return &Registerer{
  15. GetRoutes: GetClusterScopedRoutes,
  16. Children: children,
  17. }
  18. }
  19. func GetClusterScopedRoutes(
  20. r chi.Router,
  21. config *config.Config,
  22. basePath *types.Path,
  23. factory shared.APIEndpointFactory,
  24. children ...*Registerer,
  25. ) []*Route {
  26. routes, projPath := getClusterRoutes(r, config, basePath, factory)
  27. if len(children) > 0 {
  28. r.Route(projPath.RelativePath, func(r chi.Router) {
  29. for _, child := range children {
  30. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  31. routes = append(routes, childRoutes...)
  32. }
  33. })
  34. }
  35. return routes
  36. }
  37. func getClusterRoutes(
  38. r chi.Router,
  39. config *config.Config,
  40. basePath *types.Path,
  41. factory shared.APIEndpointFactory,
  42. ) ([]*Route, *types.Path) {
  43. relPath := "/clusters/{cluster_id}"
  44. newPath := &types.Path{
  45. Parent: basePath,
  46. RelativePath: relPath,
  47. }
  48. routes := make([]*Route, 0)
  49. // POST /api/projects/{project_id}/clusters -> project.NewCreateClusterManualHandler
  50. createEndpoint := factory.NewAPIEndpoint(
  51. &types.APIRequestMetadata{
  52. Verb: types.APIVerbCreate,
  53. Method: types.HTTPVerbPost,
  54. Path: &types.Path{
  55. Parent: basePath,
  56. RelativePath: "/clusters",
  57. },
  58. Scopes: []types.PermissionScope{
  59. types.UserScope,
  60. types.ProjectScope,
  61. },
  62. },
  63. )
  64. createHandler := cluster.NewCreateClusterManualHandler(
  65. config,
  66. factory.GetDecoderValidator(),
  67. factory.GetResultWriter(),
  68. )
  69. routes = append(routes, &Route{
  70. Endpoint: createEndpoint,
  71. Handler: createHandler,
  72. Router: r,
  73. })
  74. // POST /api/projects/{project_id}/clusters/candidates -> project.NewCreateClusterCandidateHandler
  75. createCandidateEndpoint := factory.NewAPIEndpoint(
  76. &types.APIRequestMetadata{
  77. Verb: types.APIVerbCreate,
  78. Method: types.HTTPVerbPost,
  79. Path: &types.Path{
  80. Parent: basePath,
  81. RelativePath: "/clusters/candidates",
  82. },
  83. Scopes: []types.PermissionScope{
  84. types.UserScope,
  85. types.ProjectScope,
  86. },
  87. CheckUsage: true,
  88. UsageMetric: types.Clusters,
  89. },
  90. )
  91. createCandidateHandler := cluster.NewCreateClusterCandidateHandler(
  92. config,
  93. factory.GetDecoderValidator(),
  94. factory.GetResultWriter(),
  95. )
  96. routes = append(routes, &Route{
  97. Endpoint: createCandidateEndpoint,
  98. Handler: createCandidateHandler,
  99. Router: r,
  100. })
  101. // GET /api/projects/{project_id}/clusters/candidates -> project.NewListClusterCandidatesHandler
  102. listCandidatesEndpoint := factory.NewAPIEndpoint(
  103. &types.APIRequestMetadata{
  104. Verb: types.APIVerbList,
  105. Method: types.HTTPVerbGet,
  106. Path: &types.Path{
  107. Parent: basePath,
  108. RelativePath: "/clusters/candidates",
  109. },
  110. Scopes: []types.PermissionScope{
  111. types.UserScope,
  112. types.ProjectScope,
  113. },
  114. },
  115. )
  116. listCandidatesHandler := cluster.NewListClusterCandidatesHandler(
  117. config,
  118. factory.GetResultWriter(),
  119. )
  120. routes = append(routes, &Route{
  121. Endpoint: listCandidatesEndpoint,
  122. Handler: listCandidatesHandler,
  123. Router: r,
  124. })
  125. // POST /api/projects/{project_id}/clusters/candidates/{candidate_id}/resolve -> project.NewResolveClusterCandidateHandler
  126. resolveCandidateEndpoint := factory.NewAPIEndpoint(
  127. &types.APIRequestMetadata{
  128. Verb: types.APIVerbCreate,
  129. Method: types.HTTPVerbPost,
  130. Path: &types.Path{
  131. Parent: basePath,
  132. RelativePath: fmt.Sprintf(
  133. "/clusters/candidates/{%s}/resolve",
  134. types.URLParamCandidateID,
  135. ),
  136. },
  137. Scopes: []types.PermissionScope{
  138. types.UserScope,
  139. types.ProjectScope,
  140. },
  141. CheckUsage: true,
  142. UsageMetric: types.Clusters,
  143. },
  144. )
  145. resolveCandidateHandler := cluster.NewResolveClusterCandidateHandler(
  146. config,
  147. factory.GetDecoderValidator(),
  148. factory.GetResultWriter(),
  149. )
  150. routes = append(routes, &Route{
  151. Endpoint: resolveCandidateEndpoint,
  152. Handler: resolveCandidateHandler,
  153. Router: r,
  154. })
  155. // POST /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterUpdateHandler
  156. updateClusterEndpoint := factory.NewAPIEndpoint(
  157. &types.APIRequestMetadata{
  158. Verb: types.APIVerbUpdate,
  159. Method: types.HTTPVerbPost,
  160. Path: &types.Path{
  161. Parent: basePath,
  162. RelativePath: relPath,
  163. },
  164. Scopes: []types.PermissionScope{
  165. types.UserScope,
  166. types.ProjectScope,
  167. types.ClusterScope,
  168. },
  169. },
  170. )
  171. updateClusterHandler := cluster.NewClusterUpdateHandler(
  172. config,
  173. factory.GetDecoderValidator(),
  174. factory.GetResultWriter(),
  175. )
  176. routes = append(routes, &Route{
  177. Endpoint: updateClusterEndpoint,
  178. Handler: updateClusterHandler,
  179. Router: r,
  180. })
  181. // DELETE /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterDeleteHandler
  182. deleteClusterEndpoint := factory.NewAPIEndpoint(
  183. &types.APIRequestMetadata{
  184. Verb: types.APIVerbDelete,
  185. Method: types.HTTPVerbDelete,
  186. Path: &types.Path{
  187. Parent: basePath,
  188. RelativePath: relPath,
  189. },
  190. Scopes: []types.PermissionScope{
  191. types.UserScope,
  192. types.ProjectScope,
  193. types.ClusterScope,
  194. },
  195. },
  196. )
  197. deleteClusterHandler := cluster.NewClusterDeleteHandler(
  198. config,
  199. factory.GetResultWriter(),
  200. )
  201. routes = append(routes, &Route{
  202. Endpoint: deleteClusterEndpoint,
  203. Handler: deleteClusterHandler,
  204. Router: r,
  205. })
  206. // GET /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterGetHandler
  207. getEndpoint := factory.NewAPIEndpoint(
  208. &types.APIRequestMetadata{
  209. Verb: types.APIVerbGet,
  210. Method: types.HTTPVerbGet,
  211. Path: &types.Path{
  212. Parent: basePath,
  213. RelativePath: relPath,
  214. },
  215. Scopes: []types.PermissionScope{
  216. types.UserScope,
  217. types.ProjectScope,
  218. types.ClusterScope,
  219. },
  220. },
  221. )
  222. getHandler := cluster.NewClusterGetHandler(
  223. config,
  224. factory.GetResultWriter(),
  225. )
  226. routes = append(routes, &Route{
  227. Endpoint: getEndpoint,
  228. Handler: getHandler,
  229. Router: r,
  230. })
  231. // GET /api/projects/{project_id}/clusters/{cluster_id}/databases -> database.NewDatabaseListHandler
  232. listDatabaseEndpoint := factory.NewAPIEndpoint(
  233. &types.APIRequestMetadata{
  234. Verb: types.APIVerbList,
  235. Method: types.HTTPVerbGet,
  236. Path: &types.Path{
  237. Parent: basePath,
  238. RelativePath: relPath + "/databases",
  239. },
  240. Scopes: []types.PermissionScope{
  241. types.UserScope,
  242. types.ProjectScope,
  243. types.ClusterScope,
  244. },
  245. },
  246. )
  247. listDatabaseHandler := database.NewDatabaseListHandler(
  248. config,
  249. factory.GetResultWriter(),
  250. )
  251. routes = append(routes, &Route{
  252. Endpoint: listDatabaseEndpoint,
  253. Handler: listDatabaseHandler,
  254. Router: r,
  255. })
  256. if config.ServerConf.GithubIncomingWebhookSecret != "" {
  257. // GET /api/projects/{project_id}/clusters/{cluster_id}/environments -> environment.NewListEnvironmentHandler
  258. listEnvEndpoint := factory.NewAPIEndpoint(
  259. &types.APIRequestMetadata{
  260. Verb: types.APIVerbGet,
  261. Method: types.HTTPVerbGet,
  262. Path: &types.Path{
  263. Parent: basePath,
  264. RelativePath: relPath + "/environments",
  265. },
  266. Scopes: []types.PermissionScope{
  267. types.UserScope,
  268. types.ProjectScope,
  269. types.ClusterScope,
  270. },
  271. },
  272. )
  273. listEnvHandler := environment.NewListEnvironmentHandler(
  274. config,
  275. factory.GetResultWriter(),
  276. )
  277. routes = append(routes, &Route{
  278. Endpoint: listEnvEndpoint,
  279. Handler: listEnvHandler,
  280. Router: r,
  281. })
  282. // GET /api/projects/{project_id}/clusters/{cluster_id}/deployments -> environment.NewListDeploymentsByClusterHandler
  283. listDeploymentsEndpoint := factory.NewAPIEndpoint(
  284. &types.APIRequestMetadata{
  285. Verb: types.APIVerbGet,
  286. Method: types.HTTPVerbGet,
  287. Path: &types.Path{
  288. Parent: basePath,
  289. RelativePath: relPath + "/deployments",
  290. },
  291. Scopes: []types.PermissionScope{
  292. types.UserScope,
  293. types.ProjectScope,
  294. types.ClusterScope,
  295. },
  296. },
  297. )
  298. listDeploymentsHandler := environment.NewListDeploymentsByClusterHandler(
  299. config,
  300. factory.GetDecoderValidator(),
  301. factory.GetResultWriter(),
  302. )
  303. routes = append(routes, &Route{
  304. Endpoint: listDeploymentsEndpoint,
  305. Handler: listDeploymentsHandler,
  306. Router: r,
  307. })
  308. // GET /api/projects/{project_id}/clusters/{cluster_id}/{environment_id}/deployment -> environment.NewGetDeploymentByClusterHandler
  309. getDeploymentEndpoint := factory.NewAPIEndpoint(
  310. &types.APIRequestMetadata{
  311. Verb: types.APIVerbGet,
  312. Method: types.HTTPVerbGet,
  313. Path: &types.Path{
  314. Parent: basePath,
  315. RelativePath: relPath + "/{environment_id}/deployment",
  316. },
  317. Scopes: []types.PermissionScope{
  318. types.UserScope,
  319. types.ProjectScope,
  320. types.ClusterScope,
  321. },
  322. },
  323. )
  324. getDeploymentHandler := environment.NewGetDeploymentByClusterHandler(
  325. config,
  326. factory.GetDecoderValidator(),
  327. factory.GetResultWriter(),
  328. )
  329. routes = append(routes, &Route{
  330. Endpoint: getDeploymentEndpoint,
  331. Handler: getDeploymentHandler,
  332. Router: r,
  333. })
  334. // PATCH /api/projects/{project_id}/clusters/{cluster_id}/deployments/{deployment_id}/reenable -> environment.NewReenableDeploymentHandler
  335. reenableDeploymentEndpoint := factory.NewAPIEndpoint(
  336. &types.APIRequestMetadata{
  337. Verb: types.APIVerbUpdate,
  338. Method: types.HTTPVerbPatch,
  339. Path: &types.Path{
  340. Parent: basePath,
  341. RelativePath: relPath + "/deployments/{deployment_id}/reenable",
  342. },
  343. Scopes: []types.PermissionScope{
  344. types.UserScope,
  345. types.ProjectScope,
  346. types.ClusterScope,
  347. },
  348. },
  349. )
  350. reenableDeploymentHandler := environment.NewReenableDeploymentHandler(
  351. config,
  352. factory.GetDecoderValidator(),
  353. factory.GetResultWriter(),
  354. )
  355. routes = append(routes, &Route{
  356. Endpoint: reenableDeploymentEndpoint,
  357. Handler: reenableDeploymentHandler,
  358. Router: r,
  359. })
  360. // POST /api/projects/{project_id}/clusters/{cluster_id}/deployments/{deployment_id}/trigger_workflow -> environment.NewTriggerDeploymentWorkflowHandler
  361. triggerDeploymentWorkflowEndpoint := factory.NewAPIEndpoint(
  362. &types.APIRequestMetadata{
  363. Verb: types.APIVerbCreate,
  364. Method: types.HTTPVerbPost,
  365. Path: &types.Path{
  366. Parent: basePath,
  367. RelativePath: relPath + "/deployments/{deployment_id}/trigger_workflow",
  368. },
  369. Scopes: []types.PermissionScope{
  370. types.UserScope,
  371. types.ProjectScope,
  372. types.ClusterScope,
  373. },
  374. },
  375. )
  376. triggerDeploymentWorkflowHandler := environment.NewTriggerDeploymentWorkflowHandler(
  377. config,
  378. factory.GetDecoderValidator(),
  379. factory.GetResultWriter(),
  380. )
  381. routes = append(routes, &Route{
  382. Endpoint: triggerDeploymentWorkflowEndpoint,
  383. Handler: triggerDeploymentWorkflowHandler,
  384. Router: r,
  385. })
  386. // POST /api/projects/{project_id}/clusters/{cluster_id}/deployments/pull_request -> environment.NewEnablePullRequestHandler
  387. enablePullRequestEndpoint := factory.NewAPIEndpoint(
  388. &types.APIRequestMetadata{
  389. Verb: types.APIVerbCreate,
  390. Method: types.HTTPVerbPost,
  391. Path: &types.Path{
  392. Parent: basePath,
  393. RelativePath: relPath + "/deployments/pull_request",
  394. },
  395. Scopes: []types.PermissionScope{
  396. types.UserScope,
  397. types.ProjectScope,
  398. types.ClusterScope,
  399. },
  400. },
  401. )
  402. enablePullRequestHandler := environment.NewEnablePullRequestHandler(
  403. config,
  404. factory.GetDecoderValidator(),
  405. factory.GetResultWriter(),
  406. )
  407. routes = append(routes, &Route{
  408. Endpoint: enablePullRequestEndpoint,
  409. Handler: enablePullRequestHandler,
  410. Router: r,
  411. })
  412. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/deployments/{deployment_id} ->
  413. // environment.NewDeleteDeploymentHandler
  414. deleteDeploymentEndpoint := factory.NewAPIEndpoint(
  415. &types.APIRequestMetadata{
  416. Verb: types.APIVerbDelete,
  417. Method: types.HTTPVerbDelete,
  418. Path: &types.Path{
  419. Parent: basePath,
  420. RelativePath: relPath + "/deployments/{deployment_id}",
  421. },
  422. Scopes: []types.PermissionScope{
  423. types.UserScope,
  424. types.ProjectScope,
  425. types.ClusterScope,
  426. },
  427. },
  428. )
  429. deleteDeploymentHandler := environment.NewDeleteDeploymentHandler(
  430. config,
  431. factory.GetDecoderValidator(),
  432. factory.GetResultWriter(),
  433. )
  434. routes = append(routes, &Route{
  435. Endpoint: deleteDeploymentEndpoint,
  436. Handler: deleteDeploymentHandler,
  437. Router: r,
  438. })
  439. }
  440. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces -> cluster.NewClusterListNamespacesHandler
  441. listNamespacesEndpoint := factory.NewAPIEndpoint(
  442. &types.APIRequestMetadata{
  443. Verb: types.APIVerbGet,
  444. Method: types.HTTPVerbGet,
  445. Path: &types.Path{
  446. Parent: basePath,
  447. RelativePath: relPath + "/namespaces",
  448. },
  449. Scopes: []types.PermissionScope{
  450. types.UserScope,
  451. types.ProjectScope,
  452. types.ClusterScope,
  453. },
  454. },
  455. )
  456. listNamespacesHandler := cluster.NewListNamespacesHandler(
  457. config,
  458. factory.GetResultWriter(),
  459. )
  460. routes = append(routes, &Route{
  461. Endpoint: listNamespacesEndpoint,
  462. Handler: listNamespacesHandler,
  463. Router: r,
  464. })
  465. // GET /api/projects/{project_id}/clusters/{cluster_id}/nodes -> cluster.NewListNodesHandler
  466. listNodesEndpoint := factory.NewAPIEndpoint(
  467. &types.APIRequestMetadata{
  468. Verb: types.APIVerbGet,
  469. Method: types.HTTPVerbGet,
  470. Path: &types.Path{
  471. Parent: basePath,
  472. RelativePath: relPath + "/nodes",
  473. },
  474. Scopes: []types.PermissionScope{
  475. types.UserScope,
  476. types.ProjectScope,
  477. types.ClusterScope,
  478. },
  479. },
  480. )
  481. listNodesHandler := cluster.NewListNodesHandler(
  482. config,
  483. factory.GetResultWriter(),
  484. )
  485. routes = append(routes, &Route{
  486. Endpoint: listNodesEndpoint,
  487. Handler: listNodesHandler,
  488. Router: r,
  489. })
  490. // GET /api/projects/{project_id}/clusters/{cluster_id}/nodes/{node_name} -> cluster.NewGetNodeHandler
  491. getNodeEndpoint := factory.NewAPIEndpoint(
  492. &types.APIRequestMetadata{
  493. Verb: types.APIVerbGet,
  494. Method: types.HTTPVerbGet,
  495. Path: &types.Path{
  496. Parent: basePath,
  497. RelativePath: fmt.Sprintf("%s/nodes/{%s}", relPath, types.URLParamNodeName),
  498. },
  499. Scopes: []types.PermissionScope{
  500. types.UserScope,
  501. types.ProjectScope,
  502. types.ClusterScope,
  503. },
  504. },
  505. )
  506. getNodeHandler := cluster.NewGetNodeHandler(
  507. config,
  508. factory.GetResultWriter(),
  509. )
  510. routes = append(routes, &Route{
  511. Endpoint: getNodeEndpoint,
  512. Handler: getNodeHandler,
  513. Router: r,
  514. })
  515. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/create -> cluster.NewCreateNamespaceHandler
  516. createNamespaceEndpoint := factory.NewAPIEndpoint(
  517. &types.APIRequestMetadata{
  518. Verb: types.APIVerbCreate,
  519. Method: types.HTTPVerbPost,
  520. Path: &types.Path{
  521. Parent: basePath,
  522. RelativePath: relPath + "/namespaces/create",
  523. },
  524. Scopes: []types.PermissionScope{
  525. types.UserScope,
  526. types.ProjectScope,
  527. types.ClusterScope,
  528. },
  529. },
  530. )
  531. createNamespaceHandler := cluster.NewCreateNamespaceHandler(
  532. config,
  533. factory.GetDecoderValidator(),
  534. factory.GetResultWriter(),
  535. )
  536. routes = append(routes, &Route{
  537. Endpoint: createNamespaceEndpoint,
  538. Handler: createNamespaceHandler,
  539. Router: r,
  540. })
  541. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/delete -> cluster.NewDeleteNamespaceHandler
  542. deleteNamespaceEndpoint := factory.NewAPIEndpoint(
  543. &types.APIRequestMetadata{
  544. Verb: types.APIVerbDelete,
  545. Method: types.HTTPVerbDelete,
  546. Path: &types.Path{
  547. Parent: basePath,
  548. RelativePath: relPath + "/namespaces/delete",
  549. },
  550. Scopes: []types.PermissionScope{
  551. types.UserScope,
  552. types.ProjectScope,
  553. types.ClusterScope,
  554. },
  555. },
  556. )
  557. deleteNamespaceHandler := cluster.NewDeleteNamespaceHandler(
  558. config,
  559. factory.GetDecoderValidator(),
  560. )
  561. routes = append(routes, &Route{
  562. Endpoint: deleteNamespaceEndpoint,
  563. Handler: deleteNamespaceHandler,
  564. Router: r,
  565. })
  566. // GET /api/projects/{project_id}/clusters/{cluster_id}/kubeconfig -> cluster.NewGetTemporaryKubeconfigHandler
  567. getTemporaryKubeconfigEndpoint := factory.NewAPIEndpoint(
  568. &types.APIRequestMetadata{
  569. Verb: types.APIVerbGet,
  570. Method: types.HTTPVerbGet,
  571. Path: &types.Path{
  572. Parent: basePath,
  573. RelativePath: relPath + "/kubeconfig",
  574. },
  575. Scopes: []types.PermissionScope{
  576. types.UserScope,
  577. types.ProjectScope,
  578. types.ClusterScope,
  579. },
  580. },
  581. )
  582. getTemporaryKubeconfigHandler := cluster.NewGetTemporaryKubeconfigHandler(
  583. config,
  584. factory.GetResultWriter(),
  585. )
  586. routes = append(routes, &Route{
  587. Endpoint: getTemporaryKubeconfigEndpoint,
  588. Handler: getTemporaryKubeconfigHandler,
  589. Router: r,
  590. })
  591. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/detect -> cluster.NewDetectPrometheusInstalledHandler
  592. detectPrometheusInstalledEndpoint := factory.NewAPIEndpoint(
  593. &types.APIRequestMetadata{
  594. Verb: types.APIVerbGet,
  595. Method: types.HTTPVerbGet,
  596. Path: &types.Path{
  597. Parent: basePath,
  598. RelativePath: relPath + "/prometheus/detect",
  599. },
  600. Scopes: []types.PermissionScope{
  601. types.UserScope,
  602. types.ProjectScope,
  603. types.ClusterScope,
  604. },
  605. },
  606. )
  607. detectPrometheusInstalledHandler := cluster.NewDetectPrometheusInstalledHandler(config)
  608. routes = append(routes, &Route{
  609. Endpoint: detectPrometheusInstalledEndpoint,
  610. Handler: detectPrometheusInstalledHandler,
  611. Router: r,
  612. })
  613. // GET /api/projects/{project_id}/clusters/{cluster_id}/agent/detect -> cluster.NewDetectAgentInstalledHandler
  614. detectAgentInstalledEndpoint := factory.NewAPIEndpoint(
  615. &types.APIRequestMetadata{
  616. Verb: types.APIVerbGet,
  617. Method: types.HTTPVerbGet,
  618. Path: &types.Path{
  619. Parent: basePath,
  620. RelativePath: relPath + "/agent/detect",
  621. },
  622. Scopes: []types.PermissionScope{
  623. types.UserScope,
  624. types.ProjectScope,
  625. types.ClusterScope,
  626. },
  627. },
  628. )
  629. detectAgentInstalledHandler := cluster.NewDetectAgentInstalledHandler(config, factory.GetResultWriter())
  630. routes = append(routes, &Route{
  631. Endpoint: detectAgentInstalledEndpoint,
  632. Handler: detectAgentInstalledHandler,
  633. Router: r,
  634. })
  635. // POST /api/projects/{project_id}/clusters/{cluster_id}/agent/install -> cluster.NewInstallAgentHandler
  636. installAgentEndpoint := factory.NewAPIEndpoint(
  637. &types.APIRequestMetadata{
  638. Verb: types.APIVerbCreate,
  639. Method: types.HTTPVerbPost,
  640. Path: &types.Path{
  641. Parent: basePath,
  642. RelativePath: relPath + "/agent/install",
  643. },
  644. Scopes: []types.PermissionScope{
  645. types.UserScope,
  646. types.ProjectScope,
  647. types.ClusterScope,
  648. },
  649. },
  650. )
  651. installAgentHandler := cluster.NewInstallAgentHandler(
  652. config,
  653. factory.GetDecoderValidator(),
  654. factory.GetResultWriter(),
  655. )
  656. routes = append(routes, &Route{
  657. Endpoint: installAgentEndpoint,
  658. Handler: installAgentHandler,
  659. Router: r,
  660. })
  661. // POST /api/projects/{project_id}/clusters/{cluster_id}/agent/upgrade -> cluster.NewInstallAgentHandler
  662. upgradeAgentEndpoint := factory.NewAPIEndpoint(
  663. &types.APIRequestMetadata{
  664. Verb: types.APIVerbCreate,
  665. Method: types.HTTPVerbPost,
  666. Path: &types.Path{
  667. Parent: basePath,
  668. RelativePath: relPath + "/agent/upgrade",
  669. },
  670. Scopes: []types.PermissionScope{
  671. types.UserScope,
  672. types.ProjectScope,
  673. types.ClusterScope,
  674. },
  675. },
  676. )
  677. upgradeAgentHandler := cluster.NewUpgradeAgentHandler(
  678. config,
  679. factory.GetDecoderValidator(),
  680. factory.GetResultWriter(),
  681. )
  682. routes = append(routes, &Route{
  683. Endpoint: upgradeAgentEndpoint,
  684. Handler: upgradeAgentHandler,
  685. Router: r,
  686. })
  687. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events -> kube_events.NewGetKubeEventHandler
  688. listKubeEventsEndpoint := factory.NewAPIEndpoint(
  689. &types.APIRequestMetadata{
  690. Verb: types.APIVerbGet,
  691. Method: types.HTTPVerbGet,
  692. Path: &types.Path{
  693. Parent: basePath,
  694. RelativePath: relPath + "/kube_events",
  695. },
  696. Scopes: []types.PermissionScope{
  697. types.UserScope,
  698. types.ProjectScope,
  699. types.ClusterScope,
  700. },
  701. },
  702. )
  703. listKubeEventsHandler := kube_events.NewListKubeEventsHandler(
  704. config,
  705. factory.GetDecoderValidator(),
  706. factory.GetResultWriter(),
  707. )
  708. routes = append(routes, &Route{
  709. Endpoint: listKubeEventsEndpoint,
  710. Handler: listKubeEventsHandler,
  711. Router: r,
  712. })
  713. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events -> kube_events.NewGetKubeEventHandler
  714. getKubeEventEndpoint := factory.NewAPIEndpoint(
  715. &types.APIRequestMetadata{
  716. Verb: types.APIVerbGet,
  717. Method: types.HTTPVerbGet,
  718. Path: &types.Path{
  719. Parent: basePath,
  720. RelativePath: fmt.Sprintf("%s/kube_events/{%s}", relPath, types.URLParamKubeEventID),
  721. },
  722. Scopes: []types.PermissionScope{
  723. types.UserScope,
  724. types.ProjectScope,
  725. types.ClusterScope,
  726. },
  727. },
  728. )
  729. getKubeEventHandler := kube_events.NewGetKubeEventHandler(
  730. config,
  731. factory.GetDecoderValidator(),
  732. factory.GetResultWriter(),
  733. )
  734. routes = append(routes, &Route{
  735. Endpoint: getKubeEventEndpoint,
  736. Handler: getKubeEventHandler,
  737. Router: r,
  738. })
  739. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events/{kube_event_id}/logs -> kube_events.NewGetKubeEventLogsHandler
  740. getKubeEventLogsEndpoint := factory.NewAPIEndpoint(
  741. &types.APIRequestMetadata{
  742. Verb: types.APIVerbGet,
  743. Method: types.HTTPVerbGet,
  744. Path: &types.Path{
  745. Parent: basePath,
  746. RelativePath: fmt.Sprintf("%s/kube_events/{%s}/logs", relPath, types.URLParamKubeEventID),
  747. },
  748. Scopes: []types.PermissionScope{
  749. types.UserScope,
  750. types.ProjectScope,
  751. types.ClusterScope,
  752. },
  753. },
  754. )
  755. getKubeEventLogsHandler := kube_events.NewGetKubeEventLogsHandler(
  756. config,
  757. factory.GetDecoderValidator(),
  758. factory.GetResultWriter(),
  759. )
  760. routes = append(routes, &Route{
  761. Endpoint: getKubeEventLogsEndpoint,
  762. Handler: getKubeEventLogsHandler,
  763. Router: r,
  764. })
  765. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events/{kube_event_id}/log_buckets -> kube_events.NewGetKubeEventLogBucketsHandler
  766. getKubeEventLogBucketsEndpoint := factory.NewAPIEndpoint(
  767. &types.APIRequestMetadata{
  768. Verb: types.APIVerbGet,
  769. Method: types.HTTPVerbGet,
  770. Path: &types.Path{
  771. Parent: basePath,
  772. RelativePath: fmt.Sprintf("%s/kube_events/{%s}/log_buckets", relPath, types.URLParamKubeEventID),
  773. },
  774. Scopes: []types.PermissionScope{
  775. types.UserScope,
  776. types.ProjectScope,
  777. types.ClusterScope,
  778. },
  779. },
  780. )
  781. getKubeEventLogBucketsHandler := kube_events.NewGetKubeEventLogBucketsHandler(
  782. config,
  783. factory.GetDecoderValidator(),
  784. factory.GetResultWriter(),
  785. )
  786. routes = append(routes, &Route{
  787. Endpoint: getKubeEventLogBucketsEndpoint,
  788. Handler: getKubeEventLogBucketsHandler,
  789. Router: r,
  790. })
  791. // POST /api/projects/{project_id}/clusters/{cluster_id}/kube_events -> kube_events.NewCreateKubeEventHandler
  792. createKubeEventsEndpoint := factory.NewAPIEndpoint(
  793. &types.APIRequestMetadata{
  794. Verb: types.APIVerbCreate,
  795. Method: types.HTTPVerbPost,
  796. Path: &types.Path{
  797. Parent: basePath,
  798. RelativePath: relPath + "/kube_events",
  799. },
  800. Scopes: []types.PermissionScope{
  801. types.UserScope,
  802. types.ProjectScope,
  803. types.ClusterScope,
  804. },
  805. },
  806. )
  807. createKubeEventsHandler := kube_events.NewCreateKubeEventHandler(
  808. config,
  809. factory.GetDecoderValidator(),
  810. factory.GetResultWriter(),
  811. )
  812. routes = append(routes, &Route{
  813. Endpoint: createKubeEventsEndpoint,
  814. Handler: createKubeEventsHandler,
  815. Router: r,
  816. })
  817. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/ingresses -> cluster.NewListNGINXIngressesHandler
  818. listNGINXIngressesEndpoint := factory.NewAPIEndpoint(
  819. &types.APIRequestMetadata{
  820. Verb: types.APIVerbGet,
  821. Method: types.HTTPVerbGet,
  822. Path: &types.Path{
  823. Parent: basePath,
  824. RelativePath: relPath + "/prometheus/ingresses",
  825. },
  826. Scopes: []types.PermissionScope{
  827. types.UserScope,
  828. types.ProjectScope,
  829. types.ClusterScope,
  830. },
  831. },
  832. )
  833. listNGINXIngressesHandler := cluster.NewListNGINXIngressesHandler(
  834. config,
  835. factory.GetResultWriter(),
  836. )
  837. routes = append(routes, &Route{
  838. Endpoint: listNGINXIngressesEndpoint,
  839. Handler: listNGINXIngressesHandler,
  840. Router: r,
  841. })
  842. // GET /api/projects/{project_id}/clusters/{cluster_id}/metrics -> cluster.NewGetPodMetricsHandler
  843. getPodMetricsEndpoint := factory.NewAPIEndpoint(
  844. &types.APIRequestMetadata{
  845. Verb: types.APIVerbGet,
  846. Method: types.HTTPVerbGet,
  847. Path: &types.Path{
  848. Parent: basePath,
  849. RelativePath: relPath + "/metrics",
  850. },
  851. Scopes: []types.PermissionScope{
  852. types.UserScope,
  853. types.ProjectScope,
  854. types.ClusterScope,
  855. },
  856. },
  857. )
  858. getPodMetricsHandler := cluster.NewGetPodMetricsHandler(
  859. config,
  860. factory.GetDecoderValidator(),
  861. factory.GetResultWriter(),
  862. )
  863. routes = append(routes, &Route{
  864. Endpoint: getPodMetricsEndpoint,
  865. Handler: getPodMetricsHandler,
  866. Router: r,
  867. })
  868. // GET /api/projects/{project_id}/clusters/{cluster_id}/helm_release -> cluster.NewStreamHelmReleaseHandler
  869. streamHelmReleaseEndpoint := factory.NewAPIEndpoint(
  870. &types.APIRequestMetadata{
  871. Verb: types.APIVerbGet,
  872. Method: types.HTTPVerbGet,
  873. Path: &types.Path{
  874. Parent: basePath,
  875. RelativePath: relPath + "/helm_release",
  876. },
  877. Scopes: []types.PermissionScope{
  878. types.UserScope,
  879. types.ProjectScope,
  880. types.ClusterScope,
  881. },
  882. IsWebsocket: true,
  883. },
  884. )
  885. streamHelmReleaseHandler := cluster.NewStreamHelmReleaseHandler(
  886. config,
  887. factory.GetDecoderValidator(),
  888. factory.GetResultWriter(),
  889. )
  890. routes = append(routes, &Route{
  891. Endpoint: streamHelmReleaseEndpoint,
  892. Handler: streamHelmReleaseHandler,
  893. Router: r,
  894. })
  895. // GET /api/projects/{project_id}/clusters/{cluster_id}/{kind}/status -> cluster.NewStreamStatusHandler
  896. streamStatusEndpoint := factory.NewAPIEndpoint(
  897. &types.APIRequestMetadata{
  898. Verb: types.APIVerbGet,
  899. Method: types.HTTPVerbGet,
  900. Path: &types.Path{
  901. Parent: basePath,
  902. RelativePath: fmt.Sprintf(
  903. "%s/{%s}/status",
  904. relPath,
  905. types.URLParamKind,
  906. ),
  907. },
  908. Scopes: []types.PermissionScope{
  909. types.UserScope,
  910. types.ProjectScope,
  911. types.ClusterScope,
  912. },
  913. IsWebsocket: true,
  914. },
  915. )
  916. streamStatusHandler := cluster.NewStreamStatusHandler(
  917. config,
  918. factory.GetDecoderValidator(),
  919. factory.GetResultWriter(),
  920. )
  921. routes = append(routes, &Route{
  922. Endpoint: streamStatusEndpoint,
  923. Handler: streamStatusHandler,
  924. Router: r,
  925. })
  926. // GET /api/projects/{project_id}/clusters/{cluster_id}/pods -> cluster.NewGetPodsHandler
  927. getPodsEndpoint := factory.NewAPIEndpoint(
  928. &types.APIRequestMetadata{
  929. Verb: types.APIVerbGet,
  930. Method: types.HTTPVerbGet,
  931. Path: &types.Path{
  932. Parent: basePath,
  933. RelativePath: relPath + "/pods",
  934. },
  935. Scopes: []types.PermissionScope{
  936. types.UserScope,
  937. types.ProjectScope,
  938. types.ClusterScope,
  939. },
  940. },
  941. )
  942. getPodsHandler := cluster.NewGetPodsHandler(
  943. config,
  944. factory.GetDecoderValidator(),
  945. factory.GetResultWriter(),
  946. )
  947. routes = append(routes, &Route{
  948. Endpoint: getPodsEndpoint,
  949. Handler: getPodsHandler,
  950. Router: r,
  951. })
  952. // GET /api/projects/{project_id}/clusters/{cluster_id}/incidents -> cluster.NewGetIncidentsHandler
  953. getIncidentsEndpoint := factory.NewAPIEndpoint(
  954. &types.APIRequestMetadata{
  955. Verb: types.APIVerbGet,
  956. Method: types.HTTPVerbGet,
  957. Path: &types.Path{
  958. Parent: basePath,
  959. RelativePath: relPath + "/incidents",
  960. },
  961. Scopes: []types.PermissionScope{
  962. types.UserScope,
  963. types.ProjectScope,
  964. types.ClusterScope,
  965. },
  966. },
  967. )
  968. getIncidentsHandler := cluster.NewGetIncidentsHandler(
  969. config,
  970. factory.GetDecoderValidator(),
  971. factory.GetResultWriter(),
  972. )
  973. routes = append(routes, &Route{
  974. Endpoint: getIncidentsEndpoint,
  975. Handler: getIncidentsHandler,
  976. Router: r,
  977. })
  978. // GET /api/projects/{project_id}/clusters/{cluster_id}/incidents/logs -> cluster.NewGetIncidentsHandler
  979. getIncidentEventLogsEndpoint := factory.NewAPIEndpoint(
  980. &types.APIRequestMetadata{
  981. Verb: types.APIVerbGet,
  982. Method: types.HTTPVerbGet,
  983. Path: &types.Path{
  984. Parent: basePath,
  985. RelativePath: relPath + "/incidents/logs",
  986. },
  987. Scopes: []types.PermissionScope{
  988. types.UserScope,
  989. types.ProjectScope,
  990. types.ClusterScope,
  991. },
  992. },
  993. )
  994. getIncidentEventLogsHandler := cluster.NewGetIncidentEventLogsHandler(
  995. config,
  996. factory.GetDecoderValidator(),
  997. factory.GetResultWriter(),
  998. )
  999. routes = append(routes, &Route{
  1000. Endpoint: getIncidentEventLogsEndpoint,
  1001. Handler: getIncidentEventLogsHandler,
  1002. Router: r,
  1003. })
  1004. // POST /api/projects/{project_id}/clusters/{cluster_id}/incidents/notify_new -> cluster.NewNotifyNewIncidentHandler
  1005. notifyNewIncidentEndpoint := factory.NewAPIEndpoint(
  1006. &types.APIRequestMetadata{
  1007. Verb: types.APIVerbCreate,
  1008. Method: types.HTTPVerbPost,
  1009. Path: &types.Path{
  1010. Parent: basePath,
  1011. RelativePath: relPath + "/incidents/notify_new",
  1012. },
  1013. Scopes: []types.PermissionScope{
  1014. types.UserScope,
  1015. types.ProjectScope,
  1016. types.ClusterScope,
  1017. },
  1018. },
  1019. )
  1020. notifyNewIncidentHandler := cluster.NewNotifyNewIncidentHandler(
  1021. config,
  1022. factory.GetDecoderValidator(),
  1023. factory.GetResultWriter(),
  1024. )
  1025. routes = append(routes, &Route{
  1026. Endpoint: notifyNewIncidentEndpoint,
  1027. Handler: notifyNewIncidentHandler,
  1028. Router: r,
  1029. })
  1030. // POST /api/projects/{project_id}/clusters/{cluster_id}/incidents/notify_resolved -> cluster.NewNotifyResolvedIncidentHandler
  1031. notifyResolvedIncidentEndpoint := factory.NewAPIEndpoint(
  1032. &types.APIRequestMetadata{
  1033. Verb: types.APIVerbCreate,
  1034. Method: types.HTTPVerbPost,
  1035. Path: &types.Path{
  1036. Parent: basePath,
  1037. RelativePath: relPath + "/incidents/notify_resolved",
  1038. },
  1039. Scopes: []types.PermissionScope{
  1040. types.UserScope,
  1041. types.ProjectScope,
  1042. types.ClusterScope,
  1043. },
  1044. },
  1045. )
  1046. notifyResolvedIncidentHandler := cluster.NewNotifyResolvedIncidentHandler(
  1047. config,
  1048. factory.GetDecoderValidator(),
  1049. factory.GetResultWriter(),
  1050. )
  1051. routes = append(routes, &Route{
  1052. Endpoint: notifyResolvedIncidentEndpoint,
  1053. Handler: notifyResolvedIncidentHandler,
  1054. Router: r,
  1055. })
  1056. return routes, newPath
  1057. }