cluster.go 30 KB

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