| 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 types100 |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"fmt" |
| 20 |
"io" |
| 21 |
"net" |
| 22 |
"os" |
| 23 |
|
| 24 |
"github.com/containernetworking/cni/pkg/types" |
| 25 |
types040 "github.com/containernetworking/cni/pkg/types/040" |
| 26 |
convert "github.com/containernetworking/cni/pkg/types/internal" |
| 27 |
) |
| 28 |
|
| 29 |
// The types did not change between v1.0 and v1.1 |
| 30 |
const ImplementedSpecVersion string = "1.1.0" |
| 31 |
|
| 32 |
var supportedVersions = []string{"1.0.0", "1.1.0"} |
| 33 |
|
| 34 |
// Register converters for all versions less than the implemented spec version |
| 35 |
func init() { |
| 36 |
// Up-converters |
| 37 |
convert.RegisterConverter("0.1.0", supportedVersions, convertFrom02x) |
| 38 |
convert.RegisterConverter("0.2.0", supportedVersions, convertFrom02x) |
| 39 |
convert.RegisterConverter("0.3.0", supportedVersions, convertFrom04x) |
| 40 |
convert.RegisterConverter("0.3.1", supportedVersions, convertFrom04x) |
| 41 |
convert.RegisterConverter("0.4.0", supportedVersions, convertFrom04x) |
| 42 |
convert.RegisterConverter("1.0.0", []string{"1.1.0"}, convertFrom100) |
| 43 |
|
| 44 |
// Down-converters |
| 45 |
convert.RegisterConverter("1.0.0", []string{"0.3.0", "0.3.1", "0.4.0"}, convertTo04x) |
| 46 |
convert.RegisterConverter("1.0.0", []string{"0.1.0", "0.2.0"}, convertTo02x) |
| 47 |
convert.RegisterConverter("1.1.0", []string{"0.3.0", "0.3.1", "0.4.0"}, convertTo04x) |
| 48 |
convert.RegisterConverter("1.1.0", []string{"0.1.0", "0.2.0"}, convertTo02x) |
| 49 |
convert.RegisterConverter("1.1.0", []string{"1.0.0"}, convertFrom100) |
| 50 |
|
| 51 |
// Creator |
| 52 |
convert.RegisterCreator(supportedVersions, NewResult) |
| 53 |
} |
| 54 |
|
| 55 |
func NewResult(data []byte) (types.Result, error) { |
| 56 |
result := &Result{} |
| 57 |
if err := json.Unmarshal(data, result); err != nil { |
| 58 |
return nil, err |
| 59 |
} |
| 60 |
for _, v := range supportedVersions { |
| 61 |
if result.CNIVersion == v { |
| 62 |
return result, nil |
| 63 |
} |
| 64 |
} |
| 65 |
return nil, fmt.Errorf("result type supports %v but unmarshalled CNIVersion is %q", |
| 66 |
supportedVersions, result.CNIVersion) |
| 67 |
} |
| 68 |
|
| 69 |
func GetResult(r types.Result) (*Result, error) { |
| 70 |
resultCurrent, err := r.GetAsVersion(ImplementedSpecVersion) |
| 71 |
if err != nil { |
| 72 |
return nil, err |
| 73 |
} |
| 74 |
result, ok := resultCurrent.(*Result) |
| 75 |
if !ok { |
| 76 |
return nil, fmt.Errorf("failed to convert result") |
| 77 |
} |
| 78 |
return result, nil |
| 79 |
} |
| 80 |
|
| 81 |
func NewResultFromResult(result types.Result) (*Result, error) { |
| 82 |
newResult, err := convert.Convert(result, ImplementedSpecVersion) |
| 83 |
if err != nil { |
| 84 |
return nil, err |
| 85 |
} |
| 86 |
return newResult.(*Result), nil |
| 87 |
} |
| 88 |
|
| 89 |
// Result is what gets returned from the plugin (via stdout) to the caller |
| 90 |
type Result struct { |
| 91 |
CNIVersion string `json:"cniVersion,omitempty"` |
| 92 |
Interfaces []*Interface `json:"interfaces,omitempty"` |
| 93 |
IPs []*IPConfig `json:"ips,omitempty"` |
| 94 |
Routes []*types.Route `json:"routes,omitempty"` |
| 95 |
DNS types.DNS `json:"dns,omitempty"` |
| 96 |
} |
| 97 |
|
| 98 |
// Note: DNS should be omit if DNS is empty but default Marshal function |
| 99 |
// will output empty structure hence need to write a Marshal function |
| 100 |
func (r *Result) MarshalJSON() ([]byte, error) { |
| 101 |
// use type alias to escape recursion for json.Marshal() to MarshalJSON() |
| 102 |
type fixObjType = Result |
| 103 |
|
| 104 |
bytes, err := json.Marshal(fixObjType(*r)) //nolint:all |
| 105 |
if err != nil { |
| 106 |
return nil, err |
| 107 |
} |
| 108 |
|
| 109 |
fixupObj := make(map[string]interface{}) |
| 110 |
if err := json.Unmarshal(bytes, &fixupObj); err != nil { |
| 111 |
return nil, err |
| 112 |
} |
| 113 |
|
| 114 |
if r.DNS.IsEmpty() { |
| 115 |
delete(fixupObj, "dns") |
| 116 |
} |
| 117 |
|
| 118 |
return json.Marshal(fixupObj) |
| 119 |
} |
| 120 |
|
| 121 |
// convertFrom100 does nothing except set the version; the types are the same |
| 122 |
func convertFrom100(from types.Result, toVersion string) (types.Result, error) { |
| 123 |
fromResult := from.(*Result) |
| 124 |
|
| 125 |
result := &Result{ |
| 126 |
CNIVersion: toVersion, |
| 127 |
Interfaces: fromResult.Interfaces, |
| 128 |
IPs: fromResult.IPs, |
| 129 |
Routes: fromResult.Routes, |
| 130 |
DNS: fromResult.DNS, |
| 131 |
} |
| 132 |
return result, nil |
| 133 |
} |
| 134 |
|
| 135 |
func convertFrom02x(from types.Result, toVersion string) (types.Result, error) { |
| 136 |
result040, err := convert.Convert(from, "0.4.0") |
| 137 |
if err != nil { |
| 138 |
return nil, err |
| 139 |
} |
| 140 |
result100, err := convertFrom04x(result040, toVersion) |
| 141 |
if err != nil { |
| 142 |
return nil, err |
| 143 |
} |
| 144 |
return result100, nil |
| 145 |
} |
| 146 |
|
| 147 |
func convertIPConfigFrom040(from *types040.IPConfig) *IPConfig { |
| 148 |
to := &IPConfig{ |
| 149 |
Address: from.Address, |
| 150 |
Gateway: from.Gateway, |
| 151 |
} |
| 152 |
if from.Interface != nil { |
| 153 |
intf := *from.Interface |
| 154 |
to.Interface = &intf |
| 155 |
} |
| 156 |
return to |
| 157 |
} |
| 158 |
|
| 159 |
func convertInterfaceFrom040(from *types040.Interface) *Interface { |
| 160 |
return &Interface{ |
| 161 |
Name: from.Name, |
| 162 |
Mac: from.Mac, |
| 163 |
Sandbox: from.Sandbox, |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
func convertFrom04x(from types.Result, toVersion string) (types.Result, error) { |
| 168 |
fromResult := from.(*types040.Result) |
| 169 |
toResult := &Result{ |
| 170 |
CNIVersion: toVersion, |
| 171 |
DNS: *fromResult.DNS.Copy(), |
| 172 |
Routes: []*types.Route{}, |
| 173 |
} |
| 174 |
for _, fromIntf := range fromResult.Interfaces { |
| 175 |
toResult.Interfaces = append(toResult.Interfaces, convertInterfaceFrom040(fromIntf)) |
| 176 |
} |
| 177 |
for _, fromIPC := range fromResult.IPs { |
| 178 |
toResult.IPs = append(toResult.IPs, convertIPConfigFrom040(fromIPC)) |
| 179 |
} |
| 180 |
for _, fromRoute := range fromResult.Routes { |
| 181 |
toResult.Routes = append(toResult.Routes, fromRoute.Copy()) |
| 182 |
} |
| 183 |
return toResult, nil |
| 184 |
} |
| 185 |
|
| 186 |
func convertIPConfigTo040(from *IPConfig) *types040.IPConfig { |
| 187 |
version := "6" |
| 188 |
if from.Address.IP.To4() != nil { |
| 189 |
version = "4" |
| 190 |
} |
| 191 |
to := &types040.IPConfig{ |
| 192 |
Version: version, |
| 193 |
Address: from.Address, |
| 194 |
Gateway: from.Gateway, |
| 195 |
} |
| 196 |
if from.Interface != nil { |
| 197 |
intf := *from.Interface |
| 198 |
to.Interface = &intf |
| 199 |
} |
| 200 |
return to |
| 201 |
} |
| 202 |
|
| 203 |
func convertInterfaceTo040(from *Interface) *types040.Interface { |
| 204 |
return &types040.Interface{ |
| 205 |
Name: from.Name, |
| 206 |
Mac: from.Mac, |
| 207 |
Sandbox: from.Sandbox, |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
func convertTo04x(from types.Result, toVersion string) (types.Result, error) { |
| 212 |
fromResult := from.(*Result) |
| 213 |
toResult := &types040.Result{ |
| 214 |
CNIVersion: toVersion, |
| 215 |
DNS: *fromResult.DNS.Copy(), |
| 216 |
Routes: []*types.Route{}, |
| 217 |
} |
| 218 |
for _, fromIntf := range fromResult.Interfaces { |
| 219 |
toResult.Interfaces = append(toResult.Interfaces, convertInterfaceTo040(fromIntf)) |
| 220 |
} |
| 221 |
for _, fromIPC := range fromResult.IPs { |
| 222 |
toResult.IPs = append(toResult.IPs, convertIPConfigTo040(fromIPC)) |
| 223 |
} |
| 224 |
for _, fromRoute := range fromResult.Routes { |
| 225 |
toResult.Routes = append(toResult.Routes, fromRoute.Copy()) |
| 226 |
} |
| 227 |
return toResult, nil |
| 228 |
} |
| 229 |
|
| 230 |
func convertTo02x(from types.Result, toVersion string) (types.Result, error) { |
| 231 |
// First convert to 0.4.0 |
| 232 |
result040, err := convertTo04x(from, "0.4.0") |
| 233 |
if err != nil { |
| 234 |
return nil, err |
| 235 |
} |
| 236 |
result02x, err := convert.Convert(result040, toVersion) |
| 237 |
if err != nil { |
| 238 |
return nil, err |
| 239 |
} |
| 240 |
return result02x, nil |
| 241 |
} |
| 242 |
|
| 243 |
func (r *Result) Version() string { |
| 244 |
return r.CNIVersion |
| 245 |
} |
| 246 |
|
| 247 |
func (r *Result) GetAsVersion(version string) (types.Result, error) { |
| 248 |
// If the creator of the result did not set the CNIVersion, assume it |
| 249 |
// should be the highest spec version implemented by this Result |
| 250 |
if r.CNIVersion == "" { |
| 251 |
r.CNIVersion = ImplementedSpecVersion |
| 252 |
} |
| 253 |
return convert.Convert(r, version) |
| 254 |
} |
| 255 |
|
| 256 |
func (r *Result) Print() error { |
| 257 |
return r.PrintTo(os.Stdout) |
| 258 |
} |
| 259 |
|
| 260 |
func (r *Result) PrintTo(writer io.Writer) error { |
| 261 |
data, err := json.MarshalIndent(r, "", " ") |
| 262 |
if err != nil { |
| 263 |
return err |
| 264 |
} |
| 265 |
_, err = writer.Write(data) |
| 266 |
return err |
| 267 |
} |
| 268 |
|
| 269 |
// Interface contains values about the created interfaces |
| 270 |
type Interface struct { |
| 271 |
Name string `json:"name"` |
| 272 |
Mac string `json:"mac,omitempty"` |
| 273 |
Mtu int `json:"mtu,omitempty"` |
| 274 |
Sandbox string `json:"sandbox,omitempty"` |
| 275 |
SocketPath string `json:"socketPath,omitempty"` |
| 276 |
PciID string `json:"pciID,omitempty"` |
| 277 |
} |
| 278 |
|
| 279 |
func (i *Interface) String() string { |
| 280 |
return fmt.Sprintf("%+v", *i) |
| 281 |
} |
| 282 |
|
| 283 |
func (i *Interface) Copy() *Interface { |
| 284 |
if i == nil { |
| 285 |
return nil |
| 286 |
} |
| 287 |
newIntf := *i |
| 288 |
return &newIntf |
| 289 |
} |
| 290 |
|
| 291 |
// Int returns a pointer to the int value passed in. Used to |
| 292 |
// set the IPConfig.Interface field. |
| 293 |
func Int(v int) *int { |
| 294 |
return &v |
| 295 |
} |
| 296 |
|
| 297 |
// IPConfig contains values necessary to configure an IP address on an interface |
| 298 |
type IPConfig struct { |
| 299 |
// Index into Result structs Interfaces list |
| 300 |
Interface *int |
| 301 |
Address net.IPNet |
| 302 |
Gateway net.IP |
| 303 |
} |
| 304 |
|
| 305 |
func (i *IPConfig) String() string { |
| 306 |
return fmt.Sprintf("%+v", *i) |
| 307 |
} |
| 308 |
|
| 309 |
func (i *IPConfig) Copy() *IPConfig { |
| 310 |
if i == nil { |
| 311 |
return nil |
| 312 |
} |
| 313 |
|
| 314 |
ipc := &IPConfig{ |
| 315 |
Address: i.Address, |
| 316 |
Gateway: i.Gateway, |
| 317 |
} |
| 318 |
if i.Interface != nil { |
| 319 |
intf := *i.Interface |
| 320 |
ipc.Interface = &intf |
| 321 |
} |
| 322 |
return ipc |
| 323 |
} |
| 324 |
|
| 325 |
// JSON (un)marshallable types |
| 326 |
type ipConfig struct { |
| 327 |
Interface *int `json:"interface,omitempty"` |
| 328 |
Address types.IPNet `json:"address"` |
| 329 |
Gateway net.IP `json:"gateway,omitempty"` |
| 330 |
} |
| 331 |
|
| 332 |
func (c *IPConfig) MarshalJSON() ([]byte, error) { |
| 333 |
ipc := ipConfig{ |
| 334 |
Interface: c.Interface, |
| 335 |
Address: types.IPNet(c.Address), |
| 336 |
Gateway: c.Gateway, |
| 337 |
} |
| 338 |
|
| 339 |
return json.Marshal(ipc) |
| 340 |
} |
| 341 |
|
| 342 |
func (c *IPConfig) UnmarshalJSON(data []byte) error { |
| 343 |
ipc := ipConfig{} |
| 344 |
if err := json.Unmarshal(data, &ipc); err != nil { |
| 345 |
return err |
| 346 |
} |
| 347 |
|
| 348 |
c.Interface = ipc.Interface |
| 349 |
c.Address = net.IPNet(ipc.Address) |
| 350 |
c.Gateway = ipc.Gateway |
| 351 |
return nil |
| 352 |
} |