Skip to content

Commit

Permalink
fix comments and use -p select from customer to start local server
Browse files Browse the repository at this point in the history
  • Loading branch information
paulyufan2 committed Apr 12, 2024
1 parent f61984e commit aa89292
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 19 deletions.
20 changes: 13 additions & 7 deletions cns/common/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 1 addition & 3 deletions cns/restserver/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1721,16 +1721,14 @@ 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 {
logger.Errorf("Failed to Init CNS, err:%v.\n", err)
return err
}

service := service.(*HTTPRestService)

err = service.Start(&config)
if err != nil {
logger.Errorf("Failed to start CNS, err:%v.\n", err)
Expand Down
2 changes: 1 addition & 1 deletion cns/restserver/restserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion cns/restserver/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions cns/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand Down
7 changes: 4 additions & 3 deletions cns/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit aa89292

Please sign in to comment.