2
0

update_app.go 13 KB

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