Moved common plugin functionality to base class
This commit is contained in:
Родитель
3d665c42bc
Коммит
cb7b07744d
|
@ -0,0 +1,23 @@
|
|||
// Copyright Microsoft Corp.
|
||||
// All rights reserved.
|
||||
|
||||
package common
|
||||
|
||||
const (
|
||||
// Libnetwork remote plugin paths
|
||||
activatePath = "/Plugin.Activate"
|
||||
)
|
||||
|
||||
//
|
||||
// Libnetwork remote plugin API
|
||||
//
|
||||
|
||||
// Error response sent by plugin when a request was decoded but failed.
|
||||
type errorResponse struct {
|
||||
Err string
|
||||
}
|
||||
|
||||
// Response sent by plugin for activation.
|
||||
type activateResponse struct {
|
||||
Implements []string
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
// Copyright Microsoft Corp.
|
||||
// All rights reserved.
|
||||
|
||||
package core
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
|
@ -0,0 +1,69 @@
|
|||
// Copyright Microsoft Corp.
|
||||
// All rights reserved.
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/Azure/Aqua/log"
|
||||
)
|
||||
|
||||
// Plugin object and interface
|
||||
type Plugin struct {
|
||||
Name string
|
||||
Version string
|
||||
Scope string
|
||||
EndpointType string
|
||||
Listener *Listener
|
||||
}
|
||||
|
||||
// Creates a new Plugin object.
|
||||
func NewPlugin(name, version, scope, endpointType string) (*Plugin, error) {
|
||||
return &Plugin{
|
||||
Name: name,
|
||||
Version: version,
|
||||
Scope: scope,
|
||||
EndpointType: endpointType,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Initializes the plugin and starts the listener.
|
||||
func (plugin *Plugin) Initialize(errChan chan error) error {
|
||||
var socketName string
|
||||
if plugin.Name != "test" {
|
||||
socketName = plugin.Name
|
||||
}
|
||||
|
||||
// Create the listener.
|
||||
listener, err := NewListener(socketName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add generic protocol handlers.
|
||||
listener.AddHandler("Plugin", "Activate", plugin.activatePlugin)
|
||||
|
||||
plugin.Listener = listener
|
||||
err = listener.Start(errChan)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Uninitializes the plugin.
|
||||
func (plugin *Plugin) Uninitialize() {
|
||||
plugin.Listener.Stop()
|
||||
}
|
||||
|
||||
//
|
||||
// Libnetwork remote plugin API
|
||||
//
|
||||
|
||||
func (plugin *Plugin) activatePlugin(w http.ResponseWriter, r *http.Request) {
|
||||
log.Request(plugin.Name, "Activate", nil, nil)
|
||||
|
||||
resp := &activateResponse{[]string{plugin.EndpointType}}
|
||||
err := plugin.Listener.Encode(w, resp)
|
||||
|
||||
log.Response(plugin.Name, "Activate", resp, err)
|
||||
}
|
103
ipam/plugin.go
103
ipam/plugin.go
|
@ -7,7 +7,7 @@ import (
|
|||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/Azure/Aqua/core"
|
||||
"github.com/Azure/Aqua/common"
|
||||
"github.com/Azure/Aqua/log"
|
||||
)
|
||||
|
||||
|
@ -16,10 +16,7 @@ const endpointType = "IpamDriver"
|
|||
|
||||
// IpamPlugin object and interface
|
||||
type ipamPlugin struct {
|
||||
name string
|
||||
version string
|
||||
scope string
|
||||
listener *core.Listener
|
||||
common.Plugin
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
|
@ -31,28 +28,25 @@ type IpamPlugin interface {
|
|||
// Creates a new IpamPlugin object.
|
||||
func NewPlugin(name string, version string) (IpamPlugin, error) {
|
||||
return &ipamPlugin{
|
||||
name: name,
|
||||
version: version,
|
||||
scope: "local",
|
||||
Plugin: common.Plugin{
|
||||
Name: name,
|
||||
Version: version,
|
||||
Scope: "local",
|
||||
EndpointType: endpointType,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Starts the plugin.
|
||||
func (plugin *ipamPlugin) Start(errChan chan error) error {
|
||||
var socketName string
|
||||
if plugin.name != "test" {
|
||||
socketName = plugin.name
|
||||
}
|
||||
|
||||
// Create the listener.
|
||||
listener, err := core.NewListener(socketName)
|
||||
err := plugin.Initialize(errChan)
|
||||
if err != nil {
|
||||
log.Printf("Failed to create listener %v", err)
|
||||
log.Printf("%s: Failed to start: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Add protocol handlers.
|
||||
listener.AddHandler("Plugin", "Activate", plugin.activatePlugin)
|
||||
listener := plugin.Listener
|
||||
listener.AddHandler(endpointType, "GetCapabilities", plugin.getCapabilities)
|
||||
listener.AddHandler(endpointType, "GetDefaultAddressSpaces", plugin.getDefaultAddressSpaces)
|
||||
listener.AddHandler(endpointType, "RequestPool", plugin.requestPool)
|
||||
|
@ -60,45 +54,28 @@ func (plugin *ipamPlugin) Start(errChan chan error) error {
|
|||
listener.AddHandler(endpointType, "RequestAddress", plugin.requestAddress)
|
||||
listener.AddHandler(endpointType, "ReleaseAddress", plugin.releaseAddress)
|
||||
|
||||
plugin.listener = listener
|
||||
|
||||
err = listener.Start(errChan)
|
||||
if err != nil {
|
||||
log.Printf("Failed to start listener %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("%s: Plugin started.", plugin.name)
|
||||
log.Printf("%s: Plugin started.", plugin.Name)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stops the plugin.
|
||||
func (plugin *ipamPlugin) Stop() {
|
||||
plugin.listener.Stop()
|
||||
log.Printf("%s: Plugin stopped.\n", plugin.name)
|
||||
plugin.Uninitialize()
|
||||
log.Printf("%s: Plugin stopped.\n", plugin.Name)
|
||||
}
|
||||
|
||||
type activateResponse struct {
|
||||
Implements []string
|
||||
}
|
||||
|
||||
func (plugin *ipamPlugin) activatePlugin(w http.ResponseWriter, r *http.Request) {
|
||||
log.Request(plugin.name, "Activate", nil, nil)
|
||||
|
||||
resp := &activateResponse{[]string{endpointType}}
|
||||
err := plugin.listener.Encode(w, resp)
|
||||
|
||||
log.Response(plugin.name, "Activate", resp, err)
|
||||
}
|
||||
//
|
||||
// Libnetwork remote IPAM plugin APIs
|
||||
//
|
||||
|
||||
func (plugin *ipamPlugin) getCapabilities(w http.ResponseWriter, r *http.Request) {
|
||||
log.Request(plugin.name, "GetCapabilities", nil, nil)
|
||||
log.Request(plugin.Name, "GetCapabilities", nil, nil)
|
||||
|
||||
resp := map[string]string{"Scope": plugin.scope}
|
||||
err := plugin.listener.Encode(w, resp)
|
||||
resp := map[string]string{"Scope": plugin.Scope}
|
||||
err := plugin.Listener.Encode(w, resp)
|
||||
|
||||
log.Response(plugin.name, "GetCapabilities", resp, err)
|
||||
log.Response(plugin.Name, "GetCapabilities", resp, err)
|
||||
}
|
||||
|
||||
type defaultAddressSpacesResponseFormat struct {
|
||||
|
@ -107,16 +84,16 @@ type defaultAddressSpacesResponseFormat struct {
|
|||
}
|
||||
|
||||
func (plugin *ipamPlugin) getDefaultAddressSpaces(w http.ResponseWriter, r *http.Request) {
|
||||
log.Request(plugin.name, "GetDefaultAddressSpaces", nil, nil)
|
||||
log.Request(plugin.Name, "GetDefaultAddressSpaces", nil, nil)
|
||||
|
||||
resp := &defaultAddressSpacesResponseFormat{
|
||||
LocalDefaultAddressSpace: "",
|
||||
GlobalDefaultAddressSpace: "",
|
||||
}
|
||||
|
||||
err := plugin.listener.Encode(w, resp)
|
||||
err := plugin.Listener.Encode(w, resp)
|
||||
|
||||
log.Response(plugin.name, "GetDefaultAddressSpaces", resp, err)
|
||||
log.Response(plugin.Name, "GetDefaultAddressSpaces", resp, err)
|
||||
}
|
||||
|
||||
type requestPoolRequestFormat struct {
|
||||
|
@ -136,17 +113,17 @@ type requestPoolResponseFormat struct {
|
|||
func (plugin *ipamPlugin) requestPool(w http.ResponseWriter, r *http.Request) {
|
||||
var req requestPoolRequestFormat
|
||||
|
||||
err := plugin.listener.Decode(w, r, &req)
|
||||
err := plugin.Listener.Decode(w, r, &req)
|
||||
|
||||
log.Request(plugin.name, "RequestPool", req, err)
|
||||
log.Request(plugin.Name, "RequestPool", req, err)
|
||||
|
||||
if err == nil {
|
||||
data := make(map[string]string)
|
||||
resp := &requestPoolResponseFormat{"", "0.0.0.0/8", data}
|
||||
|
||||
err = plugin.listener.Encode(w, resp)
|
||||
err = plugin.Listener.Encode(w, resp)
|
||||
|
||||
log.Response(plugin.name, "RequestPool", resp, err)
|
||||
log.Response(plugin.Name, "RequestPool", resp, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -160,16 +137,16 @@ type releasePoolResponseFormat struct {
|
|||
func (plugin *ipamPlugin) releasePool(w http.ResponseWriter, r *http.Request) {
|
||||
var req releasePoolRequestFormat
|
||||
|
||||
err := plugin.listener.Decode(w, r, &req)
|
||||
err := plugin.Listener.Decode(w, r, &req)
|
||||
|
||||
log.Request(plugin.name, "ReleasePool", req, err)
|
||||
log.Request(plugin.Name, "ReleasePool", req, err)
|
||||
|
||||
if err == nil {
|
||||
resp := &releasePoolRequestFormat{}
|
||||
|
||||
err = plugin.listener.Encode(w, resp)
|
||||
err = plugin.Listener.Encode(w, resp)
|
||||
|
||||
log.Response(plugin.name, "ReleasePool", resp, err)
|
||||
log.Response(plugin.Name, "ReleasePool", resp, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -188,16 +165,16 @@ type requestAddressResponseFormat struct {
|
|||
func (plugin *ipamPlugin) requestAddress(w http.ResponseWriter, r *http.Request) {
|
||||
var req requestAddressRequestFormat
|
||||
|
||||
err := plugin.listener.Decode(w, r, &req)
|
||||
err := plugin.Listener.Decode(w, r, &req)
|
||||
|
||||
log.Request(plugin.name, "RequestAddress", req, err)
|
||||
log.Request(plugin.Name, "RequestAddress", req, err)
|
||||
|
||||
if err == nil {
|
||||
resp := &requestAddressResponseFormat{"", "", make(map[string]string)}
|
||||
|
||||
err = plugin.listener.Encode(w, resp)
|
||||
err = plugin.Listener.Encode(w, resp)
|
||||
|
||||
log.Response(plugin.name, "RequestAddress", resp, err)
|
||||
log.Response(plugin.Name, "RequestAddress", resp, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -209,15 +186,15 @@ type releaseAddressRequestFormat struct {
|
|||
func (plugin *ipamPlugin) releaseAddress(w http.ResponseWriter, r *http.Request) {
|
||||
var req releaseAddressRequestFormat
|
||||
|
||||
err := plugin.listener.Decode(w, r, &req)
|
||||
err := plugin.Listener.Decode(w, r, &req)
|
||||
|
||||
log.Request(plugin.name, "ReleaseAddress", req, err)
|
||||
log.Request(plugin.Name, "ReleaseAddress", req, err)
|
||||
|
||||
if err == nil {
|
||||
resp := map[string]string{}
|
||||
|
||||
err = plugin.listener.Encode(w, resp)
|
||||
err = plugin.Listener.Encode(w, resp)
|
||||
|
||||
log.Response(plugin.name, "ReleaseAddress", resp, err)
|
||||
log.Response(plugin.Name, "ReleaseAddress", resp, err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ func TestMain(m *testing.M) {
|
|||
}
|
||||
|
||||
// Get the internal http mux as test hook.
|
||||
mux = plugin.(*ipamPlugin).listener.GetMux()
|
||||
mux = plugin.(*ipamPlugin).Listener.GetMux()
|
||||
|
||||
// Run tests.
|
||||
exitCode := m.Run()
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"sync"
|
||||
|
||||
"github.com/Azure/Aqua/core"
|
||||
"github.com/Azure/Aqua/common"
|
||||
"github.com/Azure/Aqua/log"
|
||||
)
|
||||
|
||||
|
@ -20,7 +21,7 @@ type netPlugin struct {
|
|||
name string
|
||||
version string
|
||||
scope string
|
||||
listener *core.Listener
|
||||
listener *common.Listener
|
||||
networks map[string]*azureNetwork
|
||||
sync.Mutex
|
||||
}
|
||||
|
@ -47,7 +48,7 @@ func (plugin *netPlugin) Start(errChan chan error) error {
|
|||
}
|
||||
|
||||
// Create the listener.
|
||||
listener, err := core.NewListener(socketName)
|
||||
listener, err := common.NewListener(socketName)
|
||||
if err != nil {
|
||||
log.Printf("Failed to create listener %v", err)
|
||||
return err
|
||||
|
|
Загрузка…
Ссылка в новой задаче