2016-12-01 05:00:08 +03:00
|
|
|
// Copyright Microsoft Corp.
|
|
|
|
// All rights reserved.
|
|
|
|
|
|
|
|
package cni
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-12-10 05:05:33 +03:00
|
|
|
|
|
|
|
cniSkel "github.com/containernetworking/cni/pkg/skel"
|
|
|
|
cniTypes "github.com/containernetworking/cni/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-01-20 03:34:03 +03:00
|
|
|
// Supported CNI versions.
|
|
|
|
Version = "0.2.0"
|
2016-12-10 05:05:33 +03:00
|
|
|
|
2017-01-20 03:34:03 +03:00
|
|
|
// CNI commands.
|
2016-12-10 05:05:33 +03:00
|
|
|
CmdAdd = "ADD"
|
|
|
|
CmdDel = "DEL"
|
2017-01-20 03:34:03 +03:00
|
|
|
|
|
|
|
Internal = "internal"
|
2016-12-01 05:00:08 +03:00
|
|
|
)
|
|
|
|
|
2016-12-10 05:05:33 +03:00
|
|
|
// CNI contract.
|
|
|
|
type CniPlugin interface {
|
|
|
|
Add(args *cniSkel.CmdArgs) error
|
|
|
|
Delete(args *cniSkel.CmdArgs) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// CallPlugin calls the given CNI plugin through the internal interface.
|
|
|
|
func CallPlugin(plugin CniPlugin, cmd string, args *cniSkel.CmdArgs, nwCfg *NetworkConfig) (*cniTypes.Result, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
savedType := nwCfg.Ipam.Type
|
|
|
|
nwCfg.Ipam.Type = Internal
|
|
|
|
args.StdinData = nwCfg.Serialize()
|
|
|
|
|
|
|
|
// Call the plugin's internal interface.
|
|
|
|
if cmd == CmdAdd {
|
|
|
|
err = plugin.Add(args)
|
|
|
|
} else {
|
|
|
|
err = plugin.Delete(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
nwCfg.Ipam.Type = savedType
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read back the result.
|
|
|
|
var result cniTypes.Result
|
|
|
|
err = json.Unmarshal(args.StdinData, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
2016-12-01 05:00:08 +03:00
|
|
|
// NetworkConfig represents the Azure CNI plugin's network configuration.
|
|
|
|
type NetworkConfig struct {
|
|
|
|
CniVersion string `json:"cniVersion"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
2017-01-20 03:34:03 +03:00
|
|
|
Master string `json:"master"`
|
2016-12-02 00:29:23 +03:00
|
|
|
Bridge string `json:"bridge,omitempty"`
|
2016-12-10 05:05:33 +03:00
|
|
|
LogLevel string `json:"logLevel,omitempty"`
|
|
|
|
LogTarget string `json:"logTarget,omitempty"`
|
2016-12-01 05:00:08 +03:00
|
|
|
Ipam struct {
|
2016-12-10 05:05:33 +03:00
|
|
|
Type string `json:"type"`
|
|
|
|
Environment string `json:"environment,omitempty"`
|
|
|
|
AddrSpace string `json:"addressSpace,omitempty"`
|
|
|
|
Subnet string `json:"subnet,omitempty"`
|
|
|
|
Address string `json:"ipAddress,omitempty"`
|
|
|
|
QueryInterval string `json:"queryInterval,omitempty"`
|
2016-12-01 05:00:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseNetworkConfig unmarshals network configuration from bytes.
|
|
|
|
func ParseNetworkConfig(b []byte) (*NetworkConfig, error) {
|
|
|
|
nwCfg := NetworkConfig{}
|
|
|
|
|
|
|
|
err := json.Unmarshal(b, &nwCfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &nwCfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize marshals a network configuration to bytes.
|
|
|
|
func (nwcfg *NetworkConfig) Serialize() []byte {
|
|
|
|
bytes, _ := json.Marshal(nwcfg)
|
|
|
|
return bytes
|
|
|
|
}
|