cluster.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/types"
  7. )
  8. func NewClusterScopedRegisterer(children ...*Registerer) *Registerer {
  9. return &Registerer{
  10. GetRoutes: GetClusterScopedRoutes,
  11. Children: children,
  12. }
  13. }
  14. func GetClusterScopedRoutes(
  15. r chi.Router,
  16. config *shared.Config,
  17. basePath *types.Path,
  18. factory shared.APIEndpointFactory,
  19. children ...*Registerer,
  20. ) []*Route {
  21. routes, projPath := getClusterRoutes(r, config, basePath, factory)
  22. if len(children) > 0 {
  23. r.Route(projPath.RelativePath, func(r chi.Router) {
  24. for _, child := range children {
  25. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  26. routes = append(routes, childRoutes...)
  27. }
  28. })
  29. }
  30. return routes
  31. }
  32. func getClusterRoutes(
  33. r chi.Router,
  34. config *shared.Config,
  35. basePath *types.Path,
  36. factory shared.APIEndpointFactory,
  37. ) ([]*Route, *types.Path) {
  38. relPath := "/clusters/{cluster_id}"
  39. newPath := &types.Path{
  40. Parent: basePath,
  41. RelativePath: relPath,
  42. }
  43. routes := make([]*Route, 0)
  44. // GET /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterGetHandler
  45. getEndpoint := factory.NewAPIEndpoint(
  46. &types.APIRequestMetadata{
  47. Verb: types.APIVerbGet,
  48. Method: types.HTTPVerbGet,
  49. Path: &types.Path{
  50. Parent: basePath,
  51. RelativePath: relPath,
  52. },
  53. Scopes: []types.PermissionScope{
  54. types.UserScope,
  55. types.ProjectScope,
  56. types.ClusterScope,
  57. },
  58. },
  59. )
  60. getHandler := cluster.NewClusterGetHandler(
  61. config,
  62. factory.GetResultWriter(),
  63. )
  64. routes = append(routes, &Route{
  65. Endpoint: getEndpoint,
  66. Handler: getHandler,
  67. Router: r,
  68. })
  69. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces -> cluster.NewClusterListNamespacesHandler
  70. listNamespacesEndpoint := factory.NewAPIEndpoint(
  71. &types.APIRequestMetadata{
  72. Verb: types.APIVerbGet,
  73. Method: types.HTTPVerbGet,
  74. Path: &types.Path{
  75. Parent: basePath,
  76. RelativePath: relPath + "/namespaces",
  77. },
  78. Scopes: []types.PermissionScope{
  79. types.UserScope,
  80. types.ProjectScope,
  81. types.ClusterScope,
  82. },
  83. },
  84. )
  85. listNamespacesHandler := cluster.NewClusterListNamespacesHandler(
  86. config,
  87. factory.GetResultWriter(),
  88. )
  89. routes = append(routes, &Route{
  90. Endpoint: listNamespacesEndpoint,
  91. Handler: listNamespacesHandler,
  92. Router: r,
  93. })
  94. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/create -> cluster.NewCreateNamespaceHandler
  95. createNamespaceEndpoint := factory.NewAPIEndpoint(
  96. &types.APIRequestMetadata{
  97. Verb: types.APIVerbCreate,
  98. Method: types.HTTPVerbPost,
  99. Path: &types.Path{
  100. Parent: basePath,
  101. RelativePath: relPath + "/namespaces/create",
  102. },
  103. Scopes: []types.PermissionScope{
  104. types.UserScope,
  105. types.ProjectScope,
  106. types.ClusterScope,
  107. },
  108. },
  109. )
  110. createNamespaceHandler := cluster.NewCreateNamespaceHandler(
  111. config,
  112. factory.GetDecoderValidator(),
  113. factory.GetResultWriter(),
  114. )
  115. routes = append(routes, &Route{
  116. Endpoint: createNamespaceEndpoint,
  117. Handler: createNamespaceHandler,
  118. Router: r,
  119. })
  120. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/delete -> cluster.NewDeleteNamespaceHandler
  121. deleteNamespaceEndpoint := factory.NewAPIEndpoint(
  122. &types.APIRequestMetadata{
  123. Verb: types.APIVerbDelete,
  124. Method: types.HTTPVerbDelete,
  125. Path: &types.Path{
  126. Parent: basePath,
  127. RelativePath: relPath + "/namespaces/delete",
  128. },
  129. Scopes: []types.PermissionScope{
  130. types.UserScope,
  131. types.ProjectScope,
  132. types.ClusterScope,
  133. },
  134. },
  135. )
  136. deleteNamespaceHandler := cluster.NewDeleteNamespaceHandler(
  137. config,
  138. factory.GetDecoderValidator(),
  139. )
  140. routes = append(routes, &Route{
  141. Endpoint: deleteNamespaceEndpoint,
  142. Handler: deleteNamespaceHandler,
  143. Router: r,
  144. })
  145. // GET /api/projects/{project_id}/clusters/{cluster_id}/kubeconfig -> cluster.NewGetTemporaryKubeconfigHandler
  146. getTemporaryKubeconfigEndpoint := factory.NewAPIEndpoint(
  147. &types.APIRequestMetadata{
  148. Verb: types.APIVerbGet,
  149. Method: types.HTTPVerbGet,
  150. Path: &types.Path{
  151. Parent: basePath,
  152. RelativePath: relPath + "/kubeconfig",
  153. },
  154. Scopes: []types.PermissionScope{
  155. types.UserScope,
  156. types.ProjectScope,
  157. types.ClusterScope,
  158. },
  159. },
  160. )
  161. getTemporaryKubeconfigHandler := cluster.NewGetTemporaryKubeconfigHandler(
  162. config,
  163. factory.GetResultWriter(),
  164. )
  165. routes = append(routes, &Route{
  166. Endpoint: getTemporaryKubeconfigEndpoint,
  167. Handler: getTemporaryKubeconfigHandler,
  168. Router: r,
  169. })
  170. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/detect -> cluster.NewDetectPrometheusInstalledHandler
  171. detectPrometheusInstalledEndpoint := factory.NewAPIEndpoint(
  172. &types.APIRequestMetadata{
  173. Verb: types.APIVerbGet,
  174. Method: types.HTTPVerbGet,
  175. Path: &types.Path{
  176. Parent: basePath,
  177. RelativePath: relPath + "/prometheus/detect",
  178. },
  179. Scopes: []types.PermissionScope{
  180. types.UserScope,
  181. types.ProjectScope,
  182. types.ClusterScope,
  183. },
  184. },
  185. )
  186. detectPrometheusInstalledHandler := cluster.NewDetectPrometheusInstalledHandler(config)
  187. routes = append(routes, &Route{
  188. Endpoint: detectPrometheusInstalledEndpoint,
  189. Handler: detectPrometheusInstalledHandler,
  190. Router: r,
  191. })
  192. return routes, newPath
  193. }