cluster.go 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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/pull_request -> environment.NewEnablePullRequestHandler
  361. enablePullRequestEndpoint := factory.NewAPIEndpoint(
  362. &types.APIRequestMetadata{
  363. Verb: types.APIVerbCreate,
  364. Method: types.HTTPVerbPost,
  365. Path: &types.Path{
  366. Parent: basePath,
  367. RelativePath: relPath + "/deployments/pull_request",
  368. },
  369. Scopes: []types.PermissionScope{
  370. types.UserScope,
  371. types.ProjectScope,
  372. types.ClusterScope,
  373. },
  374. },
  375. )
  376. enablePullRequestHandler := environment.NewEnablePullRequestHandler(
  377. config,
  378. factory.GetDecoderValidator(),
  379. factory.GetResultWriter(),
  380. )
  381. routes = append(routes, &Route{
  382. Endpoint: enablePullRequestEndpoint,
  383. Handler: enablePullRequestHandler,
  384. Router: r,
  385. })
  386. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/deployments/{environment_id}/{owner}/{name}/{pr_number} ->
  387. // environment.NewDeleteDeploymentHandler
  388. deleteDeploymentEndpoint := factory.NewAPIEndpoint(
  389. &types.APIRequestMetadata{
  390. Verb: types.APIVerbDelete,
  391. Method: types.HTTPVerbDelete,
  392. Path: &types.Path{
  393. Parent: basePath,
  394. RelativePath: fmt.Sprintf(
  395. "%s/deployments/{environment_id}/{%s}/{%s}/{pr_number}",
  396. relPath,
  397. types.URLParamGitRepoOwner,
  398. types.URLParamGitRepoName,
  399. ),
  400. },
  401. Scopes: []types.PermissionScope{
  402. types.UserScope,
  403. types.ProjectScope,
  404. types.ClusterScope,
  405. },
  406. },
  407. )
  408. deleteDeploymentHandler := environment.NewDeleteDeploymentHandler(
  409. config,
  410. factory.GetDecoderValidator(),
  411. factory.GetResultWriter(),
  412. )
  413. routes = append(routes, &Route{
  414. Endpoint: deleteDeploymentEndpoint,
  415. Handler: deleteDeploymentHandler,
  416. Router: r,
  417. })
  418. }
  419. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces -> cluster.NewClusterListNamespacesHandler
  420. listNamespacesEndpoint := factory.NewAPIEndpoint(
  421. &types.APIRequestMetadata{
  422. Verb: types.APIVerbGet,
  423. Method: types.HTTPVerbGet,
  424. Path: &types.Path{
  425. Parent: basePath,
  426. RelativePath: relPath + "/namespaces",
  427. },
  428. Scopes: []types.PermissionScope{
  429. types.UserScope,
  430. types.ProjectScope,
  431. types.ClusterScope,
  432. },
  433. },
  434. )
  435. listNamespacesHandler := cluster.NewListNamespacesHandler(
  436. config,
  437. factory.GetResultWriter(),
  438. )
  439. routes = append(routes, &Route{
  440. Endpoint: listNamespacesEndpoint,
  441. Handler: listNamespacesHandler,
  442. Router: r,
  443. })
  444. // GET /api/projects/{project_id}/clusters/{cluster_id}/nodes -> cluster.NewListNodesHandler
  445. listNodesEndpoint := factory.NewAPIEndpoint(
  446. &types.APIRequestMetadata{
  447. Verb: types.APIVerbGet,
  448. Method: types.HTTPVerbGet,
  449. Path: &types.Path{
  450. Parent: basePath,
  451. RelativePath: relPath + "/nodes",
  452. },
  453. Scopes: []types.PermissionScope{
  454. types.UserScope,
  455. types.ProjectScope,
  456. types.ClusterScope,
  457. },
  458. },
  459. )
  460. listNodesHandler := cluster.NewListNodesHandler(
  461. config,
  462. factory.GetResultWriter(),
  463. )
  464. routes = append(routes, &Route{
  465. Endpoint: listNodesEndpoint,
  466. Handler: listNodesHandler,
  467. Router: r,
  468. })
  469. // GET /api/projects/{project_id}/clusters/{cluster_id}/nodes/{node_name} -> cluster.NewGetNodeHandler
  470. getNodeEndpoint := factory.NewAPIEndpoint(
  471. &types.APIRequestMetadata{
  472. Verb: types.APIVerbGet,
  473. Method: types.HTTPVerbGet,
  474. Path: &types.Path{
  475. Parent: basePath,
  476. RelativePath: fmt.Sprintf("%s/nodes/{%s}", relPath, types.URLParamNodeName),
  477. },
  478. Scopes: []types.PermissionScope{
  479. types.UserScope,
  480. types.ProjectScope,
  481. types.ClusterScope,
  482. },
  483. },
  484. )
  485. getNodeHandler := cluster.NewGetNodeHandler(
  486. config,
  487. factory.GetResultWriter(),
  488. )
  489. routes = append(routes, &Route{
  490. Endpoint: getNodeEndpoint,
  491. Handler: getNodeHandler,
  492. Router: r,
  493. })
  494. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/create -> cluster.NewCreateNamespaceHandler
  495. createNamespaceEndpoint := factory.NewAPIEndpoint(
  496. &types.APIRequestMetadata{
  497. Verb: types.APIVerbCreate,
  498. Method: types.HTTPVerbPost,
  499. Path: &types.Path{
  500. Parent: basePath,
  501. RelativePath: relPath + "/namespaces/create",
  502. },
  503. Scopes: []types.PermissionScope{
  504. types.UserScope,
  505. types.ProjectScope,
  506. types.ClusterScope,
  507. },
  508. },
  509. )
  510. createNamespaceHandler := cluster.NewCreateNamespaceHandler(
  511. config,
  512. factory.GetDecoderValidator(),
  513. factory.GetResultWriter(),
  514. )
  515. routes = append(routes, &Route{
  516. Endpoint: createNamespaceEndpoint,
  517. Handler: createNamespaceHandler,
  518. Router: r,
  519. })
  520. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/delete -> cluster.NewDeleteNamespaceHandler
  521. deleteNamespaceEndpoint := factory.NewAPIEndpoint(
  522. &types.APIRequestMetadata{
  523. Verb: types.APIVerbDelete,
  524. Method: types.HTTPVerbDelete,
  525. Path: &types.Path{
  526. Parent: basePath,
  527. RelativePath: relPath + "/namespaces/delete",
  528. },
  529. Scopes: []types.PermissionScope{
  530. types.UserScope,
  531. types.ProjectScope,
  532. types.ClusterScope,
  533. },
  534. },
  535. )
  536. deleteNamespaceHandler := cluster.NewDeleteNamespaceHandler(
  537. config,
  538. factory.GetDecoderValidator(),
  539. )
  540. routes = append(routes, &Route{
  541. Endpoint: deleteNamespaceEndpoint,
  542. Handler: deleteNamespaceHandler,
  543. Router: r,
  544. })
  545. // GET /api/projects/{project_id}/clusters/{cluster_id}/kubeconfig -> cluster.NewGetTemporaryKubeconfigHandler
  546. getTemporaryKubeconfigEndpoint := factory.NewAPIEndpoint(
  547. &types.APIRequestMetadata{
  548. Verb: types.APIVerbGet,
  549. Method: types.HTTPVerbGet,
  550. Path: &types.Path{
  551. Parent: basePath,
  552. RelativePath: relPath + "/kubeconfig",
  553. },
  554. Scopes: []types.PermissionScope{
  555. types.UserScope,
  556. types.ProjectScope,
  557. types.ClusterScope,
  558. },
  559. },
  560. )
  561. getTemporaryKubeconfigHandler := cluster.NewGetTemporaryKubeconfigHandler(
  562. config,
  563. factory.GetResultWriter(),
  564. )
  565. routes = append(routes, &Route{
  566. Endpoint: getTemporaryKubeconfigEndpoint,
  567. Handler: getTemporaryKubeconfigHandler,
  568. Router: r,
  569. })
  570. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/detect -> cluster.NewDetectPrometheusInstalledHandler
  571. detectPrometheusInstalledEndpoint := factory.NewAPIEndpoint(
  572. &types.APIRequestMetadata{
  573. Verb: types.APIVerbGet,
  574. Method: types.HTTPVerbGet,
  575. Path: &types.Path{
  576. Parent: basePath,
  577. RelativePath: relPath + "/prometheus/detect",
  578. },
  579. Scopes: []types.PermissionScope{
  580. types.UserScope,
  581. types.ProjectScope,
  582. types.ClusterScope,
  583. },
  584. },
  585. )
  586. detectPrometheusInstalledHandler := cluster.NewDetectPrometheusInstalledHandler(config)
  587. routes = append(routes, &Route{
  588. Endpoint: detectPrometheusInstalledEndpoint,
  589. Handler: detectPrometheusInstalledHandler,
  590. Router: r,
  591. })
  592. // GET /api/projects/{project_id}/clusters/{cluster_id}/agent/detect -> cluster.NewDetectAgentInstalledHandler
  593. detectAgentInstalledEndpoint := factory.NewAPIEndpoint(
  594. &types.APIRequestMetadata{
  595. Verb: types.APIVerbGet,
  596. Method: types.HTTPVerbGet,
  597. Path: &types.Path{
  598. Parent: basePath,
  599. RelativePath: relPath + "/agent/detect",
  600. },
  601. Scopes: []types.PermissionScope{
  602. types.UserScope,
  603. types.ProjectScope,
  604. types.ClusterScope,
  605. },
  606. },
  607. )
  608. detectAgentInstalledHandler := cluster.NewDetectAgentInstalledHandler(config, factory.GetResultWriter())
  609. routes = append(routes, &Route{
  610. Endpoint: detectAgentInstalledEndpoint,
  611. Handler: detectAgentInstalledHandler,
  612. Router: r,
  613. })
  614. // POST /api/projects/{project_id}/clusters/{cluster_id}/agent/install -> cluster.NewInstallAgentHandler
  615. installAgentEndpoint := factory.NewAPIEndpoint(
  616. &types.APIRequestMetadata{
  617. Verb: types.APIVerbCreate,
  618. Method: types.HTTPVerbPost,
  619. Path: &types.Path{
  620. Parent: basePath,
  621. RelativePath: relPath + "/agent/install",
  622. },
  623. Scopes: []types.PermissionScope{
  624. types.UserScope,
  625. types.ProjectScope,
  626. types.ClusterScope,
  627. },
  628. },
  629. )
  630. installAgentHandler := cluster.NewInstallAgentHandler(
  631. config,
  632. factory.GetDecoderValidator(),
  633. factory.GetResultWriter(),
  634. )
  635. routes = append(routes, &Route{
  636. Endpoint: installAgentEndpoint,
  637. Handler: installAgentHandler,
  638. Router: r,
  639. })
  640. // POST /api/projects/{project_id}/clusters/{cluster_id}/agent/upgrade -> cluster.NewInstallAgentHandler
  641. upgradeAgentEndpoint := factory.NewAPIEndpoint(
  642. &types.APIRequestMetadata{
  643. Verb: types.APIVerbCreate,
  644. Method: types.HTTPVerbPost,
  645. Path: &types.Path{
  646. Parent: basePath,
  647. RelativePath: relPath + "/agent/upgrade",
  648. },
  649. Scopes: []types.PermissionScope{
  650. types.UserScope,
  651. types.ProjectScope,
  652. types.ClusterScope,
  653. },
  654. },
  655. )
  656. upgradeAgentHandler := cluster.NewUpgradeAgentHandler(
  657. config,
  658. factory.GetDecoderValidator(),
  659. factory.GetResultWriter(),
  660. )
  661. routes = append(routes, &Route{
  662. Endpoint: upgradeAgentEndpoint,
  663. Handler: upgradeAgentHandler,
  664. Router: r,
  665. })
  666. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events -> kube_events.NewGetKubeEventHandler
  667. listKubeEventsEndpoint := factory.NewAPIEndpoint(
  668. &types.APIRequestMetadata{
  669. Verb: types.APIVerbGet,
  670. Method: types.HTTPVerbGet,
  671. Path: &types.Path{
  672. Parent: basePath,
  673. RelativePath: relPath + "/kube_events",
  674. },
  675. Scopes: []types.PermissionScope{
  676. types.UserScope,
  677. types.ProjectScope,
  678. types.ClusterScope,
  679. },
  680. },
  681. )
  682. listKubeEventsHandler := kube_events.NewListKubeEventsHandler(
  683. config,
  684. factory.GetDecoderValidator(),
  685. factory.GetResultWriter(),
  686. )
  687. routes = append(routes, &Route{
  688. Endpoint: listKubeEventsEndpoint,
  689. Handler: listKubeEventsHandler,
  690. Router: r,
  691. })
  692. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events -> kube_events.NewGetKubeEventHandler
  693. getKubeEventEndpoint := factory.NewAPIEndpoint(
  694. &types.APIRequestMetadata{
  695. Verb: types.APIVerbGet,
  696. Method: types.HTTPVerbGet,
  697. Path: &types.Path{
  698. Parent: basePath,
  699. RelativePath: fmt.Sprintf("%s/kube_events/{%s}", relPath, types.URLParamKubeEventID),
  700. },
  701. Scopes: []types.PermissionScope{
  702. types.UserScope,
  703. types.ProjectScope,
  704. types.ClusterScope,
  705. },
  706. },
  707. )
  708. getKubeEventHandler := kube_events.NewGetKubeEventHandler(
  709. config,
  710. factory.GetDecoderValidator(),
  711. factory.GetResultWriter(),
  712. )
  713. routes = append(routes, &Route{
  714. Endpoint: getKubeEventEndpoint,
  715. Handler: getKubeEventHandler,
  716. Router: r,
  717. })
  718. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events/{kube_event_id}/logs -> kube_events.NewGetKubeEventLogsHandler
  719. getKubeEventLogsEndpoint := factory.NewAPIEndpoint(
  720. &types.APIRequestMetadata{
  721. Verb: types.APIVerbGet,
  722. Method: types.HTTPVerbGet,
  723. Path: &types.Path{
  724. Parent: basePath,
  725. RelativePath: fmt.Sprintf("%s/kube_events/{%s}/logs", relPath, types.URLParamKubeEventID),
  726. },
  727. Scopes: []types.PermissionScope{
  728. types.UserScope,
  729. types.ProjectScope,
  730. types.ClusterScope,
  731. },
  732. },
  733. )
  734. getKubeEventLogsHandler := kube_events.NewGetKubeEventLogsHandler(
  735. config,
  736. factory.GetDecoderValidator(),
  737. factory.GetResultWriter(),
  738. )
  739. routes = append(routes, &Route{
  740. Endpoint: getKubeEventLogsEndpoint,
  741. Handler: getKubeEventLogsHandler,
  742. Router: r,
  743. })
  744. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events/{kube_event_id}/log_buckets -> kube_events.NewGetKubeEventLogBucketsHandler
  745. getKubeEventLogBucketsEndpoint := factory.NewAPIEndpoint(
  746. &types.APIRequestMetadata{
  747. Verb: types.APIVerbGet,
  748. Method: types.HTTPVerbGet,
  749. Path: &types.Path{
  750. Parent: basePath,
  751. RelativePath: fmt.Sprintf("%s/kube_events/{%s}/log_buckets", relPath, types.URLParamKubeEventID),
  752. },
  753. Scopes: []types.PermissionScope{
  754. types.UserScope,
  755. types.ProjectScope,
  756. types.ClusterScope,
  757. },
  758. },
  759. )
  760. getKubeEventLogBucketsHandler := kube_events.NewGetKubeEventLogBucketsHandler(
  761. config,
  762. factory.GetDecoderValidator(),
  763. factory.GetResultWriter(),
  764. )
  765. routes = append(routes, &Route{
  766. Endpoint: getKubeEventLogBucketsEndpoint,
  767. Handler: getKubeEventLogBucketsHandler,
  768. Router: r,
  769. })
  770. // POST /api/projects/{project_id}/clusters/{cluster_id}/kube_events -> kube_events.NewCreateKubeEventHandler
  771. createKubeEventsEndpoint := factory.NewAPIEndpoint(
  772. &types.APIRequestMetadata{
  773. Verb: types.APIVerbCreate,
  774. Method: types.HTTPVerbPost,
  775. Path: &types.Path{
  776. Parent: basePath,
  777. RelativePath: relPath + "/kube_events",
  778. },
  779. Scopes: []types.PermissionScope{
  780. types.UserScope,
  781. types.ProjectScope,
  782. types.ClusterScope,
  783. },
  784. },
  785. )
  786. createKubeEventsHandler := kube_events.NewCreateKubeEventHandler(
  787. config,
  788. factory.GetDecoderValidator(),
  789. factory.GetResultWriter(),
  790. )
  791. routes = append(routes, &Route{
  792. Endpoint: createKubeEventsEndpoint,
  793. Handler: createKubeEventsHandler,
  794. Router: r,
  795. })
  796. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/ingresses -> cluster.NewListNGINXIngressesHandler
  797. listNGINXIngressesEndpoint := factory.NewAPIEndpoint(
  798. &types.APIRequestMetadata{
  799. Verb: types.APIVerbGet,
  800. Method: types.HTTPVerbGet,
  801. Path: &types.Path{
  802. Parent: basePath,
  803. RelativePath: relPath + "/prometheus/ingresses",
  804. },
  805. Scopes: []types.PermissionScope{
  806. types.UserScope,
  807. types.ProjectScope,
  808. types.ClusterScope,
  809. },
  810. },
  811. )
  812. listNGINXIngressesHandler := cluster.NewListNGINXIngressesHandler(
  813. config,
  814. factory.GetResultWriter(),
  815. )
  816. routes = append(routes, &Route{
  817. Endpoint: listNGINXIngressesEndpoint,
  818. Handler: listNGINXIngressesHandler,
  819. Router: r,
  820. })
  821. // GET /api/projects/{project_id}/clusters/{cluster_id}/metrics -> cluster.NewGetPodMetricsHandler
  822. getPodMetricsEndpoint := factory.NewAPIEndpoint(
  823. &types.APIRequestMetadata{
  824. Verb: types.APIVerbGet,
  825. Method: types.HTTPVerbGet,
  826. Path: &types.Path{
  827. Parent: basePath,
  828. RelativePath: relPath + "/metrics",
  829. },
  830. Scopes: []types.PermissionScope{
  831. types.UserScope,
  832. types.ProjectScope,
  833. types.ClusterScope,
  834. },
  835. },
  836. )
  837. getPodMetricsHandler := cluster.NewGetPodMetricsHandler(
  838. config,
  839. factory.GetDecoderValidator(),
  840. factory.GetResultWriter(),
  841. )
  842. routes = append(routes, &Route{
  843. Endpoint: getPodMetricsEndpoint,
  844. Handler: getPodMetricsHandler,
  845. Router: r,
  846. })
  847. // GET /api/projects/{project_id}/clusters/{cluster_id}/helm_release -> cluster.NewStreamHelmReleaseHandler
  848. streamHelmReleaseEndpoint := factory.NewAPIEndpoint(
  849. &types.APIRequestMetadata{
  850. Verb: types.APIVerbGet,
  851. Method: types.HTTPVerbGet,
  852. Path: &types.Path{
  853. Parent: basePath,
  854. RelativePath: relPath + "/helm_release",
  855. },
  856. Scopes: []types.PermissionScope{
  857. types.UserScope,
  858. types.ProjectScope,
  859. types.ClusterScope,
  860. },
  861. IsWebsocket: true,
  862. },
  863. )
  864. streamHelmReleaseHandler := cluster.NewStreamHelmReleaseHandler(
  865. config,
  866. factory.GetDecoderValidator(),
  867. factory.GetResultWriter(),
  868. )
  869. routes = append(routes, &Route{
  870. Endpoint: streamHelmReleaseEndpoint,
  871. Handler: streamHelmReleaseHandler,
  872. Router: r,
  873. })
  874. // GET /api/projects/{project_id}/clusters/{cluster_id}/{kind}/status -> cluster.NewStreamStatusHandler
  875. streamStatusEndpoint := factory.NewAPIEndpoint(
  876. &types.APIRequestMetadata{
  877. Verb: types.APIVerbGet,
  878. Method: types.HTTPVerbGet,
  879. Path: &types.Path{
  880. Parent: basePath,
  881. RelativePath: fmt.Sprintf(
  882. "%s/{%s}/status",
  883. relPath,
  884. types.URLParamKind,
  885. ),
  886. },
  887. Scopes: []types.PermissionScope{
  888. types.UserScope,
  889. types.ProjectScope,
  890. types.ClusterScope,
  891. },
  892. IsWebsocket: true,
  893. },
  894. )
  895. streamStatusHandler := cluster.NewStreamStatusHandler(
  896. config,
  897. factory.GetDecoderValidator(),
  898. factory.GetResultWriter(),
  899. )
  900. routes = append(routes, &Route{
  901. Endpoint: streamStatusEndpoint,
  902. Handler: streamStatusHandler,
  903. Router: r,
  904. })
  905. // GET /api/projects/{project_id}/clusters/{cluster_id}/pods -> cluster.NewGetPodsHandler
  906. getPodsEndpoint := factory.NewAPIEndpoint(
  907. &types.APIRequestMetadata{
  908. Verb: types.APIVerbGet,
  909. Method: types.HTTPVerbGet,
  910. Path: &types.Path{
  911. Parent: basePath,
  912. RelativePath: relPath + "/pods",
  913. },
  914. Scopes: []types.PermissionScope{
  915. types.UserScope,
  916. types.ProjectScope,
  917. types.ClusterScope,
  918. },
  919. },
  920. )
  921. getPodsHandler := cluster.NewGetPodsHandler(
  922. config,
  923. factory.GetDecoderValidator(),
  924. factory.GetResultWriter(),
  925. )
  926. routes = append(routes, &Route{
  927. Endpoint: getPodsEndpoint,
  928. Handler: getPodsHandler,
  929. Router: r,
  930. })
  931. // GET /api/projects/{project_id}/clusters/{cluster_id}/incidents -> cluster.NewGetIncidentsHandler
  932. getIncidentsEndpoint := factory.NewAPIEndpoint(
  933. &types.APIRequestMetadata{
  934. Verb: types.APIVerbGet,
  935. Method: types.HTTPVerbGet,
  936. Path: &types.Path{
  937. Parent: basePath,
  938. RelativePath: relPath + "/incidents",
  939. },
  940. Scopes: []types.PermissionScope{
  941. types.UserScope,
  942. types.ProjectScope,
  943. types.ClusterScope,
  944. },
  945. },
  946. )
  947. getIncidentsHandler := cluster.NewGetIncidentsHandler(
  948. config,
  949. factory.GetDecoderValidator(),
  950. factory.GetResultWriter(),
  951. )
  952. routes = append(routes, &Route{
  953. Endpoint: getIncidentsEndpoint,
  954. Handler: getIncidentsHandler,
  955. Router: r,
  956. })
  957. // GET /api/projects/{project_id}/clusters/{cluster_id}/incidents/logs -> cluster.NewGetIncidentsHandler
  958. getIncidentEventLogsEndpoint := factory.NewAPIEndpoint(
  959. &types.APIRequestMetadata{
  960. Verb: types.APIVerbGet,
  961. Method: types.HTTPVerbGet,
  962. Path: &types.Path{
  963. Parent: basePath,
  964. RelativePath: relPath + "/incidents/logs",
  965. },
  966. Scopes: []types.PermissionScope{
  967. types.UserScope,
  968. types.ProjectScope,
  969. types.ClusterScope,
  970. },
  971. },
  972. )
  973. getIncidentEventLogsHandler := cluster.NewGetIncidentEventLogsHandler(
  974. config,
  975. factory.GetDecoderValidator(),
  976. factory.GetResultWriter(),
  977. )
  978. routes = append(routes, &Route{
  979. Endpoint: getIncidentEventLogsEndpoint,
  980. Handler: getIncidentEventLogsHandler,
  981. Router: r,
  982. })
  983. // POST /api/projects/{project_id}/clusters/{cluster_id}/incidents/notify_new -> cluster.NewNotifyNewIncidentHandler
  984. notifyNewIncidentEndpoint := factory.NewAPIEndpoint(
  985. &types.APIRequestMetadata{
  986. Verb: types.APIVerbCreate,
  987. Method: types.HTTPVerbPost,
  988. Path: &types.Path{
  989. Parent: basePath,
  990. RelativePath: relPath + "/incidents/notify_new",
  991. },
  992. Scopes: []types.PermissionScope{
  993. types.UserScope,
  994. types.ProjectScope,
  995. types.ClusterScope,
  996. },
  997. },
  998. )
  999. notifyNewIncidentHandler := cluster.NewNotifyNewIncidentHandler(
  1000. config,
  1001. factory.GetDecoderValidator(),
  1002. factory.GetResultWriter(),
  1003. )
  1004. routes = append(routes, &Route{
  1005. Endpoint: notifyNewIncidentEndpoint,
  1006. Handler: notifyNewIncidentHandler,
  1007. Router: r,
  1008. })
  1009. // POST /api/projects/{project_id}/clusters/{cluster_id}/incidents/notify_resolved -> cluster.NewNotifyResolvedIncidentHandler
  1010. notifyResolvedIncidentEndpoint := factory.NewAPIEndpoint(
  1011. &types.APIRequestMetadata{
  1012. Verb: types.APIVerbCreate,
  1013. Method: types.HTTPVerbPost,
  1014. Path: &types.Path{
  1015. Parent: basePath,
  1016. RelativePath: relPath + "/incidents/notify_resolved",
  1017. },
  1018. Scopes: []types.PermissionScope{
  1019. types.UserScope,
  1020. types.ProjectScope,
  1021. types.ClusterScope,
  1022. },
  1023. },
  1024. )
  1025. notifyResolvedIncidentHandler := cluster.NewNotifyResolvedIncidentHandler(
  1026. config,
  1027. factory.GetDecoderValidator(),
  1028. factory.GetResultWriter(),
  1029. )
  1030. routes = append(routes, &Route{
  1031. Endpoint: notifyResolvedIncidentEndpoint,
  1032. Handler: notifyResolvedIncidentHandler,
  1033. Router: r,
  1034. })
  1035. return routes, newPath
  1036. }