apply.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package porter_app
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "errors"
  6. "fmt"
  7. "net/http"
  8. "connectrpc.com/connect"
  9. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  10. "github.com/porter-dev/api-contracts/generated/go/helpers"
  11. "github.com/porter-dev/porter/internal/deployment_target"
  12. "github.com/porter-dev/porter/internal/porter_app"
  13. v2 "github.com/porter-dev/porter/internal/porter_app/v2"
  14. "github.com/porter-dev/porter/internal/telemetry"
  15. "github.com/porter-dev/porter/api/server/authz"
  16. "github.com/porter-dev/porter/api/server/handlers"
  17. "github.com/porter-dev/porter/api/server/shared"
  18. "github.com/porter-dev/porter/api/server/shared/apierrors"
  19. "github.com/porter-dev/porter/api/server/shared/config"
  20. "github.com/porter-dev/porter/api/types"
  21. "github.com/porter-dev/porter/internal/models"
  22. )
  23. // ApplyPorterAppHandler is the handler for the /apps/parse endpoint
  24. type ApplyPorterAppHandler struct {
  25. handlers.PorterHandlerReadWriter
  26. authz.KubernetesAgentGetter
  27. }
  28. // NewApplyPorterAppHandler handles POST requests to the endpoint /apps/apply
  29. func NewApplyPorterAppHandler(
  30. config *config.Config,
  31. decoderValidator shared.RequestDecoderValidator,
  32. writer shared.ResultWriter,
  33. ) *ApplyPorterAppHandler {
  34. return &ApplyPorterAppHandler{
  35. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  36. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  37. }
  38. }
  39. // ApplyPorterAppRequest is the request object for the /apps/apply endpoint
  40. type ApplyPorterAppRequest struct {
  41. Base64AppProto string `json:"b64_app_proto"`
  42. DeploymentTargetId string `json:"deployment_target_id"`
  43. AppRevisionID string `json:"app_revision_id"`
  44. ForceBuild bool `json:"force_build"`
  45. Variables map[string]string `json:"variables"`
  46. Secrets map[string]string `json:"secrets"`
  47. // HardEnvUpdate 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,
  48. // and leave all other variables untouched.
  49. HardEnvUpdate bool `json:"hard_env_update"`
  50. }
  51. // ApplyPorterAppResponse is the response object for the /apps/apply endpoint
  52. type ApplyPorterAppResponse struct {
  53. AppRevisionId string `json:"app_revision_id"`
  54. CLIAction porterv1.EnumCLIAction `json:"cli_action"`
  55. }
  56. // ServeHTTP translates the request into a ApplyPorterApp request, forwards to the cluster control plane, and returns the response
  57. func (c *ApplyPorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  58. ctx, span := telemetry.NewSpan(r.Context(), "serve-apply-porter-app")
  59. defer span.End()
  60. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  61. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  62. telemetry.WithAttributes(span,
  63. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  64. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  65. )
  66. if !project.GetFeatureFlag(models.ValidateApplyV2, c.Config().LaunchDarklyClient) {
  67. err := telemetry.Error(ctx, span, nil, "project does not have validate apply v2 enabled")
  68. c.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
  69. return
  70. }
  71. request := &ApplyPorterAppRequest{}
  72. if ok := c.DecodeAndValidate(w, r, request); !ok {
  73. err := telemetry.Error(ctx, span, nil, "error decoding request")
  74. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  75. return
  76. }
  77. var appRevisionID string
  78. var appProto *porterv1.PorterApp
  79. var deploymentTargetID string
  80. if request.AppRevisionID != "" {
  81. appRevisionID = request.AppRevisionID
  82. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-revision-id", Value: request.AppRevisionID})
  83. } else {
  84. if request.Base64AppProto == "" {
  85. err := telemetry.Error(ctx, span, nil, "b64 yaml is empty")
  86. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  87. return
  88. }
  89. decoded, err := base64.StdEncoding.DecodeString(request.Base64AppProto)
  90. if err != nil {
  91. err := telemetry.Error(ctx, span, err, "error decoding base yaml")
  92. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  93. return
  94. }
  95. appProto = &porterv1.PorterApp{}
  96. err = helpers.UnmarshalContractObject(decoded, appProto)
  97. if err != nil {
  98. err := telemetry.Error(ctx, span, err, "error unmarshalling app proto")
  99. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  100. return
  101. }
  102. app, err := v2.AppFromProto(appProto)
  103. if err != nil {
  104. err := telemetry.Error(ctx, span, err, "error converting app proto to app")
  105. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  106. return
  107. }
  108. if request.DeploymentTargetId == "" {
  109. err := telemetry.Error(ctx, span, err, "deployment target id is empty")
  110. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  111. return
  112. }
  113. deploymentTargetID = request.DeploymentTargetId
  114. telemetry.WithAttributes(span,
  115. telemetry.AttributeKV{Key: "app-name", Value: appProto.Name},
  116. telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetId},
  117. )
  118. deploymentTargetDetails, err := deployment_target.DeploymentTargetDetails(ctx, deployment_target.DeploymentTargetDetailsInput{
  119. ProjectID: int64(project.ID),
  120. ClusterID: int64(cluster.ID),
  121. DeploymentTargetID: deploymentTargetID,
  122. CCPClient: c.Config().ClusterControlPlaneClient,
  123. })
  124. if err != nil {
  125. err := telemetry.Error(ctx, span, err, "error getting deployment target details")
  126. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  127. return
  128. }
  129. agent, err := c.GetAgent(r, cluster, "")
  130. if err != nil {
  131. err := telemetry.Error(ctx, span, err, "error getting kubernetes agent")
  132. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  133. return
  134. }
  135. subdomainCreateInput := porter_app.CreatePorterSubdomainInput{
  136. AppName: app.Name,
  137. RootDomain: c.Config().ServerConf.AppRootDomain,
  138. DNSClient: c.Config().DNSClient,
  139. DNSRecordRepository: c.Repo().DNSRecord(),
  140. KubernetesAgent: agent,
  141. }
  142. appWithDomains, err := addPorterSubdomainsIfNecessary(ctx, app, deploymentTargetDetails, subdomainCreateInput)
  143. if err != nil {
  144. err := telemetry.Error(ctx, span, err, "error adding porter subdomains")
  145. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  146. return
  147. }
  148. appProto, _, err = v2.ProtoFromApp(ctx, appWithDomains)
  149. if err != nil {
  150. err := telemetry.Error(ctx, span, err, "error converting app to proto")
  151. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  152. return
  153. }
  154. }
  155. applyReq := connect.NewRequest(&porterv1.ApplyPorterAppRequest{
  156. ProjectId: int64(project.ID),
  157. DeploymentTargetId: deploymentTargetID,
  158. App: appProto,
  159. PorterAppRevisionId: appRevisionID,
  160. ForceBuild: request.ForceBuild,
  161. AppEnv: &porterv1.EnvGroupVariables{
  162. Normal: request.Variables,
  163. Secret: request.Secrets,
  164. },
  165. IsHardEnvUpdate: request.HardEnvUpdate,
  166. })
  167. ccpResp, err := c.Config().ClusterControlPlaneClient.ApplyPorterApp(ctx, applyReq)
  168. if err != nil {
  169. err := telemetry.Error(ctx, span, err, "error calling ccp apply porter app")
  170. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  171. return
  172. }
  173. if ccpResp == nil {
  174. err := telemetry.Error(ctx, span, err, "ccp resp is nil")
  175. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  176. return
  177. }
  178. if ccpResp.Msg == nil {
  179. err := telemetry.Error(ctx, span, err, "ccp resp msg is nil")
  180. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  181. return
  182. }
  183. if ccpResp.Msg.PorterAppRevisionId == "" {
  184. err := telemetry.Error(ctx, span, err, "ccp resp app revision id is nil")
  185. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  186. return
  187. }
  188. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "resp-app-revision-id", Value: ccpResp.Msg.PorterAppRevisionId})
  189. if ccpResp.Msg.CliAction == porterv1.EnumCLIAction_ENUM_CLI_ACTION_UNSPECIFIED {
  190. err := telemetry.Error(ctx, span, err, "ccp resp cli action is nil")
  191. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  192. return
  193. }
  194. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "cli-action", Value: ccpResp.Msg.CliAction.String()})
  195. response := &ApplyPorterAppResponse{
  196. AppRevisionId: ccpResp.Msg.PorterAppRevisionId,
  197. CLIAction: ccpResp.Msg.CliAction,
  198. }
  199. c.WriteResult(w, r, response)
  200. }
  201. // addPorterSubdomainsIfNecessary adds porter subdomains to the app proto if a web service is changed to private and has no domains
  202. func addPorterSubdomainsIfNecessary(ctx context.Context, app v2.PorterApp, deploymentTarget deployment_target.DeploymentTarget, createSubdomainInput porter_app.CreatePorterSubdomainInput) (v2.PorterApp, error) {
  203. ctx, span := telemetry.NewSpan(ctx, "add-porter-subdomains-if-necessary")
  204. defer span.End()
  205. services := make([]v2.Service, 0)
  206. for _, service := range app.Services {
  207. if service.Type == v2.ServiceType_Web {
  208. if service.Private != nil && !*service.Private && service.Domains != nil && len(service.Domains) == 0 {
  209. if deploymentTarget.Namespace != DeploymentTargetSelector_Default {
  210. createSubdomainInput.AppName = fmt.Sprintf("%s-%s", createSubdomainInput.AppName, deploymentTarget.ID[:6])
  211. }
  212. subdomain, err := porter_app.CreatePorterSubdomain(ctx, createSubdomainInput)
  213. if err != nil {
  214. return app, fmt.Errorf("error creating subdomain: %w", err)
  215. }
  216. if subdomain == "" {
  217. return app, errors.New("response subdomain is empty")
  218. }
  219. service.Domains = []v2.Domains{
  220. {Name: subdomain},
  221. }
  222. }
  223. }
  224. services = append(services, service)
  225. }
  226. app.Services = services
  227. return app, nil
  228. }