azure-container-networking/cns/common/service.go

102 строки
2.8 KiB
Go
Исходник Обычный вид История

// Copyright 2017 Microsoft. All rights reserved.
// MIT License
package common
import (
"errors"
"github.com/Azure/azure-container-networking/cns/logger"
acn "github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/server/tls"
"github.com/Azure/azure-container-networking/store"
)
// Service implements behavior common to all services.
type Service struct {
Name string
Version string
Options map[string]interface{}
ErrChan chan<- error
Store store.KeyValueStore
ChannelMode string
}
// ServiceAPI defines base interface.
type ServiceAPI interface {
Init(*ServiceConfig) error
Start(*ServiceConfig) error
Stop()
GetOption(string) interface{}
SetOption(string, interface{})
}
// ServiceConfig specifies common configuration.
type ServiceConfig struct {
Name string
Version string
Listener *acn.Listener
ErrChan chan<- error
Store store.KeyValueStore
Start two CNS servers with different handlers (#2650) * start two cns servers * fix linter issues * fix linter issue * enhance echo server log * fix Quang's comments * gofumpt cns service.go * fix defaultAPIServerAddress * run servers asynchoronsly * add server_test.go * fix UT test * enhance log * fix log issue * fix an issue * add a check for defaultAPIServerURL * fix GetOption func * enhance comments * fix defaultAPI URL to 127.0.0.1 * fix restapi method in echo server * remove unnecessary log info * fix comment1 * fix comments * add context to control echo server * fix TM's comments * do not enable local server if user specifies cnsurl by -c option * add -p port check when getting nodeURL * fix comments and use -p select from customer to start local server * fix a case when customer does not provide -p option * fix the issue if customer also does not provide -p and -c option * add UTs to launch server with different combinations * fix linter issue for UTs * fix UT linter issues * UT needs to stop service * remove duplicated test case * comments fix * fix linter issue * fix Tim's comments * fix comments * fix linter issue * handle type assertion * Adding cnsURL and cnsPort options to test server The startService function is a mistake that has been perpetuated through time and needs to be removed. As a consequence of its existence, it requires mixing concerns in all tests and stands in the way of refactoring. The addition of two type assertion checks caused this function to break, because the corresponding options were not set in the Service's configuration. This is a problem in itself--there's no reason the options should be pulled out of a map[string]interface{}. Given this, we can just set these two options in startService, committing sins to offset the ones in service.(*HTTPRestService).Service.Options. --------- Signed-off-by: Paul Yu <129891899+paulyufan2@users.noreply.github.com> Co-authored-by: Tim Raymond <traymond@microsoft.com>
2024-04-30 02:16:58 +03:00
Server server
ChannelMode string
Start two CNS servers with different handlers (#2650) * start two cns servers * fix linter issues * fix linter issue * enhance echo server log * fix Quang's comments * gofumpt cns service.go * fix defaultAPIServerAddress * run servers asynchoronsly * add server_test.go * fix UT test * enhance log * fix log issue * fix an issue * add a check for defaultAPIServerURL * fix GetOption func * enhance comments * fix defaultAPI URL to 127.0.0.1 * fix restapi method in echo server * remove unnecessary log info * fix comment1 * fix comments * add context to control echo server * fix TM's comments * do not enable local server if user specifies cnsurl by -c option * add -p port check when getting nodeURL * fix comments and use -p select from customer to start local server * fix a case when customer does not provide -p option * fix the issue if customer also does not provide -p and -c option * add UTs to launch server with different combinations * fix linter issue for UTs * fix UT linter issues * UT needs to stop service * remove duplicated test case * comments fix * fix linter issue * fix Tim's comments * fix comments * fix linter issue * handle type assertion * Adding cnsURL and cnsPort options to test server The startService function is a mistake that has been perpetuated through time and needs to be removed. As a consequence of its existence, it requires mixing concerns in all tests and stands in the way of refactoring. The addition of two type assertion checks caused this function to break, because the corresponding options were not set in the Service's configuration. This is a problem in itself--there's no reason the options should be pulled out of a map[string]interface{}. Given this, we can just set these two options in startService, committing sins to offset the ones in service.(*HTTPRestService).Service.Options. --------- Signed-off-by: Paul Yu <129891899+paulyufan2@users.noreply.github.com> Co-authored-by: Tim Raymond <traymond@microsoft.com>
2024-04-30 02:16:58 +03:00
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
Port string
EnableLocalServer bool // TODO: Remove this flag once -c option gets deprecated
}
// NewService creates a new Service object.
func NewService(name, version, channelMode string, store store.KeyValueStore) (*Service, error) {
logger.Debugf("[Azure CNS] Going to create a service object with name: %v. version: %v.", name, version)
svc := &Service{
Name: name,
Version: version,
ChannelMode: channelMode,
Options: make(map[string]interface{}),
Store: store,
}
logger.Debugf("[Azure CNS] Finished creating service object with name: %v. version: %v. managed: %s", name, version, channelMode)
return svc, nil
}
// Initialize initializes the service.
func (service *Service) Initialize(config *ServiceConfig) error {
if config == nil {
err := "[Azure CNS Errror] Initialize called with nil ServiceConfig."
logger.Errorf(err)
return errors.New(err)
}
logger.Debugf("[Azure CNS] Going to initialize the service: %+v with config: %+v.", service, config)
service.ErrChan = config.ErrChan
service.Store = config.Store
service.Version = config.Version
service.ChannelMode = config.ChannelMode
logger.Debugf("[Azure CNS] nitialized service: %+v with config: %+v.", service, config)
return nil
}
// Uninitialize cleans up the service.
func (service *Service) Uninitialize() {
}
// GetOption gets the option value for the given key.
func (service *Service) GetOption(key string) interface{} {
return service.Options[key]
}
// SetOption sets the option value for the given key.
func (service *Service) SetOption(key string, value interface{}) {
service.Options[key] = value
}