update_app.go 14 KB

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