apply.go 9.9 KB

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