Skip to content

Commit 7a64314

Browse files
committed
Handle changes for CNI 1.0
Signed-off-by: Casey Callendrello <[email protected]>
1 parent 923b8cf commit 7a64314

File tree

3 files changed

+47
-52
lines changed

3 files changed

+47
-52
lines changed

pkg/ocicni/ocicni.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/containernetworking/cni/libcni"
1717
cniinvoke "github.com/containernetworking/cni/pkg/invoke"
1818
cnitypes "github.com/containernetworking/cni/pkg/types"
19-
cnicurrent "github.com/containernetworking/cni/pkg/types/current"
19+
cniv1 "github.com/containernetworking/cni/pkg/types/100"
2020
cniversion "github.com/containernetworking/cni/pkg/version"
2121
"github.com/fsnotify/fsnotify"
2222
"github.com/sirupsen/logrus"
@@ -739,22 +739,21 @@ func (network *cniNetwork) checkNetwork(ctx context.Context, rt *libcni.RuntimeC
739739
// result doesn't exist, create one
740740
logrus.Infof("Checking CNI network %s (config version=%v) nsManager=%v", network.name, network.config.CNIVersion, nsManager)
741741

742-
var cniInterface *cnicurrent.Interface
743-
ips := []*cnicurrent.IPConfig{}
742+
var cniInterface *cniv1.Interface
743+
ips := []*cniv1.IPConfig{}
744744
errs := []error{}
745745
for _, version := range []string{"4", "6"} {
746746
ip, mac, err := getContainerDetails(nsManager, netns, rt.IfName, "-"+version)
747747
if err == nil {
748748
if cniInterface == nil {
749-
cniInterface = &cnicurrent.Interface{
749+
cniInterface = &cniv1.Interface{
750750
Name: rt.IfName,
751751
Mac: mac.String(),
752752
Sandbox: netns,
753753
}
754754
}
755-
ips = append(ips, &cnicurrent.IPConfig{
756-
Version: version,
757-
Interface: cnicurrent.Int(0),
755+
ips = append(ips, &cniv1.IPConfig{
756+
Interface: cniv1.Int(0),
758757
Address: *ip,
759758
})
760759
} else {
@@ -765,9 +764,9 @@ func (network *cniNetwork) checkNetwork(ctx context.Context, rt *libcni.RuntimeC
765764
return nil, fmt.Errorf("neither IPv4 nor IPv6 found when retrieving network status: %v", errs)
766765
}
767766

768-
result = &cnicurrent.Result{
767+
result = &cniv1.Result{
769768
CNIVersion: network.config.CNIVersion,
770-
Interfaces: []*cnicurrent.Interface{cniInterface},
769+
Interfaces: []*cniv1.Interface{cniInterface},
771770
IPs: ips,
772771
}
773772

pkg/ocicni/ocicni_test.go

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
"github.com/containernetworking/cni/libcni"
1818
"github.com/containernetworking/cni/pkg/types"
19-
"github.com/containernetworking/cni/pkg/types/current"
19+
cniv04 "github.com/containernetworking/cni/pkg/types/040"
2020
"github.com/containernetworking/cni/pkg/version"
2121
"github.com/containernetworking/plugins/pkg/ns"
2222
"github.com/containernetworking/plugins/pkg/testutils"
@@ -78,7 +78,7 @@ type TestConf struct {
7878
Type string `json:"type,omitempty"`
7979
}
8080

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

475475
fake := &fakeExec{}
476-
expectedResult := &current.Result{
476+
expectedResult := &cniv04.Result{
477477
CNIVersion: "0.3.1",
478-
Interfaces: []*current.Interface{
478+
Interfaces: []*cniv04.Interface{
479479
{
480480
Name: "eth0",
481481
Mac: "01:23:45:67:89:01",
482482
Sandbox: networkNS.Path(),
483483
},
484484
},
485-
IPs: []*current.IPConfig{
485+
IPs: []*cniv04.IPConfig{
486486
{
487-
Interface: current.Int(0),
487+
Interface: cniv04.Int(0),
488488
Version: "4",
489489
Address: *ensureCIDR("1.1.1.2/24"),
490490
},
@@ -505,7 +505,7 @@ var _ = Describe("ocicni operations", func() {
505505
Expect(err).NotTo(HaveOccurred())
506506
Expect(fake.addIndex).To(Equal(len(fake.plugins)))
507507
Expect(len(results)).To(Equal(1))
508-
r := results[0].Result.(*current.Result)
508+
r := results[0].Result.(*cniv04.Result)
509509
Expect(reflect.DeepEqual(r, expectedResult)).To(BeTrue())
510510

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

537537
fake := &fakeExec{}
538-
expectedResult1 := &current.Result{
538+
expectedResult1 := &cniv04.Result{
539539
CNIVersion: "0.3.1",
540-
Interfaces: []*current.Interface{
540+
Interfaces: []*cniv04.Interface{
541541
{
542542
Name: "eth0",
543543
Mac: "01:23:45:67:89:01",
544544
Sandbox: networkNS.Path(),
545545
},
546546
},
547-
IPs: []*current.IPConfig{
547+
IPs: []*cniv04.IPConfig{
548548
{
549-
Interface: current.Int(0),
549+
Interface: cniv04.Int(0),
550550
Version: "4",
551551
Address: *ensureCIDR("1.1.1.2/24"),
552552
},
553553
},
554554
}
555555
fake.addPlugin(nil, conf1, expectedResult1, nil)
556556

557-
expectedResult2 := &current.Result{
557+
expectedResult2 := &cniv04.Result{
558558
CNIVersion: "0.3.1",
559-
Interfaces: []*current.Interface{
559+
Interfaces: []*cniv04.Interface{
560560
{
561561
Name: "eth1",
562562
Mac: "01:23:45:67:89:02",
563563
Sandbox: networkNS.Path(),
564564
},
565565
},
566-
IPs: []*current.IPConfig{
566+
IPs: []*cniv04.IPConfig{
567567
{
568-
Interface: current.Int(0),
569-
Version: "4",
568+
Interface: cniv04.Int(0),
570569
Address: *ensureCIDR("1.1.1.3/24"),
571570
},
572571
},
@@ -590,9 +589,9 @@ var _ = Describe("ocicni operations", func() {
590589
Expect(err).NotTo(HaveOccurred())
591590
Expect(fake.addIndex).To(Equal(len(fake.plugins)))
592591
Expect(len(results)).To(Equal(2))
593-
r := results[0].Result.(*current.Result)
592+
r := results[0].Result.(*cniv04.Result)
594593
Expect(reflect.DeepEqual(r, expectedResult1)).To(BeTrue())
595-
r = results[1].Result.(*current.Result)
594+
r = results[1].Result.(*cniv04.Result)
596595
Expect(reflect.DeepEqual(r, expectedResult2)).To(BeTrue())
597596

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

614613
fake := &fakeExec{}
615-
expectedResult1 := &current.Result{
614+
expectedResult1 := &cniv04.Result{
616615
CNIVersion: "0.4.0",
617-
Interfaces: []*current.Interface{
616+
Interfaces: []*cniv04.Interface{
618617
{
619618
Name: "eth0",
620619
Mac: "01:23:45:67:89:01",
621620
Sandbox: networkNS.Path(),
622621
},
623622
},
624-
IPs: []*current.IPConfig{
623+
IPs: []*cniv04.IPConfig{
625624
{
626-
Interface: current.Int(0),
625+
Interface: cniv04.Int(0),
627626
Version: "4",
628627
Address: *ensureCIDR("1.1.1.2/24"),
629628
},
630629
},
631630
}
632631
fake.addPlugin(nil, conf1, expectedResult1, nil)
633632

634-
expectedResult2 := &current.Result{
633+
expectedResult2 := &cniv04.Result{
635634
CNIVersion: "0.4.0",
636-
Interfaces: []*current.Interface{
635+
Interfaces: []*cniv04.Interface{
637636
{
638637
Name: "eth1",
639638
Mac: "01:23:45:67:89:02",
640639
Sandbox: networkNS.Path(),
641640
},
642641
},
643-
IPs: []*current.IPConfig{
642+
IPs: []*cniv04.IPConfig{
644643
{
645-
Interface: current.Int(0),
644+
Interface: cniv04.Int(0),
646645
Version: "4",
647646
Address: *ensureCIDR("1.1.1.3/24"),
648647
},
@@ -667,17 +666,17 @@ var _ = Describe("ocicni operations", func() {
667666
Expect(err).NotTo(HaveOccurred())
668667
Expect(fake.addIndex).To(Equal(len(fake.plugins)))
669668
Expect(len(results)).To(Equal(2))
670-
r := results[0].Result.(*current.Result)
669+
r := results[0].Result.(*cniv04.Result)
671670
Expect(reflect.DeepEqual(r, expectedResult1)).To(BeTrue())
672-
r = results[1].Result.(*current.Result)
671+
r = results[1].Result.(*cniv04.Result)
673672
Expect(reflect.DeepEqual(r, expectedResult2)).To(BeTrue())
674673

675674
resultsStatus, errStatus := ocicni.GetPodNetworkStatus(podNet)
676675
Expect(errStatus).NotTo(HaveOccurred())
677676
Expect(len(resultsStatus)).To(Equal(2))
678-
r = resultsStatus[0].Result.(*current.Result)
677+
r = resultsStatus[0].Result.(*cniv04.Result)
679678
Expect(reflect.DeepEqual(r, expectedResult1)).To(BeTrue())
680-
r = resultsStatus[1].Result.(*current.Result)
679+
r = resultsStatus[1].Result.(*cniv04.Result)
681680
Expect(reflect.DeepEqual(r, expectedResult2)).To(BeTrue())
682681

683682
err = ocicni.TearDownPod(podNet)

tools/ocicnitool/ocicnitool.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"path/filepath"
88
"strings"
99

10-
cnicurrent "github.com/containernetworking/cni/pkg/types/current"
10+
cniv1 "github.com/containernetworking/cni/pkg/types/040"
1111
"github.com/cri-o/ocicni/pkg/ocicni"
1212
)
1313

@@ -25,21 +25,18 @@ const (
2525

2626
func printSandboxResults(results []ocicni.NetResult) {
2727
for _, r := range results {
28-
result, _ := cnicurrent.NewResultFromResult(r.Result)
28+
result, _ := cniv1.NewResultFromResult(r.Result)
2929
if result != nil {
30-
result030, _ := cnicurrent.GetResult(result)
31-
if result030 != nil {
32-
for _, ip := range result030.IPs {
33-
intfDetails := ""
34-
if ip.Interface != nil && *ip.Interface >= 0 && *ip.Interface < len(result030.Interfaces) {
35-
intf := result030.Interfaces[*ip.Interface]
36-
// Only print container sandbox interfaces (not host ones)
37-
if intf.Sandbox != "" {
38-
intfDetails = fmt.Sprintf(" (%s %s)", intf.Name, intf.Mac)
39-
}
30+
for _, ip := range result.IPs {
31+
intfDetails := ""
32+
if ip.Interface != nil && *ip.Interface >= 0 && *ip.Interface < len(result.Interfaces) {
33+
intf := result.Interfaces[*ip.Interface]
34+
// Only print container sandbox interfaces (not host ones)
35+
if intf.Sandbox != "" {
36+
intfDetails = fmt.Sprintf(" (%s %s)", intf.Name, intf.Mac)
4037
}
41-
fmt.Fprintf(os.Stdout, "IP: %s%s\n", ip.Address.String(), intfDetails)
4238
}
39+
fmt.Fprintf(os.Stdout, "IP: %s%s\n", ip.Address.String(), intfDetails)
4340
}
4441
}
4542
}

0 commit comments

Comments
 (0)