Add cniVersion to Result

Signed-off-by: Pengfei Ni <feiskyer@gmail.com>
This commit is contained in:
Pengfei Ni 2017-05-03 21:38:28 +08:00
parent 231f6c4c53
commit 27a5b994c2
4 changed files with 22 additions and 10 deletions

View File

@ -23,9 +23,9 @@ import (
"github.com/containernetworking/cni/pkg/types" "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 // 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) { func GetResult(r types.Result) (*Result, error) {
// We expect version 0.1.0/0.2.0 results // We expect version 0.1.0/0.2.0 results
result020, err := r.GetAsVersion(implementedSpecVersion) result020, err := r.GetAsVersion(ImplementedSpecVersion)
if err != nil { if err != nil {
return nil, err 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 // Result is what gets returned from the plugin (via stdout) to the caller
type Result struct { type Result struct {
CNIVersion string `json:"cniVersion,omitempty"`
IP4 *IPConfig `json:"ip4,omitempty"` IP4 *IPConfig `json:"ip4,omitempty"`
IP6 *IPConfig `json:"ip6,omitempty"` IP6 *IPConfig `json:"ip6,omitempty"`
DNS types.DNS `json:"dns,omitempty"` DNS types.DNS `json:"dns,omitempty"`
} }
func (r *Result) Version() string { func (r *Result) Version() string {
return implementedSpecVersion return ImplementedSpecVersion
} }
func (r *Result) GetAsVersion(version string) (types.Result, error) { func (r *Result) GetAsVersion(version string) (types.Result, error) {
for _, supportedVersion := range SupportedVersions { for _, supportedVersion := range SupportedVersions {
if version == supportedVersion { if version == supportedVersion {
r.CNIVersion = version
return r, nil return r, nil
} }
} }

View File

@ -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 // Set every field of the struct to ensure source compatibility
res := types020.Result{ res := types020.Result{
CNIVersion: types020.ImplementedSpecVersion,
IP4: &types020.IPConfig{ IP4: &types020.IPConfig{
IP: *ipv4, IP: *ipv4,
Gateway: net.ParseIP("1.2.3.1"), 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(err).NotTo(HaveOccurred())
Expect(string(out)).To(Equal(`{ Expect(string(out)).To(Equal(`{
"cniVersion": "0.2.0",
"ip4": { "ip4": {
"ip": "1.2.3.30/24", "ip": "1.2.3.30/24",
"gateway": "1.2.3.1", "gateway": "1.2.3.1",

View File

@ -63,6 +63,7 @@ func convertFrom020(result types.Result) (*Result, error) {
} }
newResult := &Result{ newResult := &Result{
CNIVersion: implementedSpecVersion,
DNS: oldResult.DNS, DNS: oldResult.DNS,
Routes: []*types.Route{}, Routes: []*types.Route{},
} }
@ -117,6 +118,7 @@ func convertFrom030(result types.Result) (*Result, error) {
if !ok { if !ok {
return nil, fmt.Errorf("failed to convert result") return nil, fmt.Errorf("failed to convert result")
} }
newResult.CNIVersion = implementedSpecVersion
return newResult, nil 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 // Result is what gets returned from the plugin (via stdout) to the caller
type Result struct { type Result struct {
CNIVersion string `json:"cniVersion,omitempty"`
Interfaces []*Interface `json:"interfaces,omitempty"` Interfaces []*Interface `json:"interfaces,omitempty"`
IPs []*IPConfig `json:"ips,omitempty"` IPs []*IPConfig `json:"ips,omitempty"`
Routes []*types.Route `json:"routes,omitempty"` Routes []*types.Route `json:"routes,omitempty"`
@ -143,6 +146,7 @@ type Result struct {
// Convert to the older 0.2.0 CNI spec Result type // Convert to the older 0.2.0 CNI spec Result type
func (r *Result) convertTo020() (*types020.Result, error) { func (r *Result) convertTo020() (*types020.Result, error) {
oldResult := &types020.Result{ oldResult := &types020.Result{
CNIVersion: types020.ImplementedSpecVersion,
DNS: r.DNS, DNS: r.DNS,
} }
@ -195,6 +199,7 @@ func (r *Result) Version() string {
func (r *Result) GetAsVersion(version string) (types.Result, error) { func (r *Result) GetAsVersion(version string) (types.Result, error) {
switch version { switch version {
case "0.3.0", implementedSpecVersion: case "0.3.0", implementedSpecVersion:
r.CNIVersion = version
return r, nil return r, nil
case types020.SupportedVersions[0], types020.SupportedVersions[1], types020.SupportedVersions[2]: case types020.SupportedVersions[0], types020.SupportedVersions[1], types020.SupportedVersions[2]:
return r.convertTo020() return r.convertTo020()

View File

@ -47,6 +47,7 @@ func testResult() *current.Result {
// Set every field of the struct to ensure source compatibility // Set every field of the struct to ensure source compatibility
return &current.Result{ return &current.Result{
CNIVersion: "0.3.1",
Interfaces: []*current.Interface{ Interfaces: []*current.Interface{
{ {
Name: "eth0", Name: "eth0",
@ -103,6 +104,7 @@ var _ = Describe("Current types operations", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(string(out)).To(Equal(`{ Expect(string(out)).To(Equal(`{
"cniVersion": "0.3.1",
"interfaces": [ "interfaces": [
{ {
"name": "eth0", "name": "eth0",
@ -172,6 +174,7 @@ var _ = Describe("Current types operations", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(string(out)).To(Equal(`{ Expect(string(out)).To(Equal(`{
"cniVersion": "0.1.0",
"ip4": { "ip4": {
"ip": "1.2.3.30/24", "ip": "1.2.3.30/24",
"gateway": "1.2.3.1", "gateway": "1.2.3.1",