apply.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. package v2
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "errors"
  6. "fmt"
  7. "os"
  8. "path/filepath"
  9. "strconv"
  10. "time"
  11. "github.com/porter-dev/porter/api/server/handlers/porter_app"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/internal/kubernetes/environment_groups"
  14. "github.com/porter-dev/porter/internal/models"
  15. "github.com/cli/cli/git"
  16. "github.com/fatih/color"
  17. "github.com/porter-dev/api-contracts/generated/go/helpers"
  18. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  19. api "github.com/porter-dev/porter/api/client"
  20. "github.com/porter-dev/porter/cli/cmd/config"
  21. )
  22. // Apply implements the functionality of the `porter apply` command for validate apply v2 projects
  23. func Apply(ctx context.Context, cliConf config.CLIConfig, client api.Client, porterYamlPath string, appName string) error {
  24. const forceBuild = true
  25. var b64AppProto string
  26. targetResp, err := client.DefaultDeploymentTarget(ctx, cliConf.Project, cliConf.Cluster)
  27. if err != nil {
  28. return fmt.Errorf("error calling default deployment target endpoint: %w", err)
  29. }
  30. if targetResp.DeploymentTargetID == "" {
  31. return errors.New("deployment target id is empty")
  32. }
  33. if len(porterYamlPath) != 0 {
  34. porterYaml, err := os.ReadFile(filepath.Clean(porterYamlPath))
  35. if err != nil {
  36. return fmt.Errorf("could not read porter yaml file: %w", err)
  37. }
  38. b64YAML := base64.StdEncoding.EncodeToString(porterYaml)
  39. // last argument is passed to accommodate users with v1 porter yamls
  40. parseResp, err := client.ParseYAML(ctx, cliConf.Project, cliConf.Cluster, b64YAML, appName)
  41. if err != nil {
  42. return fmt.Errorf("error calling parse yaml endpoint: %w", err)
  43. }
  44. if parseResp.B64AppProto == "" {
  45. return errors.New("b64 app proto is empty")
  46. }
  47. b64AppProto = parseResp.B64AppProto
  48. // we only need to create the app if a porter yaml is provided (otherwise it must already exist)
  49. createPorterAppDBEntryInp, err := createPorterAppDbEntryInputFromProtoAndEnv(parseResp.B64AppProto)
  50. if err != nil {
  51. return fmt.Errorf("error creating porter app db entry input from proto: %w", err)
  52. }
  53. err = client.CreatePorterAppDBEntry(ctx, cliConf.Project, cliConf.Cluster, createPorterAppDBEntryInp)
  54. if err != nil {
  55. return fmt.Errorf("error creating porter app db entry: %w", err)
  56. }
  57. // override app name if provided
  58. appName, err = appNameFromB64AppProto(parseResp.B64AppProto)
  59. if err != nil {
  60. return fmt.Errorf("error getting app name from b64 app proto: %w", err)
  61. }
  62. envGroupResp, err := client.CreateOrUpdateAppEnvironment(ctx, cliConf.Project, cliConf.Cluster, appName, targetResp.DeploymentTargetID, parseResp.EnvVariables, parseResp.EnvSecrets, parseResp.B64AppProto)
  63. if err != nil {
  64. return fmt.Errorf("error calling create or update app environment group endpoint: %w", err)
  65. }
  66. b64AppProto, err = updateEnvGroupsInProto(ctx, b64AppProto, envGroupResp.EnvGroups)
  67. if err != nil {
  68. return fmt.Errorf("error updating app env group in proto: %w", err)
  69. }
  70. color.New(color.FgGreen).Printf("Successfully parsed Porter YAML: applying app \"%s\"\n", appName) // nolint:errcheck,gosec
  71. }
  72. var commitSHA string
  73. if os.Getenv("PORTER_COMMIT_SHA") != "" {
  74. commitSHA = os.Getenv("PORTER_COMMIT_SHA")
  75. } else if os.Getenv("GITHUB_SHA") != "" {
  76. commitSHA = os.Getenv("GITHUB_SHA")
  77. } else if commit, err := git.LastCommit(); err == nil && commit != nil {
  78. commitSHA = commit.Sha
  79. }
  80. validateResp, err := client.ValidatePorterApp(ctx, cliConf.Project, cliConf.Cluster, appName, b64AppProto, targetResp.DeploymentTargetID, commitSHA)
  81. if err != nil {
  82. return fmt.Errorf("error calling validate endpoint: %w", err)
  83. }
  84. if validateResp.ValidatedBase64AppProto == "" {
  85. return errors.New("validated b64 app proto is empty")
  86. }
  87. base64AppProto := validateResp.ValidatedBase64AppProto
  88. applyResp, err := client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, base64AppProto, targetResp.DeploymentTargetID, "", forceBuild)
  89. if err != nil {
  90. return fmt.Errorf("error calling apply endpoint: %w", err)
  91. }
  92. if applyResp.AppRevisionId == "" {
  93. return errors.New("app revision id is empty")
  94. }
  95. if applyResp.CLIAction == porterv1.EnumCLIAction_ENUM_CLI_ACTION_BUILD {
  96. color.New(color.FgGreen).Printf("Building new image...\n") // nolint:errcheck,gosec
  97. eventID, _ := createBuildEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID)
  98. if commitSHA == "" {
  99. return errors.New("Build is required but commit SHA cannot be identified. Please set the PORTER_COMMIT_SHA environment variable or run apply in git repository with access to the git CLI.")
  100. }
  101. buildSettings, err := buildSettingsFromBase64AppProto(base64AppProto)
  102. if err != nil {
  103. return fmt.Errorf("error building settings from base64 app proto: %w", err)
  104. }
  105. currentAppRevisionResp, err := client.CurrentAppRevision(ctx, cliConf.Project, cliConf.Cluster, appName, targetResp.DeploymentTargetID)
  106. if err != nil {
  107. return fmt.Errorf("error getting current app revision: %w", err)
  108. }
  109. if currentAppRevisionResp == nil {
  110. return errors.New("current app revision is nil")
  111. }
  112. appRevision := currentAppRevisionResp.AppRevision
  113. if appRevision.B64AppProto == "" {
  114. return errors.New("current app revision b64 app proto is empty")
  115. }
  116. currentImageTag, err := imageTagFromBase64AppProto(appRevision.B64AppProto)
  117. if err != nil {
  118. return fmt.Errorf("error getting image tag from current app revision: %w", err)
  119. }
  120. buildSettings.CurrentImageTag = currentImageTag
  121. buildSettings.ProjectID = cliConf.Project
  122. buildEnv, err := client.GetBuildEnv(ctx, cliConf.Project, cliConf.Cluster, appName, targetResp.DeploymentTargetID)
  123. if err != nil {
  124. return fmt.Errorf("error getting build env: %w", err)
  125. }
  126. buildSettings.Env = buildEnv.BuildEnvVariables
  127. err = build(ctx, client, buildSettings)
  128. buildMetadata := make(map[string]interface{})
  129. buildMetadata["end_time"] = time.Now().UTC()
  130. if err != nil {
  131. _ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID, eventID, types.PorterAppEventStatus_Failed, buildMetadata)
  132. _, _ = client.UpdateRevisionStatus(ctx, cliConf.Project, cliConf.Cluster, appName, applyResp.AppRevisionId, models.AppRevisionStatus_BuildFailed)
  133. return fmt.Errorf("error building app: %w", err)
  134. }
  135. color.New(color.FgGreen).Printf("Successfully built image (tag: %s)\n", buildSettings.ImageTag) // nolint:errcheck,gosec
  136. _ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID, eventID, types.PorterAppEventStatus_Success, buildMetadata)
  137. applyResp, err = client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, "", "", applyResp.AppRevisionId, !forceBuild)
  138. if err != nil {
  139. return fmt.Errorf("apply error post-build: %w", err)
  140. }
  141. }
  142. color.New(color.FgGreen).Printf("Image tag exists in repository\n") // nolint:errcheck,gosec
  143. if applyResp.CLIAction == porterv1.EnumCLIAction_ENUM_CLI_ACTION_TRACK_PREDEPLOY {
  144. color.New(color.FgGreen).Printf("Waiting for predeploy to complete...\n") // nolint:errcheck,gosec
  145. now := time.Now().UTC()
  146. eventID, _ := createPredeployEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID, now, applyResp.AppRevisionId)
  147. eventStatus := types.PorterAppEventStatus_Success
  148. for {
  149. if time.Since(now) > checkPredeployTimeout {
  150. return errors.New("timed out waiting for predeploy to complete")
  151. }
  152. predeployStatusResp, err := client.PredeployStatus(ctx, cliConf.Project, cliConf.Cluster, appName, applyResp.AppRevisionId)
  153. if err != nil {
  154. return fmt.Errorf("error calling predeploy status endpoint: %w", err)
  155. }
  156. if predeployStatusResp.Status == porter_app.PredeployStatus_Failed {
  157. eventStatus = types.PorterAppEventStatus_Failed
  158. break
  159. }
  160. if predeployStatusResp.Status == porter_app.PredeployStatus_Successful {
  161. break
  162. }
  163. time.Sleep(checkPredeployFrequency)
  164. }
  165. metadata := make(map[string]interface{})
  166. metadata["end_time"] = time.Now().UTC()
  167. _ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID, eventID, eventStatus, metadata)
  168. applyResp, err = client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, "", "", applyResp.AppRevisionId, !forceBuild)
  169. if err != nil {
  170. return fmt.Errorf("apply error post-predeploy: %w", err)
  171. }
  172. }
  173. if applyResp.CLIAction != porterv1.EnumCLIAction_ENUM_CLI_ACTION_NONE {
  174. return fmt.Errorf("unexpected CLI action: %s", applyResp.CLIAction)
  175. }
  176. color.New(color.FgGreen).Printf("Successfully applied new revision %s for app %s\n", applyResp.AppRevisionId, appName) // nolint:errcheck,gosec
  177. return nil
  178. }
  179. // checkPredeployTimeout is the maximum amount of time the CLI will wait for a predeploy to complete before calling apply again
  180. const checkPredeployTimeout = 60 * time.Minute
  181. // checkPredeployFrequency is the frequency at which the CLI will check the status of a predeploy
  182. const checkPredeployFrequency = 10 * time.Second
  183. func appNameFromB64AppProto(base64AppProto string) (string, error) {
  184. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  185. if err != nil {
  186. return "", fmt.Errorf("unable to decode base64 app for revision: %w", err)
  187. }
  188. app := &porterv1.PorterApp{}
  189. err = helpers.UnmarshalContractObject(decoded, app)
  190. if err != nil {
  191. return "", fmt.Errorf("unable to unmarshal app for revision: %w", err)
  192. }
  193. if app.Name == "" {
  194. return "", fmt.Errorf("app does not contain name")
  195. }
  196. return app.Name, nil
  197. }
  198. func createPorterAppDbEntryInputFromProtoAndEnv(base64AppProto string) (api.CreatePorterAppDBEntryInput, error) {
  199. var input api.CreatePorterAppDBEntryInput
  200. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  201. if err != nil {
  202. return input, fmt.Errorf("unable to decode base64 app for revision: %w", err)
  203. }
  204. app := &porterv1.PorterApp{}
  205. err = helpers.UnmarshalContractObject(decoded, app)
  206. if err != nil {
  207. return input, fmt.Errorf("unable to unmarshal app for revision: %w", err)
  208. }
  209. if app.Name == "" {
  210. return input, fmt.Errorf("app does not contain name")
  211. }
  212. input.AppName = app.Name
  213. if app.Build != nil {
  214. if os.Getenv("GITHUB_REPOSITORY_ID") == "" {
  215. input.Local = true
  216. return input, nil
  217. }
  218. gitRepoId, err := strconv.Atoi(os.Getenv("GITHUB_REPOSITORY_ID"))
  219. if err != nil {
  220. return input, fmt.Errorf("unable to parse GITHUB_REPOSITORY_ID to int: %w", err)
  221. }
  222. input.GitRepoID = uint(gitRepoId)
  223. input.GitRepoName = os.Getenv("GITHUB_REPOSITORY")
  224. input.GitBranch = os.Getenv("GITHUB_REF_NAME")
  225. input.PorterYamlPath = "porter.yaml"
  226. return input, nil
  227. }
  228. if app.Image != nil {
  229. input.ImageRepository = app.Image.Repository
  230. input.ImageTag = app.Image.Tag
  231. return input, nil
  232. }
  233. return input, fmt.Errorf("app does not contain build or image settings")
  234. }
  235. func buildSettingsFromBase64AppProto(base64AppProto string) (buildInput, error) {
  236. var buildSettings buildInput
  237. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  238. if err != nil {
  239. return buildSettings, fmt.Errorf("unable to decode base64 app for revision: %w", err)
  240. }
  241. app := &porterv1.PorterApp{}
  242. err = helpers.UnmarshalContractObject(decoded, app)
  243. if err != nil {
  244. return buildSettings, fmt.Errorf("unable to unmarshal app for revision: %w", err)
  245. }
  246. if app.Name == "" {
  247. return buildSettings, fmt.Errorf("app does not contain name")
  248. }
  249. if app.Build == nil {
  250. return buildSettings, fmt.Errorf("app does not contain build settings")
  251. }
  252. if app.Image == nil {
  253. return buildSettings, fmt.Errorf("app does not contain image settings")
  254. }
  255. return buildInput{
  256. AppName: app.Name,
  257. BuildContext: app.Build.Context,
  258. Dockerfile: app.Build.Dockerfile,
  259. BuildMethod: app.Build.Method,
  260. Builder: app.Build.Builder,
  261. BuildPacks: app.Build.Buildpacks,
  262. ImageTag: app.Image.Tag,
  263. RepositoryURL: app.Image.Repository,
  264. }, nil
  265. }
  266. func imageTagFromBase64AppProto(base64AppProto string) (string, error) {
  267. var image string
  268. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  269. if err != nil {
  270. return image, fmt.Errorf("unable to decode base64 app for revision: %w", err)
  271. }
  272. app := &porterv1.PorterApp{}
  273. err = helpers.UnmarshalContractObject(decoded, app)
  274. if err != nil {
  275. return image, fmt.Errorf("unable to unmarshal app for revision: %w", err)
  276. }
  277. if app.Image == nil {
  278. return image, fmt.Errorf("app does not contain image settings")
  279. }
  280. if app.Image.Tag == "" {
  281. return image, fmt.Errorf("app does not contain image tag")
  282. }
  283. return app.Image.Tag, nil
  284. }
  285. func updateEnvGroupsInProto(ctx context.Context, base64AppProto string, envGroups []environment_groups.EnvironmentGroup) (string, error) {
  286. var editedB64AppProto string
  287. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  288. if err != nil {
  289. return editedB64AppProto, fmt.Errorf("unable to decode base64 app for revision: %w", err)
  290. }
  291. app := &porterv1.PorterApp{}
  292. err = helpers.UnmarshalContractObject(decoded, app)
  293. if err != nil {
  294. return editedB64AppProto, fmt.Errorf("unable to unmarshal app for revision: %w", err)
  295. }
  296. egs := make([]*porterv1.EnvGroup, 0)
  297. for _, envGroup := range envGroups {
  298. egs = append(egs, &porterv1.EnvGroup{
  299. Name: envGroup.Name,
  300. Version: int64(envGroup.Version),
  301. })
  302. }
  303. app.EnvGroups = egs
  304. marshalled, err := helpers.MarshalContractObject(ctx, app)
  305. if err != nil {
  306. return editedB64AppProto, fmt.Errorf("unable to marshal app back to json: %w", err)
  307. }
  308. editedB64AppProto = base64.StdEncoding.EncodeToString(marshalled)
  309. return editedB64AppProto, nil
  310. }