diff --git a/cns/common/service.go b/cns/common/service.go index adceae415e5..f37d3bba1a9 100644 --- a/cns/common/service.go +++ b/cns/common/service.go @@ -33,14 +33,20 @@ type ServiceAPI interface { // ServiceConfig specifies common configuration. type ServiceConfig struct { - Name string - Version string - Listener *acn.Listener - ErrChan chan<- error - Store store.KeyValueStore + Name string + Version string + Listener *acn.Listener + ErrChan chan<- error + Store store.KeyValueStore + Server server + ChannelMode string + TLSSettings tls.TlsSettings +} + +// server struct to store primaryInterfaceIP from VM, port where customer provides by -p and temporary flag EnableLocalServer +type server struct { PrimaryInterfaceIP string - ChannelMode string - TLSSettings tls.TlsSettings + Port string EnableLocalServer bool // TODO: Remove this flag once -c option gets deprecated } diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 8cfd648b909..32ad3929f0c 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -1721,7 +1721,7 @@ func startService() error { file.Close() // mock localhost as primary interface IP - config.PrimaryInterfaceIP = "localhost" + config.Server.PrimaryInterfaceIP = "localhost" err = service.Init(&config) if err != nil { @@ -1729,8 +1729,6 @@ func startService() error { return err } - service := service.(*HTTPRestService) - err = service.Start(&config) if err != nil { logger.Errorf("Failed to start CNS, err:%v.\n", err) diff --git a/cns/restserver/restserver.go b/cns/restserver/restserver.go index fe85290be1c..104ab50c575 100644 --- a/cns/restserver/restserver.go +++ b/cns/restserver/restserver.go @@ -186,7 +186,7 @@ func NewHTTPRestService(config *common.ServiceConfig, wscli interfaceGetter, wsp } // add primaryInterfaceIP to cns config - config.PrimaryInterfaceIP = primaryInterface.PrimaryIP + config.Server.PrimaryInterfaceIP = primaryInterface.PrimaryIP serviceState := &httpRestServiceState{ Networks: make(map[string]*networkInfo), diff --git a/cns/restserver/v2/server.go b/cns/restserver/v2/server.go index 8a3f9c53d08..c8ca1aaf993 100644 --- a/cns/restserver/v2/server.go +++ b/cns/restserver/v2/server.go @@ -45,7 +45,7 @@ func (s Server) Start(ctx context.Context, addr string) error { return errors.Wrap(err, "failed to start echo server") } - // after context is done, shutdown echo server + // after context is done, shutdown local server <-ctx.Done() if err := e.Shutdown(ctx); err != nil { logger.Errorf("failed to shutdown echo server due to %+v", err) diff --git a/cns/service.go b/cns/service.go index f53cce2b803..ba190e75950 100644 --- a/cns/service.go +++ b/cns/service.go @@ -62,13 +62,14 @@ func (service *Service) AddListener(config *common.ServiceConfig) error { cnsPort, hasPort := service.GetOption(acn.OptCnsPort).(string) if !hasURL { - config.EnableLocalServer = true + config.Server.EnableLocalServer = true // get VM primary interface's private IP // if customer does use -p option, then use port number customers provide if hasPort { - nodeURL, err = url.Parse(fmt.Sprintf("tcp://%s:%s", config.PrimaryInterfaceIP, cnsPort)) + config.Server.Port = cnsPort + nodeURL, err = url.Parse(fmt.Sprintf("tcp://%s:%s", config.Server.PrimaryInterfaceIP, cnsPort)) } else { - nodeURL, err = url.Parse(fmt.Sprintf("tcp://%s:%s", config.PrimaryInterfaceIP, defaultAPIServerPort)) + nodeURL, err = url.Parse(fmt.Sprintf("tcp://%s:%s", config.Server.PrimaryInterfaceIP, defaultAPIServerPort)) } if err != nil { @@ -79,7 +80,7 @@ func (service *Service) AddListener(config *common.ServiceConfig) error { logger.Warnf("Do not specify cns-url by -c option, this option will be deprecated!") // do not enable local server if customer uses -c option - config.EnableLocalServer = false + config.Server.EnableLocalServer = false nodeURL, err = url.Parse(cnsURL) if err != nil { return errors.Wrap(err, "Failed to parse URL that customer provides") diff --git a/cns/service/main.go b/cns/service/main.go index a7859bfde67..91da1cda1b0 100644 --- a/cns/service/main.go +++ b/cns/service/main.go @@ -102,8 +102,8 @@ const ( // envVarEnableCNIConflistGeneration enables cni conflist generation if set (value doesn't matter) envVarEnableCNIConflistGeneration = "CNS_ENABLE_CNI_CONFLIST_GENERATION" - cnsReqTimeout = 15 * time.Second - defaultAPIServerURL = "localhost:10090" + cnsReqTimeout = 15 * time.Second + defaultAPIServerIP = "localhost" ) type cniConflistScenario string @@ -887,11 +887,12 @@ func main() { // if user does not provide cns url by -c option, then start http local server // TODO: we will deprecated -c option in next phase and start local server in any case - if config.EnableLocalServer { + if config.Server.EnableLocalServer { logger.Printf("[Azure CNS] Start HTTP local server") httpLocalRestService := restserverv2.New(httpRemoteRestService) if httpLocalRestService != nil { + defaultAPIServerURL := fmt.Sprintf(defaultAPIServerIP + ":" + config.Server.Port) go func() { err = httpLocalRestService.Start(rootCtx, defaultAPIServerURL) if err != nil {