cluster.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/cluster"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/config"
  7. "github.com/porter-dev/porter/api/types"
  8. )
  9. func NewClusterScopedRegisterer(children ...*Registerer) *Registerer {
  10. return &Registerer{
  11. GetRoutes: GetClusterScopedRoutes,
  12. Children: children,
  13. }
  14. }
  15. func GetClusterScopedRoutes(
  16. r chi.Router,
  17. config *config.Config,
  18. basePath *types.Path,
  19. factory shared.APIEndpointFactory,
  20. children ...*Registerer,
  21. ) []*Route {
  22. routes, projPath := getClusterRoutes(r, config, basePath, factory)
  23. if len(children) > 0 {
  24. r.Route(projPath.RelativePath, func(r chi.Router) {
  25. for _, child := range children {
  26. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  27. routes = append(routes, childRoutes...)
  28. }
  29. })
  30. }
  31. return routes
  32. }
  33. func getClusterRoutes(
  34. r chi.Router,
  35. config *config.Config,
  36. basePath *types.Path,
  37. factory shared.APIEndpointFactory,
  38. ) ([]*Route, *types.Path) {
  39. relPath := "/clusters/{cluster_id}"
  40. newPath := &types.Path{
  41. Parent: basePath,
  42. RelativePath: relPath,
  43. }
  44. routes := make([]*Route, 0)
  45. // GET /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterGetHandler
  46. getEndpoint := factory.NewAPIEndpoint(
  47. &types.APIRequestMetadata{
  48. Verb: types.APIVerbGet,
  49. Method: types.HTTPVerbGet,
  50. Path: &types.Path{
  51. Parent: basePath,
  52. RelativePath: relPath,
  53. },
  54. Scopes: []types.PermissionScope{
  55. types.UserScope,
  56. types.ProjectScope,
  57. types.ClusterScope,
  58. },
  59. },
  60. )
  61. getHandler := cluster.NewClusterGetHandler(
  62. config,
  63. factory.GetResultWriter(),
  64. )
  65. routes = append(routes, &Route{
  66. Endpoint: getEndpoint,
  67. Handler: getHandler,
  68. Router: r,
  69. })
  70. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces -> cluster.NewClusterListNamespacesHandler
  71. listNamespacesEndpoint := factory.NewAPIEndpoint(
  72. &types.APIRequestMetadata{
  73. Verb: types.APIVerbGet,
  74. Method: types.HTTPVerbGet,
  75. Path: &types.Path{
  76. Parent: basePath,
  77. RelativePath: relPath + "/namespaces",
  78. },
  79. Scopes: []types.PermissionScope{
  80. types.UserScope,
  81. types.ProjectScope,
  82. types.ClusterScope,
  83. },
  84. },
  85. )
  86. listNamespacesHandler := cluster.NewListNamespacesHandler(
  87. config,
  88. factory.GetResultWriter(),
  89. )
  90. routes = append(routes, &Route{
  91. Endpoint: listNamespacesEndpoint,
  92. Handler: listNamespacesHandler,
  93. Router: r,
  94. })
  95. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/create -> cluster.NewCreateNamespaceHandler
  96. createNamespaceEndpoint := factory.NewAPIEndpoint(
  97. &types.APIRequestMetadata{
  98. Verb: types.APIVerbCreate,
  99. Method: types.HTTPVerbPost,
  100. Path: &types.Path{
  101. Parent: basePath,
  102. RelativePath: relPath + "/namespaces/create",
  103. },
  104. Scopes: []types.PermissionScope{
  105. types.UserScope,
  106. types.ProjectScope,
  107. types.ClusterScope,
  108. },
  109. },
  110. )
  111. createNamespaceHandler := cluster.NewCreateNamespaceHandler(
  112. config,
  113. factory.GetDecoderValidator(),
  114. factory.GetResultWriter(),
  115. )
  116. routes = append(routes, &Route{
  117. Endpoint: createNamespaceEndpoint,
  118. Handler: createNamespaceHandler,
  119. Router: r,
  120. })
  121. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/delete -> cluster.NewDeleteNamespaceHandler
  122. deleteNamespaceEndpoint := factory.NewAPIEndpoint(
  123. &types.APIRequestMetadata{
  124. Verb: types.APIVerbDelete,
  125. Method: types.HTTPVerbDelete,
  126. Path: &types.Path{
  127. Parent: basePath,
  128. RelativePath: relPath + "/namespaces/delete",
  129. },
  130. Scopes: []types.PermissionScope{
  131. types.UserScope,
  132. types.ProjectScope,
  133. types.ClusterScope,
  134. },
  135. },
  136. )
  137. deleteNamespaceHandler := cluster.NewDeleteNamespaceHandler(
  138. config,
  139. factory.GetDecoderValidator(),
  140. )
  141. routes = append(routes, &Route{
  142. Endpoint: deleteNamespaceEndpoint,
  143. Handler: deleteNamespaceHandler,
  144. Router: r,
  145. })
  146. // GET /api/projects/{project_id}/clusters/{cluster_id}/kubeconfig -> cluster.NewGetTemporaryKubeconfigHandler
  147. getTemporaryKubeconfigEndpoint := factory.NewAPIEndpoint(
  148. &types.APIRequestMetadata{
  149. Verb: types.APIVerbGet,
  150. Method: types.HTTPVerbGet,
  151. Path: &types.Path{
  152. Parent: basePath,
  153. RelativePath: relPath + "/kubeconfig",
  154. },
  155. Scopes: []types.PermissionScope{
  156. types.UserScope,
  157. types.ProjectScope,
  158. types.ClusterScope,
  159. },
  160. },
  161. )
  162. getTemporaryKubeconfigHandler := cluster.NewGetTemporaryKubeconfigHandler(
  163. config,
  164. factory.GetResultWriter(),
  165. )
  166. routes = append(routes, &Route{
  167. Endpoint: getTemporaryKubeconfigEndpoint,
  168. Handler: getTemporaryKubeconfigHandler,
  169. Router: r,
  170. })
  171. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/detect -> cluster.NewDetectPrometheusInstalledHandler
  172. detectPrometheusInstalledEndpoint := factory.NewAPIEndpoint(
  173. &types.APIRequestMetadata{
  174. Verb: types.APIVerbGet,
  175. Method: types.HTTPVerbGet,
  176. Path: &types.Path{
  177. Parent: basePath,
  178. RelativePath: relPath + "/prometheus/detect",
  179. },
  180. Scopes: []types.PermissionScope{
  181. types.UserScope,
  182. types.ProjectScope,
  183. types.ClusterScope,
  184. },
  185. },
  186. )
  187. detectPrometheusInstalledHandler := cluster.NewDetectPrometheusInstalledHandler(config)
  188. routes = append(routes, &Route{
  189. Endpoint: detectPrometheusInstalledEndpoint,
  190. Handler: detectPrometheusInstalledHandler,
  191. Router: r,
  192. })
  193. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/ingresses -> cluster.NewListNGINXIngressesHandler
  194. listNGINXIngressesEndpoint := factory.NewAPIEndpoint(
  195. &types.APIRequestMetadata{
  196. Verb: types.APIVerbGet,
  197. Method: types.HTTPVerbGet,
  198. Path: &types.Path{
  199. Parent: basePath,
  200. RelativePath: relPath + "/prometheus/ingresses",
  201. },
  202. Scopes: []types.PermissionScope{
  203. types.UserScope,
  204. types.ProjectScope,
  205. types.ClusterScope,
  206. },
  207. },
  208. )
  209. listNGINXIngressesHandler := cluster.NewListNGINXIngressesHandler(
  210. config,
  211. factory.GetResultWriter(),
  212. )
  213. routes = append(routes, &Route{
  214. Endpoint: listNGINXIngressesEndpoint,
  215. Handler: listNGINXIngressesHandler,
  216. Router: r,
  217. })
  218. // GET /api/projects/{project_id}/clusters/{cluster_id}/metrics -> cluster.NewGetPodMetricsHandler
  219. getPodMetricsEndpoint := factory.NewAPIEndpoint(
  220. &types.APIRequestMetadata{
  221. Verb: types.APIVerbGet,
  222. Method: types.HTTPVerbGet,
  223. Path: &types.Path{
  224. Parent: basePath,
  225. RelativePath: relPath + "/metrics",
  226. },
  227. Scopes: []types.PermissionScope{
  228. types.UserScope,
  229. types.ProjectScope,
  230. types.ClusterScope,
  231. },
  232. },
  233. )
  234. getPodMetricsHandler := cluster.NewGetPodMetricsHandler(
  235. config,
  236. factory.GetDecoderValidator(),
  237. factory.GetResultWriter(),
  238. )
  239. routes = append(routes, &Route{
  240. Endpoint: getPodMetricsEndpoint,
  241. Handler: getPodMetricsHandler,
  242. Router: r,
  243. })
  244. return routes, newPath
  245. }