cluster_handler.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "github.com/go-chi/chi"
  7. "github.com/porter-dev/porter/internal/analytics"
  8. "github.com/porter-dev/porter/internal/forms"
  9. "github.com/porter-dev/porter/internal/kubernetes"
  10. "github.com/porter-dev/porter/internal/kubernetes/domain"
  11. "github.com/porter-dev/porter/internal/models"
  12. )
  13. // HandleCreateProjectCluster creates a new cluster
  14. func (app *App) HandleCreateProjectCluster(w http.ResponseWriter, r *http.Request) {
  15. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  16. // userID, err := app.getUserIDFromRequest(r)
  17. if err != nil || projID == 0 {
  18. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  19. return
  20. }
  21. form := &forms.CreateClusterForm{
  22. ProjectID: uint(projID),
  23. }
  24. // decode from JSON to form value
  25. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  26. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  27. return
  28. }
  29. // validate the form
  30. if err := app.validator.Struct(form); err != nil {
  31. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  32. return
  33. }
  34. // convert the form to a registry
  35. cluster, err := form.ToCluster()
  36. if err != nil {
  37. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  38. return
  39. }
  40. // handle write to the database
  41. cluster, err = app.Repo.Cluster.CreateCluster(cluster)
  42. if err != nil {
  43. app.handleErrorDataWrite(err, w)
  44. return
  45. }
  46. app.Logger.Info().Msgf("New cluster created: %d", cluster.ID)
  47. w.WriteHeader(http.StatusCreated)
  48. clusterExt := cluster.Externalize()
  49. if err := json.NewEncoder(w).Encode(clusterExt); err != nil {
  50. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  51. return
  52. }
  53. }
  54. // HandleReadProjectCluster reads a cluster by id
  55. func (app *App) HandleReadProjectCluster(w http.ResponseWriter, r *http.Request) {
  56. id, err := strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
  57. if err != nil || id == 0 {
  58. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  59. return
  60. }
  61. cluster, err := app.Repo.Cluster.ReadCluster(uint(id))
  62. if err != nil {
  63. app.handleErrorRead(err, ErrProjectDataRead, w)
  64. return
  65. }
  66. clusterExt := cluster.DetailedExternalize()
  67. form := &forms.K8sForm{
  68. OutOfClusterConfig: &kubernetes.OutOfClusterConfig{
  69. Repo: app.Repo,
  70. DigitalOceanOAuth: app.DOConf,
  71. Cluster: cluster,
  72. },
  73. }
  74. var agent *kubernetes.Agent
  75. if app.ServerConf.IsTesting {
  76. agent = app.TestAgents.K8sAgent
  77. } else {
  78. agent, err = kubernetes.GetAgentOutOfClusterConfig(form.OutOfClusterConfig)
  79. if err != nil {
  80. app.handleErrorInternal(err, w)
  81. return
  82. }
  83. }
  84. endpoint, found, ingressErr := domain.GetNGINXIngressServiceIP(agent.Clientset)
  85. if found {
  86. clusterExt.IngressIP = endpoint
  87. }
  88. if !found && ingressErr != nil {
  89. clusterExt.IngressError = kubernetes.CatchK8sConnectionError(ingressErr).Externalize()
  90. }
  91. w.WriteHeader(http.StatusOK)
  92. if err := json.NewEncoder(w).Encode(clusterExt); err != nil {
  93. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  94. return
  95. }
  96. }
  97. // HandleListProjectClusters returns a list of clusters that have linked Integrations.
  98. func (app *App) HandleListProjectClusters(w http.ResponseWriter, r *http.Request) {
  99. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  100. if err != nil || projID == 0 {
  101. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  102. return
  103. }
  104. clusters, err := app.Repo.Cluster.ListClustersByProjectID(uint(projID))
  105. if err != nil {
  106. app.handleErrorRead(err, ErrProjectDataRead, w)
  107. return
  108. }
  109. extClusters := make([]*models.ClusterExternal, 0)
  110. for _, cluster := range clusters {
  111. extClusters = append(extClusters, cluster.Externalize())
  112. }
  113. w.WriteHeader(http.StatusOK)
  114. if err := json.NewEncoder(w).Encode(extClusters); err != nil {
  115. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  116. return
  117. }
  118. }
  119. // HandleUpdateProjectCluster updates a project's cluster
  120. func (app *App) HandleUpdateProjectCluster(w http.ResponseWriter, r *http.Request) {
  121. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  122. if err != nil || projID == 0 {
  123. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  124. return
  125. }
  126. clusterID, err := strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
  127. if err != nil || clusterID == 0 {
  128. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  129. return
  130. }
  131. form := &forms.UpdateClusterForm{
  132. ID: uint(clusterID),
  133. }
  134. // decode from JSON to form value
  135. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  136. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  137. return
  138. }
  139. // validate the form
  140. if err := app.validator.Struct(form); err != nil {
  141. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  142. return
  143. }
  144. // convert the form to a registry
  145. cluster, err := form.ToCluster(app.Repo.Cluster)
  146. if err != nil {
  147. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  148. return
  149. }
  150. // handle write to the database
  151. cluster, err = app.Repo.Cluster.UpdateCluster(cluster)
  152. if err != nil {
  153. app.handleErrorDataWrite(err, w)
  154. return
  155. }
  156. w.WriteHeader(http.StatusOK)
  157. clusterExt := cluster.Externalize()
  158. if err := json.NewEncoder(w).Encode(clusterExt); err != nil {
  159. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  160. return
  161. }
  162. }
  163. // HandleDeleteProjectCluster handles the deletion of a Cluster via the cluster ID
  164. func (app *App) HandleDeleteProjectCluster(w http.ResponseWriter, r *http.Request) {
  165. id, err := strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
  166. if err != nil || id == 0 {
  167. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  168. return
  169. }
  170. cluster, err := app.Repo.Cluster.ReadCluster(uint(id))
  171. if err != nil {
  172. app.handleErrorRead(err, ErrProjectDataRead, w)
  173. return
  174. }
  175. err = app.Repo.Cluster.DeleteCluster(cluster)
  176. if err != nil {
  177. app.handleErrorRead(err, ErrProjectDataRead, w)
  178. return
  179. }
  180. w.WriteHeader(http.StatusOK)
  181. }
  182. // HandleCreateProjectClusterCandidates handles the creation of ClusterCandidates using
  183. // a kubeconfig and a project id
  184. func (app *App) HandleCreateProjectClusterCandidates(w http.ResponseWriter, r *http.Request) {
  185. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  186. if err != nil || projID == 0 {
  187. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  188. return
  189. }
  190. form := &forms.CreateClusterCandidatesForm{
  191. ProjectID: uint(projID),
  192. }
  193. // decode from JSON to form value
  194. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  195. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  196. return
  197. }
  198. // validate the form
  199. if err := app.validator.Struct(form); err != nil {
  200. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  201. return
  202. }
  203. // convert the form to a ClusterCandidate
  204. ccs, err := form.ToClusterCandidates(app.ServerConf.IsLocal)
  205. if err != nil {
  206. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  207. return
  208. }
  209. extClusters := make([]*models.ClusterCandidateExternal, 0)
  210. userID, _ := app.getUserIDFromRequest(r)
  211. for _, cc := range ccs {
  212. // handle write to the database
  213. cc, err = app.Repo.Cluster.CreateClusterCandidate(cc)
  214. if err != nil {
  215. app.handleErrorDataWrite(err, w)
  216. return
  217. }
  218. app.AnalyticsClient.Track(analytics.ClusterConnectionStartTrack(
  219. &analytics.ClusterConnectionStartTrackOpts{
  220. ProjectScopedTrackOpts: analytics.GetProjectScopedTrackOpts(userID, uint(projID)),
  221. ClusterCandidateID: cc.ID,
  222. },
  223. ))
  224. app.Logger.Info().Msgf("New cluster candidate created: %d", cc.ID)
  225. // if the ClusterCandidate does not have any actions to perform, create the Cluster
  226. // automatically
  227. if len(cc.Resolvers) == 0 {
  228. // we query the repo again to get the decrypted version of the cluster candidate
  229. cc, err = app.Repo.Cluster.ReadClusterCandidate(cc.ID)
  230. if err != nil {
  231. app.handleErrorDataRead(err, w)
  232. return
  233. }
  234. clusterForm := &forms.ResolveClusterForm{
  235. Resolver: &models.ClusterResolverAll{},
  236. ClusterCandidateID: cc.ID,
  237. ProjectID: uint(projID),
  238. UserID: userID,
  239. }
  240. err := clusterForm.ResolveIntegration(*app.Repo)
  241. if err != nil {
  242. app.handleErrorDataWrite(err, w)
  243. return
  244. }
  245. cluster, err := clusterForm.ResolveCluster(*app.Repo)
  246. if err != nil {
  247. app.handleErrorDataWrite(err, w)
  248. return
  249. }
  250. cc, err = app.Repo.Cluster.UpdateClusterCandidateCreatedClusterID(cc.ID, cluster.ID)
  251. if err != nil {
  252. app.handleErrorDataWrite(err, w)
  253. return
  254. }
  255. app.Logger.Info().Msgf("New cluster created: %d", cluster.ID)
  256. app.AnalyticsClient.Track(analytics.ClusterConnectionSuccessTrack(
  257. &analytics.ClusterConnectionSuccessTrackOpts{
  258. ClusterScopedTrackOpts: analytics.GetClusterScopedTrackOpts(userID, uint(projID), cluster.ID),
  259. ClusterCandidateID: cc.ID,
  260. },
  261. ))
  262. }
  263. extClusters = append(extClusters, cc.Externalize())
  264. }
  265. w.WriteHeader(http.StatusCreated)
  266. if err := json.NewEncoder(w).Encode(extClusters); err != nil {
  267. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  268. return
  269. }
  270. }
  271. // HandleListProjectClusterCandidates returns a list of externalized ClusterCandidates
  272. // ([]models.ClusterCandidateExternal) based on a project ID
  273. func (app *App) HandleListProjectClusterCandidates(w http.ResponseWriter, r *http.Request) {
  274. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  275. if err != nil || projID == 0 {
  276. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  277. return
  278. }
  279. ccs, err := app.Repo.Cluster.ListClusterCandidatesByProjectID(uint(projID))
  280. if err != nil {
  281. app.handleErrorRead(err, ErrProjectDataRead, w)
  282. return
  283. }
  284. extCCs := make([]*models.ClusterCandidateExternal, 0)
  285. for _, cc := range ccs {
  286. extCCs = append(extCCs, cc.Externalize())
  287. }
  288. w.WriteHeader(http.StatusOK)
  289. if err := json.NewEncoder(w).Encode(extCCs); err != nil {
  290. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  291. return
  292. }
  293. }
  294. // HandleResolveClusterCandidate accepts a list of resolving objects (ClusterResolver)
  295. // for a given ClusterCandidate, which "resolves" that ClusterCandidate and creates a
  296. // Cluster for a specific project
  297. func (app *App) HandleResolveClusterCandidate(w http.ResponseWriter, r *http.Request) {
  298. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  299. if err != nil || projID == 0 {
  300. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  301. return
  302. }
  303. candID, err := strconv.ParseUint(chi.URLParam(r, "candidate_id"), 0, 64)
  304. if err != nil || projID == 0 {
  305. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  306. return
  307. }
  308. userID, _ := app.getUserIDFromRequest(r)
  309. // decode actions from request
  310. resolver := &models.ClusterResolverAll{}
  311. if err := json.NewDecoder(r.Body).Decode(resolver); err != nil {
  312. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  313. return
  314. }
  315. clusterResolver := &forms.ResolveClusterForm{
  316. Resolver: resolver,
  317. ClusterCandidateID: uint(candID),
  318. ProjectID: uint(projID),
  319. UserID: userID,
  320. }
  321. err = clusterResolver.ResolveIntegration(*app.Repo)
  322. if err != nil {
  323. app.handleErrorDataWrite(err, w)
  324. return
  325. }
  326. cluster, err := clusterResolver.ResolveCluster(*app.Repo)
  327. if err != nil {
  328. app.handleErrorDataWrite(err, w)
  329. return
  330. }
  331. _, err = app.Repo.Cluster.UpdateClusterCandidateCreatedClusterID(uint(candID), cluster.ID)
  332. if err != nil {
  333. app.handleErrorDataWrite(err, w)
  334. return
  335. }
  336. app.Logger.Info().Msgf("New cluster created: %d", cluster.ID)
  337. app.AnalyticsClient.Track(analytics.ClusterConnectionSuccessTrack(
  338. &analytics.ClusterConnectionSuccessTrackOpts{
  339. ClusterScopedTrackOpts: analytics.GetClusterScopedTrackOpts(userID, uint(projID), cluster.ID),
  340. ClusterCandidateID: uint(candID),
  341. },
  342. ))
  343. clusterExt := cluster.Externalize()
  344. w.WriteHeader(http.StatusCreated)
  345. if err := json.NewEncoder(w).Encode(clusterExt); err != nil {
  346. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  347. return
  348. }
  349. }