| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- package environment
- import (
- "errors"
- "fmt"
- "net/http"
- "github.com/porter-dev/porter/internal/telemetry"
- "github.com/porter-dev/porter/api/server/authz"
- "github.com/porter-dev/porter/api/server/handlers"
- "github.com/porter-dev/porter/api/server/shared"
- "github.com/porter-dev/porter/api/server/shared/apierrors"
- "github.com/porter-dev/porter/api/server/shared/config"
- "github.com/porter-dev/porter/api/types"
- "github.com/porter-dev/porter/internal/models"
- "gorm.io/gorm"
- )
- type CreateDeploymentByClusterHandler struct {
- handlers.PorterHandlerReadWriter
- authz.KubernetesAgentGetter
- }
- func NewCreateDeploymentByClusterHandler(
- config *config.Config,
- decoderValidator shared.RequestDecoderValidator,
- writer shared.ResultWriter,
- ) *CreateDeploymentByClusterHandler {
- return &CreateDeploymentByClusterHandler{
- PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
- KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
- }
- }
- func (c *CreateDeploymentByClusterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- ctx, span := telemetry.NewSpan(r.Context(), "serve-create-deployment-by-cluster")
- defer span.End()
- project, _ := ctx.Value(types.ProjectScope).(*models.Project)
- cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
- telemetry.WithAttributes(span,
- telemetry.AttributeKV{Key: "project-id", Value: project.ID},
- telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
- )
- request := &types.CreateDeploymentRequest{}
- if ok := c.DecodeAndValidate(w, r, request); !ok {
- err := telemetry.Error(ctx, span, nil, "could not decode and validate request")
- c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
- return
- }
- telemetry.WithAttributes(span,
- telemetry.AttributeKV{Key: "repo-owner", Value: request.RepoOwner},
- telemetry.AttributeKV{Key: "repo-name", Value: request.RepoName},
- telemetry.AttributeKV{Key: "namespace", Value: request.Namespace},
- telemetry.AttributeKV{Key: "pull-request-id", Value: request.PullRequestID},
- telemetry.AttributeKV{Key: "pr-name", Value: request.GitHubMetadata.PRName},
- telemetry.AttributeKV{Key: "commit-sha", Value: request.GitHubMetadata.CommitSHA},
- telemetry.AttributeKV{Key: "pr-branch-from", Value: request.GitHubMetadata.PRBranchFrom},
- telemetry.AttributeKV{Key: "pr-branch-into", Value: request.GitHubMetadata.PRBranchInto},
- )
- // read the environment to get the environment id
- env, err := c.Repo().Environment().ReadEnvironmentByOwnerRepoName(
- project.ID, cluster.ID, request.RepoOwner, request.RepoName,
- )
- if err != nil {
- err = telemetry.Error(ctx, span, err, "error reading environment by owner repo name")
- if errors.Is(err, gorm.ErrRecordNotFound) {
- c.HandleAPIError(w, r, apierrors.NewErrNotFound(
- fmt.Errorf("error creating deployment: %w", errEnvironmentNotFound)),
- )
- return
- }
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- // create deployment on GitHub API
- client, err := getGithubClientFromEnvironment(c.Config(), env)
- if err != nil {
- err = telemetry.Error(ctx, span, err, "error getting github client from environment")
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- // add a check for Github PR status
- prClosed, err := isGithubPRClosed(client, request.RepoOwner, request.RepoName, int(request.PullRequestID))
- if err != nil {
- err = telemetry.Error(ctx, span, err, "error checking if github pr is closed")
- c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusConflict))
- return
- }
- telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "pr-closed", Value: prClosed})
- if prClosed {
- telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "unsuccessful-exit-reason", Value: "pr is closed"})
- c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
- fmt.Errorf("attempting to create deployment for a closed github PR"), http.StatusConflict,
- ))
- return
- }
- ghDeployment, err := createGithubDeployment(client, env, request.PRBranchFrom, request.ActionID)
- if err != nil {
- err = telemetry.Error(ctx, span, err, "error creating github deployment object")
- c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusConflict))
- return
- }
- // create the deployment
- depl, err := c.Repo().Environment().CreateDeployment(&models.Deployment{
- EnvironmentID: env.ID,
- Namespace: request.Namespace,
- Status: types.DeploymentStatusCreating,
- PullRequestID: request.PullRequestID,
- GHDeploymentID: ghDeployment.GetID(),
- RepoOwner: request.GitHubMetadata.RepoOwner,
- RepoName: request.GitHubMetadata.RepoName,
- PRName: request.GitHubMetadata.PRName,
- CommitSHA: request.GitHubMetadata.CommitSHA,
- PRBranchFrom: request.GitHubMetadata.PRBranchFrom,
- PRBranchInto: request.GitHubMetadata.PRBranchInto,
- })
- if err != nil {
- err = telemetry.Error(ctx, span, err, "error creating github deployment object")
- // try to delete the GitHub deployment
- _, deleteErr := client.Repositories.DeleteDeployment(
- ctx,
- env.GitRepoOwner,
- env.GitRepoName,
- ghDeployment.GetID(),
- )
- if deleteErr != nil {
- telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "delete-err", Value: deleteErr.Error()})
- }
- c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error creating deployment: %w", err)))
- return
- }
- c.WriteResult(w, r, depl.ToDeploymentType())
- }
|