Skip to content

Commit

Permalink
Update for CNI v1 type schemes
Browse files Browse the repository at this point in the history
Signed-off-by: Casey Callendrello <[email protected]>
  • Loading branch information
squeed committed May 26, 2021
1 parent f9d4247 commit 8480c61
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 52 deletions.
17 changes: 8 additions & 9 deletions pkg/ocicni/ocicni.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/containernetworking/cni/libcni"
cniinvoke "github.com/containernetworking/cni/pkg/invoke"
cnitypes "github.com/containernetworking/cni/pkg/types"
cnicurrent "github.com/containernetworking/cni/pkg/types/current"
cniv1 "github.com/containernetworking/cni/pkg/types/100"
cniversion "github.com/containernetworking/cni/pkg/version"
"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -739,22 +739,21 @@ func (network *cniNetwork) checkNetwork(ctx context.Context, rt *libcni.RuntimeC
// result doesn't exist, create one
logrus.Infof("Checking CNI network %s (config version=%v) nsManager=%v", network.name, network.config.CNIVersion, nsManager)

var cniInterface *cnicurrent.Interface
ips := []*cnicurrent.IPConfig{}
var cniInterface *cniv1.Interface
ips := []*cniv1.IPConfig{}
errs := []error{}
for _, version := range []string{"4", "6"} {
ip, mac, err := getContainerDetails(nsManager, netns, rt.IfName, "-"+version)
if err == nil {
if cniInterface == nil {
cniInterface = &cnicurrent.Interface{
cniInterface = &cniv1.Interface{
Name: rt.IfName,
Mac: mac.String(),
Sandbox: netns,
}
}
ips = append(ips, &cnicurrent.IPConfig{
Version: version,
Interface: cnicurrent.Int(0),
ips = append(ips, &cniv1.IPConfig{
Interface: cniv1.Int(0),
Address: *ip,
})
} else {
Expand All @@ -765,9 +764,9 @@ func (network *cniNetwork) checkNetwork(ctx context.Context, rt *libcni.RuntimeC
return nil, fmt.Errorf("neither IPv4 nor IPv6 found when retrieving network status: %v", errs)
}

result = &cnicurrent.Result{
result = &cniv1.Result{
CNIVersion: network.config.CNIVersion,
Interfaces: []*cnicurrent.Interface{cniInterface},
Interfaces: []*cniv1.Interface{cniInterface},
IPs: ips,
}

Expand Down
59 changes: 29 additions & 30 deletions pkg/ocicni/ocicni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

"github.com/containernetworking/cni/libcni"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
cniv04 "github.com/containernetworking/cni/pkg/types/040"
"github.com/containernetworking/cni/pkg/version"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/containernetworking/plugins/pkg/testutils"
Expand Down Expand Up @@ -78,7 +78,7 @@ type TestConf struct {
Type string `json:"type,omitempty"`
}

func (f *fakeExec) addPlugin(expectedEnv []string, expectedConf string, result *current.Result, err error) {
func (f *fakeExec) addPlugin(expectedEnv []string, expectedConf string, result types.Result, err error) {
f.plugins = append(f.plugins, &fakePlugin{
expectedEnv: expectedEnv,
expectedConf: expectedConf,
Expand Down Expand Up @@ -473,18 +473,18 @@ var _ = Describe("ocicni operations", func() {
Expect(err).NotTo(HaveOccurred())

fake := &fakeExec{}
expectedResult := &current.Result{
expectedResult := &cniv04.Result{
CNIVersion: "0.3.1",
Interfaces: []*current.Interface{
Interfaces: []*cniv04.Interface{
{
Name: "eth0",
Mac: "01:23:45:67:89:01",
Sandbox: networkNS.Path(),
},
},
IPs: []*current.IPConfig{
IPs: []*cniv04.IPConfig{
{
Interface: current.Int(0),
Interface: cniv04.Int(0),
Version: "4",
Address: *ensureCIDR("1.1.1.2/24"),
},
Expand All @@ -505,7 +505,7 @@ var _ = Describe("ocicni operations", func() {
Expect(err).NotTo(HaveOccurred())
Expect(fake.addIndex).To(Equal(len(fake.plugins)))
Expect(len(results)).To(Equal(1))
r := results[0].Result.(*current.Result)
r := results[0].Result.(*cniv04.Result)
Expect(reflect.DeepEqual(r, expectedResult)).To(BeTrue())

// Make sure loopback device is up
Expand Down Expand Up @@ -535,38 +535,37 @@ var _ = Describe("ocicni operations", func() {
Expect(err).NotTo(HaveOccurred())

fake := &fakeExec{}
expectedResult1 := &current.Result{
expectedResult1 := &cniv04.Result{
CNIVersion: "0.3.1",
Interfaces: []*current.Interface{
Interfaces: []*cniv04.Interface{
{
Name: "eth0",
Mac: "01:23:45:67:89:01",
Sandbox: networkNS.Path(),
},
},
IPs: []*current.IPConfig{
IPs: []*cniv04.IPConfig{
{
Interface: current.Int(0),
Interface: cniv04.Int(0),
Version: "4",
Address: *ensureCIDR("1.1.1.2/24"),
},
},
}
fake.addPlugin(nil, conf1, expectedResult1, nil)

expectedResult2 := &current.Result{
expectedResult2 := &cniv04.Result{
CNIVersion: "0.3.1",
Interfaces: []*current.Interface{
Interfaces: []*cniv04.Interface{
{
Name: "eth1",
Mac: "01:23:45:67:89:02",
Sandbox: networkNS.Path(),
},
},
IPs: []*current.IPConfig{
IPs: []*cniv04.IPConfig{
{
Interface: current.Int(0),
Version: "4",
Interface: cniv04.Int(0),
Address: *ensureCIDR("1.1.1.3/24"),
},
},
Expand All @@ -590,9 +589,9 @@ var _ = Describe("ocicni operations", func() {
Expect(err).NotTo(HaveOccurred())
Expect(fake.addIndex).To(Equal(len(fake.plugins)))
Expect(len(results)).To(Equal(2))
r := results[0].Result.(*current.Result)
r := results[0].Result.(*cniv04.Result)
Expect(reflect.DeepEqual(r, expectedResult1)).To(BeTrue())
r = results[1].Result.(*current.Result)
r = results[1].Result.(*cniv04.Result)
Expect(reflect.DeepEqual(r, expectedResult2)).To(BeTrue())

err = ocicni.TearDownPod(podNet)
Expand All @@ -612,37 +611,37 @@ var _ = Describe("ocicni operations", func() {
Expect(err).NotTo(HaveOccurred())

fake := &fakeExec{}
expectedResult1 := &current.Result{
expectedResult1 := &cniv04.Result{
CNIVersion: "0.4.0",
Interfaces: []*current.Interface{
Interfaces: []*cniv04.Interface{
{
Name: "eth0",
Mac: "01:23:45:67:89:01",
Sandbox: networkNS.Path(),
},
},
IPs: []*current.IPConfig{
IPs: []*cniv04.IPConfig{
{
Interface: current.Int(0),
Interface: cniv04.Int(0),
Version: "4",
Address: *ensureCIDR("1.1.1.2/24"),
},
},
}
fake.addPlugin(nil, conf1, expectedResult1, nil)

expectedResult2 := &current.Result{
expectedResult2 := &cniv04.Result{
CNIVersion: "0.4.0",
Interfaces: []*current.Interface{
Interfaces: []*cniv04.Interface{
{
Name: "eth1",
Mac: "01:23:45:67:89:02",
Sandbox: networkNS.Path(),
},
},
IPs: []*current.IPConfig{
IPs: []*cniv04.IPConfig{
{
Interface: current.Int(0),
Interface: cniv04.Int(0),
Version: "4",
Address: *ensureCIDR("1.1.1.3/24"),
},
Expand All @@ -667,17 +666,17 @@ var _ = Describe("ocicni operations", func() {
Expect(err).NotTo(HaveOccurred())
Expect(fake.addIndex).To(Equal(len(fake.plugins)))
Expect(len(results)).To(Equal(2))
r := results[0].Result.(*current.Result)
r := results[0].Result.(*cniv04.Result)
Expect(reflect.DeepEqual(r, expectedResult1)).To(BeTrue())
r = results[1].Result.(*current.Result)
r = results[1].Result.(*cniv04.Result)
Expect(reflect.DeepEqual(r, expectedResult2)).To(BeTrue())

resultsStatus, errStatus := ocicni.GetPodNetworkStatus(podNet)
Expect(errStatus).NotTo(HaveOccurred())
Expect(len(resultsStatus)).To(Equal(2))
r = resultsStatus[0].Result.(*current.Result)
r = resultsStatus[0].Result.(*cniv04.Result)
Expect(reflect.DeepEqual(r, expectedResult1)).To(BeTrue())
r = resultsStatus[1].Result.(*current.Result)
r = resultsStatus[1].Result.(*cniv04.Result)
Expect(reflect.DeepEqual(r, expectedResult2)).To(BeTrue())

err = ocicni.TearDownPod(podNet)
Expand Down
23 changes: 10 additions & 13 deletions tools/ocicnitool/ocicnitool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"
"strings"

cnicurrent "github.com/containernetworking/cni/pkg/types/current"
cniv1 "github.com/containernetworking/cni/pkg/types/040"
"github.com/cri-o/ocicni/pkg/ocicni"
)

Expand All @@ -25,21 +25,18 @@ const (

func printSandboxResults(results []ocicni.NetResult) {
for _, r := range results {
result, _ := cnicurrent.NewResultFromResult(r.Result)
result, _ := cniv1.NewResultFromResult(r.Result)
if result != nil {
result030, _ := cnicurrent.GetResult(result)
if result030 != nil {
for _, ip := range result030.IPs {
intfDetails := ""
if ip.Interface != nil && *ip.Interface >= 0 && *ip.Interface < len(result030.Interfaces) {
intf := result030.Interfaces[*ip.Interface]
// Only print container sandbox interfaces (not host ones)
if intf.Sandbox != "" {
intfDetails = fmt.Sprintf(" (%s %s)", intf.Name, intf.Mac)
}
for _, ip := range result.IPs {
intfDetails := ""
if ip.Interface != nil && *ip.Interface >= 0 && *ip.Interface < len(result.Interfaces) {
intf := result.Interfaces[*ip.Interface]
// Only print container sandbox interfaces (not host ones)
if intf.Sandbox != "" {
intfDetails = fmt.Sprintf(" (%s %s)", intf.Name, intf.Mac)
}
fmt.Fprintf(os.Stdout, "IP: %s%s\n", ip.Address.String(), intfDetails)
}
fmt.Fprintf(os.Stdout, "IP: %s%s\n", ip.Address.String(), intfDetails)
}
}
}
Expand Down

0 comments on commit 8480c61

Please sign in to comment.