cluster_handler.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "sync"
  7. "github.com/go-chi/chi"
  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. 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. var wg sync.WaitGroup
  106. for _, cluster := range clusters {
  107. extClusters = append(extClusters, cluster.Externalize())
  108. }
  109. w.WriteHeader(http.StatusOK)
  110. if err := json.NewEncoder(w).Encode(extClusters); err != nil {
  111. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  112. return
  113. }
  114. }
  115. // HandleUpdateProjectCluster updates a project's cluster
  116. func (app *App) HandleUpdateProjectCluster(w http.ResponseWriter, r *http.Request) {
  117. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  118. if err != nil || projID == 0 {
  119. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  120. return
  121. }
  122. clusterID, err := strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
  123. if err != nil || clusterID == 0 {
  124. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  125. return
  126. }
  127. form := &forms.UpdateClusterForm{
  128. ID: uint(clusterID),
  129. }
  130. // decode from JSON to form value
  131. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  132. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  133. return
  134. }
  135. // validate the form
  136. if err := app.validator.Struct(form); err != nil {
  137. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  138. return
  139. }
  140. // convert the form to a registry
  141. cluster, err := form.ToCluster(app.Repo.Cluster)
  142. if err != nil {
  143. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  144. return
  145. }
  146. // handle write to the database
  147. cluster, err = app.Repo.Cluster.UpdateCluster(cluster)
  148. if err != nil {
  149. app.handleErrorDataWrite(err, w)
  150. return
  151. }
  152. w.WriteHeader(http.StatusOK)
  153. clusterExt := cluster.Externalize()
  154. if err := json.NewEncoder(w).Encode(clusterExt); err != nil {
  155. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  156. return
  157. }
  158. }
  159. // HandleDeleteProjectCluster handles the deletion of a Cluster via the cluster ID
  160. func (app *App) HandleDeleteProjectCluster(w http.ResponseWriter, r *http.Request) {
  161. id, err := strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
  162. if err != nil || id == 0 {
  163. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  164. return
  165. }
  166. cluster, err := app.Repo.Cluster.ReadCluster(uint(id))
  167. if err != nil {
  168. app.handleErrorRead(err, ErrProjectDataRead, w)
  169. return
  170. }
  171. err = app.Repo.Cluster.DeleteCluster(cluster)
  172. if err != nil {
  173. app.handleErrorRead(err, ErrProjectDataRead, w)
  174. return
  175. }
  176. w.WriteHeader(http.StatusOK)
  177. }
  178. // HandleCreateProjectClusterCandidates handles the creation of ClusterCandidates using
  179. // a kubeconfig and a project id
  180. func (app *App) HandleCreateProjectClusterCandidates(w http.ResponseWriter, r *http.Request) {
  181. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  182. if err != nil || projID == 0 {
  183. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  184. return
  185. }
  186. form := &forms.CreateClusterCandidatesForm{
  187. ProjectID: uint(projID),
  188. }
  189. // decode from JSON to form value
  190. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  191. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  192. return
  193. }
  194. // validate the form
  195. if err := app.validator.Struct(form); err != nil {
  196. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  197. return
  198. }
  199. // convert the form to a ClusterCandidate
  200. ccs, err := form.ToClusterCandidates(app.ServerConf.IsLocal)
  201. if err != nil {
  202. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  203. return
  204. }
  205. extClusters := make([]*models.ClusterCandidateExternal, 0)
  206. session, err := app.Store.Get(r, app.ServerConf.CookieName)
  207. if err != nil {
  208. http.Error(w, err.Error(), http.StatusInternalServerError)
  209. return
  210. }
  211. userID, _ := session.Values["user_id"].(uint)
  212. for _, cc := range ccs {
  213. // handle write to the database
  214. cc, err = app.Repo.Cluster.CreateClusterCandidate(cc)
  215. if err != nil {
  216. app.handleErrorDataWrite(err, w)
  217. return
  218. }
  219. app.Logger.Info().Msgf("New cluster candidate created: %d", cc.ID)
  220. // if the ClusterCandidate does not have any actions to perform, create the Cluster
  221. // automatically
  222. if len(cc.Resolvers) == 0 {
  223. // we query the repo again to get the decrypted version of the cluster candidate
  224. cc, err = app.Repo.Cluster.ReadClusterCandidate(cc.ID)
  225. if err != nil {
  226. app.handleErrorDataRead(err, w)
  227. return
  228. }
  229. clusterForm := &forms.ResolveClusterForm{
  230. Resolver: &models.ClusterResolverAll{},
  231. ClusterCandidateID: cc.ID,
  232. ProjectID: uint(projID),
  233. UserID: userID,
  234. }
  235. err := clusterForm.ResolveIntegration(*app.Repo)
  236. if err != nil {
  237. app.handleErrorDataWrite(err, w)
  238. return
  239. }
  240. cluster, err := clusterForm.ResolveCluster(*app.Repo)
  241. if err != nil {
  242. app.handleErrorDataWrite(err, w)
  243. return
  244. }
  245. cc, err = app.Repo.Cluster.UpdateClusterCandidateCreatedClusterID(cc.ID, cluster.ID)
  246. if err != nil {
  247. app.handleErrorDataWrite(err, w)
  248. return
  249. }
  250. app.Logger.Info().Msgf("New cluster created: %d", cluster.ID)
  251. }
  252. extClusters = append(extClusters, cc.Externalize())
  253. }
  254. w.WriteHeader(http.StatusCreated)
  255. if err := json.NewEncoder(w).Encode(extClusters); err != nil {
  256. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  257. return
  258. }
  259. }
  260. // HandleListProjectClusterCandidates returns a list of externalized ClusterCandidates
  261. // ([]models.ClusterCandidateExternal) based on a project ID
  262. func (app *App) HandleListProjectClusterCandidates(w http.ResponseWriter, r *http.Request) {
  263. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  264. if err != nil || projID == 0 {
  265. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  266. return
  267. }
  268. ccs, err := app.Repo.Cluster.ListClusterCandidatesByProjectID(uint(projID))
  269. if err != nil {
  270. app.handleErrorRead(err, ErrProjectDataRead, w)
  271. return
  272. }
  273. extCCs := make([]*models.ClusterCandidateExternal, 0)
  274. for _, cc := range ccs {
  275. extCCs = append(extCCs, cc.Externalize())
  276. }
  277. w.WriteHeader(http.StatusOK)
  278. if err := json.NewEncoder(w).Encode(extCCs); err != nil {
  279. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  280. return
  281. }
  282. }
  283. // HandleResolveClusterCandidate accepts a list of resolving objects (ClusterResolver)
  284. // for a given ClusterCandidate, which "resolves" that ClusterCandidate and creates a
  285. // Cluster for a specific project
  286. func (app *App) HandleResolveClusterCandidate(w http.ResponseWriter, r *http.Request) {
  287. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  288. if err != nil || projID == 0 {
  289. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  290. return
  291. }
  292. candID, err := strconv.ParseUint(chi.URLParam(r, "candidate_id"), 0, 64)
  293. if err != nil || projID == 0 {
  294. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  295. return
  296. }
  297. session, err := app.Store.Get(r, app.ServerConf.CookieName)
  298. if err != nil {
  299. http.Error(w, err.Error(), http.StatusInternalServerError)
  300. return
  301. }
  302. userID, _ := session.Values["user_id"].(uint)
  303. // decode actions from request
  304. resolver := &models.ClusterResolverAll{}
  305. if err := json.NewDecoder(r.Body).Decode(resolver); err != nil {
  306. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  307. return
  308. }
  309. clusterResolver := &forms.ResolveClusterForm{
  310. Resolver: resolver,
  311. ClusterCandidateID: uint(candID),
  312. ProjectID: uint(projID),
  313. UserID: userID,
  314. }
  315. err = clusterResolver.ResolveIntegration(*app.Repo)
  316. if err != nil {
  317. app.handleErrorDataWrite(err, w)
  318. return
  319. }
  320. cluster, err := clusterResolver.ResolveCluster(*app.Repo)
  321. if err != nil {
  322. app.handleErrorDataWrite(err, w)
  323. return
  324. }
  325. _, err = app.Repo.Cluster.UpdateClusterCandidateCreatedClusterID(uint(candID), cluster.ID)
  326. if err != nil {
  327. app.handleErrorDataWrite(err, w)
  328. return
  329. }
  330. app.Logger.Info().Msgf("New cluster created: %d", cluster.ID)
  331. clusterExt := cluster.Externalize()
  332. w.WriteHeader(http.StatusCreated)
  333. if err := json.NewEncoder(w).Encode(clusterExt); err != nil {
  334. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  335. return
  336. }
  337. }