Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 5 additions & 18 deletions cni/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,20 @@ package cni

import (
"encoding/json"

cniSkel "github.com/containernetworking/cni/pkg/skel"
cniTypes "github.com/containernetworking/cni/pkg/types"
)

// Plugin is the interface implemented by CNI plugins.
type Plugin interface {
Add(args *cniSkel.CmdArgs) error
Delete(args *cniSkel.CmdArgs) error

AddImpl(args *cniSkel.CmdArgs, nwCfg *NetworkConfig) (*cniTypes.Result, error)
DeleteImpl(args *cniSkel.CmdArgs, nwCfg *NetworkConfig) (*cniTypes.Result, error)
}

// NetworkConfig represents the Azure CNI plugin's network configuration.
type NetworkConfig struct {
CniVersion string `json:"cniVersion"`
Name string `json:"name"`
Type string `json:"type"`
Bridge string `json:"bridge"`
IfName string `json:"ifName"`
Bridge string `json:"bridge,omitempty"`
IfName string `json:"ifName,omitempty"`
Ipam struct {
Type string `json:"type"`
AddrSpace string `json:"addressSpace"`
Subnet string `json:"subnet"`
Address string `json:"ipAddress"`
Result string `json:"result"`
AddrSpace string `json:"addressSpace,omitempty"`
Subnet string `json:"subnet,omitempty"`
Address string `json:"ipAddress,omitempty"`
}
}

Expand Down
81 changes: 7 additions & 74 deletions cni/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ func (plugin *ipamPlugin) Start(config *common.PluginConfig) error {
}

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

// Output response.
if nwCfg.Ipam.Result == "" {
result.Print()
} else {
args.Args = result.String()
}
result.Print()

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

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

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

// Process command.
result, err := plugin.DeleteImpl(args, nwCfg)
if err != nil {
log.Printf("[ipam] Failed to process command: %v.", err)
return nil
}

// Output response.
if result != nil {
result.Print()
}

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

return err
}

// AddImpl handles CNI add commands.
func (plugin *ipamPlugin) AddImpl(args *cniSkel.CmdArgs, nwCfg *cni.NetworkConfig) (*cniTypes.Result, error) {
// Assume default address space if not specified.
if nwCfg.Ipam.AddrSpace == "" {
nwCfg.Ipam.AddrSpace = defaultAddressSpaceId
}

// Check if an address pool is specified.
if nwCfg.Ipam.Subnet == "" {
// Allocate an address pool.
poolId, subnet, err := plugin.am.RequestPool(nwCfg.Ipam.AddrSpace, "", "", nil, false)
if err != nil {
log.Printf("[ipam] Failed to allocate pool, err:%v.", err)
return nil, err
}

nwCfg.Ipam.Subnet = subnet
log.Printf("[ipam] Allocated address poolId %v with subnet %v.", poolId, subnet)
}

// Allocate an address for the endpoint.
address, err := plugin.am.RequestAddress(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet, nwCfg.Ipam.Address, nil)
if err != nil {
log.Printf("[ipam] Failed to allocate address, err:%v.", err)
return nil, err
}

log.Printf("[ipam] Allocated address %v.", address)

// Output the result.
ip, cidr, err := net.ParseCIDR(address)
cidr.IP = ip
if err != nil {
log.Printf("[ipam] Failed to parse address, err:%v.", err)
return nil, err
}

result := &cniTypes.Result{
IP4: &cniTypes.IPConfig{IP: *cidr},
}

return result, nil
}

// DeleteImpl handles CNI delete commands.
func (plugin *ipamPlugin) DeleteImpl(args *cniSkel.CmdArgs, nwCfg *cni.NetworkConfig) (*cniTypes.Result, error) {
// Assume default address space if not specified.
if nwCfg.Ipam.AddrSpace == "" {
nwCfg.Ipam.AddrSpace = defaultAddressSpaceId
Expand All @@ -238,16 +169,18 @@ func (plugin *ipamPlugin) DeleteImpl(args *cniSkel.CmdArgs, nwCfg *cni.NetworkCo
err := plugin.am.ReleaseAddress(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet, nwCfg.Ipam.Address)
if err != nil {
log.Printf("[cni] Failed to release address, err:%v.", err)
return nil, err
return nil
}
} else {
// Release the pool.
err := plugin.am.ReleasePool(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet)
if err != nil {
log.Printf("[cni] Failed to release pool, err:%v.", err)
return nil, err
return nil
}
}

return nil, nil
log.Printf("[ipam] DEL succeeded.")

return err
}
2 changes: 1 addition & 1 deletion cni/plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func main() {
common.LogNetworkInterfaces()

// Set plugin options.
ipamPlugin.SetOption(common.OptEnvironmentKey, environment)
ipamPlugin.SetOption(common.OptEnvironment, environment)

// Start plugins.
if netPlugin != nil {
Expand Down
3 changes: 3 additions & 0 deletions cnm/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package cnm

const (
// Libnetwork plugin path
pluginPath = "/run/docker/plugins"

// Libnetwork remote plugin paths
activatePath = "/Plugin.Activate"
)
Expand Down
3 changes: 1 addition & 2 deletions cnm/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ func (plugin *ipamPlugin) Start(config *common.PluginConfig) error {
}

// Initialize address manager.
environment := plugin.GetOption(common.OptEnvironmentKey)
err = plugin.am.Initialize(config, environment)
err = plugin.am.Initialize(config, plugin.Options)
if err != nil {
log.Printf("[ipam] Failed to initialize address manager, err:%v.", err)
return err
Expand Down
Loading