Debian Codegraph @

pkg/types/040/types.go

raw
1 // Copyright 2016 CNI authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package types040
16
17 import (
18 "encoding/json"
19 "fmt"
20 "io"
21 "net"
22 "os"
23
24 "github.com/containernetworking/cni/pkg/types"
25 types020 "github.com/containernetworking/cni/pkg/types/020"
26 convert "github.com/containernetworking/cni/pkg/types/internal"
27 )
28
29 const ImplementedSpecVersion string = "0.4.0"
30
31 var supportedVersions = []string{"0.3.0", "0.3.1", ImplementedSpecVersion}
32
33 // Register converters for all versions less than the implemented spec version
34 func init() {
35 // Up-converters
36 convert.RegisterConverter("0.1.0", supportedVersions, convertFrom02x)
37 convert.RegisterConverter("0.2.0", supportedVersions, convertFrom02x)
38 convert.RegisterConverter("0.3.0", supportedVersions, convertInternal)
39 convert.RegisterConverter("0.3.1", supportedVersions, convertInternal)
40
41 // Down-converters
42 convert.RegisterConverter("0.4.0", []string{"0.3.0", "0.3.1"}, convertInternal)
43 convert.RegisterConverter("0.4.0", []string{"0.1.0", "0.2.0"}, convertTo02x)
44 convert.RegisterConverter("0.3.1", []string{"0.1.0", "0.2.0"}, convertTo02x)
45 convert.RegisterConverter("0.3.0", []string{"0.1.0", "0.2.0"}, convertTo02x)
46
47 // Creator
48 convert.RegisterCreator(supportedVersions, NewResult)
49 }
50
51 func NewResult(data []byte) (types.Result, error) {
52 result := &Result{}
53 if err := json.Unmarshal(data, result); err != nil {
54 return nil, err
55 }
56 for _, v := range supportedVersions {
57 if result.CNIVersion == v {
58 return result, nil
59 }
60 }
61 return nil, fmt.Errorf("result type supports %v but unmarshalled CNIVersion is %q",
62 supportedVersions, result.CNIVersion)
63 }
64
65 func GetResult(r types.Result) (*Result, error) {
66 resultCurrent, err := r.GetAsVersion(ImplementedSpecVersion)
67 if err != nil {
68 return nil, err
69 }
70 result, ok := resultCurrent.(*Result)
71 if !ok {
72 return nil, fmt.Errorf("failed to convert result")
73 }
74 return result, nil
75 }
76
77 func NewResultFromResult(result types.Result) (*Result, error) {
78 newResult, err := convert.Convert(result, ImplementedSpecVersion)
79 if err != nil {
80 return nil, err
81 }
82 return newResult.(*Result), nil
83 }
84
85 // Result is what gets returned from the plugin (via stdout) to the caller
86 type Result struct {
87 CNIVersion string `json:"cniVersion,omitempty"`
88 Interfaces []*Interface `json:"interfaces,omitempty"`
89 IPs []*IPConfig `json:"ips,omitempty"`
90 Routes []*types.Route `json:"routes,omitempty"`
91 DNS types.DNS `json:"dns,omitempty"`
92 }
93
94 func convert020IPConfig(from *types020.IPConfig, ipVersion string) *IPConfig {
95 return &IPConfig{
96 Version: ipVersion,
97 Address: from.IP,
98 Gateway: from.Gateway,
99 }
100 }
101
102 func convertFrom02x(from types.Result, toVersion string) (types.Result, error) {
103 fromResult := from.(*types020.Result)
104 toResult := &Result{
105 CNIVersion: toVersion,
106 DNS: *fromResult.DNS.Copy(),
107 Routes: []*types.Route{},
108 }
109 if fromResult.IP4 != nil {
110 toResult.IPs = append(toResult.IPs, convert020IPConfig(fromResult.IP4, "4"))
111 for _, fromRoute := range fromResult.IP4.Routes {
112 toResult.Routes = append(toResult.Routes, fromRoute.Copy())
113 }
114 }
115
116 if fromResult.IP6 != nil {
117 toResult.IPs = append(toResult.IPs, convert020IPConfig(fromResult.IP6, "6"))
118 for _, fromRoute := range fromResult.IP6.Routes {
119 toResult.Routes = append(toResult.Routes, fromRoute.Copy())
120 }
121 }
122
123 return toResult, nil
124 }
125
126 func convertInternal(from types.Result, toVersion string) (types.Result, error) {
127 fromResult := from.(*Result)
128 toResult := &Result{
129 CNIVersion: toVersion,
130 DNS: *fromResult.DNS.Copy(),
131 Routes: []*types.Route{},
132 }
133 for _, fromIntf := range fromResult.Interfaces {
134 toResult.Interfaces = append(toResult.Interfaces, fromIntf.Copy())
135 }
136 for _, fromIPC := range fromResult.IPs {
137 toResult.IPs = append(toResult.IPs, fromIPC.Copy())
138 }
139 for _, fromRoute := range fromResult.Routes {
140 toResult.Routes = append(toResult.Routes, fromRoute.Copy())
141 }
142 return toResult, nil
143 }
144
145 func convertTo02x(from types.Result, toVersion string) (types.Result, error) {
146 fromResult := from.(*Result)
147 toResult := &types020.Result{
148 CNIVersion: toVersion,
149 DNS: *fromResult.DNS.Copy(),
150 }
151
152 for _, fromIP := range fromResult.IPs {
153 // Only convert the first IP address of each version as 0.2.0
154 // and earlier cannot handle multiple IP addresses
155 if fromIP.Version == "4" && toResult.IP4 == nil {
156 toResult.IP4 = &types020.IPConfig{
157 IP: fromIP.Address,
158 Gateway: fromIP.Gateway,
159 }
160 } else if fromIP.Version == "6" && toResult.IP6 == nil {
161 toResult.IP6 = &types020.IPConfig{
162 IP: fromIP.Address,
163 Gateway: fromIP.Gateway,
164 }
165 }
166 if toResult.IP4 != nil && toResult.IP6 != nil {
167 break
168 }
169 }
170
171 for _, fromRoute := range fromResult.Routes {
172 is4 := fromRoute.Dst.IP.To4() != nil
173 if is4 && toResult.IP4 != nil {
174 toResult.IP4.Routes = append(toResult.IP4.Routes, types.Route{
175 Dst: fromRoute.Dst,
176 GW: fromRoute.GW,
177 })
178 } else if !is4 && toResult.IP6 != nil {
179 toResult.IP6.Routes = append(toResult.IP6.Routes, types.Route{
180 Dst: fromRoute.Dst,
181 GW: fromRoute.GW,
182 })
183 }
184 }
185
186 // 0.2.0 and earlier require at least one IP address in the Result
187 if toResult.IP4 == nil && toResult.IP6 == nil {
188 return nil, fmt.Errorf("cannot convert: no valid IP addresses")
189 }
190
191 return toResult, nil
192 }
193
194 func (r *Result) Version() string {
195 return r.CNIVersion
196 }
197
198 func (r *Result) GetAsVersion(version string) (types.Result, error) {
199 // If the creator of the result did not set the CNIVersion, assume it
200 // should be the highest spec version implemented by this Result
201 if r.CNIVersion == "" {
202 r.CNIVersion = ImplementedSpecVersion
203 }
204 return convert.Convert(r, version)
205 }
206
207 func (r *Result) Print() error {
208 return r.PrintTo(os.Stdout)
209 }
210
211 func (r *Result) PrintTo(writer io.Writer) error {
212 data, err := json.MarshalIndent(r, "", " ")
213 if err != nil {
214 return err
215 }
216 _, err = writer.Write(data)
217 return err
218 }
219
220 // Interface contains values about the created interfaces
221 type Interface struct {
222 Name string `json:"name"`
223 Mac string `json:"mac,omitempty"`
224 Sandbox string `json:"sandbox,omitempty"`
225 }
226
227 func (i *Interface) String() string {
228 return fmt.Sprintf("%+v", *i)
229 }
230
231 func (i *Interface) Copy() *Interface {
232 if i == nil {
233 return nil
234 }
235 newIntf := *i
236 return &newIntf
237 }
238
239 // Int returns a pointer to the int value passed in. Used to
240 // set the IPConfig.Interface field.
241 func Int(v int) *int {
242 return &v
243 }
244
245 // IPConfig contains values necessary to configure an IP address on an interface
246 type IPConfig struct {
247 // IP version, either "4" or "6"
248 Version string
249 // Index into Result structs Interfaces list
250 Interface *int
251 Address net.IPNet
252 Gateway net.IP
253 }
254
255 func (i *IPConfig) String() string {
256 return fmt.Sprintf("%+v", *i)
257 }
258
259 func (i *IPConfig) Copy() *IPConfig {
260 if i == nil {
261 return nil
262 }
263
264 ipc := &IPConfig{
265 Version: i.Version,
266 Address: i.Address,
267 Gateway: i.Gateway,
268 }
269 if i.Interface != nil {
270 intf := *i.Interface
271 ipc.Interface = &intf
272 }
273 return ipc
274 }
275
276 // JSON (un)marshallable types
277 type ipConfig struct {
278 Version string `json:"version"`
279 Interface *int `json:"interface,omitempty"`
280 Address types.IPNet `json:"address"`
281 Gateway net.IP `json:"gateway,omitempty"`
282 }
283
284 func (c *IPConfig) MarshalJSON() ([]byte, error) {
285 ipc := ipConfig{
286 Version: c.Version,
287 Interface: c.Interface,
288 Address: types.IPNet(c.Address),
289 Gateway: c.Gateway,
290 }
291
292 return json.Marshal(ipc)
293 }
294
295 func (c *IPConfig) UnmarshalJSON(data []byte) error {
296 ipc := ipConfig{}
297 if err := json.Unmarshal(data, &ipc); err != nil {
298 return err
299 }
300
301 c.Version = ipc.Version
302 c.Interface = ipc.Interface
303 c.Address = net.IPNet(ipc.Address)
304 c.Gateway = ipc.Gateway
305 return nil
306 }