From d3f97d405796b23494698f8d792f283f6adc4480 Mon Sep 17 00:00:00 2001 From: WoodenMaiden Date: Wed, 22 May 2024 15:26:14 +0200 Subject: [PATCH] fix: better handling of hosts and ports --- .env.example | 4 +++- main.go | 6 ++++-- routes/function/controller.go | 2 +- scheduler/lambdoService.go | 11 ++++++++--- scheduler/scheduler.go | 4 +++- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/.env.example b/.env.example index 6a7ef07..a38d88b 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,4 @@ # ROOT_FS_STORAGE_DSN= -LAMBDO_URL="http://localhost:3000" VM_STATE_URL=redis://localhost:6379 FUNCTION_STATE_STORAGE_DSN='host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable TimeZone=Asia/Shanghai' JWT_SECRET="I am so secret! Hopefully someone doesn't commit me.." @@ -9,3 +8,6 @@ BUILDER_ENDPOINT="http://localhost:8080" # grobuzin's builder service endpoint MINIO_ENDPOINT="localhost:9000" MINIO_ACCESS_KEY="access_key" MINIO_SECRET_KEY="secret_key" + +LAMBDO_HOST="localhost" +LAMBDO_PORT="3000" \ No newline at end of file diff --git a/main.go b/main.go index 30f24bc..546b0fa 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,8 @@ import ( type Config struct { // rootFsStorageDSN string `env:"ROOT_FS_STORAGE_DSN,notEmpty"` - LambdoURL string `env:"LAMBDO_URL,notEmpty"` + LambdoHost string `env:"LAMBDO_HOST,notEmpty"` + LambdoPort int `env:"LAMBDO_PORT,notEmpty"` VMStateURL string `env:"VM_STATE_URL,notEmpty"` FuntionStateStorageDSN string `env:"FUNCTION_STATE_STORAGE_DSN,notEmpty" envDefault:"host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable TimeZone=Asia/Shanghai"` JWTSecret string `env:"JWT_SECRET,notEmpty"` @@ -51,7 +52,8 @@ func main() { Redis: redis, Context: &ctx, Lambdo: &scheduler.LambdoService{ - URL: cfg.LambdoURL, + Host: cfg.LambdoHost, + Port: cfg.LambdoPort, BucketURL: fmt.Sprint( bucketPrefix, cfg.MinioEndpoint, diff --git a/routes/function/controller.go b/routes/function/controller.go index 26d2a70..ab198a6 100644 --- a/routes/function/controller.go +++ b/routes/function/controller.go @@ -261,7 +261,7 @@ func (c *Controller) RunFunction(ctx *gin.Context) { } _, err = http.Post( - fmt.Sprint("http://", string(fnState.Address), ":", fnState.Port, "/execute"), + fmt.Sprint(string(fnState.Address), ":", fnState.Port, "/execute"), "application/json", ctx.Request.Body, ) diff --git a/scheduler/lambdoService.go b/scheduler/lambdoService.go index 0c12d58..dbfdb0a 100644 --- a/scheduler/lambdoService.go +++ b/scheduler/lambdoService.go @@ -19,7 +19,8 @@ const ( ) type LambdoService struct { - URL string + Host string + Port int BucketURL string } @@ -44,6 +45,10 @@ type LambdoSpawnResponse struct { Ports [][2]uint16 `json:"port_mapping"` } +func (service *LambdoService) url() string { + return fmt.Sprint("http://", service.Host, ":", service.Port) +} + func (service *LambdoService) SpawnVM(function database.Function) (data LambdoSpawnResponse, err error) { var res *http.Response defer func() { @@ -69,7 +74,7 @@ func (service *LambdoService) SpawnVM(function database.Function) (data LambdoSp } res, err = http.Post( - fmt.Sprint(service.URL, "/spawn"), + fmt.Sprint(service.url(), "/spawn"), "application/json", bytes.NewReader(body), ) @@ -90,7 +95,7 @@ func (service *LambdoService) SpawnVM(function database.Function) (data LambdoSp } func (service *LambdoService) DeleteVM(VMID string) (err error) { - req, err := http.NewRequest(http.MethodDelete, fmt.Sprint(service.URL, "/destroy/", VMID ), nil) + req, err := http.NewRequest(http.MethodDelete, fmt.Sprint(service.url(), "/destroy/", VMID ), nil) if err != nil { return } diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index a34c443..03f1a6c 100644 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "time" + "github.com/do4-2022/grobuzin/database" "github.com/google/uuid" @@ -67,6 +68,7 @@ func (s *Scheduler) LookForReadyInstance(functionId uuid.UUID, cursor uint64) (f } func (s *Scheduler) SpawnVM(function database.Function) (fnState database.FunctionState, err error) { + url := fmt.Sprint("http://", s.Lambdo.Host) res, err := s.Lambdo.SpawnVM(function) if (err != nil) { @@ -77,7 +79,7 @@ func (s *Scheduler) SpawnVM(function database.Function) (fnState database.Functi fnState = database.FunctionState{ ID: res.ID, - Address: s.Lambdo.URL, + Address: url, Port: res.Ports[0][0], Status: int(database.FnReady), LastUsed: "never",