update_app.go 14 KB

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