update_app.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. // Exact is a flag to indicate whether to apply the update exactly as specified in the request (default is to merge with existing app)
  70. Exact bool `json:"exact"`
  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. var previewEnvVariables map[string]string
  113. envVariables := request.Variables
  114. // get app definition from either base64 yaml or base64 porter app proto
  115. if request.Base64AppProto != "" {
  116. decoded, err := base64.StdEncoding.DecodeString(request.Base64AppProto)
  117. if err != nil {
  118. err := telemetry.Error(ctx, span, err, "error decoding base yaml")
  119. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  120. return
  121. }
  122. err = helpers.UnmarshalContractObject(decoded, appProto)
  123. if err != nil {
  124. err := telemetry.Error(ctx, span, err, "error unmarshalling app proto")
  125. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  126. return
  127. }
  128. }
  129. for _, b64AddonProto := range request.Base64AddonProtos {
  130. decoded, err := base64.StdEncoding.DecodeString(b64AddonProto)
  131. if err != nil {
  132. err := telemetry.Error(ctx, span, err, "error decoding base yaml")
  133. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  134. return
  135. }
  136. addon := &porterv1.Addon{}
  137. err = helpers.UnmarshalContractObject(decoded, addon)
  138. if err != nil {
  139. err := telemetry.Error(ctx, span, err, "error unmarshalling addon proto")
  140. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  141. return
  142. }
  143. addons = append(addons, addon)
  144. }
  145. if request.Base64PorterYAML != "" {
  146. decoded, err := base64.StdEncoding.DecodeString(request.Base64PorterYAML)
  147. if err != nil {
  148. err := telemetry.Error(ctx, span, err, "error decoding base yaml")
  149. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  150. return
  151. }
  152. appFromYaml, err := porter_app.ParseYAML(ctx, decoded, request.Name)
  153. if err != nil {
  154. err := telemetry.Error(ctx, span, err, "error parsing yaml")
  155. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  156. return
  157. }
  158. appProto = appFromYaml.AppProto
  159. // only public variables can be defined in porter.yaml
  160. envVariables = mergeEnvVariables(request.Variables, appFromYaml.EnvVariables)
  161. if appFromYaml.PreviewApp != nil {
  162. overrides = appFromYaml.PreviewApp.AppProto
  163. addonOverrides = appFromYaml.PreviewApp.Addons
  164. previewEnvVariables = appFromYaml.PreviewApp.EnvVariables
  165. }
  166. addons = appFromYaml.Addons
  167. }
  168. if appProto.Name == "" {
  169. if request.Name == "" {
  170. err := telemetry.Error(ctx, span, nil, "app name is empty")
  171. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  172. return
  173. }
  174. appProto.Name = request.Name
  175. }
  176. sourceType, image, err := sourceFromAppAndGitSource(ctx, appProto, request.GitSource)
  177. if err != nil {
  178. err := telemetry.Error(ctx, span, err, "error getting source from app and git source")
  179. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  180. return
  181. }
  182. // create porter app if it doesn't exist for the given name
  183. _, err = porter_app.CreateOrGetAppRecord(ctx, porter_app.CreateOrGetAppRecordInput{
  184. ClusterID: cluster.ID,
  185. ProjectID: project.ID,
  186. Name: appProto.Name,
  187. SourceType: sourceType,
  188. GitBranch: request.GitSource.GitBranch,
  189. GitRepoName: request.GitSource.GitRepoName,
  190. GitRepoID: request.GitSource.GitRepoID,
  191. PorterYamlPath: request.PorterYAMLPath,
  192. Image: image,
  193. PorterAppRepository: c.Repo().PorterApp(),
  194. })
  195. if err != nil {
  196. err := telemetry.Error(ctx, span, err, "error creating or getting porter app")
  197. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  198. return
  199. }
  200. var serviceDeletions map[string]*porterv1.ServiceDeletions
  201. if request.Deletions.ServiceDeletions != nil {
  202. serviceDeletions = make(map[string]*porterv1.ServiceDeletions)
  203. for k, v := range request.Deletions.ServiceDeletions {
  204. serviceDeletions[k] = &porterv1.ServiceDeletions{
  205. DomainNames: v.DomainNames,
  206. IngressAnnotations: v.IngressAnnotationKeys,
  207. }
  208. }
  209. }
  210. if request.ImageTagOverride != "" {
  211. if appProto.Image == nil {
  212. appProto.Image = &porterv1.AppImage{}
  213. }
  214. appProto.Image.Tag = request.ImageTagOverride
  215. }
  216. updateReq := connect.NewRequest(&porterv1.UpdateAppRequest{
  217. ProjectId: int64(project.ID),
  218. DeploymentTargetIdentifier: &porterv1.DeploymentTargetIdentifier{
  219. Id: deploymentTargetID,
  220. },
  221. App: appProto,
  222. AppRevisionId: request.AppRevisionID,
  223. AppEnv: &porterv1.EnvGroupVariables{
  224. Normal: envVariables,
  225. Secret: request.Secrets,
  226. },
  227. AppEnvOverrides: &porterv1.EnvGroupVariables{
  228. Normal: previewEnvVariables,
  229. },
  230. Deletions: &porterv1.Deletions{
  231. ServiceNames: request.Deletions.ServiceNames,
  232. PredeployNames: request.Deletions.Predeploy,
  233. EnvVariableNames: request.Deletions.EnvVariableNames,
  234. EnvGroupNames: request.Deletions.EnvGroupNames,
  235. ServiceDeletions: serviceDeletions,
  236. },
  237. AppOverrides: overrides,
  238. CommitSha: request.CommitSHA,
  239. IsEnvOverride: request.IsEnvOverride,
  240. Addons: addons,
  241. AddonOverrides: addonOverrides,
  242. IsPredeployEligible: request.WithPredeploy,
  243. Exact: request.Exact,
  244. })
  245. ccpResp, err := c.Config().ClusterControlPlaneClient.UpdateApp(ctx, updateReq)
  246. if err != nil {
  247. err := telemetry.Error(ctx, span, err, "error calling ccp update app")
  248. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  249. return
  250. }
  251. if ccpResp == nil {
  252. err := telemetry.Error(ctx, span, err, "ccp resp is nil")
  253. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  254. return
  255. }
  256. if ccpResp.Msg == nil {
  257. err := telemetry.Error(ctx, span, err, "ccp resp msg is nil")
  258. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  259. return
  260. }
  261. if ccpResp.Msg.AppRevisionId == "" {
  262. err := telemetry.Error(ctx, span, err, "ccp resp app revision id is empty")
  263. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  264. return
  265. }
  266. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "resp-app-revision-id", Value: ccpResp.Msg.AppRevisionId})
  267. response := &UpdateAppResponse{
  268. AppRevisionId: ccpResp.Msg.AppRevisionId,
  269. AppName: appProto.Name,
  270. }
  271. c.WriteResult(w, r, response)
  272. }
  273. func sourceFromAppAndGitSource(ctx context.Context, appProto *porterv1.PorterApp, gitSource GitSource) (porter_app.SourceType, *porter_app.Image, error) {
  274. ctx, span := telemetry.NewSpan(ctx, "source-from-app-and-git-source")
  275. defer span.End()
  276. var sourceType porter_app.SourceType
  277. var image *porter_app.Image
  278. if appProto == nil {
  279. return sourceType, image, telemetry.Error(ctx, span, nil, "app proto is nil")
  280. }
  281. telemetry.WithAttributes(span,
  282. telemetry.AttributeKV{Key: "app-name", Value: appProto.Name},
  283. telemetry.AttributeKV{Key: "git-repo-id", Value: gitSource.GitRepoID},
  284. telemetry.AttributeKV{Key: "has-build", Value: appProto.Build != nil},
  285. telemetry.AttributeKV{Key: "has-image", Value: appProto.Image != nil},
  286. )
  287. if appProto.Build != nil {
  288. if gitSource.GitRepoID == 0 {
  289. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "source-type", Value: porter_app.SourceType_Local})
  290. return porter_app.SourceType_Local, image, nil
  291. }
  292. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "source-type", Value: porter_app.SourceType_Github})
  293. return porter_app.SourceType_Github, image, nil
  294. }
  295. if appProto.Image != nil {
  296. sourceType = porter_app.SourceType_DockerRegistry
  297. image = &porter_app.Image{
  298. Repository: appProto.Image.Repository,
  299. Tag: appProto.Image.Tag,
  300. }
  301. }
  302. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "source-type", Value: sourceType})
  303. return sourceType, image, nil
  304. }
  305. func mergeEnvVariables(currentEnv, previousEnv map[string]string) map[string]string {
  306. env := make(map[string]string)
  307. for k, v := range previousEnv {
  308. env[k] = v
  309. }
  310. for k, v := range currentEnv {
  311. env[k] = v
  312. }
  313. return env
  314. }