From 2fb237d1406da63875ffacf490b8ad3857f5ea47 Mon Sep 17 00:00:00 2001 From: Dario P Bazan Date: Tue, 3 Sep 2019 17:46:32 -0700 Subject: [PATCH] Adding support for multiple CNI versions --- cni/cni.go | 47 ++++++++++++++++++++++++------------------ cni/plugin.go | 2 +- common/core/network.go | 17 +++++++++++++-- 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/cni/cni.go b/cni/cni.go index 13dbd37..5f3d6ea 100644 --- a/cni/cni.go +++ b/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 diff --git a/cni/plugin.go b/cni/plugin.go index ee637d9..4bfd9e4 100644 --- a/cni/plugin.go +++ b/cni/plugin.go @@ -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") diff --git a/common/core/network.go b/common/core/network.go index 4ce4137..270c46a 100644 --- a/common/core/network.go +++ b/common/core/network.go @@ -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