apply.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package v2
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "errors"
  6. "fmt"
  7. "os"
  8. "path/filepath"
  9. "github.com/fatih/color"
  10. "github.com/porter-dev/api-contracts/generated/go/helpers"
  11. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  12. api "github.com/porter-dev/porter/api/client"
  13. "github.com/porter-dev/porter/cli/cmd/config"
  14. )
  15. // Apply implements the functionality of the `porter apply` command for validate apply v2 projects
  16. func Apply(ctx context.Context, cliConf config.CLIConfig, client api.Client, porterYamlPath string) error {
  17. if len(porterYamlPath) == 0 {
  18. return fmt.Errorf("porter yaml is empty")
  19. }
  20. porterYaml, err := os.ReadFile(filepath.Clean(porterYamlPath))
  21. if err != nil {
  22. return fmt.Errorf("could not read porter yaml file: %w", err)
  23. }
  24. b64YAML := base64.StdEncoding.EncodeToString(porterYaml)
  25. parseResp, err := client.ParseYAML(ctx, cliConf.Project, cliConf.Cluster, b64YAML)
  26. if err != nil {
  27. return fmt.Errorf("error calling parse yaml endpoint: %w", err)
  28. }
  29. if parseResp.B64AppProto == "" {
  30. return errors.New("b64 app proto is empty")
  31. }
  32. targetResp, err := client.DefaultDeploymentTarget(ctx, cliConf.Project, cliConf.Cluster)
  33. if err != nil {
  34. return fmt.Errorf("error calling default deployment target endpoint: %w", err)
  35. }
  36. if targetResp.DeploymentTargetID == "" {
  37. return errors.New("deployment target id is empty")
  38. }
  39. validateResp, err := client.ValidatePorterApp(ctx, cliConf.Project, cliConf.Cluster, parseResp.B64AppProto, targetResp.DeploymentTargetID)
  40. if err != nil {
  41. return fmt.Errorf("error calling validate endpoint: %w", err)
  42. }
  43. if validateResp.ValidatedBase64AppProto == "" {
  44. return errors.New("validated b64 app proto is empty")
  45. }
  46. base64AppProto := validateResp.ValidatedBase64AppProto
  47. base64AppProtoWithSubdomains, err := addPorterSubdomainsIfNecessary(ctx, client, cliConf.Project, cliConf.Cluster, base64AppProto)
  48. if err != nil {
  49. return fmt.Errorf("error creating subdomains: %w", err)
  50. }
  51. applyResp, err := client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, base64AppProtoWithSubdomains, targetResp.DeploymentTargetID, "")
  52. if err != nil {
  53. return fmt.Errorf("error calling apply endpoint: %w", err)
  54. }
  55. if applyResp.AppRevisionId == "" {
  56. return errors.New("app revision id is empty")
  57. }
  58. if applyResp.CLIAction == porterv1.EnumCLIAction_ENUM_CLI_ACTION_BUILD {
  59. buildSettings, err := buildSettingsFromBase64AppProto(base64AppProto)
  60. if err != nil {
  61. return fmt.Errorf("error building settings from base64 app proto: %w", err)
  62. }
  63. currentAppRevisionResp, err := client.CurrentAppRevision(ctx, cliConf.Project, cliConf.Cluster, buildSettings.AppName, targetResp.DeploymentTargetID)
  64. if err != nil {
  65. return fmt.Errorf("error getting current app revision: %w", err)
  66. }
  67. if currentAppRevisionResp == nil {
  68. return errors.New("current app revision is nil")
  69. }
  70. appRevision := currentAppRevisionResp.AppRevision
  71. if appRevision.B64AppProto == "" {
  72. return errors.New("current app revision b64 app proto is empty")
  73. }
  74. currentImageTag, err := imageTagFromBase64AppProto(appRevision.B64AppProto)
  75. if err != nil {
  76. return fmt.Errorf("error getting image tag from current app revision: %w", err)
  77. }
  78. buildSettings.CurrentImageTag = currentImageTag
  79. buildSettings.ProjectID = cliConf.Project
  80. err = build(ctx, client, buildSettings)
  81. if err != nil {
  82. return fmt.Errorf("error building app: %w", err)
  83. }
  84. applyResp, err = client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, "", "", applyResp.AppRevisionId)
  85. if err != nil {
  86. return fmt.Errorf("error calling apply endpoint after build: %w", err)
  87. }
  88. }
  89. if applyResp.CLIAction != porterv1.EnumCLIAction_ENUM_CLI_ACTION_NONE {
  90. return fmt.Errorf("unexpected CLI action: %s", applyResp.CLIAction)
  91. }
  92. color.New(color.FgGreen).Printf("Successfully applied Porter YAML as revision %v, next action: %v\n", applyResp.AppRevisionId, applyResp.CLIAction) // nolint:errcheck,gosec
  93. return nil
  94. }
  95. func addPorterSubdomainsIfNecessary(ctx context.Context, client api.Client, project uint, cluster uint, base64AppProto string) (string, error) {
  96. var editedB64AppProto string
  97. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  98. if err != nil {
  99. return editedB64AppProto, fmt.Errorf("unable to decode base64 app for revision: %w", err)
  100. }
  101. app := &porterv1.PorterApp{}
  102. err = helpers.UnmarshalContractObject(decoded, app)
  103. if err != nil {
  104. return editedB64AppProto, fmt.Errorf("unable to unmarshal app for revision: %w", err)
  105. }
  106. for serviceName, service := range app.Services {
  107. if service.Type == porterv1.ServiceType_SERVICE_TYPE_WEB {
  108. if service.GetWebConfig() == nil {
  109. return editedB64AppProto, fmt.Errorf("web service %s does not contain web config", serviceName)
  110. }
  111. webConfig := service.GetWebConfig()
  112. if !webConfig.Private && len(webConfig.Domains) == 0 {
  113. color.New(color.FgYellow).Printf("Service %s is public but does not contain any domains, creating Porter domain\n", serviceName) // nolint:errcheck,gosec
  114. domain, err := client.CreateSubdomain(ctx, project, cluster, app.Name, serviceName)
  115. if err != nil {
  116. return editedB64AppProto, fmt.Errorf("error creating subdomain: %w", err)
  117. }
  118. if domain.Subdomain == "" {
  119. return editedB64AppProto, errors.New("response subdomain is empty")
  120. }
  121. webConfig.Domains = []*porterv1.Domain{
  122. {Name: domain.Subdomain},
  123. }
  124. service.Config = &porterv1.Service_WebConfig{WebConfig: webConfig}
  125. }
  126. }
  127. }
  128. marshalled, err := helpers.MarshalContractObject(ctx, app)
  129. if err != nil {
  130. return editedB64AppProto, fmt.Errorf("unable to marshal app back to json: %w", err)
  131. }
  132. editedB64AppProto = base64.StdEncoding.EncodeToString(marshalled)
  133. return editedB64AppProto, nil
  134. }
  135. func buildSettingsFromBase64AppProto(base64AppProto string) (buildInput, error) {
  136. var buildSettings buildInput
  137. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  138. if err != nil {
  139. return buildSettings, fmt.Errorf("unable to decode base64 app for revision: %w", err)
  140. }
  141. app := &porterv1.PorterApp{}
  142. err = helpers.UnmarshalContractObject(decoded, app)
  143. if err != nil {
  144. return buildSettings, fmt.Errorf("unable to unmarshal app for revision: %w", err)
  145. }
  146. if app.Name == "" {
  147. return buildSettings, fmt.Errorf("app does not contain name")
  148. }
  149. if app.Build == nil {
  150. return buildSettings, fmt.Errorf("app does not contain build settings")
  151. }
  152. if app.Image == nil {
  153. return buildSettings, fmt.Errorf("app does not contain image settings")
  154. }
  155. return buildInput{
  156. AppName: app.Name,
  157. BuildContext: app.Build.Context,
  158. Dockerfile: app.Build.Dockerfile,
  159. BuildMethod: app.Build.Method,
  160. Builder: app.Build.Builder,
  161. BuildPacks: app.Build.Buildpacks,
  162. ImageTag: app.Image.Tag,
  163. RepositoryURL: app.Image.Repository,
  164. }, nil
  165. }
  166. func imageTagFromBase64AppProto(base64AppProto string) (string, error) {
  167. var image string
  168. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  169. if err != nil {
  170. return image, fmt.Errorf("unable to decode base64 app for revision: %w", err)
  171. }
  172. app := &porterv1.PorterApp{}
  173. err = helpers.UnmarshalContractObject(decoded, app)
  174. if err != nil {
  175. return image, fmt.Errorf("unable to unmarshal app for revision: %w", err)
  176. }
  177. if app.Image == nil {
  178. return image, fmt.Errorf("app does not contain image settings")
  179. }
  180. if app.Image.Tag == "" {
  181. return image, fmt.Errorf("app does not contain image tag")
  182. }
  183. return app.Image.Tag, nil
  184. }