cluster.go 29 KB

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