cluster.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. },
  137. )
  138. resolveCandidateHandler := cluster.NewResolveClusterCandidateHandler(
  139. config,
  140. factory.GetDecoderValidator(),
  141. factory.GetResultWriter(),
  142. )
  143. routes = append(routes, &Route{
  144. Endpoint: resolveCandidateEndpoint,
  145. Handler: resolveCandidateHandler,
  146. Router: r,
  147. })
  148. // POST /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterUpdateHandler
  149. updateClusterEndpoint := factory.NewAPIEndpoint(
  150. &types.APIRequestMetadata{
  151. Verb: types.APIVerbUpdate,
  152. Method: types.HTTPVerbPost,
  153. Path: &types.Path{
  154. Parent: basePath,
  155. RelativePath: relPath,
  156. },
  157. Scopes: []types.PermissionScope{
  158. types.UserScope,
  159. types.ProjectScope,
  160. types.ClusterScope,
  161. },
  162. },
  163. )
  164. updateClusterHandler := cluster.NewClusterUpdateHandler(
  165. config,
  166. factory.GetDecoderValidator(),
  167. factory.GetResultWriter(),
  168. )
  169. routes = append(routes, &Route{
  170. Endpoint: updateClusterEndpoint,
  171. Handler: updateClusterHandler,
  172. Router: r,
  173. })
  174. // DELETE /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterDeleteHandler
  175. deleteClusterEndpoint := factory.NewAPIEndpoint(
  176. &types.APIRequestMetadata{
  177. Verb: types.APIVerbDelete,
  178. Method: types.HTTPVerbDelete,
  179. Path: &types.Path{
  180. Parent: basePath,
  181. RelativePath: relPath,
  182. },
  183. Scopes: []types.PermissionScope{
  184. types.UserScope,
  185. types.ProjectScope,
  186. types.ClusterScope,
  187. },
  188. },
  189. )
  190. deleteClusterHandler := cluster.NewClusterDeleteHandler(
  191. config,
  192. factory.GetResultWriter(),
  193. )
  194. routes = append(routes, &Route{
  195. Endpoint: deleteClusterEndpoint,
  196. Handler: deleteClusterHandler,
  197. Router: r,
  198. })
  199. // GET /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterGetHandler
  200. getEndpoint := factory.NewAPIEndpoint(
  201. &types.APIRequestMetadata{
  202. Verb: types.APIVerbGet,
  203. Method: types.HTTPVerbGet,
  204. Path: &types.Path{
  205. Parent: basePath,
  206. RelativePath: relPath,
  207. },
  208. Scopes: []types.PermissionScope{
  209. types.UserScope,
  210. types.ProjectScope,
  211. types.ClusterScope,
  212. },
  213. },
  214. )
  215. getHandler := cluster.NewClusterGetHandler(
  216. config,
  217. factory.GetResultWriter(),
  218. )
  219. routes = append(routes, &Route{
  220. Endpoint: getEndpoint,
  221. Handler: getHandler,
  222. Router: r,
  223. })
  224. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces -> cluster.NewClusterListNamespacesHandler
  225. listNamespacesEndpoint := factory.NewAPIEndpoint(
  226. &types.APIRequestMetadata{
  227. Verb: types.APIVerbGet,
  228. Method: types.HTTPVerbGet,
  229. Path: &types.Path{
  230. Parent: basePath,
  231. RelativePath: relPath + "/namespaces",
  232. },
  233. Scopes: []types.PermissionScope{
  234. types.UserScope,
  235. types.ProjectScope,
  236. types.ClusterScope,
  237. },
  238. },
  239. )
  240. listNamespacesHandler := cluster.NewListNamespacesHandler(
  241. config,
  242. factory.GetResultWriter(),
  243. )
  244. routes = append(routes, &Route{
  245. Endpoint: listNamespacesEndpoint,
  246. Handler: listNamespacesHandler,
  247. Router: r,
  248. })
  249. // GET /api/projects/{project_id}/clusters/{cluster_id}/nodes -> cluster.NewListNodesHandler
  250. listNodesEndpoint := factory.NewAPIEndpoint(
  251. &types.APIRequestMetadata{
  252. Verb: types.APIVerbGet,
  253. Method: types.HTTPVerbGet,
  254. Path: &types.Path{
  255. Parent: basePath,
  256. RelativePath: relPath + "/nodes",
  257. },
  258. Scopes: []types.PermissionScope{
  259. types.UserScope,
  260. types.ProjectScope,
  261. types.ClusterScope,
  262. },
  263. },
  264. )
  265. listNodesHandler := cluster.NewListNodesHandler(
  266. config,
  267. factory.GetResultWriter(),
  268. )
  269. routes = append(routes, &Route{
  270. Endpoint: listNodesEndpoint,
  271. Handler: listNodesHandler,
  272. Router: r,
  273. })
  274. // GET /api/projects/{project_id}/clusters/{cluster_id}/nodes/{node_name} -> cluster.NewGetNodeHandler
  275. getNodeEndpoint := factory.NewAPIEndpoint(
  276. &types.APIRequestMetadata{
  277. Verb: types.APIVerbGet,
  278. Method: types.HTTPVerbGet,
  279. Path: &types.Path{
  280. Parent: basePath,
  281. RelativePath: fmt.Sprintf("%s/nodes/{%s}", relPath, types.URLParamNodeName),
  282. },
  283. Scopes: []types.PermissionScope{
  284. types.UserScope,
  285. types.ProjectScope,
  286. types.ClusterScope,
  287. },
  288. },
  289. )
  290. getNodeHandler := cluster.NewGetNodeHandler(
  291. config,
  292. factory.GetResultWriter(),
  293. )
  294. routes = append(routes, &Route{
  295. Endpoint: getNodeEndpoint,
  296. Handler: getNodeHandler,
  297. Router: r,
  298. })
  299. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/create -> cluster.NewCreateNamespaceHandler
  300. createNamespaceEndpoint := factory.NewAPIEndpoint(
  301. &types.APIRequestMetadata{
  302. Verb: types.APIVerbCreate,
  303. Method: types.HTTPVerbPost,
  304. Path: &types.Path{
  305. Parent: basePath,
  306. RelativePath: relPath + "/namespaces/create",
  307. },
  308. Scopes: []types.PermissionScope{
  309. types.UserScope,
  310. types.ProjectScope,
  311. types.ClusterScope,
  312. },
  313. },
  314. )
  315. createNamespaceHandler := cluster.NewCreateNamespaceHandler(
  316. config,
  317. factory.GetDecoderValidator(),
  318. factory.GetResultWriter(),
  319. )
  320. routes = append(routes, &Route{
  321. Endpoint: createNamespaceEndpoint,
  322. Handler: createNamespaceHandler,
  323. Router: r,
  324. })
  325. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/delete -> cluster.NewDeleteNamespaceHandler
  326. deleteNamespaceEndpoint := factory.NewAPIEndpoint(
  327. &types.APIRequestMetadata{
  328. Verb: types.APIVerbDelete,
  329. Method: types.HTTPVerbDelete,
  330. Path: &types.Path{
  331. Parent: basePath,
  332. RelativePath: relPath + "/namespaces/delete",
  333. },
  334. Scopes: []types.PermissionScope{
  335. types.UserScope,
  336. types.ProjectScope,
  337. types.ClusterScope,
  338. },
  339. },
  340. )
  341. deleteNamespaceHandler := cluster.NewDeleteNamespaceHandler(
  342. config,
  343. factory.GetDecoderValidator(),
  344. )
  345. routes = append(routes, &Route{
  346. Endpoint: deleteNamespaceEndpoint,
  347. Handler: deleteNamespaceHandler,
  348. Router: r,
  349. })
  350. // GET /api/projects/{project_id}/clusters/{cluster_id}/kubeconfig -> cluster.NewGetTemporaryKubeconfigHandler
  351. getTemporaryKubeconfigEndpoint := factory.NewAPIEndpoint(
  352. &types.APIRequestMetadata{
  353. Verb: types.APIVerbGet,
  354. Method: types.HTTPVerbGet,
  355. Path: &types.Path{
  356. Parent: basePath,
  357. RelativePath: relPath + "/kubeconfig",
  358. },
  359. Scopes: []types.PermissionScope{
  360. types.UserScope,
  361. types.ProjectScope,
  362. types.ClusterScope,
  363. },
  364. },
  365. )
  366. getTemporaryKubeconfigHandler := cluster.NewGetTemporaryKubeconfigHandler(
  367. config,
  368. factory.GetResultWriter(),
  369. )
  370. routes = append(routes, &Route{
  371. Endpoint: getTemporaryKubeconfigEndpoint,
  372. Handler: getTemporaryKubeconfigHandler,
  373. Router: r,
  374. })
  375. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/detect -> cluster.NewDetectPrometheusInstalledHandler
  376. detectPrometheusInstalledEndpoint := factory.NewAPIEndpoint(
  377. &types.APIRequestMetadata{
  378. Verb: types.APIVerbGet,
  379. Method: types.HTTPVerbGet,
  380. Path: &types.Path{
  381. Parent: basePath,
  382. RelativePath: relPath + "/prometheus/detect",
  383. },
  384. Scopes: []types.PermissionScope{
  385. types.UserScope,
  386. types.ProjectScope,
  387. types.ClusterScope,
  388. },
  389. },
  390. )
  391. detectPrometheusInstalledHandler := cluster.NewDetectPrometheusInstalledHandler(config)
  392. routes = append(routes, &Route{
  393. Endpoint: detectPrometheusInstalledEndpoint,
  394. Handler: detectPrometheusInstalledHandler,
  395. Router: r,
  396. })
  397. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/ingresses -> cluster.NewListNGINXIngressesHandler
  398. listNGINXIngressesEndpoint := factory.NewAPIEndpoint(
  399. &types.APIRequestMetadata{
  400. Verb: types.APIVerbGet,
  401. Method: types.HTTPVerbGet,
  402. Path: &types.Path{
  403. Parent: basePath,
  404. RelativePath: relPath + "/prometheus/ingresses",
  405. },
  406. Scopes: []types.PermissionScope{
  407. types.UserScope,
  408. types.ProjectScope,
  409. types.ClusterScope,
  410. },
  411. },
  412. )
  413. listNGINXIngressesHandler := cluster.NewListNGINXIngressesHandler(
  414. config,
  415. factory.GetResultWriter(),
  416. )
  417. routes = append(routes, &Route{
  418. Endpoint: listNGINXIngressesEndpoint,
  419. Handler: listNGINXIngressesHandler,
  420. Router: r,
  421. })
  422. // GET /api/projects/{project_id}/clusters/{cluster_id}/metrics -> cluster.NewGetPodMetricsHandler
  423. getPodMetricsEndpoint := factory.NewAPIEndpoint(
  424. &types.APIRequestMetadata{
  425. Verb: types.APIVerbGet,
  426. Method: types.HTTPVerbGet,
  427. Path: &types.Path{
  428. Parent: basePath,
  429. RelativePath: relPath + "/metrics",
  430. },
  431. Scopes: []types.PermissionScope{
  432. types.UserScope,
  433. types.ProjectScope,
  434. types.ClusterScope,
  435. },
  436. },
  437. )
  438. getPodMetricsHandler := cluster.NewGetPodMetricsHandler(
  439. config,
  440. factory.GetDecoderValidator(),
  441. factory.GetResultWriter(),
  442. )
  443. routes = append(routes, &Route{
  444. Endpoint: getPodMetricsEndpoint,
  445. Handler: getPodMetricsHandler,
  446. Router: r,
  447. })
  448. // GET /api/projects/{project_id}/clusters/{cluster_id}/helm_release -> cluster.NewStreamHelmReleaseHandler
  449. streamHelmReleaseEndpoint := factory.NewAPIEndpoint(
  450. &types.APIRequestMetadata{
  451. Verb: types.APIVerbGet,
  452. Method: types.HTTPVerbGet,
  453. Path: &types.Path{
  454. Parent: basePath,
  455. RelativePath: relPath + "/helm_release",
  456. },
  457. Scopes: []types.PermissionScope{
  458. types.UserScope,
  459. types.ProjectScope,
  460. types.ClusterScope,
  461. },
  462. },
  463. )
  464. streamHelmReleaseHandler := cluster.NewStreamHelmReleaseHandler(
  465. config,
  466. factory.GetDecoderValidator(),
  467. factory.GetResultWriter(),
  468. )
  469. routes = append(routes, &Route{
  470. Endpoint: streamHelmReleaseEndpoint,
  471. Handler: streamHelmReleaseHandler,
  472. Router: r,
  473. })
  474. // GET /api/projects/{project_id}/clusters/{cluster_id}/{kind}/status -> cluster.NewStreamStatusHandler
  475. streamStatusEndpoint := factory.NewAPIEndpoint(
  476. &types.APIRequestMetadata{
  477. Verb: types.APIVerbGet,
  478. Method: types.HTTPVerbGet,
  479. Path: &types.Path{
  480. Parent: basePath,
  481. RelativePath: fmt.Sprintf(
  482. "%s/{%s}/status",
  483. relPath,
  484. types.URLParamKind,
  485. ),
  486. },
  487. Scopes: []types.PermissionScope{
  488. types.UserScope,
  489. types.ProjectScope,
  490. types.ClusterScope,
  491. },
  492. },
  493. )
  494. streamStatusHandler := cluster.NewStreamStatusHandler(
  495. config,
  496. factory.GetDecoderValidator(),
  497. factory.GetResultWriter(),
  498. )
  499. routes = append(routes, &Route{
  500. Endpoint: streamStatusEndpoint,
  501. Handler: streamStatusHandler,
  502. Router: r,
  503. })
  504. // GET /api/projects/{project_id}/clusters/{cluster_id}/pods -> cluster.NewGetPodsHandler
  505. getPodsEndpoint := factory.NewAPIEndpoint(
  506. &types.APIRequestMetadata{
  507. Verb: types.APIVerbGet,
  508. Method: types.HTTPVerbGet,
  509. Path: &types.Path{
  510. Parent: basePath,
  511. RelativePath: relPath + "/pods",
  512. },
  513. Scopes: []types.PermissionScope{
  514. types.UserScope,
  515. types.ProjectScope,
  516. types.ClusterScope,
  517. },
  518. },
  519. )
  520. getPodsHandler := cluster.NewGetPodsHandler(
  521. config,
  522. factory.GetDecoderValidator(),
  523. factory.GetResultWriter(),
  524. )
  525. routes = append(routes, &Route{
  526. Endpoint: getPodsEndpoint,
  527. Handler: getPodsHandler,
  528. Router: r,
  529. })
  530. return routes, newPath
  531. }