namespace.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. IsWebsocket: true,
  254. },
  255. )
  256. streamPodLogsHandler := namespace.NewStreamPodLogsHandler(
  257. config,
  258. factory.GetDecoderValidator(),
  259. factory.GetResultWriter(),
  260. )
  261. routes = append(routes, &Route{
  262. Endpoint: streamPodLogsEndpoint,
  263. Handler: streamPodLogsHandler,
  264. Router: r,
  265. })
  266. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/jobs/{name}/pods -> jobs.NewGetPodsHandler
  267. getJobPodsEndpoint := factory.NewAPIEndpoint(
  268. &types.APIRequestMetadata{
  269. Verb: types.APIVerbList,
  270. Method: types.HTTPVerbGet,
  271. Path: &types.Path{
  272. Parent: basePath,
  273. RelativePath: fmt.Sprintf(
  274. "%s/jobs/{%s}/pods",
  275. relPath,
  276. types.URLParamJobName,
  277. ),
  278. },
  279. Scopes: []types.PermissionScope{
  280. types.UserScope,
  281. types.ProjectScope,
  282. types.ClusterScope,
  283. types.NamespaceScope,
  284. },
  285. },
  286. )
  287. getJobPodsHandler := job.NewGetPodsHandler(
  288. config,
  289. factory.GetResultWriter(),
  290. )
  291. routes = append(routes, &Route{
  292. Endpoint: getJobPodsEndpoint,
  293. Handler: getJobPodsHandler,
  294. Router: r,
  295. })
  296. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/jobs/{name} -> jobs.NewDeleteHandler
  297. deleteJobEndpoint := factory.NewAPIEndpoint(
  298. &types.APIRequestMetadata{
  299. Verb: types.APIVerbDelete,
  300. Method: types.HTTPVerbDelete,
  301. Path: &types.Path{
  302. Parent: basePath,
  303. RelativePath: fmt.Sprintf(
  304. "%s/jobs/{%s}",
  305. relPath,
  306. types.URLParamJobName,
  307. ),
  308. },
  309. Scopes: []types.PermissionScope{
  310. types.UserScope,
  311. types.ProjectScope,
  312. types.ClusterScope,
  313. types.NamespaceScope,
  314. },
  315. },
  316. )
  317. deleteJobHandler := job.NewDeleteHandler(
  318. config,
  319. )
  320. routes = append(routes, &Route{
  321. Endpoint: deleteJobEndpoint,
  322. Handler: deleteJobHandler,
  323. Router: r,
  324. })
  325. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/jobs/{name}/stop -> jobs.NewStopHandler
  326. stopJobEndpoint := factory.NewAPIEndpoint(
  327. &types.APIRequestMetadata{
  328. Verb: types.APIVerbUpdate,
  329. Method: types.HTTPVerbPost,
  330. Path: &types.Path{
  331. Parent: basePath,
  332. RelativePath: fmt.Sprintf(
  333. "%s/jobs/{%s}/stop",
  334. relPath,
  335. types.URLParamJobName,
  336. ),
  337. },
  338. Scopes: []types.PermissionScope{
  339. types.UserScope,
  340. types.ProjectScope,
  341. types.ClusterScope,
  342. types.NamespaceScope,
  343. },
  344. },
  345. )
  346. stopJobHandler := job.NewStopHandler(
  347. config,
  348. )
  349. routes = append(routes, &Route{
  350. Endpoint: stopJobEndpoint,
  351. Handler: stopJobHandler,
  352. Router: r,
  353. })
  354. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/pods/{name} -> namespace.NewDeletePodHandler
  355. deletePodEndpoint := factory.NewAPIEndpoint(
  356. &types.APIRequestMetadata{
  357. Verb: types.APIVerbDelete,
  358. Method: types.HTTPVerbDelete,
  359. Path: &types.Path{
  360. Parent: basePath,
  361. RelativePath: fmt.Sprintf(
  362. "%s/pods/{%s}",
  363. relPath,
  364. types.URLParamPodName,
  365. ),
  366. },
  367. Scopes: []types.PermissionScope{
  368. types.UserScope,
  369. types.ProjectScope,
  370. types.ClusterScope,
  371. types.NamespaceScope,
  372. },
  373. },
  374. )
  375. deletePodHandler := namespace.NewDeletePodHandler(
  376. config,
  377. )
  378. routes = append(routes, &Route{
  379. Endpoint: deletePodEndpoint,
  380. Handler: deletePodHandler,
  381. Router: r,
  382. })
  383. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/pods/{name}/events -> namespace.NewGetPodEventsHandler
  384. getPodEventsEndpoint := factory.NewAPIEndpoint(
  385. &types.APIRequestMetadata{
  386. Verb: types.APIVerbList,
  387. Method: types.HTTPVerbGet,
  388. Path: &types.Path{
  389. Parent: basePath,
  390. RelativePath: fmt.Sprintf(
  391. "%s/pods/{%s}/events",
  392. relPath,
  393. types.URLParamPodName,
  394. ),
  395. },
  396. Scopes: []types.PermissionScope{
  397. types.UserScope,
  398. types.ProjectScope,
  399. types.ClusterScope,
  400. types.NamespaceScope,
  401. },
  402. },
  403. )
  404. getPodEventsHandler := namespace.NewGetPodEventsHandler(
  405. config,
  406. factory.GetResultWriter(),
  407. )
  408. routes = append(routes, &Route{
  409. Endpoint: getPodEventsEndpoint,
  410. Handler: getPodEventsHandler,
  411. Router: r,
  412. })
  413. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/ingresses/{name} ->
  414. // release.NewGetJobsHandler
  415. getIngressEndpoint := factory.NewAPIEndpoint(
  416. &types.APIRequestMetadata{
  417. Verb: types.APIVerbGet,
  418. Method: types.HTTPVerbGet,
  419. Path: &types.Path{
  420. Parent: basePath,
  421. RelativePath: relPath + "/ingresses/{name}",
  422. },
  423. Scopes: []types.PermissionScope{
  424. types.UserScope,
  425. types.ProjectScope,
  426. types.ClusterScope,
  427. types.NamespaceScope,
  428. },
  429. },
  430. )
  431. getIngressHandler := namespace.NewGetIngressHandler(
  432. config,
  433. factory.GetResultWriter(),
  434. )
  435. routes = append(routes, &Route{
  436. Endpoint: getIngressEndpoint,
  437. Handler: getIngressHandler,
  438. Router: r,
  439. })
  440. return routes, newPath
  441. }