From 93e2ea6e22a56a9aad09cb47e07ac5fe76560253 Mon Sep 17 00:00:00 2001 From: Toma Puljak Date: Thu, 3 Oct 2024 14:29:41 +0000 Subject: [PATCH 1/2] refactor: server start - headscale, local container registry and the api server are responsible for their own FRP connection - headscale and local CR are started in the entrypoint rather than in the server start method Signed-off-by: Toma Puljak --- pkg/api/server.go | 48 ++++++++++++- pkg/cmd/purge.go | 66 ++++++++++++++++- pkg/cmd/server/serve.go | 91 ++++++++++++++++------- pkg/cmd/server/server.go | 2 +- pkg/server/headscale/server.go | 47 +++++++++++- pkg/server/purge.go | 28 +------- pkg/server/registry/service.go | 52 +++++++++++++- pkg/server/server.go | 128 ++------------------------------- pkg/server/types.go | 2 +- 9 files changed, 278 insertions(+), 186 deletions(-) diff --git a/pkg/api/server.go b/pkg/api/server.go index 7138974daf..1e4cd9fb5d 100644 --- a/pkg/api/server.go +++ b/pkg/api/server.go @@ -28,6 +28,7 @@ import ( "github.com/daytonaio/daytona/pkg/api/docs" "github.com/daytonaio/daytona/pkg/api/middlewares" + "github.com/daytonaio/daytona/pkg/frpc" "github.com/daytonaio/daytona/pkg/telemetry" "github.com/gin-contrib/cors" @@ -52,6 +53,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/daytonaio/daytona/internal/constants" + daytonaServer "github.com/daytonaio/daytona/pkg/server" swaggerfiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" ) @@ -60,6 +62,8 @@ type ApiServerConfig struct { ApiPort int Version string TelemetryService telemetry.TelemetryService + Frps *daytonaServer.FRPSConfig + ServerId string } func NewApiServer(config ApiServerConfig) *ApiServer { @@ -67,6 +71,8 @@ func NewApiServer(config ApiServerConfig) *ApiServer { apiPort: config.ApiPort, telemetryService: config.TelemetryService, version: config.Version, + frps: config.Frps, + serverId: config.ServerId, } } @@ -76,6 +82,8 @@ type ApiServer struct { httpServer *http.Server router *gin.Engine version string + frps *daytonaServer.FRPSConfig + serverId string } func (a *ApiServer) Start() error { @@ -262,8 +270,44 @@ func (a *ApiServer) Start() error { return err } - log.Infof("Starting api server on port %d", a.apiPort) - return a.httpServer.Serve(listener) + errChan := make(chan error) + go func() { + errChan <- a.httpServer.Serve(listener) + }() + + if a.frps != nil { + frpcHealthCheck, frpcService, err := frpc.GetService(frpc.FrpcConnectParams{ + ServerDomain: a.frps.Domain, + ServerPort: int(a.frps.Port), + Name: fmt.Sprintf("daytona-server-api-%s", a.serverId), + Port: int(a.apiPort), + SubDomain: fmt.Sprintf("api-%s", a.serverId), + }) + if err != nil { + return err + } + + go func() { + err := frpcService.Run(context.Background()) + if err != nil { + errChan <- err + } + }() + + for i := 0; i < 5; i++ { + if err = frpcHealthCheck(); err != nil { + log.Debugf("Failed to connect to api frpc: %s", err) + time.Sleep(2 * time.Second) + } else { + break + } + } + if err != nil { + return err + } + } + + return <-errChan } func (a *ApiServer) HealthCheck() error { diff --git a/pkg/cmd/purge.go b/pkg/cmd/purge.go index 40353003b6..5c7f082d0c 100644 --- a/pkg/cmd/purge.go +++ b/pkg/cmd/purge.go @@ -5,6 +5,7 @@ package cmd import ( "context" + "errors" "fmt" "os" @@ -139,9 +140,72 @@ var purgeCmd = &cobra.Command{ } }() + headscaleServerStartedChan := make(chan struct{}) + headscaleServerErrChan := make(chan error) + + go func() { + err := server.TailscaleServer.Start(headscaleServerErrChan) + if err != nil { + headscaleServerErrChan <- err + return + } + headscaleServerStartedChan <- struct{}{} + }() + + localContainerRegistryErrChan := make(chan error) + + go func() { + if server.LocalContainerRegistry != nil { + localContainerRegistryErrChan <- server.LocalContainerRegistry.Start() + } else { + localContainerRegistryErrChan <- nil + } + }() + + select { + case <-headscaleServerStartedChan: + go func() { + headscaleServerErrChan <- server.TailscaleServer.Connect() + }() + case err := <-headscaleServerErrChan: + return err + } + + err = <-localContainerRegistryErrChan + if err != nil { + return err + } + errs := server.Purge(ctx, forceFlag) if len(errs) > 0 { - return errs[0] + errMessage := "" + for _, err := range errs { + errMessage += fmt.Sprintf("Failed to purge: %v\n", err) + } + + return errors.New(errMessage) + } + + if server.LocalContainerRegistry != nil { + fmt.Println("Purging local container registry...") + err := server.LocalContainerRegistry.Purge() + if err != nil { + if !forceFlag { + return err + } else { + fmt.Printf("Failed to purge local container registry: %v\n", err) + } + } + } + + fmt.Println("Purging Tailscale server...") + err = server.TailscaleServer.Purge() + if err != nil { + if !forceFlag { + return err + } else { + fmt.Printf("Failed to purge Tailscale server: %v\n", err) + } } fmt.Println("Server purged.") diff --git a/pkg/cmd/server/serve.go b/pkg/cmd/server/serve.go index 09f41991c0..28c6f16bff 100644 --- a/pkg/cmd/server/serve.go +++ b/pkg/cmd/server/serve.go @@ -75,19 +75,12 @@ var ServeCmd = &cobra.Command{ Version: internal.Version, }) - go func() { - interruptChannel := make(chan os.Signal, 1) - signal.Notify(interruptChannel, os.Interrupt) - - for range interruptChannel { - log.Info("Shutting down") - } - }() - apiServer := api.NewApiServer(api.ApiServerConfig{ ApiPort: int(c.ApiPort), TelemetryService: telemetryService, Version: internal.Version, + ServerId: c.Id, + Frps: c.Frps, }) server, err := GetInstance(c, configDir, internal.Version, telemetryService) @@ -95,13 +88,6 @@ var ServeCmd = &cobra.Command{ return err } - errCh := make(chan error) - - err = server.Start(errCh) - if err != nil { - return err - } - buildRunnerConfig, err := build.GetConfig() if err != nil { return err @@ -117,22 +103,58 @@ var ServeCmd = &cobra.Command{ return err } + apiServerErrChan := make(chan error) + + go func() { + log.Infof("Starting api server on port %d", c.ApiPort) + apiServerErrChan <- apiServer.Start() + }() + + headscaleServerStartedChan := make(chan struct{}) + headscaleServerErrChan := make(chan error) + go func() { - err := apiServer.Start() + log.Info("Starting headscale server...") + err := server.TailscaleServer.Start(headscaleServerErrChan) if err != nil { - errCh <- err + headscaleServerErrChan <- err + return } + headscaleServerStartedChan <- struct{}{} }() + localContainerRegistryErrChan := make(chan error) + go func() { - err := <-errCh - if err != nil { - buildRunner.Stop() + if server.LocalContainerRegistry != nil { + log.Info("Starting local container registry...") + localContainerRegistryErrChan <- server.LocalContainerRegistry.Start() + } else { + localContainerRegistryErrChan <- registry.RemoveRegistryContainer() } }() - err = waitForServerToStart(apiServer) + select { + case <-headscaleServerStartedChan: + log.Info("Headscale server started") + go func() { + headscaleServerErrChan <- server.TailscaleServer.Connect() + }() + case err := <-headscaleServerErrChan: + return err + } + err = server.Start() + if err != nil { + return err + } + + err = waitForApiServerToStart(apiServer) + if err != nil { + return err + } + + err = <-localContainerRegistryErrChan if err != nil { return err } @@ -144,7 +166,19 @@ var ServeCmd = &cobra.Command{ return err } - return <-errCh + interruptChannel := make(chan os.Signal, 1) + signal.Notify(interruptChannel, os.Interrupt) + + select { + case err := <-apiServerErrChan: + return err + case err := <-headscaleServerErrChan: + return err + case <-interruptChannel: + log.Info("Shutting down") + // Exit will be handled by command PreRun + select {} + } }, } @@ -205,6 +239,7 @@ func GetInstance(c *server.Config, configDir string, version string, telemetrySe FrpsProtocol: c.Frps.Protocol, HeadscalePort: c.HeadscalePort, ConfigDir: filepath.Join(configDir, "headscale"), + Frps: c.Frps, }) err = headscaleServer.Init() if err != nil { @@ -255,6 +290,8 @@ func GetInstance(c *server.Config, configDir string, version string, telemetrySe Port: c.LocalBuilderRegistryPort, Image: c.LocalBuilderRegistryImage, Logger: log.StandardLogger().Writer(), + Frps: c.Frps, + ServerId: c.Id, }) c.BuilderRegistryServer = util.GetFrpcRegistryDomain(c.Id, c.Frps.Domain) } @@ -311,7 +348,7 @@ func GetInstance(c *server.Config, configDir string, version string, telemetrySe ProfileDataStore: profileDataStore, }) - return server.GetInstance(&server.ServerInstanceConfig{ + s := server.GetInstance(&server.ServerInstanceConfig{ Config: *c, Version: version, TailscaleServer: headscaleServer, @@ -326,7 +363,9 @@ func GetInstance(c *server.Config, configDir string, version string, telemetrySe ProviderManager: providerManager, ProfileDataService: profileDataService, TelemetryService: telemetryService, - }), nil + }) + + return s, s.Initialize() } func GetBuildRunner(c *server.Config, buildRunnerConfig *build.Config, telemetryService telemetry.TelemetryService) (*build.BuildRunner, error) { @@ -412,7 +451,7 @@ func GetBuildRunner(c *server.Config, buildRunnerConfig *build.Config, telemetry }), nil } -func waitForServerToStart(apiServer *api.ApiServer) error { +func waitForApiServerToStart(apiServer *api.ApiServer) error { var err error for i := 0; i < 30; i++ { time.Sleep(1 * time.Second) diff --git a/pkg/cmd/server/server.go b/pkg/cmd/server/server.go index 50aa4cf66b..e431987935 100644 --- a/pkg/cmd/server/server.go +++ b/pkg/cmd/server/server.go @@ -56,7 +56,7 @@ var ServerCmd = &cobra.Command{ if err != nil { return err } - err = waitForServerToStart(apiServer) + err = waitForApiServerToStart(apiServer) if err != nil { return err } diff --git a/pkg/server/headscale/server.go b/pkg/server/headscale/server.go index 94fe189326..8c593334b1 100644 --- a/pkg/server/headscale/server.go +++ b/pkg/server/headscale/server.go @@ -4,11 +4,17 @@ package headscale import ( + "context" "fmt" "net" "os" + "time" + "github.com/daytonaio/daytona/pkg/frpc" + "github.com/daytonaio/daytona/pkg/server" "github.com/juanfont/headscale/hscontrol" + + log "github.com/sirupsen/logrus" ) type HeadscaleServerConfig struct { @@ -17,6 +23,7 @@ type HeadscaleServerConfig struct { FrpsProtocol string HeadscalePort uint32 ConfigDir string + Frps *server.FRPSConfig } func NewHeadscaleServer(config *HeadscaleServerConfig) *HeadscaleServer { @@ -26,6 +33,7 @@ func NewHeadscaleServer(config *HeadscaleServerConfig) *HeadscaleServer { frpsProtocol: config.FrpsProtocol, headscalePort: config.HeadscalePort, configDir: config.ConfigDir, + frps: config.Frps, } } @@ -35,6 +43,7 @@ type HeadscaleServer struct { frpsProtocol string headscalePort uint32 configDir string + frps *server.FRPSConfig stopChan chan struct{} } @@ -43,7 +52,8 @@ func (s *HeadscaleServer) Init() error { return os.MkdirAll(s.configDir, 0700) } -func (s *HeadscaleServer) Start() error { +func (s *HeadscaleServer) Start(errChan chan error) error { + // Check if port is already in use _, err := net.Dial("tcp", fmt.Sprintf(":%d", s.headscalePort)) if err == nil { return fmt.Errorf("cannot start Headscale server, port %d is already in use", s.headscalePort) @@ -60,7 +70,6 @@ func (s *HeadscaleServer) Start() error { } s.stopChan = make(chan struct{}) - errChan := make(chan error) go func() { select { @@ -72,7 +81,39 @@ func (s *HeadscaleServer) Start() error { } }() - return <-errChan + if s.frps != nil { + healthCheck, frpcService, err := frpc.GetService(frpc.FrpcConnectParams{ + ServerDomain: s.frps.Domain, + ServerPort: int(s.frps.Port), + Name: fmt.Sprintf("daytona-server-%s", s.serverId), + Port: int(s.headscalePort), + SubDomain: s.serverId, + }) + if err != nil { + return err + } + + go func() { + err := frpcService.Run(context.Background()) + if err != nil { + errChan <- err + } + }() + + for i := 0; i < 5; i++ { + if err = healthCheck(); err != nil { + log.Debugf("Failed to connect to headscale frpc: %s", err) + time.Sleep(2 * time.Second) + } else { + break + } + } + if err != nil { + return err + } + } + + return err } func (s *HeadscaleServer) Stop() error { diff --git a/pkg/server/purge.go b/pkg/server/purge.go index eeb764f2e9..cd292f368f 100644 --- a/pkg/server/purge.go +++ b/pkg/server/purge.go @@ -30,9 +30,7 @@ func (s *Server) Purge(ctx context.Context, force bool) []error { fmt.Println("Deleting all workspaces...") - errCh := make(chan error) - - err := server.Start(errCh) + err := server.Start() if err != nil { s.trackPurgeError(ctx, force, err) return []error{err} @@ -64,30 +62,6 @@ func (s *Server) Purge(ctx context.Context, force bool) []error { fmt.Printf("Failed to list workspaces: %v\n", err) } - if s.LocalContainerRegistry != nil { - fmt.Println("Purging local container registry...") - err := s.LocalContainerRegistry.Purge() - if err != nil { - s.trackPurgeError(ctx, force, err) - if !force { - return []error{err} - } else { - fmt.Printf("Failed to purge local container registry: %v\n", err) - } - } - } - - fmt.Println("Purging Tailscale server...") - err = s.TailscaleServer.Purge() - if err != nil { - s.trackPurgeError(ctx, force, err) - if !force { - return []error{err} - } else { - fmt.Printf("Failed to purge Tailscale server: %v\n", err) - } - } - fmt.Println("Purging providers...") err = s.ProviderManager.Purge() if err != nil { diff --git a/pkg/server/registry/service.go b/pkg/server/registry/service.go index f6f30a396a..c4d35f1ddb 100644 --- a/pkg/server/registry/service.go +++ b/pkg/server/registry/service.go @@ -10,11 +10,16 @@ import ( "io" "net" "os" + "time" "github.com/daytonaio/daytona/pkg/docker" + "github.com/daytonaio/daytona/pkg/frpc" + "github.com/daytonaio/daytona/pkg/server" "github.com/docker/docker/api/types/container" "github.com/docker/docker/client" "github.com/docker/go-connections/nat" + + log "github.com/sirupsen/logrus" ) const registryContainerName = "daytona-registry" @@ -24,6 +29,8 @@ type LocalContainerRegistryConfig struct { Port uint32 Image string Logger io.Writer + Frps *server.FRPSConfig + ServerId string } func NewLocalContainerRegistry(config *LocalContainerRegistryConfig) *LocalContainerRegistry { @@ -32,6 +39,8 @@ func NewLocalContainerRegistry(config *LocalContainerRegistryConfig) *LocalConta port: config.Port, image: config.Image, logger: config.Logger, + frps: config.Frps, + serverId: config.ServerId, } } @@ -40,6 +49,8 @@ type LocalContainerRegistry struct { port uint32 image string logger io.Writer + frps *server.FRPSConfig + serverId string } func (s *LocalContainerRegistry) Start() error { @@ -112,7 +123,44 @@ func (s *LocalContainerRegistry) Start() error { return err } - return cli.ContainerStart(ctx, resp.ID, container.StartOptions{}) + errChan := make(chan error) + go func() { + errChan <- cli.ContainerStart(ctx, resp.ID, container.StartOptions{}) + }() + + if s.frps != nil { + healthCheck, frpcService, err := frpc.GetService(frpc.FrpcConnectParams{ + ServerDomain: s.frps.Domain, + ServerPort: int(s.frps.Port), + Name: fmt.Sprintf("daytona-server-registry-%s", s.serverId), + Port: int(s.port), + SubDomain: fmt.Sprintf("registry-%s", s.serverId), + }) + if err != nil { + return err + } + + go func() { + err := frpcService.Run(context.Background()) + if err != nil { + errChan <- err + } + }() + + for i := 0; i < 5; i++ { + if err = healthCheck(); err != nil { + log.Debugf("Failed to connect to registry frpc: %s", err) + time.Sleep(2 * time.Second) + } else { + break + } + } + if err != nil { + return err + } + } + + return <-errChan } func (s *LocalContainerRegistry) Stop() error { @@ -149,7 +197,7 @@ func RemoveRegistryContainer() error { } if err := cli.ContainerRemove(ctx, c.ID, removeOptions); err != nil { - return err + return fmt.Errorf("failed to remove local container registry: %w", err) } return nil } diff --git a/pkg/server/server.go b/pkg/server/server.go index 4d489db458..5c960cb05f 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -4,13 +4,9 @@ package server import ( - "context" - "fmt" "os" "os/signal" - "time" - "github.com/daytonaio/daytona/pkg/frpc" "github.com/daytonaio/daytona/pkg/provider/manager" "github.com/daytonaio/daytona/pkg/server/apikeys" "github.com/daytonaio/daytona/pkg/server/builds" @@ -19,7 +15,6 @@ import ( "github.com/daytonaio/daytona/pkg/server/profiledata" "github.com/daytonaio/daytona/pkg/server/projectconfig" "github.com/daytonaio/daytona/pkg/server/providertargets" - "github.com/daytonaio/daytona/pkg/server/registry" "github.com/daytonaio/daytona/pkg/server/workspaces" "github.com/daytonaio/daytona/pkg/telemetry" "github.com/hashicorp/go-plugin" @@ -95,82 +90,13 @@ type Server struct { TelemetryService telemetry.TelemetryService } -func (s *Server) Start(errCh chan error) error { - err := s.initLogs() - if err != nil { - return err - } +func (s *Server) Initialize() error { + return s.initLogs() +} +func (s *Server) Start() error { log.Info("Starting Daytona server") - headscaleFrpcHealthCheck, headscaleFrpcService, err := frpc.GetService(frpc.FrpcConnectParams{ - ServerDomain: s.config.Frps.Domain, - ServerPort: int(s.config.Frps.Port), - Name: fmt.Sprintf("daytona-server-%s", s.config.Id), - Port: int(s.config.HeadscalePort), - SubDomain: s.config.Id, - }) - if err != nil { - return err - } - - if s.LocalContainerRegistry != nil { - log.Info("Starting local container registry") - err = s.LocalContainerRegistry.Start() - if err != nil { - log.Fatal(err) - } - } else { - err := registry.RemoveRegistryContainer() - if err != nil { - log.Fatalf("Failed to remove local container registry: %s", err.Error()) - } - } - - go func() { - err := headscaleFrpcService.Run(context.Background()) - if err != nil { - errCh <- err - } - }() - - apiFrpcHealthCheck, apiFrpcService, err := frpc.GetService(frpc.FrpcConnectParams{ - ServerDomain: s.config.Frps.Domain, - ServerPort: int(s.config.Frps.Port), - Name: fmt.Sprintf("daytona-server-api-%s", s.config.Id), - Port: int(s.config.ApiPort), - SubDomain: fmt.Sprintf("api-%s", s.config.Id), - }) - if err != nil { - return err - } - - go func() { - err := apiFrpcService.Run(context.Background()) - if err != nil { - errCh <- err - } - }() - - if s.LocalContainerRegistry != nil { - _, registryFrpcService, err := frpc.GetService(frpc.FrpcConnectParams{ - ServerDomain: s.config.Frps.Domain, - ServerPort: int(s.config.Frps.Port), - Name: fmt.Sprintf("daytona-server-registry-%s", s.config.Id), - Port: int(s.config.LocalBuilderRegistryPort), - SubDomain: fmt.Sprintf("registry-%s", s.config.Id), - }) - if err != nil { - return err - } - go func() { - err := registryFrpcService.Run(context.Background()) - if err != nil { - errCh <- err - } - }() - } - go func() { interruptChannel := make(chan os.Signal, 1) signal.Notify(interruptChannel, os.Interrupt) @@ -180,52 +106,8 @@ func (s *Server) Start(errCh chan error) error { } }() - go func() { - errChan := make(chan error) - go func() { - errChan <- s.TailscaleServer.Start() - }() - - select { - case err := <-errChan: - errCh <- err - case <-time.After(1 * time.Second): - go func() { - errChan <- s.TailscaleServer.Connect() - }() - } - - if err := <-errChan; err != nil { - errCh <- err - } - }() - - for i := 0; i < 5; i++ { - if err = headscaleFrpcHealthCheck(); err != nil { - log.Debugf("Failed to connect to headscale frpc: %s", err) - time.Sleep(2 * time.Second) - } else { - break - } - } - if err != nil { - return err - } - - for i := 0; i < 5; i++ { - if err = apiFrpcHealthCheck(); err != nil { - log.Debugf("Failed to connect to api frpc: %s", err) - time.Sleep(2 * time.Second) - } else { - break - } - } - if err != nil { - return err - } - // Terminate orphaned provider processes - err = s.ProviderManager.TerminateProviderProcesses(s.config.ProvidersDir) + err := s.ProviderManager.TerminateProviderProcesses(s.config.ProvidersDir) if err != nil { log.Errorf("Failed to terminate orphaned provider processes: %s", err) } diff --git a/pkg/server/types.go b/pkg/server/types.go index a6fcefb7f5..9efdf2b93d 100644 --- a/pkg/server/types.go +++ b/pkg/server/types.go @@ -12,7 +12,7 @@ type TailscaleServer interface { CreateAuthKey() (string, error) CreateUser() error HTTPClient() *http.Client - Start() error + Start(errChan chan error) error Stop() error Purge() error } From fed30fa1ca14c6aafb4b13e9a6e9e9b12470c591 Mon Sep 17 00:00:00 2001 From: Toma Puljak Date: Fri, 4 Oct 2024 10:16:38 +0000 Subject: [PATCH 2/2] fix: code cleanup Signed-off-by: Toma Puljak --- pkg/api/server.go | 54 ++++++++++++++++++---------------- pkg/server/headscale/server.go | 51 ++++++++++++++++---------------- pkg/server/registry/service.go | 54 ++++++++++++++++++---------------- 3 files changed, 81 insertions(+), 78 deletions(-) diff --git a/pkg/api/server.go b/pkg/api/server.go index 1e4cd9fb5d..421a2f655d 100644 --- a/pkg/api/server.go +++ b/pkg/api/server.go @@ -275,37 +275,39 @@ func (a *ApiServer) Start() error { errChan <- a.httpServer.Serve(listener) }() - if a.frps != nil { - frpcHealthCheck, frpcService, err := frpc.GetService(frpc.FrpcConnectParams{ - ServerDomain: a.frps.Domain, - ServerPort: int(a.frps.Port), - Name: fmt.Sprintf("daytona-server-api-%s", a.serverId), - Port: int(a.apiPort), - SubDomain: fmt.Sprintf("api-%s", a.serverId), - }) + if a.frps == nil { + return <-errChan + } + + frpcHealthCheck, frpcService, err := frpc.GetService(frpc.FrpcConnectParams{ + ServerDomain: a.frps.Domain, + ServerPort: int(a.frps.Port), + Name: fmt.Sprintf("daytona-server-api-%s", a.serverId), + Port: int(a.apiPort), + SubDomain: fmt.Sprintf("api-%s", a.serverId), + }) + if err != nil { + return err + } + + go func() { + err := frpcService.Run(context.Background()) if err != nil { - return err + errChan <- err } + }() - go func() { - err := frpcService.Run(context.Background()) - if err != nil { - errChan <- err - } - }() - - for i := 0; i < 5; i++ { - if err = frpcHealthCheck(); err != nil { - log.Debugf("Failed to connect to api frpc: %s", err) - time.Sleep(2 * time.Second) - } else { - break - } - } - if err != nil { - return err + for i := 0; i < 5; i++ { + if err = frpcHealthCheck(); err != nil { + log.Debugf("Failed to connect to api frpc: %s", err) + time.Sleep(2 * time.Second) + } else { + break } } + if err != nil { + return err + } return <-errChan } diff --git a/pkg/server/headscale/server.go b/pkg/server/headscale/server.go index 8c593334b1..da9224b769 100644 --- a/pkg/server/headscale/server.go +++ b/pkg/server/headscale/server.go @@ -81,35 +81,34 @@ func (s *HeadscaleServer) Start(errChan chan error) error { } }() - if s.frps != nil { - healthCheck, frpcService, err := frpc.GetService(frpc.FrpcConnectParams{ - ServerDomain: s.frps.Domain, - ServerPort: int(s.frps.Port), - Name: fmt.Sprintf("daytona-server-%s", s.serverId), - Port: int(s.headscalePort), - SubDomain: s.serverId, - }) + if s.frps == nil { + return err + } + + healthCheck, frpcService, err := frpc.GetService(frpc.FrpcConnectParams{ + ServerDomain: s.frps.Domain, + ServerPort: int(s.frps.Port), + Name: fmt.Sprintf("daytona-server-%s", s.serverId), + Port: int(s.headscalePort), + SubDomain: s.serverId, + }) + if err != nil { + return err + } + + go func() { + err := frpcService.Run(context.Background()) if err != nil { - return err + errChan <- err } + }() - go func() { - err := frpcService.Run(context.Background()) - if err != nil { - errChan <- err - } - }() - - for i := 0; i < 5; i++ { - if err = healthCheck(); err != nil { - log.Debugf("Failed to connect to headscale frpc: %s", err) - time.Sleep(2 * time.Second) - } else { - break - } - } - if err != nil { - return err + for i := 0; i < 5; i++ { + if err = healthCheck(); err != nil { + log.Debugf("Failed to connect to headscale frpc: %s", err) + time.Sleep(2 * time.Second) + } else { + break } } diff --git a/pkg/server/registry/service.go b/pkg/server/registry/service.go index c4d35f1ddb..e8c5f54323 100644 --- a/pkg/server/registry/service.go +++ b/pkg/server/registry/service.go @@ -128,37 +128,39 @@ func (s *LocalContainerRegistry) Start() error { errChan <- cli.ContainerStart(ctx, resp.ID, container.StartOptions{}) }() - if s.frps != nil { - healthCheck, frpcService, err := frpc.GetService(frpc.FrpcConnectParams{ - ServerDomain: s.frps.Domain, - ServerPort: int(s.frps.Port), - Name: fmt.Sprintf("daytona-server-registry-%s", s.serverId), - Port: int(s.port), - SubDomain: fmt.Sprintf("registry-%s", s.serverId), - }) + if s.frps == nil { + return <-errChan + } + + healthCheck, frpcService, err := frpc.GetService(frpc.FrpcConnectParams{ + ServerDomain: s.frps.Domain, + ServerPort: int(s.frps.Port), + Name: fmt.Sprintf("daytona-server-registry-%s", s.serverId), + Port: int(s.port), + SubDomain: fmt.Sprintf("registry-%s", s.serverId), + }) + if err != nil { + return err + } + + go func() { + err := frpcService.Run(context.Background()) if err != nil { - return err + errChan <- err } + }() - go func() { - err := frpcService.Run(context.Background()) - if err != nil { - errChan <- err - } - }() - - for i := 0; i < 5; i++ { - if err = healthCheck(); err != nil { - log.Debugf("Failed to connect to registry frpc: %s", err) - time.Sleep(2 * time.Second) - } else { - break - } - } - if err != nil { - return err + for i := 0; i < 5; i++ { + if err = healthCheck(); err != nil { + log.Debugf("Failed to connect to registry frpc: %s", err) + time.Sleep(2 * time.Second) + } else { + break } } + if err != nil { + return err + } return <-errChan }