cluster_handler.go 11 KB

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