default_deployment_target.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package porter_app
  2. import (
  3. "context"
  4. "net/http"
  5. "time"
  6. "github.com/porter-dev/api-contracts/generated/go/porter/v1/porterv1connect"
  7. "github.com/google/uuid"
  8. "connectrpc.com/connect"
  9. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  10. "github.com/porter-dev/porter/internal/telemetry"
  11. "github.com/porter-dev/porter/api/server/handlers"
  12. "github.com/porter-dev/porter/api/server/shared"
  13. "github.com/porter-dev/porter/api/server/shared/apierrors"
  14. "github.com/porter-dev/porter/api/server/shared/config"
  15. "github.com/porter-dev/porter/api/types"
  16. "github.com/porter-dev/porter/internal/models"
  17. )
  18. // DefaultDeploymentTargetHandler handles requests to the /default-deployment-target endpoint
  19. type DefaultDeploymentTargetHandler struct {
  20. handlers.PorterHandlerReadWriter
  21. }
  22. // NewDefaultDeploymentTargetHandler returns a new DefaultDeploymentTargetHandler
  23. func NewDefaultDeploymentTargetHandler(
  24. config *config.Config,
  25. decoderValidator shared.RequestDecoderValidator,
  26. writer shared.ResultWriter,
  27. ) *DefaultDeploymentTargetHandler {
  28. return &DefaultDeploymentTargetHandler{
  29. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  30. }
  31. }
  32. // DefaultDeploymentTargetRequest is the request object for the /default-deployment-target endpoint
  33. type DefaultDeploymentTargetRequest struct{}
  34. // DefaultDeploymentTargetResponse is the response object for the /default-deployment-target endpoint
  35. type DefaultDeploymentTargetResponse struct {
  36. // Deprecated: use inline types.DeploymentTarget fields instead
  37. DeploymentTargetID string `json:"deployment_target_id"`
  38. types.DeploymentTarget `json:"deployment_target"`
  39. }
  40. const (
  41. // DeploymentTargetSelector_Default is the selector for the default deployment target in a cluster
  42. DeploymentTargetSelector_Default = "default"
  43. // DeploymentTargetSelectorType_Default is the selector type for the default deployment target in a cluster
  44. DeploymentTargetSelectorType_Default = "NAMESPACE"
  45. )
  46. // ServeHTTP receives a project id and cluster id and returns the default deployment target in the cluster
  47. func (c *DefaultDeploymentTargetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  48. ctx, span := telemetry.NewSpan(r.Context(), "serve-default-deployment-target")
  49. defer span.End()
  50. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  51. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  52. telemetry.WithAttributes(span,
  53. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  54. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  55. )
  56. defaultDeploymentTarget, err := defaultDeploymentTarget(ctx, defaultDeploymentTargetInput{
  57. ProjectID: project.ID,
  58. ClusterID: cluster.ID,
  59. ClusterControlPlaneClient: c.Config().ClusterControlPlaneClient,
  60. })
  61. if err != nil {
  62. err := telemetry.Error(ctx, span, err, "error getting default deployment target")
  63. c.WriteResult(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  64. return
  65. }
  66. response := &DefaultDeploymentTargetResponse{
  67. DeploymentTargetID: defaultDeploymentTarget.ID.String(),
  68. DeploymentTarget: defaultDeploymentTarget,
  69. }
  70. c.WriteResult(w, r, response)
  71. }
  72. type defaultDeploymentTargetInput struct {
  73. ProjectID uint
  74. ClusterID uint
  75. ClusterControlPlaneClient porterv1connect.ClusterControlPlaneServiceClient
  76. }
  77. func defaultDeploymentTarget(ctx context.Context, input defaultDeploymentTargetInput) (types.DeploymentTarget, error) {
  78. ctx, span := telemetry.NewSpan(ctx, "default-deployment-target")
  79. defer span.End()
  80. var defaultDeploymentTarget types.DeploymentTarget
  81. telemetry.WithAttributes(span,
  82. telemetry.AttributeKV{Key: "project-id", Value: input.ProjectID},
  83. telemetry.AttributeKV{Key: "cluster-id", Value: input.ClusterID},
  84. )
  85. defaultDeploymentTargetReq := connect.NewRequest(&porterv1.DefaultDeploymentTargetRequest{
  86. ProjectId: int64(input.ProjectID),
  87. ClusterId: int64(input.ClusterID),
  88. })
  89. defaultDeploymentTargetResp, err := input.ClusterControlPlaneClient.DefaultDeploymentTarget(ctx, defaultDeploymentTargetReq)
  90. if err != nil {
  91. return defaultDeploymentTarget, telemetry.Error(ctx, span, err, "error getting default deployment target")
  92. }
  93. if defaultDeploymentTargetResp == nil || defaultDeploymentTargetResp.Msg == nil {
  94. return defaultDeploymentTarget, telemetry.Error(ctx, span, nil, "default deployment target response is nil")
  95. }
  96. deploymentTargetProto := defaultDeploymentTargetResp.Msg.DeploymentTarget
  97. id, err := uuid.Parse(deploymentTargetProto.Id)
  98. if err != nil {
  99. return defaultDeploymentTarget, telemetry.Error(ctx, span, err, "error parsing default deployment target id")
  100. }
  101. if id == uuid.Nil {
  102. return defaultDeploymentTarget, telemetry.Error(ctx, span, nil, "default deployment target id is nil")
  103. }
  104. defaultDeploymentTarget = types.DeploymentTarget{
  105. ID: id,
  106. ProjectID: uint(deploymentTargetProto.ProjectId),
  107. ClusterID: uint(deploymentTargetProto.ClusterId),
  108. Name: deploymentTargetProto.Name,
  109. Namespace: deploymentTargetProto.Namespace,
  110. IsPreview: deploymentTargetProto.IsPreview,
  111. IsDefault: deploymentTargetProto.IsDefault,
  112. CreatedAtUTC: time.Time{}, // not provided by default deployment target response
  113. UpdatedAtUTC: time.Time{}, // not provided by default deployment target response
  114. }
  115. return defaultDeploymentTarget, nil
  116. }