| 1 |
// Copyright 2015 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 types |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"fmt" |
| 20 |
"io" |
| 21 |
"net" |
| 22 |
"os" |
| 23 |
) |
| 24 |
|
| 25 |
// like net.IPNet but adds JSON marshalling and unmarshalling |
| 26 |
type IPNet net.IPNet |
| 27 |
|
| 28 |
// ParseCIDR takes a string like "10.2.3.1/24" and |
| 29 |
// return IPNet with "10.2.3.1" and /24 mask |
| 30 |
func ParseCIDR(s string) (*net.IPNet, error) { |
| 31 |
ip, ipn, err := net.ParseCIDR(s) |
| 32 |
if err != nil { |
| 33 |
return nil, err |
| 34 |
} |
| 35 |
|
| 36 |
ipn.IP = ip |
| 37 |
return ipn, nil |
| 38 |
} |
| 39 |
|
| 40 |
func (n IPNet) MarshalJSON() ([]byte, error) { |
| 41 |
return json.Marshal((*net.IPNet)(&n).String()) |
| 42 |
} |
| 43 |
|
| 44 |
func (n *IPNet) UnmarshalJSON(data []byte) error { |
| 45 |
var s string |
| 46 |
if err := json.Unmarshal(data, &s); err != nil { |
| 47 |
return err |
| 48 |
} |
| 49 |
|
| 50 |
tmp, err := ParseCIDR(s) |
| 51 |
if err != nil { |
| 52 |
return err |
| 53 |
} |
| 54 |
|
| 55 |
*n = IPNet(*tmp) |
| 56 |
return nil |
| 57 |
} |
| 58 |
|
| 59 |
// Use PluginConf instead of NetConf, the NetConf |
| 60 |
// backwards-compat alias will be removed in a future release. |
| 61 |
type NetConf = PluginConf |
| 62 |
|
| 63 |
// PluginConf describes a plugin configuration for a specific network. |
| 64 |
type PluginConf struct { |
| 65 |
CNIVersion string `json:"cniVersion,omitempty"` |
| 66 |
|
| 67 |
Name string `json:"name,omitempty"` |
| 68 |
Type string `json:"type,omitempty"` |
| 69 |
Capabilities map[string]bool `json:"capabilities,omitempty"` |
| 70 |
IPAM IPAM `json:"ipam,omitempty"` |
| 71 |
DNS DNS `json:"dns,omitempty"` |
| 72 |
|
| 73 |
RawPrevResult map[string]interface{} `json:"prevResult,omitempty"` |
| 74 |
PrevResult Result `json:"-"` |
| 75 |
|
| 76 |
// ValidAttachments is only supplied when executing a GC operation |
| 77 |
ValidAttachments []GCAttachment `json:"cni.dev/valid-attachments,omitempty"` |
| 78 |
} |
| 79 |
|
| 80 |
// GCAttachment is the parameters to a GC call -- namely, |
| 81 |
// the container ID and ifname pair that represents a |
| 82 |
// still-valid attachment. |
| 83 |
type GCAttachment struct { |
| 84 |
ContainerID string `json:"containerID"` |
| 85 |
IfName string `json:"ifname"` |
| 86 |
} |
| 87 |
|
| 88 |
// Note: DNS should be omit if DNS is empty but default Marshal function |
| 89 |
// will output empty structure hence need to write a Marshal function |
| 90 |
func (n *PluginConf) MarshalJSON() ([]byte, error) { |
| 91 |
bytes, err := json.Marshal(*n) |
| 92 |
if err != nil { |
| 93 |
return nil, err |
| 94 |
} |
| 95 |
|
| 96 |
fixupObj := make(map[string]interface{}) |
| 97 |
if err := json.Unmarshal(bytes, &fixupObj); err != nil { |
| 98 |
return nil, err |
| 99 |
} |
| 100 |
|
| 101 |
if n.DNS.IsEmpty() { |
| 102 |
delete(fixupObj, "dns") |
| 103 |
} |
| 104 |
|
| 105 |
return json.Marshal(fixupObj) |
| 106 |
} |
| 107 |
|
| 108 |
type IPAM struct { |
| 109 |
Type string `json:"type,omitempty"` |
| 110 |
} |
| 111 |
|
| 112 |
// IsEmpty returns true if IPAM structure has no value, otherwise return false |
| 113 |
func (i *IPAM) IsEmpty() bool { |
| 114 |
return i.Type == "" |
| 115 |
} |
| 116 |
|
| 117 |
// NetConfList describes an ordered list of networks. |
| 118 |
type NetConfList struct { |
| 119 |
CNIVersion string `json:"cniVersion,omitempty"` |
| 120 |
|
| 121 |
Name string `json:"name,omitempty"` |
| 122 |
DisableCheck bool `json:"disableCheck,omitempty"` |
| 123 |
DisableGC bool `json:"disableGC,omitempty"` |
| 124 |
Plugins []*PluginConf `json:"plugins,omitempty"` |
| 125 |
} |
| 126 |
|
| 127 |
// Result is an interface that provides the result of plugin execution |
| 128 |
type Result interface { |
| 129 |
// The highest CNI specification result version the result supports |
| 130 |
// without having to convert |
| 131 |
Version() string |
| 132 |
|
| 133 |
// Returns the result converted into the requested CNI specification |
| 134 |
// result version, or an error if conversion failed |
| 135 |
GetAsVersion(version string) (Result, error) |
| 136 |
|
| 137 |
// Prints the result in JSON format to stdout |
| 138 |
Print() error |
| 139 |
|
| 140 |
// Prints the result in JSON format to provided writer |
| 141 |
PrintTo(writer io.Writer) error |
| 142 |
} |
| 143 |
|
| 144 |
func PrintResult(result Result, version string) error { |
| 145 |
newResult, err := result.GetAsVersion(version) |
| 146 |
if err != nil { |
| 147 |
return err |
| 148 |
} |
| 149 |
return newResult.Print() |
| 150 |
} |
| 151 |
|
| 152 |
// DNS contains values interesting for DNS resolvers |
| 153 |
type DNS struct { |
| 154 |
Nameservers []string `json:"nameservers,omitempty"` |
| 155 |
Domain string `json:"domain,omitempty"` |
| 156 |
Search []string `json:"search,omitempty"` |
| 157 |
Options []string `json:"options,omitempty"` |
| 158 |
} |
| 159 |
|
| 160 |
// IsEmpty returns true if DNS structure has no value, otherwise return false |
| 161 |
func (d *DNS) IsEmpty() bool { |
| 162 |
if len(d.Nameservers) == 0 && d.Domain == "" && len(d.Search) == 0 && len(d.Options) == 0 { |
| 163 |
return true |
| 164 |
} |
| 165 |
return false |
| 166 |
} |
| 167 |
|
| 168 |
func (d *DNS) Copy() *DNS { |
| 169 |
if d == nil { |
| 170 |
return nil |
| 171 |
} |
| 172 |
|
| 173 |
to := &DNS{Domain: d.Domain} |
| 174 |
to.Nameservers = append(to.Nameservers, d.Nameservers...) |
| 175 |
to.Search = append(to.Search, d.Search...) |
| 176 |
to.Options = append(to.Options, d.Options...) |
| 177 |
return to |
| 178 |
} |
| 179 |
|
| 180 |
type Route struct { |
| 181 |
Dst net.IPNet |
| 182 |
GW net.IP |
| 183 |
MTU int |
| 184 |
AdvMSS int |
| 185 |
Priority int |
| 186 |
Table *int |
| 187 |
Scope *int |
| 188 |
} |
| 189 |
|
| 190 |
func (r *Route) String() string { |
| 191 |
table := "<nil>" |
| 192 |
if r.Table != nil { |
| 193 |
table = fmt.Sprintf("%d", *r.Table) |
| 194 |
} |
| 195 |
|
| 196 |
scope := "<nil>" |
| 197 |
if r.Scope != nil { |
| 198 |
scope = fmt.Sprintf("%d", *r.Scope) |
| 199 |
} |
| 200 |
|
| 201 |
return fmt.Sprintf("{Dst:%+v GW:%v MTU:%d AdvMSS:%d Priority:%d Table:%s Scope:%s}", r.Dst, r.GW, r.MTU, r.AdvMSS, r.Priority, table, scope) |
| 202 |
} |
| 203 |
|
| 204 |
func (r *Route) Copy() *Route { |
| 205 |
if r == nil { |
| 206 |
return nil |
| 207 |
} |
| 208 |
|
| 209 |
route := &Route{ |
| 210 |
Dst: r.Dst, |
| 211 |
GW: r.GW, |
| 212 |
MTU: r.MTU, |
| 213 |
AdvMSS: r.AdvMSS, |
| 214 |
Priority: r.Priority, |
| 215 |
Scope: r.Scope, |
| 216 |
} |
| 217 |
|
| 218 |
if r.Table != nil { |
| 219 |
table := *r.Table |
| 220 |
route.Table = &table |
| 221 |
} |
| 222 |
|
| 223 |
if r.Scope != nil { |
| 224 |
scope := *r.Scope |
| 225 |
route.Scope = &scope |
| 226 |
} |
| 227 |
|
| 228 |
return route |
| 229 |
} |
| 230 |
|
| 231 |
// Well known error codes |
| 232 |
// see https://github.com/containernetworking/cni/blob/main/SPEC.md#well-known-error-codes |
| 233 |
const ( |
| 234 |
ErrUnknown uint = iota // 0 |
| 235 |
ErrIncompatibleCNIVersion // 1 |
| 236 |
ErrUnsupportedField // 2 |
| 237 |
ErrUnknownContainer // 3 |
| 238 |
ErrInvalidEnvironmentVariables // 4 |
| 239 |
ErrIOFailure // 5 |
| 240 |
ErrDecodingFailure // 6 |
| 241 |
ErrInvalidNetworkConfig // 7 |
| 242 |
ErrInvalidNetNS // 8 |
| 243 |
ErrTryAgainLater uint = 11 |
| 244 |
ErrInternal uint = 999 |
| 245 |
) |
| 246 |
|
| 247 |
type Error struct { |
| 248 |
Code uint `json:"code"` |
| 249 |
Msg string `json:"msg"` |
| 250 |
Details string `json:"details,omitempty"` |
| 251 |
} |
| 252 |
|
| 253 |
func NewError(code uint, msg, details string) *Error { |
| 254 |
return &Error{ |
| 255 |
Code: code, |
| 256 |
Msg: msg, |
| 257 |
Details: details, |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
func (e *Error) Error() string { |
| 262 |
details := "" |
| 263 |
if e.Details != "" { |
| 264 |
details = fmt.Sprintf("; %v", e.Details) |
| 265 |
} |
| 266 |
return fmt.Sprintf("%v%v", e.Msg, details) |
| 267 |
} |
| 268 |
|
| 269 |
func (e *Error) Print() error { |
| 270 |
return prettyPrint(e) |
| 271 |
} |
| 272 |
|
| 273 |
// net.IPNet is not JSON (un)marshallable so this duality is needed |
| 274 |
// for our custom IPNet type |
| 275 |
|
| 276 |
// JSON (un)marshallable types |
| 277 |
type route struct { |
| 278 |
Dst IPNet `json:"dst"` |
| 279 |
GW net.IP `json:"gw,omitempty"` |
| 280 |
MTU int `json:"mtu,omitempty"` |
| 281 |
AdvMSS int `json:"advmss,omitempty"` |
| 282 |
Priority int `json:"priority,omitempty"` |
| 283 |
Table *int `json:"table,omitempty"` |
| 284 |
Scope *int `json:"scope,omitempty"` |
| 285 |
} |
| 286 |
|
| 287 |
func (r *Route) UnmarshalJSON(data []byte) error { |
| 288 |
rt := route{} |
| 289 |
if err := json.Unmarshal(data, &rt); err != nil { |
| 290 |
return err |
| 291 |
} |
| 292 |
|
| 293 |
r.Dst = net.IPNet(rt.Dst) |
| 294 |
r.GW = rt.GW |
| 295 |
r.MTU = rt.MTU |
| 296 |
r.AdvMSS = rt.AdvMSS |
| 297 |
r.Priority = rt.Priority |
| 298 |
r.Table = rt.Table |
| 299 |
r.Scope = rt.Scope |
| 300 |
|
| 301 |
return nil |
| 302 |
} |
| 303 |
|
| 304 |
func (r Route) MarshalJSON() ([]byte, error) { |
| 305 |
rt := route{ |
| 306 |
Dst: IPNet(r.Dst), |
| 307 |
GW: r.GW, |
| 308 |
MTU: r.MTU, |
| 309 |
AdvMSS: r.AdvMSS, |
| 310 |
Priority: r.Priority, |
| 311 |
Table: r.Table, |
| 312 |
Scope: r.Scope, |
| 313 |
} |
| 314 |
|
| 315 |
return json.Marshal(rt) |
| 316 |
} |
| 317 |
|
| 318 |
func prettyPrint(obj interface{}) error { |
| 319 |
data, err := json.MarshalIndent(obj, "", " ") |
| 320 |
if err != nil { |
| 321 |
return err |
| 322 |
} |
| 323 |
_, err = os.Stdout.Write(data) |
| 324 |
return err |
| 325 |
} |