revisions.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package porter_app
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "fmt"
  6. "time"
  7. "connectrpc.com/connect"
  8. "github.com/google/uuid"
  9. "github.com/porter-dev/api-contracts/generated/go/helpers"
  10. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  11. "github.com/porter-dev/api-contracts/generated/go/porter/v1/porterv1connect"
  12. "github.com/porter-dev/porter/internal/deployment_target"
  13. "github.com/porter-dev/porter/internal/kubernetes"
  14. "github.com/porter-dev/porter/internal/kubernetes/environment_groups"
  15. "github.com/porter-dev/porter/internal/models"
  16. "github.com/porter-dev/porter/internal/repository"
  17. "github.com/porter-dev/porter/internal/telemetry"
  18. )
  19. // Revision represents the data for a single revision
  20. type Revision struct {
  21. // ID is the revision id
  22. ID string `json:"id"`
  23. // B64AppProto is the base64 encoded app proto definition
  24. B64AppProto string `json:"b64_app_proto"`
  25. // Status is the status of the revision
  26. Status models.AppRevisionStatus `json:"status"`
  27. // RevisionNumber is the revision number with respect to the app and deployment target
  28. RevisionNumber uint64 `json:"revision_number"`
  29. // CreatedAt is the time the revision was created
  30. CreatedAt time.Time `json:"created_at"`
  31. // UpdatedAt is the time the revision was updated
  32. UpdatedAt time.Time `json:"updated_at"`
  33. // DeploymentTargetID is the id of the deployment target the revision is associated with
  34. DeploymentTargetID string `json:"deployment_target_id"`
  35. // Env is the environment variables for the revision
  36. Env environment_groups.EnvironmentGroup `json:"env,omitempty"`
  37. }
  38. // GetAppRevisionInput is the input struct for GetAppRevisions
  39. type GetAppRevisionInput struct {
  40. ProjectID uint
  41. AppRevisionID uuid.UUID
  42. CCPClient porterv1connect.ClusterControlPlaneServiceClient
  43. }
  44. // GetAppRevision returns a single app revision by id
  45. func GetAppRevision(ctx context.Context, inp GetAppRevisionInput) (Revision, error) {
  46. ctx, span := telemetry.NewSpan(ctx, "get-app-revision")
  47. defer span.End()
  48. var revision Revision
  49. if inp.ProjectID == 0 {
  50. return revision, telemetry.Error(ctx, span, nil, "must provide a project id")
  51. }
  52. if inp.AppRevisionID == uuid.Nil {
  53. return revision, telemetry.Error(ctx, span, nil, "must provide an app revision id")
  54. }
  55. getRevisionReq := connect.NewRequest(&porterv1.GetAppRevisionRequest{
  56. ProjectId: int64(inp.ProjectID),
  57. AppRevisionId: inp.AppRevisionID.String(),
  58. })
  59. ccpResp, err := inp.CCPClient.GetAppRevision(ctx, getRevisionReq)
  60. if err != nil {
  61. return revision, telemetry.Error(ctx, span, err, "error getting app revision")
  62. }
  63. if ccpResp == nil || ccpResp.Msg == nil {
  64. return revision, telemetry.Error(ctx, span, nil, "get app revision response is nil")
  65. }
  66. appRevisionProto := ccpResp.Msg.AppRevision
  67. revision, err = EncodedRevisionFromProto(ctx, appRevisionProto)
  68. if err != nil {
  69. return revision, telemetry.Error(ctx, span, err, "error converting app revision from proto")
  70. }
  71. return revision, nil
  72. }
  73. // EncodedRevisionFromProto converts an AppRevision proto object into a Revision object
  74. func EncodedRevisionFromProto(ctx context.Context, appRevision *porterv1.AppRevision) (Revision, error) {
  75. ctx, span := telemetry.NewSpan(ctx, "encoded-revision-from-proto")
  76. defer span.End()
  77. var revision Revision
  78. if appRevision == nil {
  79. return revision, telemetry.Error(ctx, span, nil, "current app revision definition is nil")
  80. }
  81. appProto := appRevision.App
  82. if appProto == nil {
  83. return revision, telemetry.Error(ctx, span, nil, "app proto is nil")
  84. }
  85. encoded, err := helpers.MarshalContractObject(ctx, appProto)
  86. if err != nil {
  87. return revision, telemetry.Error(ctx, span, err, "error marshalling app proto back to json")
  88. }
  89. b64 := base64.StdEncoding.EncodeToString(encoded)
  90. status, err := appRevisionStatusFromProto(appRevision.Status)
  91. if err != nil {
  92. return revision, telemetry.Error(ctx, span, err, "error getting app revision status from proto")
  93. }
  94. revision = Revision{
  95. B64AppProto: b64,
  96. Status: status,
  97. ID: appRevision.Id,
  98. RevisionNumber: appRevision.RevisionNumber,
  99. CreatedAt: appRevision.CreatedAt.AsTime(),
  100. UpdatedAt: appRevision.UpdatedAt.AsTime(),
  101. DeploymentTargetID: appRevision.DeploymentTargetId,
  102. }
  103. return revision, nil
  104. }
  105. // AttachEnvToRevisionInput is the input struct for AttachEnvToRevision
  106. type AttachEnvToRevisionInput struct {
  107. ProjectID uint
  108. ClusterID int
  109. Revision Revision
  110. DeploymentTarget deployment_target.DeploymentTarget
  111. K8SAgent *kubernetes.Agent
  112. PorterAppRepository repository.PorterAppRepository
  113. }
  114. // AttachEnvToRevision attaches the environment variables from the app's default env group to a revision
  115. // These are the variables that are displayed to the user in the UI as associated with the app rather than an env group
  116. func AttachEnvToRevision(ctx context.Context, inp AttachEnvToRevisionInput) (Revision, error) {
  117. ctx, span := telemetry.NewSpan(ctx, "attach-env-to-revision")
  118. defer span.End()
  119. revision := inp.Revision
  120. if inp.ProjectID == 0 {
  121. return revision, telemetry.Error(ctx, span, nil, "must provide a project id")
  122. }
  123. if inp.ClusterID == 0 {
  124. return revision, telemetry.Error(ctx, span, nil, "must provide a cluster id")
  125. }
  126. if inp.K8SAgent == nil {
  127. return revision, telemetry.Error(ctx, span, nil, "k8s agent is nil")
  128. }
  129. decoded, err := base64.StdEncoding.DecodeString(revision.B64AppProto)
  130. if err != nil {
  131. return revision, telemetry.Error(ctx, span, err, "error decoding app proto")
  132. }
  133. appDef := &porterv1.PorterApp{}
  134. err = helpers.UnmarshalContractObject(decoded, appDef)
  135. if err != nil {
  136. return revision, telemetry.Error(ctx, span, err, "error unmarshalling app proto")
  137. }
  138. envName, err := AppEnvGroupName(ctx, appDef.Name, inp.Revision.DeploymentTargetID, uint(inp.ClusterID), inp.PorterAppRepository)
  139. if err != nil {
  140. return revision, telemetry.Error(ctx, span, err, "error getting app env group name")
  141. }
  142. envNameFilter := []string{envName}
  143. envFromProtoInp := AppEnvironmentFromProtoInput{
  144. ProjectID: inp.ProjectID,
  145. ClusterID: inp.ClusterID,
  146. App: appDef,
  147. K8SAgent: inp.K8SAgent,
  148. DeploymentTarget: inp.DeploymentTarget,
  149. }
  150. envGroups, err := AppEnvironmentFromProto(ctx, envFromProtoInp, WithEnvGroupFilter(envNameFilter), WithSecrets())
  151. if err != nil {
  152. return revision, telemetry.Error(ctx, span, err, "error getting app environment from revision")
  153. }
  154. if len(envGroups) > 1 {
  155. return revision, telemetry.Error(ctx, span, err, "multiple app envs groups returned for same name")
  156. }
  157. if len(envGroups) == 1 {
  158. revision.Env = envGroups[0]
  159. }
  160. return revision, nil
  161. }
  162. func appRevisionStatusFromProto(status string) (models.AppRevisionStatus, error) {
  163. var appRevisionStatus models.AppRevisionStatus
  164. switch status {
  165. case string(models.AppRevisionStatus_AwaitingBuild):
  166. appRevisionStatus = models.AppRevisionStatus_AwaitingBuild
  167. case string(models.AppRevisionStatus_AwaitingPredeploy):
  168. appRevisionStatus = models.AppRevisionStatus_AwaitingPredeploy
  169. case string(models.AppRevisionStatus_Deployed):
  170. appRevisionStatus = models.AppRevisionStatus_Deployed
  171. case string(models.AppRevisionStatus_BuildCanceled):
  172. appRevisionStatus = models.AppRevisionStatus_BuildCanceled
  173. case string(models.AppRevisionStatus_BuildFailed):
  174. appRevisionStatus = models.AppRevisionStatus_BuildFailed
  175. case string(models.AppRevisionStatus_PredeployFailed):
  176. appRevisionStatus = models.AppRevisionStatus_PredeployFailed
  177. case string(models.AppRevisionStatus_DeployFailed):
  178. appRevisionStatus = models.AppRevisionStatus_DeployFailed
  179. case string(models.AppRevisionStatus_Created):
  180. appRevisionStatus = models.AppRevisionStatus_Created
  181. default:
  182. return appRevisionStatus, fmt.Errorf("unknown app revision status")
  183. }
  184. return appRevisionStatus, nil
  185. }