cluster_handler.go 11 KB

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