Skip to content

Commit 6373b2b

Browse files
committed
Added support for sharing the same listener between CNM net and IPAM plugins
1 parent 25495a6 commit 6373b2b

File tree

5 files changed

+60
-41
lines changed

5 files changed

+60
-41
lines changed

cnm/ipam/ipam.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func (plugin *ipamPlugin) Start(config *common.PluginConfig) error {
7171

7272
// Add protocol handlers.
7373
listener := plugin.Listener
74+
listener.AddEndpoint(plugin.EndpointType)
7475
listener.AddHandler(getCapabilitiesPath, plugin.getCapabilities)
7576
listener.AddHandler(getAddressSpacesPath, plugin.getDefaultAddressSpaces)
7677
listener.AddHandler(requestPoolPath, plugin.requestPool)

cnm/network/network.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func (plugin *netPlugin) Start(config *common.PluginConfig) error {
7676

7777
// Add protocol handlers.
7878
listener := plugin.Listener
79+
listener.AddEndpoint(plugin.EndpointType)
7980
listener.AddHandler(getCapabilitiesPath, plugin.getCapabilities)
8081
listener.AddHandler(createNetworkPath, plugin.createNetwork)
8182
listener.AddHandler(deleteNetworkPath, plugin.deleteNetwork)

cnm/plugin.go

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,36 @@ func (plugin *Plugin) Initialize(config *common.PluginConfig) error {
3838
// Initialize the base plugin.
3939
plugin.Plugin.Initialize(config)
4040

41-
// Create the plugin path.
42-
os.MkdirAll(pluginPath, 0660)
43-
44-
// Create the listener.
45-
var localAddr string
46-
if plugin.Name != "test" {
47-
localAddr = path.Join(pluginPath, config.Name+plugin.Name)
41+
if config.Listener == nil {
42+
// Create the plugin path.
43+
os.MkdirAll(pluginPath, 0660)
44+
45+
// Create the listener.
46+
var localAddr string
47+
if plugin.Name != "test" {
48+
localAddr = path.Join(pluginPath, config.Name+plugin.Name)
49+
}
50+
51+
listener, err := common.NewListener("unix", localAddr)
52+
if err != nil {
53+
return err
54+
}
55+
56+
// Add generic protocol handlers.
57+
listener.AddHandler(activatePath, plugin.activate)
58+
59+
// Start the listener.
60+
err = listener.Start(config.ErrChan)
61+
if err != nil {
62+
return err
63+
}
64+
65+
config.Listener = listener
4866
}
4967

50-
listener, err := common.NewListener("unix", localAddr)
51-
if err != nil {
52-
return err
53-
}
54-
55-
// Add generic protocol handlers.
56-
listener.AddHandler(activatePath, plugin.activate)
57-
58-
// Start the listener.
59-
err = listener.Start(config.ErrChan)
60-
plugin.Listener = listener
68+
plugin.Listener = config.Listener
6169

62-
return err
70+
return nil
6371
}
6472

6573
// Uninitializes the plugin.
@@ -78,7 +86,7 @@ func (plugin *Plugin) activate(w http.ResponseWriter, r *http.Request) {
7886

7987
log.Request(plugin.Name, &req, nil)
8088

81-
resp := activateResponse{[]string{plugin.EndpointType}}
89+
resp := activateResponse{Implements: plugin.Listener.GetEndpoints()}
8290
err := plugin.Listener.Encode(w, &resp)
8391

8492
log.Response(plugin.Name, &resp, err)

common/listener.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ import (
1313
"github.com/Azure/azure-container-networking/log"
1414
)
1515

16-
// Listener object
16+
// Listener represents an HTTP listener.
1717
type Listener struct {
1818
protocol string
1919
localAddress string
20+
endpoints []string
21+
active bool
2022
l net.Listener
2123
mux *http.ServeMux
2224
}
2325

24-
// Creates a new Listener.
26+
// NewListener creates a new Listener.
2527
func NewListener(protocol string, localAddress string) (*Listener, error) {
2628
listener := Listener{
2729
protocol: protocol,
@@ -59,16 +61,17 @@ func (listener *Listener) Start(errChan chan error) error {
5961
errChan <- http.Serve(listener.l, listener.mux)
6062
}()
6163

64+
listener.active = true
6265
return nil
6366
}
6467

65-
// Stops listening for requests from libnetwork.
68+
// Stop stops listening for requests.
6669
func (listener *Listener) Stop() {
67-
68-
// Succeed early if no socket was requested.
69-
if listener.localAddress == "" {
70+
// Ignore if not active.
71+
if !listener.active {
7072
return
7173
}
74+
listener.active = false
7275

7376
// Stop servicing requests.
7477
listener.l.Close()
@@ -81,17 +84,27 @@ func (listener *Listener) Stop() {
8184
log.Printf("[Listener] Stopped listening on %s", listener.localAddress)
8285
}
8386

84-
// Returns the HTTP mux for the listener.
87+
// GetMux returns the HTTP mux for the listener.
8588
func (listener *Listener) GetMux() *http.ServeMux {
8689
return listener.mux
8790
}
8891

89-
// Registers a protocol handler.
92+
// GetEndpoints returns the list of registered protocol endpoints.
93+
func (listener *Listener) GetEndpoints() []string {
94+
return listener.endpoints
95+
}
96+
97+
// AddEndpoint registers a protocol endpoint.
98+
func (listener *Listener) AddEndpoint(endpoint string) {
99+
listener.endpoints = append(listener.endpoints, endpoint)
100+
}
101+
102+
// AddHandler registers a protocol handler.
90103
func (listener *Listener) AddHandler(path string, handler func(http.ResponseWriter, *http.Request)) {
91104
listener.mux.HandleFunc(path, handler)
92105
}
93106

94-
// Decodes JSON payload.
107+
// Decode receives and decodes JSON payload to a request.
95108
func (listener *Listener) Decode(w http.ResponseWriter, r *http.Request, request interface{}) error {
96109
var err error
97110

@@ -108,7 +121,7 @@ func (listener *Listener) Decode(w http.ResponseWriter, r *http.Request, request
108121
return err
109122
}
110123

111-
// Encodes JSON payload.
124+
// Encode encodes and sends a response as JSON payload.
112125
func (listener *Listener) Encode(w http.ResponseWriter, response interface{}) error {
113126
err := json.NewEncoder(w).Encode(response)
114127
if err != nil {
@@ -117,8 +130,3 @@ func (listener *Listener) Encode(w http.ResponseWriter, response interface{}) er
117130
}
118131
return err
119132
}
120-
121-
// Sends an error response.
122-
func (listener *Listener) SendError(w http.ResponseWriter, errMessage string) {
123-
json.NewEncoder(w).Encode(map[string]string{"Err": errMessage})
124-
}

common/plugin.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ type PluginApi interface {
2626

2727
// Plugin common configuration.
2828
type PluginConfig struct {
29-
Name string
30-
Version string
31-
NetApi interface{}
32-
IpamApi interface{}
33-
ErrChan chan error
34-
Store store.KeyValueStore
29+
Name string
30+
Version string
31+
NetApi interface{}
32+
IpamApi interface{}
33+
Listener *Listener
34+
ErrChan chan error
35+
Store store.KeyValueStore
3536
}
3637

3738
// NewPlugin creates a new Plugin object.

0 commit comments

Comments
 (0)