Skip to content

Commit 97c3f24

Browse files
committed
Moved CNM specific logic to CNM package
1 parent d21346c commit 97c3f24

File tree

12 files changed

+368
-319
lines changed

12 files changed

+368
-319
lines changed

cnm/cnm.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
"os/signal"
1010
"syscall"
1111

12+
"github.com/Azure/azure-container-networking/cnm/ipam"
13+
"github.com/Azure/azure-container-networking/cnm/network"
1214
"github.com/Azure/azure-container-networking/common"
13-
"github.com/Azure/azure-container-networking/ipam"
1415
"github.com/Azure/azure-container-networking/log"
15-
"github.com/Azure/azure-container-networking/network"
1616
"github.com/Azure/azure-container-networking/store"
1717
)
1818

@@ -83,11 +83,6 @@ func printVersion() {
8383

8484
// Main is the entry point for CNM plugin.
8585
func main() {
86-
var netPlugin network.NetPlugin
87-
var ipamPlugin ipam.IpamPlugin
88-
var config common.PluginConfig
89-
var err error
90-
9186
// Initialize and parse command line arguments.
9287
common.ParseArgs(&args, printVersion)
9388

@@ -102,20 +97,21 @@ func main() {
10297
}
10398

10499
// Initialize plugin common configuration.
100+
var config common.PluginConfig
105101
config.Name = name
106102
config.Version = version
107103

108104
// Create network plugin.
109-
netPlugin, err = network.NewPlugin(&config)
105+
netPlugin, err := network.NewPlugin(&config)
110106
if err != nil {
111-
fmt.Printf("Failed to create network plugin %v\n", err)
107+
fmt.Printf("Failed to create network plugin, err:%v.\n", err)
112108
return
113109
}
114110

115111
// Create IPAM plugin.
116-
ipamPlugin, err = ipam.NewPlugin(&config)
112+
ipamPlugin, err := ipam.NewPlugin(&config)
117113
if err != nil {
118-
fmt.Printf("Failed to create IPAM plugin %v\n", err)
114+
fmt.Printf("Failed to create IPAM plugin, err:%v.\n", err)
119115
return
120116
}
121117

@@ -148,15 +144,15 @@ func main() {
148144
if netPlugin != nil {
149145
err = netPlugin.Start(&config)
150146
if err != nil {
151-
fmt.Printf("Failed to start network plugin %v\n", err)
147+
fmt.Printf("Failed to start network plugin, err:%v.\n", err)
152148
return
153149
}
154150
}
155151

156152
if ipamPlugin != nil {
157153
err = ipamPlugin.Start(&config)
158154
if err != nil {
159-
fmt.Printf("Failed to start IPAM plugin %v\n", err)
155+
fmt.Printf("Failed to start IPAM plugin, err:%v.\n", err)
160156
return
161157
}
162158
}

cnm/ipam/api.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright Microsoft Corp.
2+
// All rights reserved.
3+
4+
package ipam
5+
6+
const (
7+
// Libnetwork IPAM plugin endpoint type
8+
EndpointType = "IpamDriver"
9+
10+
// Libnetwork IPAM plugin remote API paths
11+
getCapabilitiesPath = "/IpamDriver.GetCapabilities"
12+
getAddressSpacesPath = "/IpamDriver.GetDefaultAddressSpaces"
13+
requestPoolPath = "/IpamDriver.RequestPool"
14+
releasePoolPath = "/IpamDriver.ReleasePool"
15+
requestAddressPath = "/IpamDriver.RequestAddress"
16+
releaseAddressPath = "/IpamDriver.ReleaseAddress"
17+
)
18+
19+
// Request sent by libnetwork when querying plugin capabilities.
20+
type getCapabilitiesRequest struct {
21+
}
22+
23+
// Response sent by plugin when registering its capabilities with libnetwork.
24+
type getCapabilitiesResponse struct {
25+
RequiresMACAddress bool
26+
RequiresRequestReplay bool
27+
}
28+
29+
// Request sent by libnetwork when querying the default address space names.
30+
type getDefaultAddressSpacesRequest struct {
31+
}
32+
33+
// Response sent by plugin when returning the default address space names.
34+
type getDefaultAddressSpacesResponse struct {
35+
LocalDefaultAddressSpace string
36+
GlobalDefaultAddressSpace string
37+
}
38+
39+
// Request sent by libnetwork when acquiring a reference to an address pool.
40+
type requestPoolRequest struct {
41+
AddressSpace string
42+
Pool string
43+
SubPool string
44+
Options map[string]string
45+
V6 bool
46+
}
47+
48+
// Response sent by plugin when an address pool is successfully referenced.
49+
type requestPoolResponse struct {
50+
PoolID string
51+
Pool string
52+
Data map[string]string
53+
}
54+
55+
// Request sent by libnetwork when releasing a previously registered address pool.
56+
type releasePoolRequest struct {
57+
PoolID string
58+
}
59+
60+
// Response sent by plugin when an address pool is successfully released.
61+
type releasePoolResponse struct {
62+
}
63+
64+
// Request sent by libnetwork when reserving an address from a pool.
65+
type requestAddressRequest struct {
66+
PoolID string
67+
Address string
68+
Options map[string]string
69+
}
70+
71+
// Response sent by plugin when an address is successfully reserved.
72+
type requestAddressResponse struct {
73+
Address string
74+
Data map[string]string
75+
}
76+
77+
// Request sent by libnetwork when releasing an address back to the pool.
78+
type releaseAddressRequest struct {
79+
PoolID string
80+
Address string
81+
}
82+
83+
// Response sent by plugin when an address is successfully released.
84+
type releaseAddressResponse struct {
85+
}

ipam/plugin.go renamed to cnm/ipam/ipam.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,41 @@ package ipam
66
import (
77
"net/http"
88

9+
"github.com/Azure/azure-container-networking/cnm/plugin"
910
"github.com/Azure/azure-container-networking/common"
11+
"github.com/Azure/azure-container-networking/ipam"
1012
"github.com/Azure/azure-container-networking/log"
1113
)
1214

1315
const (
1416
// Plugin name.
1517
name = "ipam"
1618

17-
// Libnetwork IPAM plugin capabilities.
19+
// Plugin capabilities reported to libnetwork.
1820
requiresMACAddress = false
1921
requiresRequestReplay = false
2022
)
2123

22-
// IpamPlugin object and interface
24+
// IpamPlugin represents a CNM (libnetwork) IPAM plugin.
2325
type ipamPlugin struct {
24-
*common.Plugin
25-
am AddressManager
26+
*plugin.Plugin
27+
am ipam.AddressManager
2628
}
2729

2830
type IpamPlugin interface {
2931
common.PluginApi
3032
}
3133

32-
// Creates a new IpamPlugin object.
34+
// NewPlugin creates a new IpamPlugin object.
3335
func NewPlugin(config *common.PluginConfig) (IpamPlugin, error) {
3436
// Setup base plugin.
35-
plugin, err := common.NewPlugin(name, config.Version, endpointType)
37+
plugin, err := plugin.NewPlugin(name, config.Version, EndpointType)
3638
if err != nil {
3739
return nil, err
3840
}
3941

4042
// Setup address manager.
41-
am, err := NewAddressManager()
43+
am, err := ipam.NewAddressManager()
4244
if err != nil {
4345
return nil, err
4446
}
@@ -51,7 +53,7 @@ func NewPlugin(config *common.PluginConfig) (IpamPlugin, error) {
5153
}, nil
5254
}
5355

54-
// Starts the plugin.
56+
// Start starts the plugin.
5557
func (plugin *ipamPlugin) Start(config *common.PluginConfig) error {
5658
// Initialize base plugin.
5759
err := plugin.Initialize(config)
@@ -82,7 +84,7 @@ func (plugin *ipamPlugin) Start(config *common.PluginConfig) error {
8284
return nil
8385
}
8486

85-
// Stops the plugin.
87+
// Stop stops the plugin.
8688
func (plugin *ipamPlugin) Stop() {
8789
plugin.am.Uninitialize()
8890
plugin.Uninitialize()
@@ -147,7 +149,7 @@ func (plugin *ipamPlugin) requestPool(w http.ResponseWriter, r *http.Request) {
147149

148150
// Encode response.
149151
data := make(map[string]string)
150-
poolId = NewAddressPoolId(req.AddressSpace, poolId, "").String()
152+
poolId = ipam.NewAddressPoolId(req.AddressSpace, poolId, "").String()
151153
resp := requestPoolResponse{PoolID: poolId, Pool: subnet, Data: data}
152154

153155
err = plugin.Listener.Encode(w, &resp)
@@ -167,7 +169,7 @@ func (plugin *ipamPlugin) releasePool(w http.ResponseWriter, r *http.Request) {
167169
}
168170

169171
// Process request.
170-
poolId, err := NewAddressPoolIdFromString(req.PoolID)
172+
poolId, err := ipam.NewAddressPoolIdFromString(req.PoolID)
171173
if err != nil {
172174
plugin.SendErrorResponse(w, err)
173175
return
@@ -199,7 +201,7 @@ func (plugin *ipamPlugin) requestAddress(w http.ResponseWriter, r *http.Request)
199201
}
200202

201203
// Process request.
202-
poolId, err := NewAddressPoolIdFromString(req.PoolID)
204+
poolId, err := ipam.NewAddressPoolIdFromString(req.PoolID)
203205
if err != nil {
204206
plugin.SendErrorResponse(w, err)
205207
return
@@ -232,7 +234,7 @@ func (plugin *ipamPlugin) releaseAddress(w http.ResponseWriter, r *http.Request)
232234
}
233235

234236
// Process request.
235-
poolId, err := NewAddressPoolIdFromString(req.PoolID)
237+
poolId, err := ipam.NewAddressPoolIdFromString(req.PoolID)
236238
if err != nil {
237239
plugin.SendErrorResponse(w, err)
238240
return

ipam/plugin_test.go renamed to cnm/ipam/ipam_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestMain(m *testing.M) {
5252
mux = plugin.(*ipamPlugin).Listener.GetMux()
5353

5454
// Get the internal config sink interface.
55-
sink = plugin.(*ipamPlugin).am.(*addressManager)
55+
sink = plugin.(*ipamPlugin).am
5656

5757
// Run tests.
5858
exitCode := m.Run()

0 commit comments

Comments
 (0)