Added common configuration and kvstore to base plugin

This commit is contained in:
Onur Filiz 2016-09-13 14:21:11 -07:00
Родитель bb58dcba87
Коммит 6d6df51ddf
4 изменённых файлов: 37 добавлений и 14 удалений

Просмотреть файл

@ -7,6 +7,7 @@ import (
"net/http"
"github.com/Azure/Aqua/log"
"github.com/Azure/Aqua/store"
)
// Plugin object and interface
@ -15,9 +16,16 @@ type Plugin struct {
Version string
EndpointType string
Options map[string]string
Store store.KeyValueStore
Listener *Listener
}
// Plugin common configuration.
type PluginConfig struct {
ErrChan chan error
Store store.KeyValueStore
}
// Creates a new Plugin object.
func NewPlugin(name, version, endpointType string) (*Plugin, error) {
return &Plugin{
@ -29,7 +37,7 @@ func NewPlugin(name, version, endpointType string) (*Plugin, error) {
}
// Initializes the plugin and starts the listener.
func (plugin *Plugin) Initialize(errChan chan error) error {
func (plugin *Plugin) Initialize(config *PluginConfig) error {
var socketName string
if plugin.Name != "test" {
socketName = plugin.Name
@ -44,8 +52,12 @@ func (plugin *Plugin) Initialize(errChan chan error) error {
// Add generic protocol handlers.
listener.AddHandler(activatePath, plugin.activate)
// Initialize plugin properties.
plugin.Listener = listener
err = listener.Start(errChan)
plugin.Store = config.Store
// Start the listener.
err = listener.Start(config.ErrChan)
return err
}

Просмотреть файл

@ -25,7 +25,7 @@ type ipamPlugin struct {
}
type IpamPlugin interface {
Start(chan error) error
Start(*common.PluginConfig) error
Stop()
SetOption(string, string)
@ -48,10 +48,11 @@ func NewPlugin(name string, version string) (IpamPlugin, error) {
}
// Starts the plugin.
func (plugin *ipamPlugin) Start(errChan chan error) error {
err := plugin.Initialize(errChan)
func (plugin *ipamPlugin) Start(config *common.PluginConfig) error {
// Initialize base plugin.
err := plugin.Initialize(config)
if err != nil {
log.Printf("%s: Failed to start: %v", plugin.Name, err)
log.Printf("%s: Failed to initialize base plugin: %v", plugin.Name, err)
return err
}

18
main.go
Просмотреть файл

@ -10,9 +10,11 @@ import (
"strings"
"syscall"
"github.com/Azure/Aqua/common"
"github.com/Azure/Aqua/ipam"
"github.com/Azure/Aqua/log"
"github.com/Azure/Aqua/network"
"github.com/Azure/Aqua/store"
)
// Binary version
@ -30,6 +32,7 @@ func printHelp() {
func main() {
var netPlugin network.NetPlugin
var ipamPlugin ipam.IpamPlugin
var config common.PluginConfig
var err error
// Set defaults.
@ -89,7 +92,14 @@ func main() {
}
// Create a channel to receive unhandled errors from the plugins.
errorChan := make(chan error, 1)
config.ErrChan = make(chan error, 1)
// Create the key value store.
config.Store, err = store.NewJsonFileStore("")
if err != nil {
fmt.Printf("Failed to create store: %v\n", err)
return
}
// Create logging provider.
err = log.SetTarget(logTarget)
@ -100,7 +110,7 @@ func main() {
// Start plugins.
if netPlugin != nil {
err = netPlugin.Start(errorChan)
err = netPlugin.Start(&config)
if err != nil {
fmt.Printf("Failed to start network plugin %v\n", err)
return
@ -108,7 +118,7 @@ func main() {
}
if ipamPlugin != nil {
err = ipamPlugin.Start(errorChan)
err = ipamPlugin.Start(&config)
if err != nil {
fmt.Printf("Failed to start IPAM plugin %v\n", err)
return
@ -127,7 +137,7 @@ func main() {
select {
case sig := <-osSignalChannel:
fmt.Printf("\nCaught signal <" + sig.String() + "> shutting down...\n")
case err := <-errorChan:
case err := <-config.ErrChan:
if err != nil {
fmt.Printf("\nReceived unhandled error %v, shutting down...\n", err)
}

Просмотреть файл

@ -25,7 +25,7 @@ type netPlugin struct {
}
type NetPlugin interface {
Start(chan error) error
Start(*common.PluginConfig) error
Stop()
}
@ -51,11 +51,11 @@ func NewPlugin(name string, version string) (NetPlugin, error) {
}
// Starts the plugin.
func (plugin *netPlugin) Start(errChan chan error) error {
func (plugin *netPlugin) Start(config *common.PluginConfig) error {
// Initialize base plugin.
err := plugin.Initialize(errChan)
err := plugin.Initialize(config)
if err != nil {
log.Printf("%s: Failed to start: %v", plugin.Name, err)
log.Printf("%s: Failed to initialize base plugin: %v", plugin.Name, err)
return err
}