package environment import ( "context" "net/http" "strings" "github.com/google/go-github/v41/github" "github.com/porter-dev/porter/api/server/authz" "github.com/porter-dev/porter/api/server/handlers" "github.com/porter-dev/porter/api/server/handlers/gitinstallation" "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" "github.com/porter-dev/porter/internal/models/integrations" ) type DeleteDeploymentHandler struct { handlers.PorterHandlerReadWriter authz.KubernetesAgentGetter } func NewDeleteDeploymentHandler( config *config.Config, decoderValidator shared.RequestDecoderValidator, writer shared.ResultWriter, ) *DeleteDeploymentHandler { return &DeleteDeploymentHandler{ PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer), KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config), } } func (c *DeleteDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation) project, _ := r.Context().Value(types.ProjectScope).(*models.Project) cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster) owner, name, ok := gitinstallation.GetOwnerAndNameParams(c, w, r) if !ok { return } request := &types.DeleteDeploymentRequest{} if ok := c.DecodeAndValidate(w, r, request); !ok { return } // read the environment to get the environment id env, err := c.Repo().Environment().ReadEnvironment(project.ID, cluster.ID, uint(ga.InstallationID), owner, name) if err != nil { c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) return } // read the deployment depl, err := c.Repo().Environment().ReadDeployment(env.ID, request.Namespace) if err != nil { c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) return } // delete corresponding namespace agent, err := c.GetAgent(r, cluster, "") if err != nil { c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) return } // make sure we don't delete default or kube-system by checking for prefix, for now if strings.Contains(depl.Namespace, "pr-") { err = agent.DeleteNamespace(depl.Namespace) if err != nil { c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) return } } client, err := getGithubClientFromEnvironment(c.Config(), env) if err != nil { c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) return } // Create new deployment status to indicate deployment is ready state := "inactive" deploymentStatusRequest := github.DeploymentStatusRequest{ State: &state, } _, _, err = client.Repositories.CreateDeploymentStatus( context.Background(), env.GitRepoOwner, env.GitRepoName, depl.GHDeploymentID, &deploymentStatusRequest, ) if err != nil { c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) return } depl.Status = types.DeploymentStatusInactive // update the deployment to mark it inactive depl, err = c.Repo().Environment().UpdateDeployment(depl) if err != nil { c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) return } c.WriteResult(w, r, depl.ToDeploymentType()) }