From 27a5b994c2a55d1fceca08ec88139b61d4ad55fd Mon Sep 17 00:00:00 2001 From: Pengfei Ni Date: Wed, 3 May 2017 21:38:28 +0800 Subject: [PATCH 1/2] Add cniVersion to Result Signed-off-by: Pengfei Ni --- pkg/types/020/types.go | 16 +++++++++------- pkg/types/020/types_test.go | 2 ++ pkg/types/current/types.go | 11 ++++++++--- pkg/types/current/types_test.go | 3 +++ 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/pkg/types/020/types.go b/pkg/types/020/types.go index 666cfe93..2833aba7 100644 --- a/pkg/types/020/types.go +++ b/pkg/types/020/types.go @@ -23,9 +23,9 @@ import ( "github.com/containernetworking/cni/pkg/types" ) -const implementedSpecVersion string = "0.2.0" +const ImplementedSpecVersion string = "0.2.0" -var SupportedVersions = []string{"", "0.1.0", implementedSpecVersion} +var SupportedVersions = []string{"", "0.1.0", ImplementedSpecVersion} // Compatibility types for CNI version 0.1.0 and 0.2.0 @@ -39,7 +39,7 @@ func NewResult(data []byte) (types.Result, error) { func GetResult(r types.Result) (*Result, error) { // We expect version 0.1.0/0.2.0 results - result020, err := r.GetAsVersion(implementedSpecVersion) + result020, err := r.GetAsVersion(ImplementedSpecVersion) if err != nil { return nil, err } @@ -52,18 +52,20 @@ func GetResult(r types.Result) (*Result, error) { // Result is what gets returned from the plugin (via stdout) to the caller type Result struct { - IP4 *IPConfig `json:"ip4,omitempty"` - IP6 *IPConfig `json:"ip6,omitempty"` - DNS types.DNS `json:"dns,omitempty"` + CNIVersion string `json:"cniVersion,omitempty"` + IP4 *IPConfig `json:"ip4,omitempty"` + IP6 *IPConfig `json:"ip6,omitempty"` + DNS types.DNS `json:"dns,omitempty"` } func (r *Result) Version() string { - return implementedSpecVersion + return ImplementedSpecVersion } func (r *Result) GetAsVersion(version string) (types.Result, error) { for _, supportedVersion := range SupportedVersions { if version == supportedVersion { + r.CNIVersion = version return r, nil } } diff --git a/pkg/types/020/types_test.go b/pkg/types/020/types_test.go index 1bcdda73..4f08ca49 100644 --- a/pkg/types/020/types_test.go +++ b/pkg/types/020/types_test.go @@ -48,6 +48,7 @@ var _ = Describe("Ensures compatibility with the 0.1.0/0.2.0 spec", func() { // Set every field of the struct to ensure source compatibility res := types020.Result{ + CNIVersion: types020.ImplementedSpecVersion, IP4: &types020.IPConfig{ IP: *ipv4, Gateway: net.ParseIP("1.2.3.1"), @@ -88,6 +89,7 @@ var _ = Describe("Ensures compatibility with the 0.1.0/0.2.0 spec", func() { Expect(err).NotTo(HaveOccurred()) Expect(string(out)).To(Equal(`{ + "cniVersion": "0.2.0", "ip4": { "ip": "1.2.3.30/24", "gateway": "1.2.3.1", diff --git a/pkg/types/current/types.go b/pkg/types/current/types.go index b89a5d3a..26e1dd96 100644 --- a/pkg/types/current/types.go +++ b/pkg/types/current/types.go @@ -63,8 +63,9 @@ func convertFrom020(result types.Result) (*Result, error) { } newResult := &Result{ - DNS: oldResult.DNS, - Routes: []*types.Route{}, + CNIVersion: implementedSpecVersion, + DNS: oldResult.DNS, + Routes: []*types.Route{}, } if oldResult.IP4 != nil { @@ -117,6 +118,7 @@ func convertFrom030(result types.Result) (*Result, error) { if !ok { return nil, fmt.Errorf("failed to convert result") } + newResult.CNIVersion = implementedSpecVersion return newResult, nil } @@ -134,6 +136,7 @@ func NewResultFromResult(result types.Result) (*Result, error) { // Result is what gets returned from the plugin (via stdout) to the caller type Result struct { + CNIVersion string `json:"cniVersion,omitempty"` Interfaces []*Interface `json:"interfaces,omitempty"` IPs []*IPConfig `json:"ips,omitempty"` Routes []*types.Route `json:"routes,omitempty"` @@ -143,7 +146,8 @@ type Result struct { // Convert to the older 0.2.0 CNI spec Result type func (r *Result) convertTo020() (*types020.Result, error) { oldResult := &types020.Result{ - DNS: r.DNS, + CNIVersion: types020.ImplementedSpecVersion, + DNS: r.DNS, } for _, ip := range r.IPs { @@ -195,6 +199,7 @@ func (r *Result) Version() string { func (r *Result) GetAsVersion(version string) (types.Result, error) { switch version { case "0.3.0", implementedSpecVersion: + r.CNIVersion = version return r, nil case types020.SupportedVersions[0], types020.SupportedVersions[1], types020.SupportedVersions[2]: return r.convertTo020() diff --git a/pkg/types/current/types_test.go b/pkg/types/current/types_test.go index 9d29cf22..eb7e7c62 100644 --- a/pkg/types/current/types_test.go +++ b/pkg/types/current/types_test.go @@ -47,6 +47,7 @@ func testResult() *current.Result { // Set every field of the struct to ensure source compatibility return ¤t.Result{ + CNIVersion: "0.3.1", Interfaces: []*current.Interface{ { Name: "eth0", @@ -103,6 +104,7 @@ var _ = Describe("Current types operations", func() { Expect(err).NotTo(HaveOccurred()) Expect(string(out)).To(Equal(`{ + "cniVersion": "0.3.1", "interfaces": [ { "name": "eth0", @@ -172,6 +174,7 @@ var _ = Describe("Current types operations", func() { Expect(err).NotTo(HaveOccurred()) Expect(string(out)).To(Equal(`{ + "cniVersion": "0.1.0", "ip4": { "ip": "1.2.3.30/24", "gateway": "1.2.3.1", From f197c01b624aad277ef6aadee8f504e97f331943 Mon Sep 17 00:00:00 2001 From: Pengfei Ni Date: Wed, 3 May 2017 23:21:44 +0800 Subject: [PATCH 2/2] Fix testings accross the project Signed-off-by: Pengfei Ni --- libcni/api_test.go | 2 ++ pkg/types/current/types.go | 14 +++++++------- pkg/types/current/types_test.go | 2 +- plugins/test/noop/noop_test.go | 1 + 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/libcni/api_test.go b/libcni/api_test.go index a66453db..0616bf4e 100644 --- a/libcni/api_test.go +++ b/libcni/api_test.go @@ -297,6 +297,7 @@ var _ = Describe("Invoking plugins", func() { Expect(err).NotTo(HaveOccurred()) Expect(result).To(Equal(¤t.Result{ + CNIVersion: current.ImplementedSpecVersion, IPs: []*current.IPConfig{ { Version: "4", @@ -479,6 +480,7 @@ var _ = Describe("Invoking plugins", func() { Expect(err).NotTo(HaveOccurred()) Expect(result).To(Equal(¤t.Result{ + CNIVersion: current.ImplementedSpecVersion, // IP4 added by first plugin IPs: []*current.IPConfig{ { diff --git a/pkg/types/current/types.go b/pkg/types/current/types.go index 26e1dd96..b5715fe6 100644 --- a/pkg/types/current/types.go +++ b/pkg/types/current/types.go @@ -24,9 +24,9 @@ import ( "github.com/containernetworking/cni/pkg/types/020" ) -const implementedSpecVersion string = "0.3.1" +const ImplementedSpecVersion string = "0.3.1" -var SupportedVersions = []string{"0.3.0", implementedSpecVersion} +var SupportedVersions = []string{"0.3.0", ImplementedSpecVersion} func NewResult(data []byte) (types.Result, error) { result := &Result{} @@ -37,7 +37,7 @@ func NewResult(data []byte) (types.Result, error) { } func GetResult(r types.Result) (*Result, error) { - resultCurrent, err := r.GetAsVersion(implementedSpecVersion) + resultCurrent, err := r.GetAsVersion(ImplementedSpecVersion) if err != nil { return nil, err } @@ -63,7 +63,7 @@ func convertFrom020(result types.Result) (*Result, error) { } newResult := &Result{ - CNIVersion: implementedSpecVersion, + CNIVersion: ImplementedSpecVersion, DNS: oldResult.DNS, Routes: []*types.Route{}, } @@ -118,7 +118,7 @@ func convertFrom030(result types.Result) (*Result, error) { if !ok { return nil, fmt.Errorf("failed to convert result") } - newResult.CNIVersion = implementedSpecVersion + newResult.CNIVersion = ImplementedSpecVersion return newResult, nil } @@ -193,12 +193,12 @@ func (r *Result) convertTo020() (*types020.Result, error) { } func (r *Result) Version() string { - return implementedSpecVersion + return ImplementedSpecVersion } func (r *Result) GetAsVersion(version string) (types.Result, error) { switch version { - case "0.3.0", implementedSpecVersion: + case "0.3.0", ImplementedSpecVersion: r.CNIVersion = version return r, nil case types020.SupportedVersions[0], types020.SupportedVersions[1], types020.SupportedVersions[2]: diff --git a/pkg/types/current/types_test.go b/pkg/types/current/types_test.go index eb7e7c62..afc68670 100644 --- a/pkg/types/current/types_test.go +++ b/pkg/types/current/types_test.go @@ -174,7 +174,7 @@ var _ = Describe("Current types operations", func() { Expect(err).NotTo(HaveOccurred()) Expect(string(out)).To(Equal(`{ - "cniVersion": "0.1.0", + "cniVersion": "0.2.0", "ip4": { "ip": "1.2.3.30/24", "gateway": "1.2.3.1", diff --git a/plugins/test/noop/noop_test.go b/plugins/test/noop/noop_test.go index 880869b0..bf92dabb 100644 --- a/plugins/test/noop/noop_test.go +++ b/plugins/test/noop/noop_test.go @@ -130,6 +130,7 @@ var _ = Describe("No-op plugin", func() { Expect(err).NotTo(HaveOccurred()) Eventually(session).Should(gexec.Exit(0)) Expect(session.Out.Contents()).To(MatchJSON(`{ + "cniVersion": "0.3.1", "ips": [{"version": "4", "address": "10.1.2.3/24"}], "dns": {"nameservers": ["1.2.3.4"]} }`))