Adding support for multiple CNI versions
This commit is contained in:
Родитель
975307b6e6
Коммит
2fb237d140
47
cni/cni.go
47
cni/cni.go
|
@ -14,14 +14,11 @@ import (
|
|||
network "github.com/Microsoft/windows-container-networking/network"
|
||||
cniSkel "github.com/containernetworking/cni/pkg/skel"
|
||||
cniTypes "github.com/containernetworking/cni/pkg/types"
|
||||
cniTypes020 "github.com/containernetworking/cni/pkg/types/020"
|
||||
cniTypesCurr "github.com/containernetworking/cni/pkg/types/current"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
// Supported CNI versions.
|
||||
Version = "0.2.0"
|
||||
|
||||
// CNI commands.
|
||||
CmdAdd = "ADD"
|
||||
CmdDel = "DEL"
|
||||
|
@ -29,6 +26,9 @@ const (
|
|||
Internal = "internal"
|
||||
)
|
||||
|
||||
// Supported CNI versions.
|
||||
var VersionsSupported = []string{"0.2.0", "0.3.0"}
|
||||
|
||||
type KVP struct {
|
||||
Name string `json:"name"`
|
||||
Value json.RawMessage `json:"value"`
|
||||
|
@ -345,27 +345,34 @@ func (config *NetworkConfig) GetEndpointInfo(
|
|||
return epInfo, nil
|
||||
}
|
||||
|
||||
// GetResult gets the result object
|
||||
func GetResult(network *network.NetworkInfo, endpoint *network.EndpointInfo) Result {
|
||||
// GetCurrResult gets the result object
|
||||
func GetCurrResult(network *network.NetworkInfo, endpoint *network.EndpointInfo, ifname string) cniTypesCurr.Result {
|
||||
result := cniTypesCurr.Result{
|
||||
IPs: []*cniTypesCurr.IPConfig{},
|
||||
Routes: []*cniTypes.Route{}}
|
||||
|
||||
var iFace = GetInterface(endpoint)
|
||||
var ip = GetIP(network, endpoint)
|
||||
return Result{
|
||||
CniVersion: Version,
|
||||
Interfaces: []Interface{iFace},
|
||||
IP: []IP{ip},
|
||||
}
|
||||
}
|
||||
ip.InterfaceIndex = 0
|
||||
|
||||
// GetResult020 gets the v020 result object
|
||||
func GetResult020(network *network.NetworkInfo, endpoint *network.EndpointInfo) cniTypes020.Result {
|
||||
var ip = GetIP(network, endpoint)
|
||||
var ip4 = &cniTypes020.IPConfig{
|
||||
IP: net.IPNet(ip.Address),
|
||||
Gateway: ip.Gateway,
|
||||
cIP := cniTypesCurr.IPConfig{
|
||||
Version: ip.Version,
|
||||
Address: net.IPNet{
|
||||
IP: ip.Address.IP,
|
||||
Mask: ip.Address.Mask},
|
||||
Gateway: ip.Gateway,
|
||||
Interface: &ip.InterfaceIndex,
|
||||
}
|
||||
return cniTypes020.Result{
|
||||
IP4: ip4,
|
||||
result.IPs = append(result.IPs, &cIP)
|
||||
|
||||
// Add Interfaces to result.
|
||||
iface := &cniTypesCurr.Interface{
|
||||
Name: ifname,
|
||||
Mac: string(iFace.MacAddress),
|
||||
}
|
||||
result.Interfaces = append(result.Interfaces, iface)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// GetIP returns the IP for the corresponding endpoint
|
||||
|
|
|
@ -43,7 +43,7 @@ func (plugin *Plugin) Uninitialize() {
|
|||
// Execute executes the CNI command.
|
||||
func (plugin *Plugin) Execute(api PluginApi) error {
|
||||
// Set supported CNI versions.
|
||||
pluginInfo := cniVers.PluginSupports(Version)
|
||||
pluginInfo := cniVers.PluginSupports(VersionsSupported...)
|
||||
|
||||
// Parse args and call the appropriate cmd handler.
|
||||
cniErr := cniSkel.PluginMainWithError(api.Add, nil, api.Delete, pluginInfo, "CNI plugin WinCni")
|
||||
|
|
|
@ -138,7 +138,13 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) (resultError error) {
|
|||
// An endpoint already exists in the same network.
|
||||
// Do not allow creation of more endpoints on same network
|
||||
logrus.Debugf("[cni-net] Endpoint exists on same network, ignoring add : [%v].", epInfo)
|
||||
result := cni.GetResult020(nwConfig, hnsEndpoint)
|
||||
// Convert result to the requested CNI version.
|
||||
res := cni.GetCurrResult(nwConfig, hnsEndpoint, args.IfName)
|
||||
result, err := res.GetAsVersion(cniConfig.CniVersion)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result.Print()
|
||||
return nil
|
||||
}
|
||||
|
@ -175,7 +181,14 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) (resultError error) {
|
|||
return err
|
||||
}
|
||||
|
||||
result := cni.GetResult020(nwConfig, epInfo)
|
||||
// Convert result to the requested CNI version.
|
||||
res := cni.GetCurrResult(nwConfig, epInfo, args.IfName)
|
||||
result, err := res.GetAsVersion(cniConfig.CniVersion)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// result := cni.GetResult020(nwConfig, epInfo)
|
||||
result.Print()
|
||||
logrus.Debugf("[cni-net] result: %v", result.String())
|
||||
return nil
|
||||
|
|
Загрузка…
Ссылка в новой задаче