create.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. package release
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "github.com/porter-dev/porter/api/server/authz"
  8. "github.com/porter-dev/porter/api/server/handlers"
  9. "github.com/porter-dev/porter/api/server/shared"
  10. "github.com/porter-dev/porter/api/server/shared/apierrors"
  11. "github.com/porter-dev/porter/api/server/shared/config"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/internal/analytics"
  14. "github.com/porter-dev/porter/internal/auth/token"
  15. "github.com/porter-dev/porter/internal/encryption"
  16. "github.com/porter-dev/porter/internal/helm"
  17. "github.com/porter-dev/porter/internal/helm/loader"
  18. "github.com/porter-dev/porter/internal/integrations/ci/actions"
  19. "github.com/porter-dev/porter/internal/models"
  20. "github.com/porter-dev/porter/internal/oauth"
  21. "github.com/porter-dev/porter/internal/registry"
  22. "gopkg.in/yaml.v2"
  23. "helm.sh/helm/v3/pkg/release"
  24. v1 "k8s.io/api/core/v1"
  25. )
  26. type CreateReleaseHandler struct {
  27. handlers.PorterHandlerReadWriter
  28. authz.KubernetesAgentGetter
  29. }
  30. func NewCreateReleaseHandler(
  31. config *config.Config,
  32. decoderValidator shared.RequestDecoderValidator,
  33. writer shared.ResultWriter,
  34. ) *CreateReleaseHandler {
  35. return &CreateReleaseHandler{
  36. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  37. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  38. }
  39. }
  40. func (c *CreateReleaseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  41. user, _ := r.Context().Value(types.UserScope).(*models.User)
  42. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  43. namespace := r.Context().Value(types.NamespaceScope).(string)
  44. operationID := oauth.CreateRandomState()
  45. c.Config().AnalyticsClient.Track(analytics.ApplicationLaunchStartTrack(
  46. &analytics.ApplicationLaunchStartTrackOpts{
  47. ClusterScopedTrackOpts: analytics.GetClusterScopedTrackOpts(user.ID, cluster.ProjectID, cluster.ID),
  48. FlowID: operationID,
  49. },
  50. ))
  51. helmAgent, err := c.GetHelmAgent(r, cluster, "")
  52. if err != nil {
  53. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  54. return
  55. }
  56. request := &types.CreateReleaseRequest{}
  57. if ok := c.DecodeAndValidate(w, r, request); !ok {
  58. return
  59. }
  60. if request.RepoURL == "" {
  61. request.RepoURL = c.Config().ServerConf.DefaultApplicationHelmRepoURL
  62. }
  63. if request.TemplateVersion == "latest" {
  64. request.TemplateVersion = ""
  65. }
  66. chart, err := loader.LoadChartPublic(request.RepoURL, request.TemplateName, request.TemplateVersion)
  67. if err != nil {
  68. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  69. return
  70. }
  71. registries, err := c.Repo().Registry().ListRegistriesByProjectID(cluster.ProjectID)
  72. if err != nil {
  73. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  74. return
  75. }
  76. conf := &helm.InstallChartConfig{
  77. Chart: chart,
  78. Name: request.Name,
  79. Namespace: namespace,
  80. Values: request.Values,
  81. Cluster: cluster,
  82. Repo: c.Repo(),
  83. Registries: registries,
  84. }
  85. helmRelease, err := helmAgent.InstallChart(conf, c.Config().DOConf)
  86. if err != nil {
  87. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  88. fmt.Errorf("error installing a new chart: %s", err.Error()),
  89. http.StatusBadRequest,
  90. ))
  91. return
  92. }
  93. k8sAgent, err := c.GetAgent(r, cluster, "")
  94. if err != nil {
  95. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  96. return
  97. }
  98. configMaps := make([]*v1.ConfigMap, 0)
  99. if request.SyncedEnvGroups != nil && len(request.SyncedEnvGroups) > 0 {
  100. for _, envGroupName := range request.SyncedEnvGroups {
  101. // read the attached configmap
  102. cm, _, err := k8sAgent.GetLatestVersionedConfigMap(envGroupName, namespace)
  103. if err != nil {
  104. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("Couldn't find the env group"), http.StatusNotFound))
  105. return
  106. }
  107. configMaps = append(configMaps, cm)
  108. }
  109. }
  110. release, err := createReleaseFromHelmRelease(c.Config(), cluster.ProjectID, cluster.ID, helmRelease)
  111. if err != nil {
  112. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  113. return
  114. }
  115. if len(configMaps) > 0 {
  116. for _, cm := range configMaps {
  117. _, err = k8sAgent.AddApplicationToVersionedConfigMap(cm, release.Name)
  118. if err != nil {
  119. c.HandleAPIErrorNoWrite(w, r, apierrors.NewErrInternal(fmt.Errorf("Couldn't add %s to the config map %s", release.Name, cm.Name)))
  120. }
  121. }
  122. }
  123. if request.Tags != nil {
  124. tags, err := c.Repo().Tag().LinkTagsToRelease(request.Tags, release)
  125. if err == nil {
  126. release.Tags = append(release.Tags, tags...)
  127. }
  128. }
  129. if request.GithubActionConfig != nil {
  130. _, _, err := createGitAction(
  131. c.Config(),
  132. user.ID,
  133. cluster.ProjectID,
  134. cluster.ID,
  135. request.GithubActionConfig,
  136. request.Name,
  137. namespace,
  138. release,
  139. )
  140. if err != nil {
  141. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  142. return
  143. }
  144. }
  145. if request.BuildConfig != nil {
  146. _, err = createBuildConfig(c.Config(), release, request.BuildConfig)
  147. }
  148. if err != nil {
  149. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  150. return
  151. }
  152. c.Config().AnalyticsClient.Track(analytics.ApplicationLaunchSuccessTrack(
  153. &analytics.ApplicationLaunchSuccessTrackOpts{
  154. ApplicationScopedTrackOpts: analytics.GetApplicationScopedTrackOpts(
  155. user.ID,
  156. cluster.ProjectID,
  157. cluster.ID,
  158. release.Name,
  159. release.Namespace,
  160. chart.Metadata.Name,
  161. ),
  162. FlowID: operationID,
  163. },
  164. ))
  165. }
  166. func createReleaseFromHelmRelease(
  167. config *config.Config,
  168. projectID, clusterID uint,
  169. helmRelease *release.Release,
  170. ) (*models.Release, error) {
  171. token, err := encryption.GenerateRandomBytes(16)
  172. if err != nil {
  173. return nil, err
  174. }
  175. // create release with webhook token in db
  176. image, ok := helmRelease.Config["image"].(map[string]interface{})
  177. if !ok {
  178. return nil, fmt.Errorf("Could not find field image in config")
  179. }
  180. repository := image["repository"]
  181. repoStr, ok := repository.(string)
  182. if !ok {
  183. return nil, fmt.Errorf("Could not find field repository in config")
  184. }
  185. release := &models.Release{
  186. ClusterID: clusterID,
  187. ProjectID: projectID,
  188. Namespace: helmRelease.Namespace,
  189. Name: helmRelease.Name,
  190. WebhookToken: token,
  191. ImageRepoURI: repoStr,
  192. }
  193. return config.Repo.Release().CreateRelease(release)
  194. }
  195. func createGitAction(
  196. config *config.Config,
  197. userID, projectID, clusterID uint,
  198. request *types.CreateGitActionConfigRequest,
  199. name, namespace string,
  200. release *models.Release,
  201. ) (*types.GitActionConfig, []byte, error) {
  202. // if the registry was provisioned through Porter, create a repository if necessary
  203. if release != nil && request.RegistryID != 0 {
  204. // read the registry
  205. reg, err := config.Repo.Registry().ReadRegistry(projectID, request.RegistryID)
  206. if err != nil {
  207. return nil, nil, err
  208. }
  209. _reg := registry.Registry(*reg)
  210. regAPI := &_reg
  211. // parse the name from the registry
  212. nameSpl := strings.Split(request.ImageRepoURI, "/")
  213. repoName := nameSpl[len(nameSpl)-1]
  214. err = regAPI.CreateRepository(config.Repo, repoName)
  215. if err != nil {
  216. return nil, nil, err
  217. }
  218. }
  219. repoSplit := strings.Split(request.GitRepo, "/")
  220. if len(repoSplit) != 2 {
  221. return nil, nil, fmt.Errorf("invalid formatting of repo name")
  222. }
  223. // generate porter jwt token
  224. jwt, err := token.GetTokenForAPI(userID, projectID)
  225. if err != nil {
  226. return nil, nil, err
  227. }
  228. encoded, err := jwt.EncodeToken(config.TokenConf)
  229. if err != nil {
  230. return nil, nil, err
  231. }
  232. // create the commit in the git repo
  233. gaRunner := &actions.GithubActions{
  234. InstanceName: config.ServerConf.InstanceName,
  235. ServerURL: config.ServerConf.ServerURL,
  236. GithubOAuthIntegration: nil,
  237. GithubAppID: config.GithubAppConf.AppID,
  238. GithubAppSecretPath: config.GithubAppConf.SecretPath,
  239. GithubInstallationID: request.GitRepoID,
  240. GitRepoName: repoSplit[1],
  241. GitRepoOwner: repoSplit[0],
  242. Repo: config.Repo,
  243. ProjectID: projectID,
  244. ClusterID: clusterID,
  245. ReleaseName: name,
  246. ReleaseNamespace: namespace,
  247. GitBranch: request.GitBranch,
  248. DockerFilePath: request.DockerfilePath,
  249. FolderPath: request.FolderPath,
  250. ImageRepoURL: request.ImageRepoURI,
  251. PorterToken: encoded,
  252. Version: "v0.1.0",
  253. ShouldCreateWorkflow: request.ShouldCreateWorkflow,
  254. DryRun: release == nil,
  255. }
  256. // Save the github err for after creating the git action config. However, we
  257. // need to call Setup() in order to get the workflow file before writing the
  258. // action config, in the case of a dry run, since the dry run does not create
  259. // a git action config.
  260. workflowYAML, githubErr := gaRunner.Setup()
  261. if gaRunner.DryRun {
  262. if githubErr != nil {
  263. return nil, nil, githubErr
  264. }
  265. return nil, workflowYAML, nil
  266. }
  267. // handle write to the database
  268. ga, err := config.Repo.GitActionConfig().CreateGitActionConfig(&models.GitActionConfig{
  269. ReleaseID: release.ID,
  270. GitRepo: request.GitRepo,
  271. GitBranch: request.GitBranch,
  272. ImageRepoURI: request.ImageRepoURI,
  273. GitRepoID: request.GitRepoID,
  274. DockerfilePath: request.DockerfilePath,
  275. FolderPath: request.FolderPath,
  276. IsInstallation: true,
  277. Version: "v0.1.0",
  278. })
  279. if err != nil {
  280. return nil, nil, err
  281. }
  282. // update the release in the db with the image repo uri
  283. release.ImageRepoURI = ga.ImageRepoURI
  284. _, err = config.Repo.Release().UpdateRelease(release)
  285. if err != nil {
  286. return nil, nil, err
  287. }
  288. if githubErr != nil {
  289. return nil, nil, githubErr
  290. }
  291. return ga.ToGitActionConfigType(), workflowYAML, nil
  292. }
  293. func createBuildConfig(
  294. config *config.Config,
  295. release *models.Release,
  296. bcRequest *types.CreateBuildConfigRequest,
  297. ) (*types.BuildConfig, error) {
  298. data, err := json.Marshal(bcRequest.Config)
  299. if err != nil {
  300. return nil, err
  301. }
  302. // handle write to the database
  303. bc, err := config.Repo.BuildConfig().CreateBuildConfig(&models.BuildConfig{
  304. Builder: bcRequest.Builder,
  305. Buildpacks: strings.Join(bcRequest.Buildpacks, ","),
  306. Config: data,
  307. })
  308. if err != nil {
  309. return nil, err
  310. }
  311. release.BuildConfig = bc.ID
  312. _, err = config.Repo.Release().UpdateRelease(release)
  313. if err != nil {
  314. return nil, err
  315. }
  316. return bc.ToBuildConfigType(), nil
  317. }
  318. type containerEnvConfig struct {
  319. Container struct {
  320. Env struct {
  321. Normal map[string]string `yaml:"normal"`
  322. } `yaml:"env"`
  323. } `yaml:"container"`
  324. }
  325. func getGARunner(
  326. config *config.Config,
  327. userID, projectID, clusterID uint,
  328. ga *models.GitActionConfig,
  329. name, namespace string,
  330. release *models.Release,
  331. helmRelease *release.Release,
  332. ) (*actions.GithubActions, error) {
  333. cEnv := &containerEnvConfig{}
  334. rawValues, err := yaml.Marshal(helmRelease.Config)
  335. if err == nil {
  336. err = yaml.Unmarshal(rawValues, cEnv)
  337. // if unmarshal error, just set to empty map
  338. if err != nil {
  339. cEnv.Container.Env.Normal = make(map[string]string)
  340. }
  341. }
  342. repoSplit := strings.Split(ga.GitRepo, "/")
  343. if len(repoSplit) != 2 {
  344. return nil, fmt.Errorf("invalid formatting of repo name")
  345. }
  346. // create the commit in the git repo
  347. return &actions.GithubActions{
  348. ServerURL: config.ServerConf.ServerURL,
  349. GithubOAuthIntegration: nil,
  350. BuildEnv: cEnv.Container.Env.Normal,
  351. GithubAppID: config.GithubAppConf.AppID,
  352. GithubAppSecretPath: config.GithubAppConf.SecretPath,
  353. GithubInstallationID: ga.GitRepoID,
  354. GitRepoName: repoSplit[1],
  355. GitRepoOwner: repoSplit[0],
  356. Repo: config.Repo,
  357. ProjectID: projectID,
  358. ClusterID: clusterID,
  359. ReleaseName: name,
  360. GitBranch: ga.GitBranch,
  361. DockerFilePath: ga.DockerfilePath,
  362. FolderPath: ga.FolderPath,
  363. ImageRepoURL: ga.ImageRepoURI,
  364. Version: "v0.1.0",
  365. }, nil
  366. }