namespace.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi"
  5. "github.com/porter-dev/porter/api/server/handlers/job"
  6. "github.com/porter-dev/porter/api/server/handlers/namespace"
  7. "github.com/porter-dev/porter/api/server/shared"
  8. "github.com/porter-dev/porter/api/server/shared/config"
  9. "github.com/porter-dev/porter/api/types"
  10. )
  11. func NewNamespaceScopedRegisterer(children ...*Registerer) *Registerer {
  12. return &Registerer{
  13. GetRoutes: GetNamespaceScopedRoutes,
  14. Children: children,
  15. }
  16. }
  17. func GetNamespaceScopedRoutes(
  18. r chi.Router,
  19. config *config.Config,
  20. basePath *types.Path,
  21. factory shared.APIEndpointFactory,
  22. children ...*Registerer,
  23. ) []*Route {
  24. routes, projPath := getNamespaceRoutes(r, config, basePath, factory)
  25. if len(children) > 0 {
  26. r.Route(projPath.RelativePath, func(r chi.Router) {
  27. for _, child := range children {
  28. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  29. routes = append(routes, childRoutes...)
  30. }
  31. })
  32. }
  33. return routes
  34. }
  35. func getNamespaceRoutes(
  36. r chi.Router,
  37. config *config.Config,
  38. basePath *types.Path,
  39. factory shared.APIEndpointFactory,
  40. ) ([]*Route, *types.Path) {
  41. relPath := "/namespaces/{namespace}"
  42. newPath := &types.Path{
  43. Parent: basePath,
  44. RelativePath: relPath,
  45. }
  46. routes := make([]*Route, 0)
  47. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/configmap/list -> namespace.NewListConfigMapsHandler
  48. listConfigMapsEndpoint := factory.NewAPIEndpoint(
  49. &types.APIRequestMetadata{
  50. Verb: types.APIVerbGet,
  51. Method: types.HTTPVerbGet,
  52. Path: &types.Path{
  53. Parent: basePath,
  54. RelativePath: relPath + "/configmap/list",
  55. },
  56. Scopes: []types.PermissionScope{
  57. types.UserScope,
  58. types.ProjectScope,
  59. types.ClusterScope,
  60. types.NamespaceScope,
  61. },
  62. },
  63. )
  64. listConfigMapsHandler := namespace.NewListConfigMapsHandler(
  65. config,
  66. factory.GetResultWriter(),
  67. )
  68. routes = append(routes, &Route{
  69. Endpoint: listConfigMapsEndpoint,
  70. Handler: listConfigMapsHandler,
  71. Router: r,
  72. })
  73. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/configmap -> namespace.NewGetConfigMapHandler
  74. getConfigMapEndpoint := factory.NewAPIEndpoint(
  75. &types.APIRequestMetadata{
  76. Verb: types.APIVerbGet,
  77. Method: types.HTTPVerbGet,
  78. Path: &types.Path{
  79. Parent: basePath,
  80. RelativePath: relPath + "/configmap",
  81. },
  82. Scopes: []types.PermissionScope{
  83. types.UserScope,
  84. types.ProjectScope,
  85. types.ClusterScope,
  86. types.NamespaceScope,
  87. },
  88. },
  89. )
  90. getConfigMapHandler := namespace.NewGetConfigMapHandler(
  91. config,
  92. factory.GetDecoderValidator(),
  93. factory.GetResultWriter(),
  94. )
  95. routes = append(routes, &Route{
  96. Endpoint: getConfigMapEndpoint,
  97. Handler: getConfigMapHandler,
  98. Router: r,
  99. })
  100. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/configmap/create -> namespace.NewCreateConfigMapHandler
  101. createConfigMapEndpoint := factory.NewAPIEndpoint(
  102. &types.APIRequestMetadata{
  103. Verb: types.APIVerbCreate,
  104. Method: types.HTTPVerbPost,
  105. Path: &types.Path{
  106. Parent: basePath,
  107. RelativePath: relPath + "/configmap/create",
  108. },
  109. Scopes: []types.PermissionScope{
  110. types.UserScope,
  111. types.ProjectScope,
  112. types.ClusterScope,
  113. types.NamespaceScope,
  114. },
  115. },
  116. )
  117. createConfigMapHandler := namespace.NewCreateConfigMapHandler(
  118. config,
  119. factory.GetDecoderValidator(),
  120. factory.GetResultWriter(),
  121. )
  122. routes = append(routes, &Route{
  123. Endpoint: createConfigMapEndpoint,
  124. Handler: createConfigMapHandler,
  125. Router: r,
  126. })
  127. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/configmap/update -> namespace.NewUpdateConfigMapHandler
  128. updateConfigMapEndpoint := factory.NewAPIEndpoint(
  129. &types.APIRequestMetadata{
  130. Verb: types.APIVerbUpdate,
  131. Method: types.HTTPVerbPost,
  132. Path: &types.Path{
  133. Parent: basePath,
  134. RelativePath: relPath + "/configmap/update",
  135. },
  136. Scopes: []types.PermissionScope{
  137. types.UserScope,
  138. types.ProjectScope,
  139. types.ClusterScope,
  140. types.NamespaceScope,
  141. },
  142. },
  143. )
  144. updateConfigMapHandler := namespace.NewUpdateConfigMapHandler(
  145. config,
  146. factory.GetDecoderValidator(),
  147. factory.GetResultWriter(),
  148. )
  149. routes = append(routes, &Route{
  150. Endpoint: updateConfigMapEndpoint,
  151. Handler: updateConfigMapHandler,
  152. Router: r,
  153. })
  154. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/configmap/rename -> namespace.NewRenameConfigMapHandler
  155. renameConfigMapEndpoint := factory.NewAPIEndpoint(
  156. &types.APIRequestMetadata{
  157. Verb: types.APIVerbUpdate,
  158. Method: types.HTTPVerbPost,
  159. Path: &types.Path{
  160. Parent: basePath,
  161. RelativePath: relPath + "/configmap/rename",
  162. },
  163. Scopes: []types.PermissionScope{
  164. types.UserScope,
  165. types.ProjectScope,
  166. types.ClusterScope,
  167. types.NamespaceScope,
  168. },
  169. },
  170. )
  171. renameConfigMapHandler := namespace.NewRenameConfigMapHandler(
  172. config,
  173. factory.GetDecoderValidator(),
  174. factory.GetResultWriter(),
  175. )
  176. routes = append(routes, &Route{
  177. Endpoint: renameConfigMapEndpoint,
  178. Handler: renameConfigMapHandler,
  179. Router: r,
  180. })
  181. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/configmap/delete -> namespace.NewDeleteConfigMapHandler
  182. deleteConfigMapEndpoint := factory.NewAPIEndpoint(
  183. &types.APIRequestMetadata{
  184. Verb: types.APIVerbDelete,
  185. Method: types.HTTPVerbDelete,
  186. Path: &types.Path{
  187. Parent: basePath,
  188. RelativePath: relPath + "/configmap/delete",
  189. },
  190. Scopes: []types.PermissionScope{
  191. types.UserScope,
  192. types.ProjectScope,
  193. types.ClusterScope,
  194. types.NamespaceScope,
  195. },
  196. },
  197. )
  198. deleteConfigMapHandler := namespace.NewDeleteConfigMapHandler(
  199. config,
  200. factory.GetDecoderValidator(),
  201. )
  202. routes = append(routes, &Route{
  203. Endpoint: deleteConfigMapEndpoint,
  204. Handler: deleteConfigMapHandler,
  205. Router: r,
  206. })
  207. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases -> namespace.NewListReleasesHandler
  208. listReleasesEndpoint := factory.NewAPIEndpoint(
  209. &types.APIRequestMetadata{
  210. Verb: types.APIVerbGet,
  211. Method: types.HTTPVerbGet,
  212. Path: &types.Path{
  213. Parent: basePath,
  214. RelativePath: relPath + "/releases",
  215. },
  216. Scopes: []types.PermissionScope{
  217. types.UserScope,
  218. types.ProjectScope,
  219. types.ClusterScope,
  220. types.NamespaceScope,
  221. },
  222. },
  223. )
  224. listReleasesHandler := namespace.NewListReleasesHandler(
  225. config,
  226. factory.GetDecoderValidator(),
  227. factory.GetResultWriter(),
  228. )
  229. routes = append(routes, &Route{
  230. Endpoint: listReleasesEndpoint,
  231. Handler: listReleasesHandler,
  232. Router: r,
  233. })
  234. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/pod/{name}/logs -> namespace.NewStreamPodLogsHandler
  235. streamPodLogsEndpoint := factory.NewAPIEndpoint(
  236. &types.APIRequestMetadata{
  237. Verb: types.APIVerbGet,
  238. Method: types.HTTPVerbGet,
  239. Path: &types.Path{
  240. Parent: basePath,
  241. RelativePath: fmt.Sprintf(
  242. "%s/pod/{%s}/logs",
  243. relPath,
  244. types.URLParamPodName,
  245. ),
  246. },
  247. Scopes: []types.PermissionScope{
  248. types.UserScope,
  249. types.ProjectScope,
  250. types.ClusterScope,
  251. types.NamespaceScope,
  252. },
  253. },
  254. )
  255. streamPodLogsHandler := namespace.NewStreamPodLogsHandler(
  256. config,
  257. factory.GetDecoderValidator(),
  258. factory.GetResultWriter(),
  259. )
  260. routes = append(routes, &Route{
  261. Endpoint: streamPodLogsEndpoint,
  262. Handler: streamPodLogsHandler,
  263. Router: r,
  264. })
  265. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/jobs/{name}/pods -> jobs.NewGetPodsHandler
  266. getJobPodsEndpoint := factory.NewAPIEndpoint(
  267. &types.APIRequestMetadata{
  268. Verb: types.APIVerbList,
  269. Method: types.HTTPVerbGet,
  270. Path: &types.Path{
  271. Parent: basePath,
  272. RelativePath: fmt.Sprintf(
  273. "%s/jobs/{%s}/pods",
  274. relPath,
  275. types.URLParamJobName,
  276. ),
  277. },
  278. Scopes: []types.PermissionScope{
  279. types.UserScope,
  280. types.ProjectScope,
  281. types.ClusterScope,
  282. types.NamespaceScope,
  283. },
  284. },
  285. )
  286. getJobPodsHandler := job.NewGetPodsHandler(
  287. config,
  288. factory.GetResultWriter(),
  289. )
  290. routes = append(routes, &Route{
  291. Endpoint: getJobPodsEndpoint,
  292. Handler: getJobPodsHandler,
  293. Router: r,
  294. })
  295. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/jobs/{name} -> jobs.NewDeleteHandler
  296. deleteJobEndpoint := factory.NewAPIEndpoint(
  297. &types.APIRequestMetadata{
  298. Verb: types.APIVerbDelete,
  299. Method: types.HTTPVerbDelete,
  300. Path: &types.Path{
  301. Parent: basePath,
  302. RelativePath: fmt.Sprintf(
  303. "%s/jobs/{%s}",
  304. relPath,
  305. types.URLParamJobName,
  306. ),
  307. },
  308. Scopes: []types.PermissionScope{
  309. types.UserScope,
  310. types.ProjectScope,
  311. types.ClusterScope,
  312. types.NamespaceScope,
  313. },
  314. },
  315. )
  316. deleteJobHandler := job.NewDeleteHandler(
  317. config,
  318. )
  319. routes = append(routes, &Route{
  320. Endpoint: deleteJobEndpoint,
  321. Handler: deleteJobHandler,
  322. Router: r,
  323. })
  324. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/jobs/{name}/stop -> jobs.NewStopHandler
  325. stopJobEndpoint := factory.NewAPIEndpoint(
  326. &types.APIRequestMetadata{
  327. Verb: types.APIVerbUpdate,
  328. Method: types.HTTPVerbPost,
  329. Path: &types.Path{
  330. Parent: basePath,
  331. RelativePath: fmt.Sprintf(
  332. "%s/jobs/{%s}/stop",
  333. relPath,
  334. types.URLParamJobName,
  335. ),
  336. },
  337. Scopes: []types.PermissionScope{
  338. types.UserScope,
  339. types.ProjectScope,
  340. types.ClusterScope,
  341. types.NamespaceScope,
  342. },
  343. },
  344. )
  345. stopJobHandler := job.NewStopHandler(
  346. config,
  347. )
  348. routes = append(routes, &Route{
  349. Endpoint: stopJobEndpoint,
  350. Handler: stopJobHandler,
  351. Router: r,
  352. })
  353. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/pods/{name} -> namespace.NewDeletePodHandler
  354. deletePodEndpoint := factory.NewAPIEndpoint(
  355. &types.APIRequestMetadata{
  356. Verb: types.APIVerbDelete,
  357. Method: types.HTTPVerbDelete,
  358. Path: &types.Path{
  359. Parent: basePath,
  360. RelativePath: fmt.Sprintf(
  361. "%s/pods/{%s}",
  362. relPath,
  363. types.URLParamPodName,
  364. ),
  365. },
  366. Scopes: []types.PermissionScope{
  367. types.UserScope,
  368. types.ProjectScope,
  369. types.ClusterScope,
  370. types.NamespaceScope,
  371. },
  372. },
  373. )
  374. deletePodHandler := namespace.NewDeletePodHandler(
  375. config,
  376. )
  377. routes = append(routes, &Route{
  378. Endpoint: deletePodEndpoint,
  379. Handler: deletePodHandler,
  380. Router: r,
  381. })
  382. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/pods/{name}/events -> namespace.NewGetPodEventsHandler
  383. getPodEventsEndpoint := factory.NewAPIEndpoint(
  384. &types.APIRequestMetadata{
  385. Verb: types.APIVerbList,
  386. Method: types.HTTPVerbGet,
  387. Path: &types.Path{
  388. Parent: basePath,
  389. RelativePath: fmt.Sprintf(
  390. "%s/pods/{%s}/events",
  391. relPath,
  392. types.URLParamPodName,
  393. ),
  394. },
  395. Scopes: []types.PermissionScope{
  396. types.UserScope,
  397. types.ProjectScope,
  398. types.ClusterScope,
  399. types.NamespaceScope,
  400. },
  401. },
  402. )
  403. getPodEventsHandler := namespace.NewGetPodEventsHandler(
  404. config,
  405. factory.GetResultWriter(),
  406. )
  407. routes = append(routes, &Route{
  408. Endpoint: getPodEventsEndpoint,
  409. Handler: getPodEventsHandler,
  410. Router: r,
  411. })
  412. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/ingresses/{name} ->
  413. // release.NewGetJobsHandler
  414. getIngressEndpoint := factory.NewAPIEndpoint(
  415. &types.APIRequestMetadata{
  416. Verb: types.APIVerbGet,
  417. Method: types.HTTPVerbGet,
  418. Path: &types.Path{
  419. Parent: basePath,
  420. RelativePath: relPath + "/ingresses/{name}",
  421. },
  422. Scopes: []types.PermissionScope{
  423. types.UserScope,
  424. types.ProjectScope,
  425. types.ClusterScope,
  426. types.NamespaceScope,
  427. },
  428. },
  429. )
  430. getIngressHandler := namespace.NewGetIngressHandler(
  431. config,
  432. factory.GetResultWriter(),
  433. )
  434. routes = append(routes, &Route{
  435. Endpoint: getIngressEndpoint,
  436. Handler: getIngressHandler,
  437. Router: r,
  438. })
  439. return routes, newPath
  440. }