Quellcode durchsuchen

ecr link after repo create

Alexander Belanger vor 5 Jahren
Ursprung
Commit
b249059d95

+ 4 - 5
cmd/app/main.go

@@ -76,10 +76,10 @@ func main() {
 	repo := gorm.NewRepository(db, &key)
 
 	a, _ := api.New(&api.AppConfig{
-		Logger:      logger,
-		Repository:  repo,
-		ServerConf:  appConf.Server,
-		RedisClient: redis,
+		Logger:     logger,
+		Repository: repo,
+		ServerConf: appConf.Server,
+		RedisConf:  &appConf.Redis,
 	})
 
 	appRouter := router.New(a)
@@ -103,5 +103,4 @@ func main() {
 	if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
 		log.Fatal("Server startup failed", err)
 	}
-
 }

+ 4 - 4
internal/adapter/redis.go

@@ -11,10 +11,10 @@ import (
 // NewRedisClient returns a new redis client instance
 func NewRedisClient(conf *config.RedisConf) (*redis.Client, error) {
 	client := redis.NewClient(&redis.Options{
-		Addr: fmt.Sprintf("%s:%s", conf.Host, conf.Port),
-		// Username: conf.Username,
-		// Password: conf.Password,
-		// DB:       conf.DB,
+		Addr:     fmt.Sprintf("%s:%s", conf.Host, conf.Port),
+		Username: conf.Username,
+		Password: conf.Password,
+		DB:       conf.DB,
 	})
 
 	_, err := client.Ping(context.Background()).Result()

+ 5 - 2
internal/config/redis.go

@@ -2,6 +2,9 @@ package config
 
 // RedisConf is the redis config required for the provisioner container
 type RedisConf struct {
-	Host string `env:"REDIS_HOST,default=redis"`
-	Port string `env:"REDIS_PORT,default=6379"`
+	Host     string `env:"REDIS_HOST,default=redis"`
+	Port     string `env:"REDIS_PORT,default=6379"`
+	Username string `env:"REDIS_USER"`
+	Password string `env:"REDIS_PASS"`
+	DB       int    `env:"REDIS_DB,default=0"`
 }

+ 5 - 2
internal/kubernetes/agent.go

@@ -11,6 +11,7 @@ import (
 	"github.com/porter-dev/porter/internal/kubernetes/provisioner"
 	"github.com/porter-dev/porter/internal/kubernetes/provisioner/aws"
 	"github.com/porter-dev/porter/internal/kubernetes/provisioner/aws/ecr"
+	"github.com/porter-dev/porter/internal/models"
 	"github.com/porter-dev/porter/internal/models/integrations"
 
 	"github.com/gorilla/websocket"
@@ -236,10 +237,12 @@ func (a *Agent) ProvisionECR(
 	projectID uint,
 	awsConf *integrations.AWSIntegration,
 	ecrName string,
+	awsInfra *models.AWSInfra,
 ) (*batchv1.Job, error) {
+	id := awsInfra.GetID()
 	prov := &provisioner.Conf{
-		ID:   fmt.Sprintf("%s-%d", ecrName, projectID),
-		Name: fmt.Sprintf("prov-%s-%d", ecrName, projectID),
+		ID:   id,
+		Name: fmt.Sprintf("prov-%s", id),
 		Kind: provisioner.ECR,
 		AWS: &aws.Conf{
 			AWSRegion:          awsConf.AWSRegion,

+ 17 - 4
internal/kubernetes/provisioner/global_stream.go

@@ -2,7 +2,6 @@ package provisioner
 
 import (
 	"context"
-	"encoding/json"
 	"fmt"
 
 	"github.com/porter-dev/porter/internal/repository"
@@ -46,6 +45,8 @@ func InitGlobalStream(client *redis.Client) error {
 		GlobalStreamName,
 	).Result()
 
+	fmt.Println(xInfoGroups, err)
+
 	if err != nil {
 		return err
 	}
@@ -53,6 +54,7 @@ func InitGlobalStream(client *redis.Client) error {
 	for _, group := range xInfoGroups {
 		// if the group exists, return with no error
 		if group.Name == GlobalStreamGroupName {
+			fmt.Println("group already exists")
 			return nil
 		}
 	}
@@ -62,9 +64,11 @@ func InitGlobalStream(client *redis.Client) error {
 		context.Background(),
 		GlobalStreamName,
 		GlobalStreamGroupName,
-		">",
+		"$",
 	).Result()
 
+	fmt.Println("xgroup created", err)
+
 	return err
 }
 
@@ -80,6 +84,8 @@ func GlobalStreamListener(
 	repo repository.Repository,
 	errorChan chan error,
 ) {
+	fmt.Println("starting global stream listener")
+
 	for {
 		xstreams, err := client.XReadGroup(
 			context.Background(),
@@ -91,6 +97,8 @@ func GlobalStreamListener(
 			},
 		).Result()
 
+		fmt.Println(xstreams, err)
+
 		if err != nil {
 			errorChan <- err
 			return
@@ -98,6 +106,8 @@ func GlobalStreamListener(
 
 		// parse messages from the global stream
 		for _, msg := range xstreams[0].Messages {
+			fmt.Println(msg.Values)
+
 			// parse the id to identify the infra
 			kind, projID, infraID, err := models.ParseWorkspaceID(fmt.Sprintf("%v", msg.Values["id"]))
 
@@ -124,9 +134,12 @@ func GlobalStreamListener(
 					}
 
 					// parse raw data into ECR type
-					bytes, _ := msg.Values["data"].([]byte)
+					dataMap, ok := msg.Values["data"].(map[string]interface{})
 
-					json.Unmarshal(bytes, reg)
+					if ok {
+						name, _ := dataMap["name"].(string)
+						reg.Name = name
+					}
 
 					reg, err := repo.Registry.CreateRegistry(reg)
 

+ 2 - 2
internal/models/infra.go

@@ -68,8 +68,8 @@ func (ai *AWSInfra) Externalize() *AWSInfraExternal {
 	}
 }
 
-// GetWorkspaceID returns the unique workspace id for this infra
-func (ai *AWSInfra) GetWorkspaceID() string {
+// GetID returns the unique id for this infra
+func (ai *AWSInfra) GetID() string {
 	return fmt.Sprintf("%s-%d-%d", ai.Kind, ai.ProjectID, ai.ID)
 }
 

+ 15 - 16
server/api/api.go

@@ -6,7 +6,6 @@ import (
 	"github.com/go-playground/locales/en"
 	ut "github.com/go-playground/universal-translator"
 	vr "github.com/go-playground/validator/v10"
-	"github.com/go-redis/redis/v8"
 	sessionstore "github.com/porter-dev/porter/internal/auth"
 	"github.com/porter-dev/porter/internal/oauth"
 	"golang.org/x/oauth2"
@@ -33,11 +32,11 @@ type TestAgents struct {
 
 // AppConfig is the configuration required for creating a new App
 type AppConfig struct {
-	DB          *gorm.DB
-	Logger      *lr.Logger
-	Repository  *repository.Repository
-	ServerConf  config.ServerConf
-	RedisClient *redis.Client
+	DB         *gorm.DB
+	Logger     *lr.Logger
+	Repository *repository.Repository
+	ServerConf config.ServerConf
+	RedisConf  *config.RedisConf
 
 	// TestAgents if API is in testing mode
 	TestAgents *TestAgents
@@ -61,8 +60,8 @@ type App struct {
 	// agents exposed for testing
 	TestAgents *TestAgents
 
-	// redis conf for redis connection
-	RedisClient *redis.Client
+	// redis client for redis connection
+	RedisConf *config.RedisConf
 
 	// oauth-specific clients
 	GithubConf *oauth2.Config
@@ -86,14 +85,14 @@ func New(conf *AppConfig) (*App, error) {
 	}
 
 	app := &App{
-		Logger:      conf.Logger,
-		Repo:        conf.Repository,
-		ServerConf:  conf.ServerConf,
-		RedisClient: conf.RedisClient,
-		TestAgents:  conf.TestAgents,
-		db:          conf.DB,
-		validator:   validator,
-		translator:  &translator,
+		Logger:     conf.Logger,
+		Repo:       conf.Repository,
+		ServerConf: conf.ServerConf,
+		RedisConf:  conf.RedisConf,
+		TestAgents: conf.TestAgents,
+		db:         conf.DB,
+		validator:  validator,
+		translator: &translator,
 	}
 
 	// if repository not specified, default to in-memory

+ 4 - 8
server/api/provision_handler.go

@@ -8,11 +8,11 @@ import (
 
 	"github.com/go-chi/chi"
 
-	"github.com/porter-dev/porter/internal/adapter"
-	"github.com/porter-dev/porter/internal/config"
 	"github.com/porter-dev/porter/internal/forms"
 	"github.com/porter-dev/porter/internal/kubernetes"
 	"github.com/porter-dev/porter/internal/kubernetes/provisioner"
+
+	"github.com/porter-dev/porter/internal/adapter"
 )
 
 // HandleProvisionTest will create a test resource by deploying a provisioner
@@ -103,6 +103,7 @@ func (app *App) HandleProvisionAWSECRInfra(w http.ResponseWriter, r *http.Reques
 		uint(projID),
 		awsInt,
 		form.ECRName,
+		infra,
 	)
 
 	if err != nil {
@@ -140,12 +141,7 @@ func (app *App) HandleGetProvisioningLogs(w http.ResponseWriter, r *http.Request
 		app.handleErrorUpgradeWebsocket(err, w)
 	}
 
-	conf := &config.RedisConf{
-		Host: "redis",
-		Port: "6379",
-	}
-
-	client, err := adapter.NewRedisClient(conf)
+	client, err := adapter.NewRedisClient(app.RedisConf)
 
 	if err != nil {
 		app.handleErrorInternal(err, w)

+ 2 - 4
server/router/router.go

@@ -178,10 +178,8 @@ func New(a *api.App) *chi.Mux {
 		)
 
 		// /api/projects/{project_id}/provision routes
-
-		// TODO -- restrict this endpoint
 		r.Method(
-			"GET",
+			"POST",
 			"/projects/{project_id}/provision/test",
 			auth.DoesUserHaveProjectAccess(
 				requestlog.NewHandler(a.HandleProvisionTest, l),
@@ -191,7 +189,7 @@ func New(a *api.App) *chi.Mux {
 		)
 
 		r.Method(
-			"GET",
+			"POST",
 			"/projects/{project_id}/provision/ecr",
 			auth.DoesUserHaveProjectAccess(
 				requestlog.NewHandler(a.HandleProvisionAWSECRInfra, l),