cluster.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. // GET /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterGetHandler
  47. getEndpoint := factory.NewAPIEndpoint(
  48. &types.APIRequestMetadata{
  49. Verb: types.APIVerbGet,
  50. Method: types.HTTPVerbGet,
  51. Path: &types.Path{
  52. Parent: basePath,
  53. RelativePath: relPath,
  54. },
  55. Scopes: []types.PermissionScope{
  56. types.UserScope,
  57. types.ProjectScope,
  58. types.ClusterScope,
  59. },
  60. },
  61. )
  62. getHandler := cluster.NewClusterGetHandler(
  63. config,
  64. factory.GetResultWriter(),
  65. )
  66. routes = append(routes, &Route{
  67. Endpoint: getEndpoint,
  68. Handler: getHandler,
  69. Router: r,
  70. })
  71. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces -> cluster.NewClusterListNamespacesHandler
  72. listNamespacesEndpoint := factory.NewAPIEndpoint(
  73. &types.APIRequestMetadata{
  74. Verb: types.APIVerbGet,
  75. Method: types.HTTPVerbGet,
  76. Path: &types.Path{
  77. Parent: basePath,
  78. RelativePath: relPath + "/namespaces",
  79. },
  80. Scopes: []types.PermissionScope{
  81. types.UserScope,
  82. types.ProjectScope,
  83. types.ClusterScope,
  84. },
  85. },
  86. )
  87. listNamespacesHandler := cluster.NewListNamespacesHandler(
  88. config,
  89. factory.GetResultWriter(),
  90. )
  91. routes = append(routes, &Route{
  92. Endpoint: listNamespacesEndpoint,
  93. Handler: listNamespacesHandler,
  94. Router: r,
  95. })
  96. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/create -> cluster.NewCreateNamespaceHandler
  97. createNamespaceEndpoint := factory.NewAPIEndpoint(
  98. &types.APIRequestMetadata{
  99. Verb: types.APIVerbCreate,
  100. Method: types.HTTPVerbPost,
  101. Path: &types.Path{
  102. Parent: basePath,
  103. RelativePath: relPath + "/namespaces/create",
  104. },
  105. Scopes: []types.PermissionScope{
  106. types.UserScope,
  107. types.ProjectScope,
  108. types.ClusterScope,
  109. },
  110. },
  111. )
  112. createNamespaceHandler := cluster.NewCreateNamespaceHandler(
  113. config,
  114. factory.GetDecoderValidator(),
  115. factory.GetResultWriter(),
  116. )
  117. routes = append(routes, &Route{
  118. Endpoint: createNamespaceEndpoint,
  119. Handler: createNamespaceHandler,
  120. Router: r,
  121. })
  122. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/delete -> cluster.NewDeleteNamespaceHandler
  123. deleteNamespaceEndpoint := factory.NewAPIEndpoint(
  124. &types.APIRequestMetadata{
  125. Verb: types.APIVerbDelete,
  126. Method: types.HTTPVerbDelete,
  127. Path: &types.Path{
  128. Parent: basePath,
  129. RelativePath: relPath + "/namespaces/delete",
  130. },
  131. Scopes: []types.PermissionScope{
  132. types.UserScope,
  133. types.ProjectScope,
  134. types.ClusterScope,
  135. },
  136. },
  137. )
  138. deleteNamespaceHandler := cluster.NewDeleteNamespaceHandler(
  139. config,
  140. factory.GetDecoderValidator(),
  141. )
  142. routes = append(routes, &Route{
  143. Endpoint: deleteNamespaceEndpoint,
  144. Handler: deleteNamespaceHandler,
  145. Router: r,
  146. })
  147. // GET /api/projects/{project_id}/clusters/{cluster_id}/kubeconfig -> cluster.NewGetTemporaryKubeconfigHandler
  148. getTemporaryKubeconfigEndpoint := factory.NewAPIEndpoint(
  149. &types.APIRequestMetadata{
  150. Verb: types.APIVerbGet,
  151. Method: types.HTTPVerbGet,
  152. Path: &types.Path{
  153. Parent: basePath,
  154. RelativePath: relPath + "/kubeconfig",
  155. },
  156. Scopes: []types.PermissionScope{
  157. types.UserScope,
  158. types.ProjectScope,
  159. types.ClusterScope,
  160. },
  161. },
  162. )
  163. getTemporaryKubeconfigHandler := cluster.NewGetTemporaryKubeconfigHandler(
  164. config,
  165. factory.GetResultWriter(),
  166. )
  167. routes = append(routes, &Route{
  168. Endpoint: getTemporaryKubeconfigEndpoint,
  169. Handler: getTemporaryKubeconfigHandler,
  170. Router: r,
  171. })
  172. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/detect -> cluster.NewDetectPrometheusInstalledHandler
  173. detectPrometheusInstalledEndpoint := factory.NewAPIEndpoint(
  174. &types.APIRequestMetadata{
  175. Verb: types.APIVerbGet,
  176. Method: types.HTTPVerbGet,
  177. Path: &types.Path{
  178. Parent: basePath,
  179. RelativePath: relPath + "/prometheus/detect",
  180. },
  181. Scopes: []types.PermissionScope{
  182. types.UserScope,
  183. types.ProjectScope,
  184. types.ClusterScope,
  185. },
  186. },
  187. )
  188. detectPrometheusInstalledHandler := cluster.NewDetectPrometheusInstalledHandler(config)
  189. routes = append(routes, &Route{
  190. Endpoint: detectPrometheusInstalledEndpoint,
  191. Handler: detectPrometheusInstalledHandler,
  192. Router: r,
  193. })
  194. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/ingresses -> cluster.NewListNGINXIngressesHandler
  195. listNGINXIngressesEndpoint := factory.NewAPIEndpoint(
  196. &types.APIRequestMetadata{
  197. Verb: types.APIVerbGet,
  198. Method: types.HTTPVerbGet,
  199. Path: &types.Path{
  200. Parent: basePath,
  201. RelativePath: relPath + "/prometheus/ingresses",
  202. },
  203. Scopes: []types.PermissionScope{
  204. types.UserScope,
  205. types.ProjectScope,
  206. types.ClusterScope,
  207. },
  208. },
  209. )
  210. listNGINXIngressesHandler := cluster.NewListNGINXIngressesHandler(
  211. config,
  212. factory.GetResultWriter(),
  213. )
  214. routes = append(routes, &Route{
  215. Endpoint: listNGINXIngressesEndpoint,
  216. Handler: listNGINXIngressesHandler,
  217. Router: r,
  218. })
  219. // GET /api/projects/{project_id}/clusters/{cluster_id}/metrics -> cluster.NewGetPodMetricsHandler
  220. getPodMetricsEndpoint := factory.NewAPIEndpoint(
  221. &types.APIRequestMetadata{
  222. Verb: types.APIVerbGet,
  223. Method: types.HTTPVerbGet,
  224. Path: &types.Path{
  225. Parent: basePath,
  226. RelativePath: relPath + "/metrics",
  227. },
  228. Scopes: []types.PermissionScope{
  229. types.UserScope,
  230. types.ProjectScope,
  231. types.ClusterScope,
  232. },
  233. },
  234. )
  235. getPodMetricsHandler := cluster.NewGetPodMetricsHandler(
  236. config,
  237. factory.GetDecoderValidator(),
  238. factory.GetResultWriter(),
  239. )
  240. routes = append(routes, &Route{
  241. Endpoint: getPodMetricsEndpoint,
  242. Handler: getPodMetricsHandler,
  243. Router: r,
  244. })
  245. // GET /api/projects/{project_id}/clusters/{cluster_id}/helm_release -> cluster.NewStreamHelmReleaseHandler
  246. streamHelmReleaseEndpoint := factory.NewAPIEndpoint(
  247. &types.APIRequestMetadata{
  248. Verb: types.APIVerbGet,
  249. Method: types.HTTPVerbGet,
  250. Path: &types.Path{
  251. Parent: basePath,
  252. RelativePath: relPath + "/helm_release",
  253. },
  254. Scopes: []types.PermissionScope{
  255. types.UserScope,
  256. types.ProjectScope,
  257. types.ClusterScope,
  258. },
  259. },
  260. )
  261. streamHelmReleaseHandler := cluster.NewStreamHelmReleaseHandler(
  262. config,
  263. factory.GetDecoderValidator(),
  264. factory.GetResultWriter(),
  265. )
  266. routes = append(routes, &Route{
  267. Endpoint: streamHelmReleaseEndpoint,
  268. Handler: streamHelmReleaseHandler,
  269. Router: r,
  270. })
  271. // GET /api/projects/{project_id}/clusters/{cluster_id}/{kind}/status -> cluster.NewStreamStatusHandler
  272. streamStatusEndpoint := factory.NewAPIEndpoint(
  273. &types.APIRequestMetadata{
  274. Verb: types.APIVerbGet,
  275. Method: types.HTTPVerbGet,
  276. Path: &types.Path{
  277. Parent: basePath,
  278. RelativePath: fmt.Sprintf(
  279. "%s/{%s}/status",
  280. relPath,
  281. types.URLParamKind,
  282. ),
  283. },
  284. Scopes: []types.PermissionScope{
  285. types.UserScope,
  286. types.ProjectScope,
  287. types.ClusterScope,
  288. },
  289. },
  290. )
  291. streamStatusHandler := cluster.NewStreamStatusHandler(
  292. config,
  293. factory.GetDecoderValidator(),
  294. factory.GetResultWriter(),
  295. )
  296. routes = append(routes, &Route{
  297. Endpoint: streamStatusEndpoint,
  298. Handler: streamStatusHandler,
  299. Router: r,
  300. })
  301. // GET /api/projects/{project_id}/clusters/{cluster_id}/pods -> cluster.NewGetPodsHandler
  302. getPodsEndpoint := factory.NewAPIEndpoint(
  303. &types.APIRequestMetadata{
  304. Verb: types.APIVerbGet,
  305. Method: types.HTTPVerbGet,
  306. Path: &types.Path{
  307. Parent: basePath,
  308. RelativePath: relPath + "/pods",
  309. },
  310. Scopes: []types.PermissionScope{
  311. types.UserScope,
  312. types.ProjectScope,
  313. types.ClusterScope,
  314. },
  315. },
  316. )
  317. getPodsHandler := cluster.NewGetPodsHandler(
  318. config,
  319. factory.GetDecoderValidator(),
  320. factory.GetResultWriter(),
  321. )
  322. routes = append(routes, &Route{
  323. Endpoint: getPodsEndpoint,
  324. Handler: getPodsHandler,
  325. Router: r,
  326. })
  327. return routes, newPath
  328. }