cluster.go 29 KB

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