Skip to content

Commit baec8e8

Browse files
authored
Merge pull request #40 from Azure/development
Development
2 parents 16d59cf + d8b6546 commit baec8e8

File tree

17 files changed

+641
-294
lines changed

17 files changed

+641
-294
lines changed

cni/cni.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,20 @@ package cni
55

66
import (
77
"encoding/json"
8-
9-
cniSkel "github.com/containernetworking/cni/pkg/skel"
10-
cniTypes "github.com/containernetworking/cni/pkg/types"
118
)
129

13-
// Plugin is the interface implemented by CNI plugins.
14-
type Plugin interface {
15-
Add(args *cniSkel.CmdArgs) error
16-
Delete(args *cniSkel.CmdArgs) error
17-
18-
AddImpl(args *cniSkel.CmdArgs, nwCfg *NetworkConfig) (*cniTypes.Result, error)
19-
DeleteImpl(args *cniSkel.CmdArgs, nwCfg *NetworkConfig) (*cniTypes.Result, error)
20-
}
21-
2210
// NetworkConfig represents the Azure CNI plugin's network configuration.
2311
type NetworkConfig struct {
2412
CniVersion string `json:"cniVersion"`
2513
Name string `json:"name"`
2614
Type string `json:"type"`
27-
Bridge string `json:"bridge"`
28-
IfName string `json:"ifName"`
15+
Bridge string `json:"bridge,omitempty"`
16+
IfName string `json:"ifName,omitempty"`
2917
Ipam struct {
3018
Type string `json:"type"`
31-
AddrSpace string `json:"addressSpace"`
32-
Subnet string `json:"subnet"`
33-
Address string `json:"ipAddress"`
34-
Result string `json:"result"`
19+
AddrSpace string `json:"addressSpace,omitempty"`
20+
Subnet string `json:"subnet,omitempty"`
21+
Address string `json:"ipAddress,omitempty"`
3522
}
3623
}
3724

cni/ipam/ipam.go

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ func (plugin *ipamPlugin) Start(config *common.PluginConfig) error {
6161
}
6262

6363
// Initialize address manager.
64-
environment := plugin.GetOption(common.OptEnvironmentKey)
65-
err = plugin.am.Initialize(config, environment)
64+
err = plugin.am.Initialize(config, plugin.Options)
6665
if err != nil {
6766
log.Printf("[ipam] Failed to initialize address manager, err:%v.", err)
6867
return err
@@ -138,12 +137,7 @@ func (plugin *ipamPlugin) Add(args *cniSkel.CmdArgs) error {
138137
IP4: &cniTypes.IPConfig{IP: *cidr},
139138
}
140139

141-
// Output response.
142-
if nwCfg.Ipam.Result == "" {
143-
result.Print()
144-
} else {
145-
args.Args = result.String()
146-
}
140+
result.Print()
147141

148142
log.Printf("[ipam] ADD succeeded with output %+v.", result)
149143

@@ -164,69 +158,6 @@ func (plugin *ipamPlugin) Delete(args *cniSkel.CmdArgs) error {
164158

165159
log.Printf("[ipam] Read network configuration %+v.", nwCfg)
166160

167-
// Process command.
168-
result, err := plugin.DeleteImpl(args, nwCfg)
169-
if err != nil {
170-
log.Printf("[ipam] Failed to process command: %v.", err)
171-
return nil
172-
}
173-
174-
// Output response.
175-
if result != nil {
176-
result.Print()
177-
}
178-
179-
log.Printf("[ipam] DEL succeeded with output %+v.", result)
180-
181-
return err
182-
}
183-
184-
// AddImpl handles CNI add commands.
185-
func (plugin *ipamPlugin) AddImpl(args *cniSkel.CmdArgs, nwCfg *cni.NetworkConfig) (*cniTypes.Result, error) {
186-
// Assume default address space if not specified.
187-
if nwCfg.Ipam.AddrSpace == "" {
188-
nwCfg.Ipam.AddrSpace = defaultAddressSpaceId
189-
}
190-
191-
// Check if an address pool is specified.
192-
if nwCfg.Ipam.Subnet == "" {
193-
// Allocate an address pool.
194-
poolId, subnet, err := plugin.am.RequestPool(nwCfg.Ipam.AddrSpace, "", "", nil, false)
195-
if err != nil {
196-
log.Printf("[ipam] Failed to allocate pool, err:%v.", err)
197-
return nil, err
198-
}
199-
200-
nwCfg.Ipam.Subnet = subnet
201-
log.Printf("[ipam] Allocated address poolId %v with subnet %v.", poolId, subnet)
202-
}
203-
204-
// Allocate an address for the endpoint.
205-
address, err := plugin.am.RequestAddress(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet, nwCfg.Ipam.Address, nil)
206-
if err != nil {
207-
log.Printf("[ipam] Failed to allocate address, err:%v.", err)
208-
return nil, err
209-
}
210-
211-
log.Printf("[ipam] Allocated address %v.", address)
212-
213-
// Output the result.
214-
ip, cidr, err := net.ParseCIDR(address)
215-
cidr.IP = ip
216-
if err != nil {
217-
log.Printf("[ipam] Failed to parse address, err:%v.", err)
218-
return nil, err
219-
}
220-
221-
result := &cniTypes.Result{
222-
IP4: &cniTypes.IPConfig{IP: *cidr},
223-
}
224-
225-
return result, nil
226-
}
227-
228-
// DeleteImpl handles CNI delete commands.
229-
func (plugin *ipamPlugin) DeleteImpl(args *cniSkel.CmdArgs, nwCfg *cni.NetworkConfig) (*cniTypes.Result, error) {
230161
// Assume default address space if not specified.
231162
if nwCfg.Ipam.AddrSpace == "" {
232163
nwCfg.Ipam.AddrSpace = defaultAddressSpaceId
@@ -238,16 +169,18 @@ func (plugin *ipamPlugin) DeleteImpl(args *cniSkel.CmdArgs, nwCfg *cni.NetworkCo
238169
err := plugin.am.ReleaseAddress(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet, nwCfg.Ipam.Address)
239170
if err != nil {
240171
log.Printf("[cni] Failed to release address, err:%v.", err)
241-
return nil, err
172+
return nil
242173
}
243174
} else {
244175
// Release the pool.
245176
err := plugin.am.ReleasePool(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet)
246177
if err != nil {
247178
log.Printf("[cni] Failed to release pool, err:%v.", err)
248-
return nil, err
179+
return nil
249180
}
250181
}
251182

252-
return nil, nil
183+
log.Printf("[ipam] DEL succeeded.")
184+
185+
return err
253186
}

cni/plugin/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func main() {
8585
common.LogNetworkInterfaces()
8686

8787
// Set plugin options.
88-
ipamPlugin.SetOption(common.OptEnvironmentKey, environment)
88+
ipamPlugin.SetOption(common.OptEnvironment, environment)
8989

9090
// Start plugins.
9191
if netPlugin != nil {

cnm/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
package cnm
55

66
const (
7+
// Libnetwork plugin path
8+
pluginPath = "/run/docker/plugins"
9+
710
// Libnetwork remote plugin paths
811
activatePath = "/Plugin.Activate"
912
)

cnm/ipam/ipam.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ func (plugin *ipamPlugin) Start(config *common.PluginConfig) error {
6363
}
6464

6565
// Initialize address manager.
66-
environment := plugin.GetOption(common.OptEnvironmentKey)
67-
err = plugin.am.Initialize(config, environment)
66+
err = plugin.am.Initialize(config, plugin.Options)
6867
if err != nil {
6968
log.Printf("[ipam] Failed to initialize address manager, err:%v.", err)
7069
return err

0 commit comments

Comments
 (0)