2017-07-06 08:31:49 +03:00
|
|
|
// Copyright 2017 Microsoft. All rights reserved.
|
|
|
|
// MIT License
|
|
|
|
|
|
|
|
package cns
|
|
|
|
|
|
|
|
import (
|
2021-03-02 20:01:49 +03:00
|
|
|
"fmt"
|
|
|
|
"github.com/Azure/azure-container-networking/cns/logger"
|
2017-07-06 08:31:49 +03:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-12-01 01:05:25 +03:00
|
|
|
"strings"
|
2017-07-06 08:31:49 +03:00
|
|
|
|
|
|
|
"github.com/Azure/azure-container-networking/cns/common"
|
|
|
|
acn "github.com/Azure/azure-container-networking/common"
|
|
|
|
"github.com/Azure/azure-container-networking/log"
|
|
|
|
"github.com/Azure/azure-container-networking/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Default CNS server URL.
|
|
|
|
defaultAPIServerURL = "tcp://localhost:10090"
|
|
|
|
genericData = "com.microsoft.azure.network.generic"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Service defines Container Networking Service.
|
|
|
|
type Service struct {
|
|
|
|
*common.Service
|
|
|
|
EndpointType string
|
|
|
|
Listener *acn.Listener
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates a new Service object.
|
2020-07-23 23:03:10 +03:00
|
|
|
func NewService(name, version, channelMode string, store store.KeyValueStore) (*Service, error) {
|
|
|
|
service, err := common.NewService(name, version, channelMode, store)
|
2017-07-06 08:31:49 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Service{
|
|
|
|
Service: service,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAPIServerURL returns the API server URL.
|
|
|
|
func (service *Service) getAPIServerURL() string {
|
2018-03-29 01:03:58 +03:00
|
|
|
urls, _ := service.GetOption(acn.OptCnsURL).(string)
|
2017-07-06 08:31:49 +03:00
|
|
|
if urls == "" {
|
|
|
|
urls = defaultAPIServerURL
|
|
|
|
}
|
|
|
|
|
|
|
|
return urls
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize initializes the service and starts the listener.
|
|
|
|
func (service *Service) Initialize(config *common.ServiceConfig) error {
|
|
|
|
log.Debugf("[Azure CNS] Going to initialize a service with config: %+v", config)
|
|
|
|
|
|
|
|
// Initialize the base service.
|
|
|
|
service.Service.Initialize(config)
|
|
|
|
|
|
|
|
// Initialize the listener.
|
|
|
|
if config.Listener == nil {
|
|
|
|
// Fetch and parse the API server URL.
|
|
|
|
u, err := url.Parse(service.getAPIServerURL())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Create the listener.
|
|
|
|
listener, err := acn.NewListener(u)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-01 01:05:25 +03:00
|
|
|
if config.TlsSettings.TLSPort != "" {
|
|
|
|
// listener.URL.Host will always be hostname:port, passed in to CNS via CNS command
|
|
|
|
// else it will default to localhost
|
|
|
|
// extract hostname and override tls port.
|
|
|
|
hostParts := strings.Split(listener.URL.Host, ":")
|
|
|
|
config.TlsSettings.TLSEndpoint = hostParts[0] + ":" + config.TlsSettings.TLSPort
|
2020-11-06 20:51:30 +03:00
|
|
|
// Start the listener and HTTP and HTTPS server.
|
|
|
|
if err = listener.StartTLS(config.ErrChan, config.TlsSettings); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 20:01:49 +03:00
|
|
|
|
|
|
|
logger.Printf("HTTP listener will be started later after CNS state has been reconciled")
|
|
|
|
config.Listener = listener
|
|
|
|
}
|
|
|
|
|
|
|
|
service.Listener = config.Listener
|
|
|
|
|
|
|
|
log.Debugf("[Azure CNS] Successfully initialized a service with config: %+v", config)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (service *Service) StartListener(config *common.ServiceConfig) error {
|
|
|
|
log.Debugf("[Azure CNS] Going to start listener: %+v", config)
|
|
|
|
|
|
|
|
// Initialize the listener.
|
|
|
|
if service.Listener != nil {
|
|
|
|
log.Debugf("[Azure CNS] Starting listener: %+v", config)
|
2017-07-06 08:31:49 +03:00
|
|
|
// Start the listener.
|
2020-11-06 20:51:30 +03:00
|
|
|
// continue to listen on the normal endpoint for http traffic, this will be supported
|
|
|
|
// for sometime until partners migrate fully to https
|
2021-03-02 20:01:49 +03:00
|
|
|
if err := service.Listener.Start(config.ErrChan); err != nil {
|
2017-07-06 08:31:49 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-03-02 20:01:49 +03:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Failed to start a listener, it is not initialized, config %+v", config)
|
2017-07-06 08:31:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uninitialize cleans up the plugin.
|
|
|
|
func (service *Service) Uninitialize() {
|
|
|
|
service.Listener.Stop()
|
|
|
|
service.Service.Uninitialize()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseOptions returns generic options from a libnetwork request.
|
|
|
|
func (service *Service) ParseOptions(options OptionMap) OptionMap {
|
|
|
|
opt, _ := options[genericData].(OptionMap)
|
|
|
|
return opt
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendErrorResponse sends and logs an error response.
|
|
|
|
func (service *Service) SendErrorResponse(w http.ResponseWriter, errMsg error) {
|
|
|
|
resp := errorResponse{errMsg.Error()}
|
|
|
|
err := service.Listener.Encode(w, &resp)
|
2018-12-12 21:45:48 +03:00
|
|
|
log.Errorf("[%s] %+v %s.", service.Name, &resp, err.Error())
|
2017-07-06 08:31:49 +03:00
|
|
|
}
|