Skip to content

Commit 27a5b99

Browse files
committed
Add cniVersion to Result
Signed-off-by: Pengfei Ni <[email protected]>
1 parent 231f6c4 commit 27a5b99

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

pkg/types/020/types.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import (
2323
"github.com/containernetworking/cni/pkg/types"
2424
)
2525

26-
const implementedSpecVersion string = "0.2.0"
26+
const ImplementedSpecVersion string = "0.2.0"
2727

28-
var SupportedVersions = []string{"", "0.1.0", implementedSpecVersion}
28+
var SupportedVersions = []string{"", "0.1.0", ImplementedSpecVersion}
2929

3030
// Compatibility types for CNI version 0.1.0 and 0.2.0
3131

@@ -39,7 +39,7 @@ func NewResult(data []byte) (types.Result, error) {
3939

4040
func GetResult(r types.Result) (*Result, error) {
4141
// We expect version 0.1.0/0.2.0 results
42-
result020, err := r.GetAsVersion(implementedSpecVersion)
42+
result020, err := r.GetAsVersion(ImplementedSpecVersion)
4343
if err != nil {
4444
return nil, err
4545
}
@@ -52,18 +52,20 @@ func GetResult(r types.Result) (*Result, error) {
5252

5353
// Result is what gets returned from the plugin (via stdout) to the caller
5454
type Result struct {
55-
IP4 *IPConfig `json:"ip4,omitempty"`
56-
IP6 *IPConfig `json:"ip6,omitempty"`
57-
DNS types.DNS `json:"dns,omitempty"`
55+
CNIVersion string `json:"cniVersion,omitempty"`
56+
IP4 *IPConfig `json:"ip4,omitempty"`
57+
IP6 *IPConfig `json:"ip6,omitempty"`
58+
DNS types.DNS `json:"dns,omitempty"`
5859
}
5960

6061
func (r *Result) Version() string {
61-
return implementedSpecVersion
62+
return ImplementedSpecVersion
6263
}
6364

6465
func (r *Result) GetAsVersion(version string) (types.Result, error) {
6566
for _, supportedVersion := range SupportedVersions {
6667
if version == supportedVersion {
68+
r.CNIVersion = version
6769
return r, nil
6870
}
6971
}

pkg/types/020/types_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var _ = Describe("Ensures compatibility with the 0.1.0/0.2.0 spec", func() {
4848

4949
// Set every field of the struct to ensure source compatibility
5050
res := types020.Result{
51+
CNIVersion: types020.ImplementedSpecVersion,
5152
IP4: &types020.IPConfig{
5253
IP: *ipv4,
5354
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() {
8889
Expect(err).NotTo(HaveOccurred())
8990

9091
Expect(string(out)).To(Equal(`{
92+
"cniVersion": "0.2.0",
9193
"ip4": {
9294
"ip": "1.2.3.30/24",
9395
"gateway": "1.2.3.1",

pkg/types/current/types.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ func convertFrom020(result types.Result) (*Result, error) {
6363
}
6464

6565
newResult := &Result{
66-
DNS: oldResult.DNS,
67-
Routes: []*types.Route{},
66+
CNIVersion: implementedSpecVersion,
67+
DNS: oldResult.DNS,
68+
Routes: []*types.Route{},
6869
}
6970

7071
if oldResult.IP4 != nil {
@@ -117,6 +118,7 @@ func convertFrom030(result types.Result) (*Result, error) {
117118
if !ok {
118119
return nil, fmt.Errorf("failed to convert result")
119120
}
121+
newResult.CNIVersion = implementedSpecVersion
120122
return newResult, nil
121123
}
122124

@@ -134,6 +136,7 @@ func NewResultFromResult(result types.Result) (*Result, error) {
134136

135137
// Result is what gets returned from the plugin (via stdout) to the caller
136138
type Result struct {
139+
CNIVersion string `json:"cniVersion,omitempty"`
137140
Interfaces []*Interface `json:"interfaces,omitempty"`
138141
IPs []*IPConfig `json:"ips,omitempty"`
139142
Routes []*types.Route `json:"routes,omitempty"`
@@ -143,7 +146,8 @@ type Result struct {
143146
// Convert to the older 0.2.0 CNI spec Result type
144147
func (r *Result) convertTo020() (*types020.Result, error) {
145148
oldResult := &types020.Result{
146-
DNS: r.DNS,
149+
CNIVersion: types020.ImplementedSpecVersion,
150+
DNS: r.DNS,
147151
}
148152

149153
for _, ip := range r.IPs {
@@ -195,6 +199,7 @@ func (r *Result) Version() string {
195199
func (r *Result) GetAsVersion(version string) (types.Result, error) {
196200
switch version {
197201
case "0.3.0", implementedSpecVersion:
202+
r.CNIVersion = version
198203
return r, nil
199204
case types020.SupportedVersions[0], types020.SupportedVersions[1], types020.SupportedVersions[2]:
200205
return r.convertTo020()

pkg/types/current/types_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func testResult() *current.Result {
4747

4848
// Set every field of the struct to ensure source compatibility
4949
return &current.Result{
50+
CNIVersion: "0.3.1",
5051
Interfaces: []*current.Interface{
5152
{
5253
Name: "eth0",
@@ -103,6 +104,7 @@ var _ = Describe("Current types operations", func() {
103104
Expect(err).NotTo(HaveOccurred())
104105

105106
Expect(string(out)).To(Equal(`{
107+
"cniVersion": "0.3.1",
106108
"interfaces": [
107109
{
108110
"name": "eth0",
@@ -172,6 +174,7 @@ var _ = Describe("Current types operations", func() {
172174
Expect(err).NotTo(HaveOccurred())
173175

174176
Expect(string(out)).To(Equal(`{
177+
"cniVersion": "0.1.0",
175178
"ip4": {
176179
"ip": "1.2.3.30/24",
177180
"gateway": "1.2.3.1",

0 commit comments

Comments
 (0)