update_app.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. package porter_app
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "net/http"
  6. "connectrpc.com/connect"
  7. "github.com/porter-dev/api-contracts/generated/go/helpers"
  8. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  9. "github.com/porter-dev/porter/api/server/authz"
  10. "github.com/porter-dev/porter/api/server/handlers"
  11. "github.com/porter-dev/porter/api/server/shared"
  12. "github.com/porter-dev/porter/api/server/shared/apierrors"
  13. "github.com/porter-dev/porter/api/server/shared/config"
  14. "github.com/porter-dev/porter/api/types"
  15. "github.com/porter-dev/porter/internal/models"
  16. "github.com/porter-dev/porter/internal/porter_app"
  17. "github.com/porter-dev/porter/internal/telemetry"
  18. )
  19. // UpdateAppHandler is the handler for the POST /apps/update endpoint
  20. type UpdateAppHandler struct {
  21. handlers.PorterHandlerReadWriter
  22. authz.KubernetesAgentGetter
  23. }
  24. // NewUpdateAppHandler handles POST requests to the endpoint POST /apps/update
  25. func NewUpdateAppHandler(
  26. config *config.Config,
  27. decoderValidator shared.RequestDecoderValidator,
  28. writer shared.ResultWriter,
  29. ) *UpdateAppHandler {
  30. return &UpdateAppHandler{
  31. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  32. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  33. }
  34. }
  35. // UpdateAppRequest is the request object for the POST /apps/update endpoint
  36. type UpdateAppRequest struct {
  37. // Name is the name of the app to update. If not specified, the name will be inferred from the porter yaml
  38. Name string `json:"name"`
  39. // GitSource is the git source configuration for the app, if applicable
  40. GitSource GitSource `json:"git_source,omitempty"`
  41. // DeploymentTargetId is the ID of the deployment target to apply the update to
  42. DeploymentTargetId string `json:"deployment_target_id"`
  43. // Variables is a map of environment variable names to values
  44. Variables map[string]string `json:"variables"`
  45. // Secrets is a map of secret names to values
  46. Secrets map[string]string `json:"secrets"`
  47. // Deletions is the set of fields to delete before applying the update
  48. Deletions Deletions `json:"deletions"`
  49. // CommitSHA is the commit sha of the git commit that triggered this update, indicating a source change and triggering a build
  50. CommitSHA string `json:"commit_sha"`
  51. // ImageTagOverride is the image tag to override the image tag in the porter.yaml (it will override the image tag in the porter.yaml if specified)
  52. ImageTagOverride string `json:"image_tag_override"`
  53. // PorterYAMLPath is the path to the porter yaml file in the git repo
  54. PorterYAMLPath string `json:"porter_yaml_path"`
  55. // AppRevisionID is the ID of the revision to perform follow up actions on after the initial apply
  56. AppRevisionID string `json:"app_revision_id"`
  57. // Only one of Base64AppProto or Base64PorterYAML should be specified
  58. // Base64AppProto is a ful base64 encoded porter app contract to apply
  59. Base64AppProto string `json:"b64_app_proto"`
  60. // Base64AddonProtos is a list of base64 encoded addon contracts to apply along with the app
  61. Base64AddonProtos []string `json:"b64_addon_protos"`
  62. // Base64PorterYAML is a base64 encoded porter yaml to apply representing a potentially partial porter app contract
  63. Base64PorterYAML string `json:"b64_porter_yaml"`
  64. // IsEnvOverride is used to remove any variables that are not specified in the request. If false, the request will only update the variables specified in the request,
  65. // and leave all other variables untouched.
  66. IsEnvOverride bool `json:"is_env_override"`
  67. // WithPredeploy is a flag to indicate whether to run the predeploy job
  68. WithPredeploy bool `json:"with_predeploy"`
  69. // Description is a user-generated description for the update
  70. Base64Description string `json:"base64_description"`
  71. }
  72. // UpdateAppResponse is the response object for the POST /apps/update endpoint
  73. type UpdateAppResponse struct {
  74. AppName string `json:"app_name"`
  75. AppRevisionId string `json:"app_revision_id"`
  76. }
  77. // ServeHTTP translates the request into an UpdateApp request, forwards to the cluster control plane, and returns the response
  78. func (c *UpdateAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  79. ctx, span := telemetry.NewSpan(r.Context(), "serve-update-app")
  80. defer span.End()
  81. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  82. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  83. request := &UpdateAppRequest{}
  84. if ok := c.DecodeAndValidate(w, r, request); !ok {
  85. err := telemetry.Error(ctx, span, nil, "error decoding request")
  86. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  87. return
  88. }
  89. if request.Base64AppProto != "" && request.Base64PorterYAML != "" {
  90. err := telemetry.Error(ctx, span, nil, "both b64 yaml and b64 porter yaml are specified")
  91. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  92. return
  93. }
  94. if request.DeploymentTargetId == "" {
  95. err := telemetry.Error(ctx, span, nil, "deployment target id is empty")
  96. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  97. return
  98. }
  99. deploymentTargetID := request.DeploymentTargetId
  100. telemetry.WithAttributes(span,
  101. telemetry.AttributeKV{Key: "name", Value: request.Name},
  102. telemetry.AttributeKV{Key: "deployment-target-id", Value: deploymentTargetID},
  103. telemetry.AttributeKV{Key: "app-revision-id", Value: request.AppRevisionID},
  104. telemetry.AttributeKV{Key: "commit-sha", Value: request.CommitSHA},
  105. telemetry.AttributeKV{Key: "porter-yaml-path", Value: request.PorterYAMLPath},
  106. telemetry.AttributeKV{Key: "is-env-override", Value: request.IsEnvOverride},
  107. telemetry.AttributeKV{Key: "with-predeploy", Value: request.WithPredeploy},
  108. )
  109. var addons, addonOverrides []*porterv1.Addon
  110. var overrides *porterv1.PorterApp
  111. appProto := &porterv1.PorterApp{}
  112. envVariables := request.Variables
  113. // get app definition from either base64 yaml or base64 porter app proto
  114. if request.Base64AppProto != "" {
  115. decoded, err := base64.StdEncoding.DecodeString(request.Base64AppProto)
  116. if err != nil {
  117. err := telemetry.Error(ctx, span, err, "error decoding base yaml")
  118. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  119. return
  120. }
  121. err = helpers.UnmarshalContractObject(decoded, appProto)
  122. if err != nil {
  123. err := telemetry.Error(ctx, span, err, "error unmarshalling app proto")
  124. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  125. return
  126. }
  127. }
  128. for _, b64AddonProto := range request.Base64AddonProtos {
  129. decoded, err := base64.StdEncoding.DecodeString(b64AddonProto)
  130. if err != nil {
  131. err := telemetry.Error(ctx, span, err, "error decoding base yaml")
  132. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  133. return
  134. }
  135. addon := &porterv1.Addon{}
  136. err = helpers.UnmarshalContractObject(decoded, addon)
  137. if err != nil {
  138. err := telemetry.Error(ctx, span, err, "error unmarshalling addon proto")
  139. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  140. return
  141. }
  142. addons = append(addons, addon)
  143. }
  144. if request.Base64PorterYAML != "" {
  145. decoded, err := base64.StdEncoding.DecodeString(request.Base64PorterYAML)
  146. if err != nil {
  147. err := telemetry.Error(ctx, span, err, "error decoding base yaml")
  148. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  149. return
  150. }
  151. appFromYaml, err := porter_app.ParseYAML(ctx, decoded, request.Name)
  152. if err != nil {
  153. err := telemetry.Error(ctx, span, err, "error parsing yaml")
  154. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  155. return
  156. }
  157. appProto = appFromYaml.AppProto
  158. // only public variables can be defined in porter.yaml
  159. envVariables = mergeEnvVariables(request.Variables, appFromYaml.EnvVariables)
  160. if appFromYaml.PreviewApp != nil {
  161. overrides = appFromYaml.PreviewApp.AppProto
  162. addonOverrides = appFromYaml.PreviewApp.Addons
  163. envVariables = mergeEnvVariables(envVariables, appFromYaml.PreviewApp.EnvVariables)
  164. }
  165. addons = appFromYaml.Addons
  166. }
  167. if appProto.Name == "" {
  168. if request.Name == "" {
  169. err := telemetry.Error(ctx, span, nil, "app name is empty")
  170. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  171. return
  172. }
  173. appProto.Name = request.Name
  174. }
  175. sourceType, image, err := sourceFromAppAndGitSource(ctx, appProto, request.GitSource)
  176. if err != nil {
  177. err := telemetry.Error(ctx, span, err, "error getting source from app and git source")
  178. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  179. return
  180. }
  181. // create porter app if it doesn't exist for the given name
  182. _, err = porter_app.CreateOrGetAppRecord(ctx, porter_app.CreateOrGetAppRecordInput{
  183. ClusterID: cluster.ID,
  184. ProjectID: project.ID,
  185. Name: appProto.Name,
  186. SourceType: sourceType,
  187. GitBranch: request.GitSource.GitBranch,
  188. GitRepoName: request.GitSource.GitRepoName,
  189. GitRepoID: request.GitSource.GitRepoID,
  190. PorterYamlPath: request.PorterYAMLPath,
  191. Image: image,
  192. PorterAppRepository: c.Repo().PorterApp(),
  193. })
  194. if err != nil {
  195. err := telemetry.Error(ctx, span, err, "error creating or getting porter app")
  196. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  197. return
  198. }
  199. var serviceDeletions map[string]*porterv1.ServiceDeletions
  200. if request.Deletions.ServiceDeletions != nil {
  201. serviceDeletions = make(map[string]*porterv1.ServiceDeletions)
  202. for k, v := range request.Deletions.ServiceDeletions {
  203. serviceDeletions[k] = &porterv1.ServiceDeletions{
  204. DomainNames: v.DomainNames,
  205. IngressAnnotations: v.IngressAnnotationKeys,
  206. }
  207. }
  208. }
  209. if request.ImageTagOverride != "" {
  210. if appProto.Image == nil {
  211. appProto.Image = &porterv1.AppImage{}
  212. }
  213. appProto.Image.Tag = request.ImageTagOverride
  214. }
  215. updateReq := connect.NewRequest(&porterv1.UpdateAppRequest{
  216. ProjectId: int64(project.ID),
  217. DeploymentTargetIdentifier: &porterv1.DeploymentTargetIdentifier{
  218. Id: deploymentTargetID,
  219. },
  220. App: appProto,
  221. AppRevisionId: request.AppRevisionID,
  222. AppEnv: &porterv1.EnvGroupVariables{
  223. Normal: envVariables,
  224. Secret: request.Secrets,
  225. },
  226. Deletions: &porterv1.Deletions{
  227. ServiceNames: request.Deletions.ServiceNames,
  228. PredeployNames: request.Deletions.Predeploy,
  229. EnvVariableNames: request.Deletions.EnvVariableNames,
  230. EnvGroupNames: request.Deletions.EnvGroupNames,
  231. ServiceDeletions: serviceDeletions,
  232. },
  233. AppOverrides: overrides,
  234. CommitSha: request.CommitSHA,
  235. IsEnvOverride: request.IsEnvOverride,
  236. Addons: addons,
  237. AddonOverrides: addonOverrides,
  238. IsPredeployEligible: request.WithPredeploy,
  239. B64Description: request.Base64Description,
  240. })
  241. ccpResp, err := c.Config().ClusterControlPlaneClient.UpdateApp(ctx, updateReq)
  242. if err != nil {
  243. err := telemetry.Error(ctx, span, err, "error calling ccp update app")
  244. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  245. return
  246. }
  247. if ccpResp == nil {
  248. err := telemetry.Error(ctx, span, err, "ccp resp is nil")
  249. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  250. return
  251. }
  252. if ccpResp.Msg == nil {
  253. err := telemetry.Error(ctx, span, err, "ccp resp msg is nil")
  254. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  255. return
  256. }
  257. if ccpResp.Msg.AppRevisionId == "" {
  258. err := telemetry.Error(ctx, span, err, "ccp resp app revision id is empty")
  259. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  260. return
  261. }
  262. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "resp-app-revision-id", Value: ccpResp.Msg.AppRevisionId})
  263. response := &UpdateAppResponse{
  264. AppRevisionId: ccpResp.Msg.AppRevisionId,
  265. AppName: appProto.Name,
  266. }
  267. c.WriteResult(w, r, response)
  268. }
  269. func sourceFromAppAndGitSource(ctx context.Context, appProto *porterv1.PorterApp, gitSource GitSource) (porter_app.SourceType, *porter_app.Image, error) {
  270. ctx, span := telemetry.NewSpan(ctx, "source-from-app-and-git-source")
  271. defer span.End()
  272. var sourceType porter_app.SourceType
  273. var image *porter_app.Image
  274. if appProto == nil {
  275. return sourceType, image, telemetry.Error(ctx, span, nil, "app proto is nil")
  276. }
  277. telemetry.WithAttributes(span,
  278. telemetry.AttributeKV{Key: "app-name", Value: appProto.Name},
  279. telemetry.AttributeKV{Key: "git-repo-id", Value: gitSource.GitRepoID},
  280. telemetry.AttributeKV{Key: "has-build", Value: appProto.Build != nil},
  281. telemetry.AttributeKV{Key: "has-image", Value: appProto.Image != nil},
  282. )
  283. if appProto.Build != nil {
  284. if gitSource.GitRepoID == 0 {
  285. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "source-type", Value: porter_app.SourceType_Local})
  286. return porter_app.SourceType_Local, image, nil
  287. }
  288. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "source-type", Value: porter_app.SourceType_Github})
  289. return porter_app.SourceType_Github, image, nil
  290. }
  291. if appProto.Image != nil {
  292. sourceType = porter_app.SourceType_DockerRegistry
  293. image = &porter_app.Image{
  294. Repository: appProto.Image.Repository,
  295. Tag: appProto.Image.Tag,
  296. }
  297. }
  298. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "source-type", Value: sourceType})
  299. return sourceType, image, nil
  300. }
  301. func mergeEnvVariables(currentEnv, previousEnv map[string]string) map[string]string {
  302. env := make(map[string]string)
  303. for k, v := range previousEnv {
  304. env[k] = v
  305. }
  306. for k, v := range currentEnv {
  307. env[k] = v
  308. }
  309. return env
  310. }