2
0

cluster.go 14 KB

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