cluster.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. // GET /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterGetHandler
  149. getEndpoint := factory.NewAPIEndpoint(
  150. &types.APIRequestMetadata{
  151. Verb: types.APIVerbGet,
  152. Method: types.HTTPVerbGet,
  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. getHandler := cluster.NewClusterGetHandler(
  165. config,
  166. factory.GetResultWriter(),
  167. )
  168. routes = append(routes, &Route{
  169. Endpoint: getEndpoint,
  170. Handler: getHandler,
  171. Router: r,
  172. })
  173. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces -> cluster.NewClusterListNamespacesHandler
  174. listNamespacesEndpoint := factory.NewAPIEndpoint(
  175. &types.APIRequestMetadata{
  176. Verb: types.APIVerbGet,
  177. Method: types.HTTPVerbGet,
  178. Path: &types.Path{
  179. Parent: basePath,
  180. RelativePath: relPath + "/namespaces",
  181. },
  182. Scopes: []types.PermissionScope{
  183. types.UserScope,
  184. types.ProjectScope,
  185. types.ClusterScope,
  186. },
  187. },
  188. )
  189. listNamespacesHandler := cluster.NewListNamespacesHandler(
  190. config,
  191. factory.GetResultWriter(),
  192. )
  193. routes = append(routes, &Route{
  194. Endpoint: listNamespacesEndpoint,
  195. Handler: listNamespacesHandler,
  196. Router: r,
  197. })
  198. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/create -> cluster.NewCreateNamespaceHandler
  199. createNamespaceEndpoint := factory.NewAPIEndpoint(
  200. &types.APIRequestMetadata{
  201. Verb: types.APIVerbCreate,
  202. Method: types.HTTPVerbPost,
  203. Path: &types.Path{
  204. Parent: basePath,
  205. RelativePath: relPath + "/namespaces/create",
  206. },
  207. Scopes: []types.PermissionScope{
  208. types.UserScope,
  209. types.ProjectScope,
  210. types.ClusterScope,
  211. },
  212. },
  213. )
  214. createNamespaceHandler := cluster.NewCreateNamespaceHandler(
  215. config,
  216. factory.GetDecoderValidator(),
  217. factory.GetResultWriter(),
  218. )
  219. routes = append(routes, &Route{
  220. Endpoint: createNamespaceEndpoint,
  221. Handler: createNamespaceHandler,
  222. Router: r,
  223. })
  224. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/delete -> cluster.NewDeleteNamespaceHandler
  225. deleteNamespaceEndpoint := factory.NewAPIEndpoint(
  226. &types.APIRequestMetadata{
  227. Verb: types.APIVerbDelete,
  228. Method: types.HTTPVerbDelete,
  229. Path: &types.Path{
  230. Parent: basePath,
  231. RelativePath: relPath + "/namespaces/delete",
  232. },
  233. Scopes: []types.PermissionScope{
  234. types.UserScope,
  235. types.ProjectScope,
  236. types.ClusterScope,
  237. },
  238. },
  239. )
  240. deleteNamespaceHandler := cluster.NewDeleteNamespaceHandler(
  241. config,
  242. factory.GetDecoderValidator(),
  243. )
  244. routes = append(routes, &Route{
  245. Endpoint: deleteNamespaceEndpoint,
  246. Handler: deleteNamespaceHandler,
  247. Router: r,
  248. })
  249. // GET /api/projects/{project_id}/clusters/{cluster_id}/kubeconfig -> cluster.NewGetTemporaryKubeconfigHandler
  250. getTemporaryKubeconfigEndpoint := factory.NewAPIEndpoint(
  251. &types.APIRequestMetadata{
  252. Verb: types.APIVerbGet,
  253. Method: types.HTTPVerbGet,
  254. Path: &types.Path{
  255. Parent: basePath,
  256. RelativePath: relPath + "/kubeconfig",
  257. },
  258. Scopes: []types.PermissionScope{
  259. types.UserScope,
  260. types.ProjectScope,
  261. types.ClusterScope,
  262. },
  263. },
  264. )
  265. getTemporaryKubeconfigHandler := cluster.NewGetTemporaryKubeconfigHandler(
  266. config,
  267. factory.GetResultWriter(),
  268. )
  269. routes = append(routes, &Route{
  270. Endpoint: getTemporaryKubeconfigEndpoint,
  271. Handler: getTemporaryKubeconfigHandler,
  272. Router: r,
  273. })
  274. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/detect -> cluster.NewDetectPrometheusInstalledHandler
  275. detectPrometheusInstalledEndpoint := factory.NewAPIEndpoint(
  276. &types.APIRequestMetadata{
  277. Verb: types.APIVerbGet,
  278. Method: types.HTTPVerbGet,
  279. Path: &types.Path{
  280. Parent: basePath,
  281. RelativePath: relPath + "/prometheus/detect",
  282. },
  283. Scopes: []types.PermissionScope{
  284. types.UserScope,
  285. types.ProjectScope,
  286. types.ClusterScope,
  287. },
  288. },
  289. )
  290. detectPrometheusInstalledHandler := cluster.NewDetectPrometheusInstalledHandler(config)
  291. routes = append(routes, &Route{
  292. Endpoint: detectPrometheusInstalledEndpoint,
  293. Handler: detectPrometheusInstalledHandler,
  294. Router: r,
  295. })
  296. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/ingresses -> cluster.NewListNGINXIngressesHandler
  297. listNGINXIngressesEndpoint := factory.NewAPIEndpoint(
  298. &types.APIRequestMetadata{
  299. Verb: types.APIVerbGet,
  300. Method: types.HTTPVerbGet,
  301. Path: &types.Path{
  302. Parent: basePath,
  303. RelativePath: relPath + "/prometheus/ingresses",
  304. },
  305. Scopes: []types.PermissionScope{
  306. types.UserScope,
  307. types.ProjectScope,
  308. types.ClusterScope,
  309. },
  310. },
  311. )
  312. listNGINXIngressesHandler := cluster.NewListNGINXIngressesHandler(
  313. config,
  314. factory.GetResultWriter(),
  315. )
  316. routes = append(routes, &Route{
  317. Endpoint: listNGINXIngressesEndpoint,
  318. Handler: listNGINXIngressesHandler,
  319. Router: r,
  320. })
  321. // GET /api/projects/{project_id}/clusters/{cluster_id}/metrics -> cluster.NewGetPodMetricsHandler
  322. getPodMetricsEndpoint := factory.NewAPIEndpoint(
  323. &types.APIRequestMetadata{
  324. Verb: types.APIVerbGet,
  325. Method: types.HTTPVerbGet,
  326. Path: &types.Path{
  327. Parent: basePath,
  328. RelativePath: relPath + "/metrics",
  329. },
  330. Scopes: []types.PermissionScope{
  331. types.UserScope,
  332. types.ProjectScope,
  333. types.ClusterScope,
  334. },
  335. },
  336. )
  337. getPodMetricsHandler := cluster.NewGetPodMetricsHandler(
  338. config,
  339. factory.GetDecoderValidator(),
  340. factory.GetResultWriter(),
  341. )
  342. routes = append(routes, &Route{
  343. Endpoint: getPodMetricsEndpoint,
  344. Handler: getPodMetricsHandler,
  345. Router: r,
  346. })
  347. // GET /api/projects/{project_id}/clusters/{cluster_id}/helm_release -> cluster.NewStreamHelmReleaseHandler
  348. streamHelmReleaseEndpoint := factory.NewAPIEndpoint(
  349. &types.APIRequestMetadata{
  350. Verb: types.APIVerbGet,
  351. Method: types.HTTPVerbGet,
  352. Path: &types.Path{
  353. Parent: basePath,
  354. RelativePath: relPath + "/helm_release",
  355. },
  356. Scopes: []types.PermissionScope{
  357. types.UserScope,
  358. types.ProjectScope,
  359. types.ClusterScope,
  360. },
  361. },
  362. )
  363. streamHelmReleaseHandler := cluster.NewStreamHelmReleaseHandler(
  364. config,
  365. factory.GetDecoderValidator(),
  366. factory.GetResultWriter(),
  367. )
  368. routes = append(routes, &Route{
  369. Endpoint: streamHelmReleaseEndpoint,
  370. Handler: streamHelmReleaseHandler,
  371. Router: r,
  372. })
  373. // GET /api/projects/{project_id}/clusters/{cluster_id}/{kind}/status -> cluster.NewStreamStatusHandler
  374. streamStatusEndpoint := factory.NewAPIEndpoint(
  375. &types.APIRequestMetadata{
  376. Verb: types.APIVerbGet,
  377. Method: types.HTTPVerbGet,
  378. Path: &types.Path{
  379. Parent: basePath,
  380. RelativePath: fmt.Sprintf(
  381. "%s/{%s}/status",
  382. relPath,
  383. types.URLParamKind,
  384. ),
  385. },
  386. Scopes: []types.PermissionScope{
  387. types.UserScope,
  388. types.ProjectScope,
  389. types.ClusterScope,
  390. },
  391. },
  392. )
  393. streamStatusHandler := cluster.NewStreamStatusHandler(
  394. config,
  395. factory.GetDecoderValidator(),
  396. factory.GetResultWriter(),
  397. )
  398. routes = append(routes, &Route{
  399. Endpoint: streamStatusEndpoint,
  400. Handler: streamStatusHandler,
  401. Router: r,
  402. })
  403. // GET /api/projects/{project_id}/clusters/{cluster_id}/pods -> cluster.NewGetPodsHandler
  404. getPodsEndpoint := factory.NewAPIEndpoint(
  405. &types.APIRequestMetadata{
  406. Verb: types.APIVerbGet,
  407. Method: types.HTTPVerbGet,
  408. Path: &types.Path{
  409. Parent: basePath,
  410. RelativePath: relPath + "/pods",
  411. },
  412. Scopes: []types.PermissionScope{
  413. types.UserScope,
  414. types.ProjectScope,
  415. types.ClusterScope,
  416. },
  417. },
  418. )
  419. getPodsHandler := cluster.NewGetPodsHandler(
  420. config,
  421. factory.GetDecoderValidator(),
  422. factory.GetResultWriter(),
  423. )
  424. routes = append(routes, &Route{
  425. Endpoint: getPodsEndpoint,
  426. Handler: getPodsHandler,
  427. Router: r,
  428. })
  429. return routes, newPath
  430. }