Debian Codegraph @

Documentation/spec-upgrades.md

raw
1 # How to Upgrade to CNI Specification v1.0
2
3 CNI v1.0 has the following changes:
4
5 - non-List configurations are removed
6 - the `version` field in the `interfaces` array was redundant and is removed
7
8 ## libcni Changes in CNI v1.0
9
10 **`/pkg/types/current` no longer exists**
11
12 This means that runtimes need to explicitly select a version they support.
13 This reduces code breakage when revendoring cni into other projects and
14 returns the decision on which CNI Spec versions a plugin supports to the
15 plugin's authors.
16
17 For example, your Go imports might look like
18
19 ```go
20 import (
21 cniv1 "github.com/containernetworking/cni/pkg/types/100"
22 )
23 ```
24
25
26 # Changes in CNI v0.4
27
28 CNI v0.4 has the following important changes:
29
30 - A new verb, "CHECK", was added. Runtimes can now ask plugins to verify the status of a container's attachment
31 - A new configuration flag, `disableCheck`, which indicates to the runtime that configuration should not be CHECK'ed
32
33 No changes were made to the result type.
34
35
36 # How to upgrade to CNI Specification v0.3.0 and later
37
38 The 0.3.0 specification contained a small error. The Result structure's `ip` field should have been renamed to `ips` to be consistent with the IPAM result structure definition; this rename was missed when updating the Result to accommodate multiple IP addresses and interfaces. All first-party CNI plugins (bridge, host-local, etc) were updated to use `ips` (and thus be inconsistent with the 0.3.0 specification) and most other plugins have not been updated to the 0.3.0 specification yet, so few (if any) users should be impacted by this change.
39
40 The 0.3.1 specification corrects the `Result` structure to use the `ips` field name as originally intended. This is the only change between 0.3.0 and 0.3.1.
41
42 Version 0.3.0 of the [CNI Specification](https://github.com/containernetworking/cni/blob/spec-v0.3.0/SPEC.md) provides rich information
43 about container network configuration, including details of network interfaces
44 and support for multiple IP addresses.
45
46 To support this new data, the specification changed in a couple significant
47 ways that will impact CNI users, plugin authors, and runtime authors.
48
49 This document provides guidance for how to upgrade:
50
51 - [For CNI Users](#for-cni-users)
52 - [For Plugin Authors](#for-plugin-authors)
53 - [For Runtime Authors](#for-runtime-authors)
54
55 **Note**: the CNI Spec is versioned independently from the GitHub releases
56 for this repo. For example, Release v0.4.0 supports Spec version v0.2.0,
57 and Release v0.5.0 supports Spec v0.3.0.
58
59 ----
60
61 ## For CNI Users
62 If you maintain CNI configuration files for a container runtime that uses CNI,
63 ensure that the configuration files specify a `cniVersion` field and that the
64 version there is supported by your container runtime and CNI plugins.
65 Configuration files without a version field should be given version 0.2.0.
66 The CNI spec includes example configuration files for
67 [single plugins](SPEC.md#example-configurations)
68 and for [lists of chained plugins](SPEC.md#example-configurations).
69
70 Consult the documentation for your runtime and plugins to determine what
71 CNI spec versions they support. Test any plugin upgrades before deploying to
72 production. You may find [cnitool](https://github.com/containernetworking/cni/tree/main/cnitool)
73 useful. Specifically, your configuration version should be the lowest common
74 version supported by your plugins.
75
76 ## For Plugin Authors
77 This section provides guidance for upgrading plugins to CNI Spec Version 0.3.0.
78
79 ### General guidance for all plugins (language agnostic)
80 To provide the smoothest upgrade path, **existing plugins should support
81 multiple versions of the CNI spec**. In particular, plugins with existing
82 installed bases should add support for CNI spec version 1.0.0 while maintaining
83 compatibility with older versions.
84
85 To do this, two changes are required. First, a plugin should advertise which
86 CNI spec versions it supports. It does this by responding to the `VERSION`
87 command with the following JSON data:
88
89 ```json
90 {
91 "cniVersion": "1.0.0",
92 "supportedVersions": [ "0.1.0", "0.2.0", "0.3.0", "0.3.1", "0.4.0", "1.0.0" ]
93 }
94 ```
95
96 Second, for the `ADD` command, a plugin must respect the `cniVersion` field
97 provided in the [network configuration JSON](SPEC.md#network-configuration).
98 That field is a request for the plugin to return results of a particular format:
99
100 - If the `cniVersion` field is not present, then spec v0.2.0 should be assumed
101 and v0.2.0 format result JSON returned.
102
103 - If the plugin doesn't support the version, the plugin must error.
104
105 - Otherwise, the plugin must return a [CNI Result](SPEC.md#result)
106 in the format requested.
107
108 Result formats for older CNI spec versions are available in the
109 [git history for SPEC.md](https://github.com/containernetworking/cni/commits/main/SPEC.md).
110
111 For example, suppose a plugin, via its `VERSION` response, advertises CNI specification
112 support for v0.2.0 and v0.3.0. When it receives `cniVersion` key of `0.2.0`,
113 the plugin must return result JSON conforming to CNI spec version 0.2.0.
114
115 ### Specific guidance for plugins written in Go
116 Plugins written in Go may leverage the Go language packages in this repository
117 to ease the process of upgrading and supporting multiple versions. CNI
118 [Library and Plugins Release v0.5.0](https://github.com/containernetworking/cni/releases/tag/v0.5.0)
119 includes important changes to the Golang APIs. Plugins using these APIs will
120 require some changes now, but should more-easily handle spec changes and
121 new features going forward.
122
123 For plugin authors, the biggest change is that `types.Result` is now an
124 interface implemented by concrete struct types in the `types/100`,
125 `types/040`, and `types/020` subpackages.
126
127 Internally, plugins should use the latest spec version (eg `types/100`) structs,
128 and convert to or from specific versions when required. A typical plugin will
129 only need to do a single conversion when it is about to complete and
130 needs to print the result JSON in the requested `cniVersion` format to stdout.
131 The library function `types.PrintResult()` simplifies this by converting and
132 printing in a single call.
133
134 Additionally, the plugin should advertise which CNI Spec versions it supports
135 via the 3rd argument to `skel.PluginMain()`.
136
137 Here is some example code
138
139 ```go
140 import (
141 "github.com/containernetworking/cni/pkg/skel"
142 "github.com/containernetworking/cni/pkg/types"
143 current "github.com/containernetworking/cni/pkg/types/100"
144 "github.com/containernetworking/cni/pkg/version"
145 )
146
147 func cmdAdd(args *skel.CmdArgs) error {
148 // determine spec version to use
149 var netConf struct {
150 types.NetConf
151 // other plugin-specific configuration goes here
152 }
153 err := json.Unmarshal(args.StdinData, &netConf)
154 cniVersion := netConf.CNIVersion
155
156 // plugin does its work...
157 // set up interfaces
158 // assign addresses, etc
159
160 // construct the result
161 result := &current.Result{
162 Interfaces: []*current.Interface{ ... },
163 IPs: []*current.IPs{ ... },
164 ...
165 }
166
167 // print result to stdout, in the format defined by the requested cniVersion
168 return types.PrintResult(result, cniVersion)
169 }
170
171 func main() {
172 skel.PluginMain(cmdAdd, cmdDel, version.All)
173 }
174 ```
175
176 Alternately, to use the result from a delegated IPAM plugin, the `result`
177 value might be formed like this:
178
179 ```go
180 ipamResult, err := ipam.ExecAdd(netConf.IPAM.Type, args.StdinData)
181 result, err := current.NewResultFromResult(ipamResult)
182 ```
183
184 Other examples of spec v0.3.0-compatible plugins are the
185 [main plugins in this repo](https://github.com/containernetworking/plugins/)
186
187
188 ## For Runtime Authors
189
190 This section provides guidance for upgrading container runtimes to support
191 CNI Spec Version 0.3.0 and later.
192
193 ### General guidance for all runtimes (language agnostic)
194
195 #### Support multiple CNI spec versions
196 To provide the smoothest upgrade path and support the broadest range of CNI
197 plugins, **container runtimes should support multiple versions of the CNI spec**.
198 In particular, runtimes with existing installed bases should add support for CNI
199 spec version 0.3.0 and later while maintaining compatibility with older versions.
200
201 To support multiple versions of the CNI spec, runtimes should be able to
202 call both new and legacy plugins, and handle the results from either.
203
204 When calling a plugin, the runtime must request that the plugin respond in a
205 particular format by specifying the `cniVersion` field in the
206 [Network Configuration](SPEC.md#network-configuration)
207 JSON block. The plugin will then respond with
208 a [Result](SPEC.md#result)
209 in the format defined by that CNI spec version, and the runtime must parse
210 and handle this result.
211
212 #### Handle errors due to version incompatibility
213 Plugins may respond with error indicating that they don't support the requested
214 CNI version (see [Well-known Error Codes](SPEC.md#well-known-error-codes)),
215 e.g.
216 ```json
217 {
218 "cniVersion": "0.2.0",
219 "code": 1,
220 "msg": "CNI version not supported"
221 }
222 ```
223 In that case, the runtime may retry with a lower CNI spec version, or take
224 some other action.
225
226 #### (optional) Discover plugin version support
227 Runtimes may discover which CNI spec versions are supported by a plugin, by
228 calling the plugin with the `VERSION` command. The `VERSION` command was
229 added in CNI spec v0.2.0, so older plugins may not respect it. In the absence
230 of a successful response to `VERSION`, assume that the plugin only supports
231 CNI spec v0.1.0.
232
233 #### Handle missing data in v0.3.0 and later results
234 The Result for the `ADD` command in CNI spec version 0.3.0 and later includes
235 a new field `interfaces`. An IP address in the `ip` field may describe which
236 interface it is assigned to, by placing a numeric index in the `interface`
237 subfield.
238
239 However, some plugins which are v0.3.0 and later compatible may nonetheless
240 omit the `interfaces` field and/or set the `interface` index value to `-1`.
241 Runtimes should gracefully handle this situation, unless they have good reason
242 to rely on the existence of the interface data. In that case, provide the user
243 an error message that helps diagnose the issue.
244
245 ### Specific guidance for container runtimes written in Go
246 Container runtimes written in Go may leverage the Go language packages in this
247 repository to ease the process of upgrading and supporting multiple versions.
248 CNI [Library and Plugins Release v0.5.0](https://github.com/containernetworking/cni/releases)
249 includes important changes to the Golang APIs. Runtimes using these APIs will
250 require some changes now, but should more-easily handle spec changes and
251 new features going forward.
252
253 For runtimes, the biggest changes to the Go libraries are in the `types` package.
254 It has been refactored to make working with versioned results simpler. The top-level
255 `types.Result` is now an opaque interface instead of a struct, and APIs exposed by
256 other packages, such as the high-level `libcni` package, have been updated to use
257 this interface. Concrete types are now per-version subpackages. The `types/current`
258 subpackage contains the latest (spec v0.3.0) types.
259
260 When up-converting older result types to spec v0.3.0 and later, fields new in
261 spec v0.3.0 and later (like `interfaces`) may be empty. Conversely, when
262 down-converting v0.3.0 and later results to an older version, any data in
263 those fields will be lost.
264
265 | From | 0.1 | 0.2 | 0.3 | 0.4 | 1.0 |
266 |--------|-----|-----|-----|-----|-----|
267 | To 0.1 | ✔ | ✔ | x | x | x |
268 | To 0.2 | ✔ | ✔ | x | x | x |
269 | To 0.3 | ✴ | ✴ | ✔ | ✔ | ✔ |
270 | To 0.4 | ✴ | ✴ | ✔ | ✔ | ✔ |
271 | To 1.0 | ✴ | ✴ | ✔ | ✔ | ✔ |
272
273 Key:
274 > ✔ : lossless conversion <br>
275 > ✴ : higher-version output may have empty fields <br>
276 > x : lower-version output is missing some data <br>
277
278
279
280 A container runtime should use `current.NewResultFromResult()` to convert the
281 opaque `types.Result` to a concrete `current.Result` struct. It may then
282 work with the fields exposed by that struct:
283
284 ```go
285 // runtime invokes the plugin to get the opaque types.Result
286 // this may conform to any CNI spec version
287 resultInterface, err := libcni.AddNetwork(ctx, netConf, runtimeConf)
288
289 // upconvert result to the current 0.3.0 spec
290 result, err := current.NewResultFromResult(resultInterface)
291
292 // use the result fields ....
293 for _, ip := range result.IPs { ... }
294 ```