create.go 12 KB

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