namespace.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/crd -> namespace.NewCRDDeleteHandler
  208. deleteCRDEndpoint := factory.NewAPIEndpoint(
  209. &types.APIRequestMetadata{
  210. Verb: types.APIVerbDelete,
  211. Method: types.HTTPVerbDelete,
  212. Path: &types.Path{
  213. Parent: basePath,
  214. RelativePath: relPath + "/crd",
  215. },
  216. Scopes: []types.PermissionScope{
  217. types.UserScope,
  218. types.ProjectScope,
  219. types.ClusterScope,
  220. },
  221. },
  222. )
  223. deleteCRDHandler := namespace.NewCRDDeleteHandler(
  224. config,
  225. factory.GetDecoderValidator(),
  226. factory.GetResultWriter(),
  227. )
  228. routes = append(routes, &Route{
  229. Endpoint: deleteCRDEndpoint,
  230. Handler: deleteCRDHandler,
  231. Router: r,
  232. })
  233. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases -> namespace.NewListReleasesHandler
  234. listReleasesEndpoint := factory.NewAPIEndpoint(
  235. &types.APIRequestMetadata{
  236. Verb: types.APIVerbGet,
  237. Method: types.HTTPVerbGet,
  238. Path: &types.Path{
  239. Parent: basePath,
  240. RelativePath: relPath + "/releases",
  241. },
  242. Scopes: []types.PermissionScope{
  243. types.UserScope,
  244. types.ProjectScope,
  245. types.ClusterScope,
  246. types.NamespaceScope,
  247. },
  248. },
  249. )
  250. listReleasesHandler := namespace.NewListReleasesHandler(
  251. config,
  252. factory.GetDecoderValidator(),
  253. factory.GetResultWriter(),
  254. )
  255. routes = append(routes, &Route{
  256. Endpoint: listReleasesEndpoint,
  257. Handler: listReleasesHandler,
  258. Router: r,
  259. })
  260. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/pod/{name}/logs -> namespace.NewStreamPodLogsHandler
  261. streamPodLogsEndpoint := factory.NewAPIEndpoint(
  262. &types.APIRequestMetadata{
  263. Verb: types.APIVerbGet,
  264. Method: types.HTTPVerbGet,
  265. Path: &types.Path{
  266. Parent: basePath,
  267. RelativePath: fmt.Sprintf(
  268. "%s/pod/{%s}/logs",
  269. relPath,
  270. types.URLParamPodName,
  271. ),
  272. },
  273. Scopes: []types.PermissionScope{
  274. types.UserScope,
  275. types.ProjectScope,
  276. types.ClusterScope,
  277. types.NamespaceScope,
  278. },
  279. IsWebsocket: true,
  280. },
  281. )
  282. streamPodLogsHandler := namespace.NewStreamPodLogsHandler(
  283. config,
  284. factory.GetDecoderValidator(),
  285. factory.GetResultWriter(),
  286. )
  287. routes = append(routes, &Route{
  288. Endpoint: streamPodLogsEndpoint,
  289. Handler: streamPodLogsHandler,
  290. Router: r,
  291. })
  292. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/jobs/{name}/pods -> jobs.NewGetPodsHandler
  293. getJobPodsEndpoint := factory.NewAPIEndpoint(
  294. &types.APIRequestMetadata{
  295. Verb: types.APIVerbList,
  296. Method: types.HTTPVerbGet,
  297. Path: &types.Path{
  298. Parent: basePath,
  299. RelativePath: fmt.Sprintf(
  300. "%s/jobs/{%s}/pods",
  301. relPath,
  302. types.URLParamJobName,
  303. ),
  304. },
  305. Scopes: []types.PermissionScope{
  306. types.UserScope,
  307. types.ProjectScope,
  308. types.ClusterScope,
  309. types.NamespaceScope,
  310. },
  311. },
  312. )
  313. getJobPodsHandler := job.NewGetPodsHandler(
  314. config,
  315. factory.GetResultWriter(),
  316. )
  317. routes = append(routes, &Route{
  318. Endpoint: getJobPodsEndpoint,
  319. Handler: getJobPodsHandler,
  320. Router: r,
  321. })
  322. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/jobs/{name} -> jobs.NewDeleteHandler
  323. deleteJobEndpoint := factory.NewAPIEndpoint(
  324. &types.APIRequestMetadata{
  325. Verb: types.APIVerbDelete,
  326. Method: types.HTTPVerbDelete,
  327. Path: &types.Path{
  328. Parent: basePath,
  329. RelativePath: fmt.Sprintf(
  330. "%s/jobs/{%s}",
  331. relPath,
  332. types.URLParamJobName,
  333. ),
  334. },
  335. Scopes: []types.PermissionScope{
  336. types.UserScope,
  337. types.ProjectScope,
  338. types.ClusterScope,
  339. types.NamespaceScope,
  340. },
  341. },
  342. )
  343. deleteJobHandler := job.NewDeleteHandler(
  344. config,
  345. )
  346. routes = append(routes, &Route{
  347. Endpoint: deleteJobEndpoint,
  348. Handler: deleteJobHandler,
  349. Router: r,
  350. })
  351. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/jobs/{name}/stop -> jobs.NewStopHandler
  352. stopJobEndpoint := factory.NewAPIEndpoint(
  353. &types.APIRequestMetadata{
  354. Verb: types.APIVerbUpdate,
  355. Method: types.HTTPVerbPost,
  356. Path: &types.Path{
  357. Parent: basePath,
  358. RelativePath: fmt.Sprintf(
  359. "%s/jobs/{%s}/stop",
  360. relPath,
  361. types.URLParamJobName,
  362. ),
  363. },
  364. Scopes: []types.PermissionScope{
  365. types.UserScope,
  366. types.ProjectScope,
  367. types.ClusterScope,
  368. types.NamespaceScope,
  369. },
  370. },
  371. )
  372. stopJobHandler := job.NewStopHandler(
  373. config,
  374. )
  375. routes = append(routes, &Route{
  376. Endpoint: stopJobEndpoint,
  377. Handler: stopJobHandler,
  378. Router: r,
  379. })
  380. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/pods/{name} -> namespace.NewGetPodHandler
  381. getPodEndpoint := factory.NewAPIEndpoint(
  382. &types.APIRequestMetadata{
  383. Verb: types.APIVerbGet,
  384. Method: types.HTTPVerbGet,
  385. Path: &types.Path{
  386. Parent: basePath,
  387. RelativePath: fmt.Sprintf(
  388. "%s/pods/{%s}",
  389. relPath,
  390. types.URLParamPodName,
  391. ),
  392. },
  393. Scopes: []types.PermissionScope{
  394. types.UserScope,
  395. types.ProjectScope,
  396. types.ClusterScope,
  397. types.NamespaceScope,
  398. },
  399. },
  400. )
  401. getPodHandler := namespace.NewGetPodHandler(
  402. config,
  403. factory.GetResultWriter(),
  404. )
  405. routes = append(routes, &Route{
  406. Endpoint: getPodEndpoint,
  407. Handler: getPodHandler,
  408. Router: r,
  409. })
  410. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/pods/{name} -> namespace.NewDeletePodHandler
  411. deletePodEndpoint := factory.NewAPIEndpoint(
  412. &types.APIRequestMetadata{
  413. Verb: types.APIVerbDelete,
  414. Method: types.HTTPVerbDelete,
  415. Path: &types.Path{
  416. Parent: basePath,
  417. RelativePath: fmt.Sprintf(
  418. "%s/pods/{%s}",
  419. relPath,
  420. types.URLParamPodName,
  421. ),
  422. },
  423. Scopes: []types.PermissionScope{
  424. types.UserScope,
  425. types.ProjectScope,
  426. types.ClusterScope,
  427. types.NamespaceScope,
  428. },
  429. },
  430. )
  431. deletePodHandler := namespace.NewDeletePodHandler(
  432. config,
  433. )
  434. routes = append(routes, &Route{
  435. Endpoint: deletePodEndpoint,
  436. Handler: deletePodHandler,
  437. Router: r,
  438. })
  439. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/pods/{name}/events -> namespace.NewGetPodEventsHandler
  440. getPodEventsEndpoint := factory.NewAPIEndpoint(
  441. &types.APIRequestMetadata{
  442. Verb: types.APIVerbList,
  443. Method: types.HTTPVerbGet,
  444. Path: &types.Path{
  445. Parent: basePath,
  446. RelativePath: fmt.Sprintf(
  447. "%s/pods/{%s}/events",
  448. relPath,
  449. types.URLParamPodName,
  450. ),
  451. },
  452. Scopes: []types.PermissionScope{
  453. types.UserScope,
  454. types.ProjectScope,
  455. types.ClusterScope,
  456. types.NamespaceScope,
  457. },
  458. },
  459. )
  460. getPodEventsHandler := namespace.NewGetPodEventsHandler(
  461. config,
  462. factory.GetResultWriter(),
  463. )
  464. routes = append(routes, &Route{
  465. Endpoint: getPodEventsEndpoint,
  466. Handler: getPodEventsHandler,
  467. Router: r,
  468. })
  469. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/ingresses/{name} ->
  470. // release.NewGetJobsHandler
  471. getIngressEndpoint := factory.NewAPIEndpoint(
  472. &types.APIRequestMetadata{
  473. Verb: types.APIVerbGet,
  474. Method: types.HTTPVerbGet,
  475. Path: &types.Path{
  476. Parent: basePath,
  477. RelativePath: relPath + "/ingresses/{name}",
  478. },
  479. Scopes: []types.PermissionScope{
  480. types.UserScope,
  481. types.ProjectScope,
  482. types.ClusterScope,
  483. types.NamespaceScope,
  484. },
  485. },
  486. )
  487. getIngressHandler := namespace.NewGetIngressHandler(
  488. config,
  489. factory.GetResultWriter(),
  490. )
  491. routes = append(routes, &Route{
  492. Endpoint: getIngressEndpoint,
  493. Handler: getIngressHandler,
  494. Router: r,
  495. })
  496. return routes, newPath
  497. }