Ver Fonte

custom namespace info in workflow file and environment creation

Mohammed Nafees há 3 anos atrás
pai
commit
8a99b32ffa

+ 12 - 0
api/server/handlers/environment/create.go

@@ -73,6 +73,17 @@ func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 		Mode:                request.Mode,
 		WebhookID:           string(webhookUID),
 		NewCommentsDisabled: false,
+		CustomNamespace:     request.CustomNamespace,
+	}
+
+	if len(request.NamespaceAnnotations) > 0 {
+		var annotations []string
+
+		for k, v := range request.NamespaceAnnotations {
+			annotations = append(annotations, fmt.Sprintf("%s=%s", k, v))
+		}
+
+		env.NamespaceAnnotations = []byte(strings.Join(annotations, ","))
 	}
 
 	// write Github actions files to the repo
@@ -179,6 +190,7 @@ func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 		GitInstallationID: uint(ga.InstallationID),
 		EnvironmentName:   request.Name,
 		InstanceName:      c.Config().ServerConf.InstanceName,
+		CustomNamespace:   env.CustomNamespace,
 	})
 
 	if err != nil {

+ 1 - 23
api/server/handlers/webhook/github_incoming.go

@@ -6,7 +6,6 @@ import (
 	"fmt"
 	"net/http"
 	"strconv"
-	"strings"
 	"sync"
 
 	"github.com/bradleyfalzon/ghinstallation/v2"
@@ -98,28 +97,7 @@ func (c *GithubIncomingWebhookHandler) processPullRequestEvent(event *github.Pul
 	}
 
 	if env.Mode == "auto" && event.GetAction() == "opened" {
-		depl := &models.Deployment{
-			EnvironmentID: env.ID,
-			Namespace: fmt.Sprintf("pr-%d-%s", event.GetPullRequest().GetNumber(),
-				strings.ToLower(strings.ReplaceAll(repo, "_", "-"))),
-			Status:        types.DeploymentStatusCreating,
-			PullRequestID: uint(event.GetPullRequest().GetNumber()),
-			PRName:        event.GetPullRequest().GetTitle(),
-			RepoName:      repo,
-			RepoOwner:     owner,
-			CommitSHA:     event.GetPullRequest().GetHead().GetSHA()[:7],
-			PRBranchFrom:  event.GetPullRequest().GetHead().GetRef(),
-			PRBranchInto:  event.GetPullRequest().GetBase().GetRef(),
-		}
-
-		_, err = c.Repo().Environment().CreateDeployment(depl)
-
-		if err != nil {
-			return fmt.Errorf("[webhookID: %s, owner: %s, repo: %s, environmentID: %d, prNumber: %d] "+
-				"error creating new deployment: %w", webhookID, owner, repo, env.ID, event.GetPullRequest().GetNumber(), err)
-		}
-
-		_, err = client.Actions.CreateWorkflowDispatchEventByFileName(
+		_, err := client.Actions.CreateWorkflowDispatchEventByFileName(
 			r.Context(), owner, repo, fmt.Sprintf("porter_%s_env.yml", env.Name),
 			github.CreateWorkflowDispatchEventRequest{
 				Ref: event.GetPullRequest().GetHead().GetRef(),

+ 4 - 3
api/types/environment.go

@@ -20,9 +20,10 @@ type Environment struct {
 }
 
 type CreateEnvironmentRequest struct {
-	Name string `json:"name" form:"required"`
-	Mode string `json:"mode" form:"oneof=auto manual" default:"manual"`
-	
+	Name                 string            `json:"name" form:"required"`
+	Mode                 string            `json:"mode" form:"oneof=auto manual" default:"manual"`
+	CustomNamespace      bool              `json:"custom_namespaces"`
+	NamespaceAnnotations map[string]string `json:"namespace_annotations"`
 }
 
 type GitHubMetadata struct {

+ 5 - 0
cli/cmd/apply.go

@@ -757,6 +757,11 @@ func NewDeploymentHook(client *api.Client, resourceGroup *switchboardTypes.Resou
 func (t *DeploymentHook) PreApply() error {
 	if isSystemNamespace(t.namespace) {
 		color.New(color.FgYellow).Printf("attempting to deploy to system namespace '%s'\n", t.namespace)
+	} else if t.namespace == "SET_CUSTOM_NAMESPACE_HERE" {
+		// user wanted to use custom namespaces but forgot to update the workflow file
+		return fmt.Errorf("you need to replace 'SET_CUSTOM_NAMESPACE_HERE' with a custom namespace of your choice in "+
+			"the workflow file: https://github.com/%s/%s/blob/%s/.github/workflows/porter_preview_env.yml",
+			t.repoOwner, t.repoName, t.branchFrom)
 	}
 
 	envList, err := t.client.ListEnvironments(

+ 2 - 0
internal/integrations/ci/actions/preview.go

@@ -19,6 +19,7 @@ type EnvOpts struct {
 	EnvironmentName                         string
 	InstanceName                            string
 	ProjectID, ClusterID, GitInstallationID uint
+	CustomNamespace                         bool
 }
 
 func SetupEnv(opts *EnvOpts) error {
@@ -232,6 +233,7 @@ func getPreviewApplyActionYAML(opts *EnvOpts) ([]byte, error) {
 			opts.GitRepoOwner,
 			opts.GitRepoName,
 			"v0.2.1",
+			opts.CustomNamespace,
 		),
 	}
 

+ 8 - 1
internal/integrations/ci/actions/steps.go

@@ -44,8 +44,9 @@ func getCreatePreviewEnvStep(
 	serverURL, porterTokenSecretName string,
 	projectID, clusterID, gitInstallationID uint,
 	repoOwner, repoName, actionVersion string,
+	customNamespace bool,
 ) GithubActionYAMLStep {
-	return GithubActionYAMLStep{
+	step := GithubActionYAMLStep{
 		Name: "Create Porter preview env",
 		Uses: fmt.Sprintf("%s@%s", createPreviewActionName, actionVersion),
 		With: map[string]string{
@@ -66,4 +67,10 @@ func getCreatePreviewEnvStep(
 		},
 		Timeout: 30,
 	}
+
+	if customNamespace {
+		step.With["namespace"] = "SET_CUSTOM_NAMESPACE_HERE"
+	}
+
+	return step
 }