2017-02-12 13:03:39 +03:00
|
|
|
// Copyright 2017 Microsoft. All rights reserved.
|
|
|
|
// MIT License
|
2016-12-01 05:00:08 +03:00
|
|
|
|
|
|
|
package ipam
|
|
|
|
|
|
|
|
import (
|
2016-12-10 05:05:33 +03:00
|
|
|
"encoding/json"
|
2016-12-01 05:00:08 +03:00
|
|
|
"net"
|
2017-01-20 03:37:57 +03:00
|
|
|
"strconv"
|
2016-12-01 05:00:08 +03:00
|
|
|
|
|
|
|
"github.com/Azure/azure-container-networking/cni"
|
|
|
|
"github.com/Azure/azure-container-networking/common"
|
|
|
|
"github.com/Azure/azure-container-networking/ipam"
|
|
|
|
"github.com/Azure/azure-container-networking/log"
|
2017-01-21 03:21:41 +03:00
|
|
|
"github.com/Azure/azure-container-networking/platform"
|
2016-12-01 05:00:08 +03:00
|
|
|
|
|
|
|
cniSkel "github.com/containernetworking/cni/pkg/skel"
|
|
|
|
cniTypes "github.com/containernetworking/cni/pkg/types"
|
2017-05-10 05:29:15 +03:00
|
|
|
cniTypesCurr "github.com/containernetworking/cni/pkg/types/current"
|
2016-12-01 05:00:08 +03:00
|
|
|
)
|
|
|
|
|
2020-04-16 08:30:48 +03:00
|
|
|
const (
|
|
|
|
ipamV6 = "azure-vnet-ipamv6"
|
|
|
|
)
|
|
|
|
|
2016-12-10 05:05:33 +03:00
|
|
|
var (
|
|
|
|
ipv4DefaultRouteDstPrefix = net.IPNet{net.IPv4zero, net.IPv4Mask(0, 0, 0, 0)}
|
|
|
|
)
|
|
|
|
|
2017-01-20 03:37:57 +03:00
|
|
|
// IpamPlugin represents the CNI IPAM plugin.
|
2016-12-01 05:00:08 +03:00
|
|
|
type ipamPlugin struct {
|
2017-01-20 03:37:57 +03:00
|
|
|
*cni.Plugin
|
2016-12-01 05:00:08 +03:00
|
|
|
am ipam.AddressManager
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPlugin creates a new ipamPlugin object.
|
2019-11-26 04:07:50 +03:00
|
|
|
func NewPlugin(name string, config *common.PluginConfig) (*ipamPlugin, error) {
|
2016-12-01 05:00:08 +03:00
|
|
|
// Setup base plugin.
|
2017-01-20 03:37:57 +03:00
|
|
|
plugin, err := cni.NewPlugin(name, config.Version)
|
2016-12-01 05:00:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup address manager.
|
|
|
|
am, err := ipam.NewAddressManager()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-10 05:05:33 +03:00
|
|
|
// Create IPAM plugin.
|
|
|
|
ipamPlg := &ipamPlugin{
|
2016-12-01 05:00:08 +03:00
|
|
|
Plugin: plugin,
|
|
|
|
am: am,
|
2016-12-10 05:05:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
config.IpamApi = ipamPlg
|
|
|
|
|
|
|
|
return ipamPlg, nil
|
2016-12-01 05:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Starts the plugin.
|
|
|
|
func (plugin *ipamPlugin) Start(config *common.PluginConfig) error {
|
|
|
|
// Initialize base plugin.
|
|
|
|
err := plugin.Initialize(config)
|
|
|
|
if err != nil {
|
2016-12-10 05:05:33 +03:00
|
|
|
log.Printf("[cni-ipam] Failed to initialize base plugin, err:%v.", err)
|
2016-12-01 05:00:08 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-21 03:21:41 +03:00
|
|
|
// Log platform information.
|
|
|
|
log.Printf("[cni-ipam] Plugin %v version %v.", plugin.Name, plugin.Version)
|
|
|
|
log.Printf("[cni-ipam] Running on %v", platform.GetOSInfo())
|
|
|
|
|
2016-12-01 05:00:08 +03:00
|
|
|
// Initialize address manager.
|
2016-12-06 22:17:47 +03:00
|
|
|
err = plugin.am.Initialize(config, plugin.Options)
|
2016-12-01 05:00:08 +03:00
|
|
|
if err != nil {
|
2016-12-10 05:05:33 +03:00
|
|
|
log.Printf("[cni-ipam] Failed to initialize address manager, err:%v.", err)
|
2016-12-01 05:00:08 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-10 05:05:33 +03:00
|
|
|
log.Printf("[cni-ipam] Plugin started.")
|
2016-12-01 05:00:08 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stops the plugin.
|
|
|
|
func (plugin *ipamPlugin) Stop() {
|
|
|
|
plugin.am.Uninitialize()
|
|
|
|
plugin.Uninitialize()
|
2016-12-10 05:05:33 +03:00
|
|
|
log.Printf("[cni-ipam] Plugin stopped.")
|
2016-12-01 05:00:08 +03:00
|
|
|
}
|
|
|
|
|
2017-01-20 03:37:57 +03:00
|
|
|
// Configure parses and applies the given network configuration.
|
|
|
|
func (plugin *ipamPlugin) Configure(stdinData []byte) (*cni.NetworkConfig, error) {
|
|
|
|
// Parse network configuration from stdin.
|
|
|
|
nwCfg, err := cni.ParseNetworkConfig(stdinData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[cni-ipam] Read network configuration %+v.", nwCfg)
|
|
|
|
|
|
|
|
// Apply IPAM configuration.
|
|
|
|
|
|
|
|
// Set deployment environment.
|
|
|
|
if nwCfg.Ipam.Environment == "" {
|
|
|
|
nwCfg.Ipam.Environment = common.OptEnvironmentAzure
|
|
|
|
}
|
|
|
|
plugin.SetOption(common.OptEnvironment, nwCfg.Ipam.Environment)
|
|
|
|
|
|
|
|
// Set query interval.
|
|
|
|
if nwCfg.Ipam.QueryInterval != "" {
|
|
|
|
i, _ := strconv.Atoi(nwCfg.Ipam.QueryInterval)
|
|
|
|
plugin.SetOption(common.OptIpamQueryInterval, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = plugin.am.StartSource(plugin.Options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set default address space if not specified.
|
|
|
|
if nwCfg.Ipam.AddrSpace == "" {
|
2017-01-25 03:20:34 +03:00
|
|
|
nwCfg.Ipam.AddrSpace = ipam.LocalDefaultAddressSpaceId
|
2017-01-20 03:37:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nwCfg, nil
|
|
|
|
}
|
|
|
|
|
2016-12-01 05:00:08 +03:00
|
|
|
//
|
|
|
|
// CNI implementation
|
|
|
|
// https://github.com/containernetworking/cni/blob/master/SPEC.md
|
|
|
|
//
|
|
|
|
|
|
|
|
// Add handles CNI add commands.
|
|
|
|
func (plugin *ipamPlugin) Add(args *cniSkel.CmdArgs) error {
|
2017-05-10 05:29:15 +03:00
|
|
|
var result *cniTypesCurr.Result
|
|
|
|
var err error
|
|
|
|
|
2019-10-18 16:58:27 +03:00
|
|
|
log.Printf("[cni-ipam] Processing ADD command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v StdinData:%s}.",
|
|
|
|
args.ContainerID, args.Netns, args.IfName, args.Args, args.Path, args.StdinData)
|
2016-12-01 05:00:08 +03:00
|
|
|
|
2017-05-10 05:29:15 +03:00
|
|
|
defer func() { log.Printf("[cni-ipam] ADD command completed with result:%+v err:%v.", result, err) }()
|
|
|
|
|
2016-12-01 05:00:08 +03:00
|
|
|
// Parse network configuration from stdin.
|
2017-01-20 03:37:57 +03:00
|
|
|
nwCfg, err := plugin.Configure(args.StdinData)
|
2016-12-01 05:00:08 +03:00
|
|
|
if err != nil {
|
2017-05-10 05:29:15 +03:00
|
|
|
err = plugin.Errorf("Failed to parse network configuration: %v", err)
|
|
|
|
return err
|
2016-12-01 05:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if an address pool is specified.
|
|
|
|
if nwCfg.Ipam.Subnet == "" {
|
2017-05-10 05:29:15 +03:00
|
|
|
var poolID string
|
|
|
|
var subnet string
|
|
|
|
|
2017-01-20 03:37:57 +03:00
|
|
|
// Select the requested interface.
|
|
|
|
options := make(map[string]string)
|
2017-05-05 06:02:03 +03:00
|
|
|
options[ipam.OptInterfaceName] = nwCfg.Master
|
2017-01-20 03:37:57 +03:00
|
|
|
|
2020-04-16 08:30:48 +03:00
|
|
|
isIpv6 := false
|
|
|
|
if nwCfg.Ipam.Type == ipamV6 {
|
|
|
|
isIpv6 = true
|
|
|
|
}
|
|
|
|
|
2016-12-01 05:00:08 +03:00
|
|
|
// Allocate an address pool.
|
2020-04-16 08:30:48 +03:00
|
|
|
poolID, subnet, err = plugin.am.RequestPool(nwCfg.Ipam.AddrSpace, "", "", options, isIpv6)
|
2016-12-01 05:00:08 +03:00
|
|
|
if err != nil {
|
2017-05-10 05:29:15 +03:00
|
|
|
err = plugin.Errorf("Failed to allocate pool: %v", err)
|
|
|
|
return err
|
2016-12-01 05:00:08 +03:00
|
|
|
}
|
|
|
|
|
2017-05-10 05:29:15 +03:00
|
|
|
// On failure, release the address pool.
|
|
|
|
defer func() {
|
|
|
|
if err != nil && poolID != "" {
|
|
|
|
log.Printf("[cni-ipam] Releasing pool %v.", poolID)
|
|
|
|
plugin.am.ReleasePool(nwCfg.Ipam.AddrSpace, poolID)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-12-01 05:00:08 +03:00
|
|
|
nwCfg.Ipam.Subnet = subnet
|
2017-05-10 05:29:15 +03:00
|
|
|
log.Printf("[cni-ipam] Allocated address poolID %v with subnet %v.", poolID, subnet)
|
2016-12-01 05:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate an address for the endpoint.
|
2018-02-22 22:17:31 +03:00
|
|
|
address, err := plugin.am.RequestAddress(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet, nwCfg.Ipam.Address, nil)
|
2016-12-01 05:00:08 +03:00
|
|
|
if err != nil {
|
2017-02-25 01:38:00 +03:00
|
|
|
err = plugin.Errorf("Failed to allocate address: %v", err)
|
2017-05-10 05:29:15 +03:00
|
|
|
return err
|
2016-12-01 05:00:08 +03:00
|
|
|
}
|
|
|
|
|
2017-05-10 05:29:15 +03:00
|
|
|
// On failure, release the address.
|
|
|
|
defer func() {
|
|
|
|
if err != nil && address != "" {
|
|
|
|
log.Printf("[cni-ipam] Releasing address %v.", address)
|
2017-06-15 00:34:52 +03:00
|
|
|
plugin.am.ReleaseAddress(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet, address, nil)
|
2017-05-10 05:29:15 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-12-10 05:05:33 +03:00
|
|
|
log.Printf("[cni-ipam] Allocated address %v.", address)
|
2016-12-01 05:00:08 +03:00
|
|
|
|
2016-12-10 05:05:33 +03:00
|
|
|
// Parse IP address.
|
2017-05-10 05:29:15 +03:00
|
|
|
ipAddress, err := platform.ConvertStringToIPNet(address)
|
2016-12-01 05:00:08 +03:00
|
|
|
if err != nil {
|
2017-02-25 01:38:00 +03:00
|
|
|
err = plugin.Errorf("Failed to parse address: %v", err)
|
2017-05-10 05:29:15 +03:00
|
|
|
return err
|
2016-12-10 05:05:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Query pool information for gateways and DNS servers.
|
2017-05-10 05:29:15 +03:00
|
|
|
apInfo, err := plugin.am.GetPoolInfo(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet)
|
2016-12-10 05:05:33 +03:00
|
|
|
if err != nil {
|
2017-02-25 01:38:00 +03:00
|
|
|
err = plugin.Errorf("Failed to get pool information: %v", err)
|
2017-05-10 05:29:15 +03:00
|
|
|
return err
|
2016-12-01 05:00:08 +03:00
|
|
|
}
|
|
|
|
|
2017-05-10 05:29:15 +03:00
|
|
|
// Populate result.
|
|
|
|
result = &cniTypesCurr.Result{
|
|
|
|
IPs: []*cniTypesCurr.IPConfig{
|
|
|
|
{
|
2017-06-27 21:15:35 +03:00
|
|
|
Version: "4",
|
|
|
|
Address: *ipAddress,
|
|
|
|
Gateway: apInfo.Gateway,
|
2017-05-10 05:29:15 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Routes: []*cniTypes.Route{
|
|
|
|
{
|
|
|
|
Dst: ipv4DefaultRouteDstPrefix,
|
|
|
|
GW: apInfo.Gateway,
|
2016-12-10 05:05:33 +03:00
|
|
|
},
|
|
|
|
},
|
2016-12-01 05:00:08 +03:00
|
|
|
}
|
|
|
|
|
2016-12-10 05:05:33 +03:00
|
|
|
// Populate DNS servers.
|
2017-05-10 05:29:15 +03:00
|
|
|
for _, dnsServer := range apInfo.DnsServers {
|
|
|
|
result.DNS.Nameservers = append(result.DNS.Nameservers, dnsServer.String())
|
2017-02-07 01:14:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert result to the requested CNI version.
|
2017-05-10 05:29:15 +03:00
|
|
|
res, err := result.GetAsVersion(nwCfg.CNIVersion)
|
2017-02-07 01:14:38 +03:00
|
|
|
if err != nil {
|
2017-02-25 01:38:00 +03:00
|
|
|
err = plugin.Errorf("Failed to convert result: %v", err)
|
2017-05-10 05:29:15 +03:00
|
|
|
return err
|
2016-12-10 05:05:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Output the result.
|
|
|
|
if nwCfg.Ipam.Type == cni.Internal {
|
|
|
|
// Called via the internal interface. Pass output back in args.
|
2017-05-10 05:29:15 +03:00
|
|
|
args.StdinData, _ = json.Marshal(res)
|
2016-12-10 05:05:33 +03:00
|
|
|
} else {
|
|
|
|
// Called via the executable interface. Print output to stdout.
|
2017-05-10 05:29:15 +03:00
|
|
|
res.Print()
|
2016-12-10 05:05:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-12-01 05:00:08 +03:00
|
|
|
}
|
|
|
|
|
2018-06-02 03:48:19 +03:00
|
|
|
// Get handles CNI Get commands.
|
|
|
|
func (plugin *ipamPlugin) Get(args *cniSkel.CmdArgs) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-01 05:00:08 +03:00
|
|
|
// Delete handles CNI delete commands.
|
|
|
|
func (plugin *ipamPlugin) Delete(args *cniSkel.CmdArgs) error {
|
2017-05-10 05:29:15 +03:00
|
|
|
var err error
|
|
|
|
|
2019-10-18 16:58:27 +03:00
|
|
|
log.Printf("[cni-ipam] Processing DEL command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v StdinData:%s}.",
|
|
|
|
args.ContainerID, args.Netns, args.IfName, args.Args, args.Path, args.StdinData)
|
2016-12-01 05:00:08 +03:00
|
|
|
|
2017-05-10 05:29:15 +03:00
|
|
|
defer func() { log.Printf("[cni-ipam] DEL command completed with err:%v.", err) }()
|
|
|
|
|
2016-12-01 05:00:08 +03:00
|
|
|
// Parse network configuration from stdin.
|
2017-01-20 03:37:57 +03:00
|
|
|
nwCfg, err := plugin.Configure(args.StdinData)
|
2016-12-01 05:00:08 +03:00
|
|
|
if err != nil {
|
2017-05-10 05:29:15 +03:00
|
|
|
err = plugin.Errorf("Failed to parse network configuration: %v", err)
|
|
|
|
return err
|
2016-12-01 05:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If an address is specified, release that address. Otherwise, release the pool.
|
|
|
|
if nwCfg.Ipam.Address != "" {
|
|
|
|
// Release the address.
|
2017-06-15 00:34:52 +03:00
|
|
|
err := plugin.am.ReleaseAddress(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet, nwCfg.Ipam.Address, nil)
|
2016-12-01 05:00:08 +03:00
|
|
|
if err != nil {
|
2017-05-10 05:29:15 +03:00
|
|
|
err = plugin.Errorf("Failed to release address: %v", err)
|
|
|
|
return err
|
2016-12-01 05:00:08 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Release the pool.
|
|
|
|
err := plugin.am.ReleasePool(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet)
|
|
|
|
if err != nil {
|
2017-05-10 05:29:15 +03:00
|
|
|
err = plugin.Errorf("Failed to release pool: %v", err)
|
|
|
|
return err
|
2016-12-01 05:00:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-10 05:05:33 +03:00
|
|
|
return nil
|
2016-12-01 05:00:08 +03:00
|
|
|
}
|
2018-10-29 21:10:27 +03:00
|
|
|
|
|
|
|
// Update handles CNI update command.
|
|
|
|
func (plugin *ipamPlugin) Update(args *cniSkel.CmdArgs) error {
|
|
|
|
return nil
|
|
|
|
}
|