cluster.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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/shared"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/types"
  9. )
  10. func NewClusterScopedRegisterer(children ...*Registerer) *Registerer {
  11. return &Registerer{
  12. GetRoutes: GetClusterScopedRoutes,
  13. Children: children,
  14. }
  15. }
  16. func GetClusterScopedRoutes(
  17. r chi.Router,
  18. config *config.Config,
  19. basePath *types.Path,
  20. factory shared.APIEndpointFactory,
  21. children ...*Registerer,
  22. ) []*Route {
  23. routes, projPath := getClusterRoutes(r, config, basePath, factory)
  24. if len(children) > 0 {
  25. r.Route(projPath.RelativePath, func(r chi.Router) {
  26. for _, child := range children {
  27. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  28. routes = append(routes, childRoutes...)
  29. }
  30. })
  31. }
  32. return routes
  33. }
  34. func getClusterRoutes(
  35. r chi.Router,
  36. config *config.Config,
  37. basePath *types.Path,
  38. factory shared.APIEndpointFactory,
  39. ) ([]*Route, *types.Path) {
  40. relPath := "/clusters/{cluster_id}"
  41. newPath := &types.Path{
  42. Parent: basePath,
  43. RelativePath: relPath,
  44. }
  45. routes := make([]*Route, 0)
  46. // POST /api/projects/{project_id}/clusters -> project.NewCreateClusterManualHandler
  47. createEndpoint := factory.NewAPIEndpoint(
  48. &types.APIRequestMetadata{
  49. Verb: types.APIVerbCreate,
  50. Method: types.HTTPVerbPost,
  51. Path: &types.Path{
  52. Parent: basePath,
  53. RelativePath: "/clusters",
  54. },
  55. Scopes: []types.PermissionScope{
  56. types.UserScope,
  57. types.ProjectScope,
  58. },
  59. },
  60. )
  61. createHandler := cluster.NewCreateClusterManualHandler(
  62. config,
  63. factory.GetDecoderValidator(),
  64. factory.GetResultWriter(),
  65. )
  66. routes = append(routes, &Route{
  67. Endpoint: createEndpoint,
  68. Handler: createHandler,
  69. Router: r,
  70. })
  71. // POST /api/projects/{project_id}/clusters/candidates -> project.NewCreateClusterCandidateHandler
  72. createCandidateEndpoint := factory.NewAPIEndpoint(
  73. &types.APIRequestMetadata{
  74. Verb: types.APIVerbCreate,
  75. Method: types.HTTPVerbPost,
  76. Path: &types.Path{
  77. Parent: basePath,
  78. RelativePath: "/clusters/candidates",
  79. },
  80. Scopes: []types.PermissionScope{
  81. types.UserScope,
  82. types.ProjectScope,
  83. },
  84. },
  85. )
  86. createCandidateHandler := cluster.NewCreateClusterCandidateHandler(
  87. config,
  88. factory.GetDecoderValidator(),
  89. factory.GetResultWriter(),
  90. )
  91. routes = append(routes, &Route{
  92. Endpoint: createCandidateEndpoint,
  93. Handler: createCandidateHandler,
  94. Router: r,
  95. })
  96. // GET /api/projects/{project_id}/clusters/candidates -> project.NewListClusterCandidatesHandler
  97. listCandidatesEndpoint := factory.NewAPIEndpoint(
  98. &types.APIRequestMetadata{
  99. Verb: types.APIVerbList,
  100. Method: types.HTTPVerbGet,
  101. Path: &types.Path{
  102. Parent: basePath,
  103. RelativePath: "/clusters/candidates",
  104. },
  105. Scopes: []types.PermissionScope{
  106. types.UserScope,
  107. types.ProjectScope,
  108. },
  109. },
  110. )
  111. listCandidatesHandler := cluster.NewListClusterCandidatesHandler(
  112. config,
  113. factory.GetResultWriter(),
  114. )
  115. routes = append(routes, &Route{
  116. Endpoint: listCandidatesEndpoint,
  117. Handler: listCandidatesHandler,
  118. Router: r,
  119. })
  120. // POST /api/projects/{project_id}/clusters/candidates/{candidate_id}/resolve -> project.NewResolveClusterCandidateHandler
  121. resolveCandidateEndpoint := factory.NewAPIEndpoint(
  122. &types.APIRequestMetadata{
  123. Verb: types.APIVerbCreate,
  124. Method: types.HTTPVerbPost,
  125. Path: &types.Path{
  126. Parent: basePath,
  127. RelativePath: fmt.Sprintf(
  128. "/clusters/candidates/{%s}/resolve",
  129. types.URLParamCandidateID,
  130. ),
  131. },
  132. Scopes: []types.PermissionScope{
  133. types.UserScope,
  134. types.ProjectScope,
  135. },
  136. CheckUsage: true,
  137. UsageMetric: types.Clusters,
  138. },
  139. )
  140. resolveCandidateHandler := cluster.NewResolveClusterCandidateHandler(
  141. config,
  142. factory.GetDecoderValidator(),
  143. factory.GetResultWriter(),
  144. )
  145. routes = append(routes, &Route{
  146. Endpoint: resolveCandidateEndpoint,
  147. Handler: resolveCandidateHandler,
  148. Router: r,
  149. })
  150. // POST /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterUpdateHandler
  151. updateClusterEndpoint := factory.NewAPIEndpoint(
  152. &types.APIRequestMetadata{
  153. Verb: types.APIVerbUpdate,
  154. Method: types.HTTPVerbPost,
  155. Path: &types.Path{
  156. Parent: basePath,
  157. RelativePath: relPath,
  158. },
  159. Scopes: []types.PermissionScope{
  160. types.UserScope,
  161. types.ProjectScope,
  162. types.ClusterScope,
  163. },
  164. },
  165. )
  166. updateClusterHandler := cluster.NewClusterUpdateHandler(
  167. config,
  168. factory.GetDecoderValidator(),
  169. factory.GetResultWriter(),
  170. )
  171. routes = append(routes, &Route{
  172. Endpoint: updateClusterEndpoint,
  173. Handler: updateClusterHandler,
  174. Router: r,
  175. })
  176. // DELETE /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterDeleteHandler
  177. deleteClusterEndpoint := factory.NewAPIEndpoint(
  178. &types.APIRequestMetadata{
  179. Verb: types.APIVerbDelete,
  180. Method: types.HTTPVerbDelete,
  181. Path: &types.Path{
  182. Parent: basePath,
  183. RelativePath: relPath,
  184. },
  185. Scopes: []types.PermissionScope{
  186. types.UserScope,
  187. types.ProjectScope,
  188. types.ClusterScope,
  189. },
  190. },
  191. )
  192. deleteClusterHandler := cluster.NewClusterDeleteHandler(
  193. config,
  194. factory.GetResultWriter(),
  195. )
  196. routes = append(routes, &Route{
  197. Endpoint: deleteClusterEndpoint,
  198. Handler: deleteClusterHandler,
  199. Router: r,
  200. })
  201. // GET /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterGetHandler
  202. getEndpoint := factory.NewAPIEndpoint(
  203. &types.APIRequestMetadata{
  204. Verb: types.APIVerbGet,
  205. Method: types.HTTPVerbGet,
  206. Path: &types.Path{
  207. Parent: basePath,
  208. RelativePath: relPath,
  209. },
  210. Scopes: []types.PermissionScope{
  211. types.UserScope,
  212. types.ProjectScope,
  213. types.ClusterScope,
  214. },
  215. },
  216. )
  217. getHandler := cluster.NewClusterGetHandler(
  218. config,
  219. factory.GetResultWriter(),
  220. )
  221. routes = append(routes, &Route{
  222. Endpoint: getEndpoint,
  223. Handler: getHandler,
  224. Router: r,
  225. })
  226. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces -> cluster.NewClusterListNamespacesHandler
  227. listNamespacesEndpoint := factory.NewAPIEndpoint(
  228. &types.APIRequestMetadata{
  229. Verb: types.APIVerbGet,
  230. Method: types.HTTPVerbGet,
  231. Path: &types.Path{
  232. Parent: basePath,
  233. RelativePath: relPath + "/namespaces",
  234. },
  235. Scopes: []types.PermissionScope{
  236. types.UserScope,
  237. types.ProjectScope,
  238. types.ClusterScope,
  239. },
  240. },
  241. )
  242. listNamespacesHandler := cluster.NewListNamespacesHandler(
  243. config,
  244. factory.GetResultWriter(),
  245. )
  246. routes = append(routes, &Route{
  247. Endpoint: listNamespacesEndpoint,
  248. Handler: listNamespacesHandler,
  249. Router: r,
  250. })
  251. // GET /api/projects/{project_id}/clusters/{cluster_id}/nodes -> cluster.NewListNodesHandler
  252. listNodesEndpoint := factory.NewAPIEndpoint(
  253. &types.APIRequestMetadata{
  254. Verb: types.APIVerbGet,
  255. Method: types.HTTPVerbGet,
  256. Path: &types.Path{
  257. Parent: basePath,
  258. RelativePath: relPath + "/nodes",
  259. },
  260. Scopes: []types.PermissionScope{
  261. types.UserScope,
  262. types.ProjectScope,
  263. types.ClusterScope,
  264. },
  265. },
  266. )
  267. listNodesHandler := cluster.NewListNodesHandler(
  268. config,
  269. factory.GetResultWriter(),
  270. )
  271. routes = append(routes, &Route{
  272. Endpoint: listNodesEndpoint,
  273. Handler: listNodesHandler,
  274. Router: r,
  275. })
  276. // GET /api/projects/{project_id}/clusters/{cluster_id}/nodes/{node_name} -> cluster.NewGetNodeHandler
  277. getNodeEndpoint := factory.NewAPIEndpoint(
  278. &types.APIRequestMetadata{
  279. Verb: types.APIVerbGet,
  280. Method: types.HTTPVerbGet,
  281. Path: &types.Path{
  282. Parent: basePath,
  283. RelativePath: fmt.Sprintf("%s/nodes/{%s}", relPath, types.URLParamNodeName),
  284. },
  285. Scopes: []types.PermissionScope{
  286. types.UserScope,
  287. types.ProjectScope,
  288. types.ClusterScope,
  289. },
  290. },
  291. )
  292. getNodeHandler := cluster.NewGetNodeHandler(
  293. config,
  294. factory.GetResultWriter(),
  295. )
  296. routes = append(routes, &Route{
  297. Endpoint: getNodeEndpoint,
  298. Handler: getNodeHandler,
  299. Router: r,
  300. })
  301. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/create -> cluster.NewCreateNamespaceHandler
  302. createNamespaceEndpoint := factory.NewAPIEndpoint(
  303. &types.APIRequestMetadata{
  304. Verb: types.APIVerbCreate,
  305. Method: types.HTTPVerbPost,
  306. Path: &types.Path{
  307. Parent: basePath,
  308. RelativePath: relPath + "/namespaces/create",
  309. },
  310. Scopes: []types.PermissionScope{
  311. types.UserScope,
  312. types.ProjectScope,
  313. types.ClusterScope,
  314. },
  315. },
  316. )
  317. createNamespaceHandler := cluster.NewCreateNamespaceHandler(
  318. config,
  319. factory.GetDecoderValidator(),
  320. factory.GetResultWriter(),
  321. )
  322. routes = append(routes, &Route{
  323. Endpoint: createNamespaceEndpoint,
  324. Handler: createNamespaceHandler,
  325. Router: r,
  326. })
  327. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/delete -> cluster.NewDeleteNamespaceHandler
  328. deleteNamespaceEndpoint := factory.NewAPIEndpoint(
  329. &types.APIRequestMetadata{
  330. Verb: types.APIVerbDelete,
  331. Method: types.HTTPVerbDelete,
  332. Path: &types.Path{
  333. Parent: basePath,
  334. RelativePath: relPath + "/namespaces/delete",
  335. },
  336. Scopes: []types.PermissionScope{
  337. types.UserScope,
  338. types.ProjectScope,
  339. types.ClusterScope,
  340. },
  341. },
  342. )
  343. deleteNamespaceHandler := cluster.NewDeleteNamespaceHandler(
  344. config,
  345. factory.GetDecoderValidator(),
  346. )
  347. routes = append(routes, &Route{
  348. Endpoint: deleteNamespaceEndpoint,
  349. Handler: deleteNamespaceHandler,
  350. Router: r,
  351. })
  352. // GET /api/projects/{project_id}/clusters/{cluster_id}/kubeconfig -> cluster.NewGetTemporaryKubeconfigHandler
  353. getTemporaryKubeconfigEndpoint := factory.NewAPIEndpoint(
  354. &types.APIRequestMetadata{
  355. Verb: types.APIVerbGet,
  356. Method: types.HTTPVerbGet,
  357. Path: &types.Path{
  358. Parent: basePath,
  359. RelativePath: relPath + "/kubeconfig",
  360. },
  361. Scopes: []types.PermissionScope{
  362. types.UserScope,
  363. types.ProjectScope,
  364. types.ClusterScope,
  365. },
  366. },
  367. )
  368. getTemporaryKubeconfigHandler := cluster.NewGetTemporaryKubeconfigHandler(
  369. config,
  370. factory.GetResultWriter(),
  371. )
  372. routes = append(routes, &Route{
  373. Endpoint: getTemporaryKubeconfigEndpoint,
  374. Handler: getTemporaryKubeconfigHandler,
  375. Router: r,
  376. })
  377. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/detect -> cluster.NewDetectPrometheusInstalledHandler
  378. detectPrometheusInstalledEndpoint := factory.NewAPIEndpoint(
  379. &types.APIRequestMetadata{
  380. Verb: types.APIVerbGet,
  381. Method: types.HTTPVerbGet,
  382. Path: &types.Path{
  383. Parent: basePath,
  384. RelativePath: relPath + "/prometheus/detect",
  385. },
  386. Scopes: []types.PermissionScope{
  387. types.UserScope,
  388. types.ProjectScope,
  389. types.ClusterScope,
  390. },
  391. },
  392. )
  393. detectPrometheusInstalledHandler := cluster.NewDetectPrometheusInstalledHandler(config)
  394. routes = append(routes, &Route{
  395. Endpoint: detectPrometheusInstalledEndpoint,
  396. Handler: detectPrometheusInstalledHandler,
  397. Router: r,
  398. })
  399. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/ingresses -> cluster.NewListNGINXIngressesHandler
  400. listNGINXIngressesEndpoint := factory.NewAPIEndpoint(
  401. &types.APIRequestMetadata{
  402. Verb: types.APIVerbGet,
  403. Method: types.HTTPVerbGet,
  404. Path: &types.Path{
  405. Parent: basePath,
  406. RelativePath: relPath + "/prometheus/ingresses",
  407. },
  408. Scopes: []types.PermissionScope{
  409. types.UserScope,
  410. types.ProjectScope,
  411. types.ClusterScope,
  412. },
  413. },
  414. )
  415. listNGINXIngressesHandler := cluster.NewListNGINXIngressesHandler(
  416. config,
  417. factory.GetResultWriter(),
  418. )
  419. routes = append(routes, &Route{
  420. Endpoint: listNGINXIngressesEndpoint,
  421. Handler: listNGINXIngressesHandler,
  422. Router: r,
  423. })
  424. // GET /api/projects/{project_id}/clusters/{cluster_id}/metrics -> cluster.NewGetPodMetricsHandler
  425. getPodMetricsEndpoint := factory.NewAPIEndpoint(
  426. &types.APIRequestMetadata{
  427. Verb: types.APIVerbGet,
  428. Method: types.HTTPVerbGet,
  429. Path: &types.Path{
  430. Parent: basePath,
  431. RelativePath: relPath + "/metrics",
  432. },
  433. Scopes: []types.PermissionScope{
  434. types.UserScope,
  435. types.ProjectScope,
  436. types.ClusterScope,
  437. },
  438. },
  439. )
  440. getPodMetricsHandler := cluster.NewGetPodMetricsHandler(
  441. config,
  442. factory.GetDecoderValidator(),
  443. factory.GetResultWriter(),
  444. )
  445. routes = append(routes, &Route{
  446. Endpoint: getPodMetricsEndpoint,
  447. Handler: getPodMetricsHandler,
  448. Router: r,
  449. })
  450. // GET /api/projects/{project_id}/clusters/{cluster_id}/helm_release -> cluster.NewStreamHelmReleaseHandler
  451. streamHelmReleaseEndpoint := factory.NewAPIEndpoint(
  452. &types.APIRequestMetadata{
  453. Verb: types.APIVerbGet,
  454. Method: types.HTTPVerbGet,
  455. Path: &types.Path{
  456. Parent: basePath,
  457. RelativePath: relPath + "/helm_release",
  458. },
  459. Scopes: []types.PermissionScope{
  460. types.UserScope,
  461. types.ProjectScope,
  462. types.ClusterScope,
  463. },
  464. IsWebsocket: true,
  465. },
  466. )
  467. streamHelmReleaseHandler := cluster.NewStreamHelmReleaseHandler(
  468. config,
  469. factory.GetDecoderValidator(),
  470. factory.GetResultWriter(),
  471. )
  472. routes = append(routes, &Route{
  473. Endpoint: streamHelmReleaseEndpoint,
  474. Handler: streamHelmReleaseHandler,
  475. Router: r,
  476. })
  477. // GET /api/projects/{project_id}/clusters/{cluster_id}/{kind}/status -> cluster.NewStreamStatusHandler
  478. streamStatusEndpoint := factory.NewAPIEndpoint(
  479. &types.APIRequestMetadata{
  480. Verb: types.APIVerbGet,
  481. Method: types.HTTPVerbGet,
  482. Path: &types.Path{
  483. Parent: basePath,
  484. RelativePath: fmt.Sprintf(
  485. "%s/{%s}/status",
  486. relPath,
  487. types.URLParamKind,
  488. ),
  489. },
  490. Scopes: []types.PermissionScope{
  491. types.UserScope,
  492. types.ProjectScope,
  493. types.ClusterScope,
  494. },
  495. IsWebsocket: true,
  496. },
  497. )
  498. streamStatusHandler := cluster.NewStreamStatusHandler(
  499. config,
  500. factory.GetDecoderValidator(),
  501. factory.GetResultWriter(),
  502. )
  503. routes = append(routes, &Route{
  504. Endpoint: streamStatusEndpoint,
  505. Handler: streamStatusHandler,
  506. Router: r,
  507. })
  508. // GET /api/projects/{project_id}/clusters/{cluster_id}/pods -> cluster.NewGetPodsHandler
  509. getPodsEndpoint := factory.NewAPIEndpoint(
  510. &types.APIRequestMetadata{
  511. Verb: types.APIVerbGet,
  512. Method: types.HTTPVerbGet,
  513. Path: &types.Path{
  514. Parent: basePath,
  515. RelativePath: relPath + "/pods",
  516. },
  517. Scopes: []types.PermissionScope{
  518. types.UserScope,
  519. types.ProjectScope,
  520. types.ClusterScope,
  521. },
  522. },
  523. )
  524. getPodsHandler := cluster.NewGetPodsHandler(
  525. config,
  526. factory.GetDecoderValidator(),
  527. factory.GetResultWriter(),
  528. )
  529. routes = append(routes, &Route{
  530. Endpoint: getPodsEndpoint,
  531. Handler: getPodsHandler,
  532. Router: r,
  533. })
  534. return routes, newPath
  535. }