| 1 |
// Copyright 2014-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 skel provides skeleton code for a CNI plugin. |
| 16 |
// In particular, it implements argument parsing and validation. |
| 17 |
package skel |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"encoding/json" |
| 22 |
"errors" |
| 23 |
"fmt" |
| 24 |
"io" |
| 25 |
"log" |
| 26 |
"os" |
| 27 |
"strings" |
| 28 |
|
| 29 |
"github.com/containernetworking/cni/pkg/ns" |
| 30 |
"github.com/containernetworking/cni/pkg/types" |
| 31 |
"github.com/containernetworking/cni/pkg/utils" |
| 32 |
"github.com/containernetworking/cni/pkg/version" |
| 33 |
) |
| 34 |
|
| 35 |
// CmdArgs captures all the arguments passed in to the plugin |
| 36 |
// via both env vars and stdin |
| 37 |
type CmdArgs struct { |
| 38 |
ContainerID string |
| 39 |
Netns string |
| 40 |
IfName string |
| 41 |
Args string |
| 42 |
Path string |
| 43 |
NetnsOverride string |
| 44 |
StdinData []byte |
| 45 |
} |
| 46 |
|
| 47 |
type dispatcher struct { |
| 48 |
Getenv func(string) string |
| 49 |
Stdin io.Reader |
| 50 |
Stdout io.Writer |
| 51 |
Stderr io.Writer |
| 52 |
|
| 53 |
ConfVersionDecoder version.ConfigDecoder |
| 54 |
VersionReconciler version.Reconciler |
| 55 |
} |
| 56 |
|
| 57 |
type reqForCmdEntry map[string]bool |
| 58 |
|
| 59 |
func (t *dispatcher) getCmdArgsFromEnv() (string, *CmdArgs, *types.Error) { |
| 60 |
var cmd, contID, netns, ifName, args, path, netnsOverride string |
| 61 |
|
| 62 |
vars := []struct { |
| 63 |
name string |
| 64 |
val *string |
| 65 |
reqForCmd reqForCmdEntry |
| 66 |
validateFn func(string) *types.Error |
| 67 |
}{ |
| 68 |
{ |
| 69 |
"CNI_COMMAND", |
| 70 |
&cmd, |
| 71 |
reqForCmdEntry{ |
| 72 |
"ADD": true, |
| 73 |
"CHECK": true, |
| 74 |
"DEL": true, |
| 75 |
"GC": true, |
| 76 |
"STATUS": true, |
| 77 |
}, |
| 78 |
nil, |
| 79 |
}, |
| 80 |
{ |
| 81 |
"CNI_CONTAINERID", |
| 82 |
&contID, |
| 83 |
reqForCmdEntry{ |
| 84 |
"ADD": true, |
| 85 |
"CHECK": true, |
| 86 |
"DEL": true, |
| 87 |
}, |
| 88 |
utils.ValidateContainerID, |
| 89 |
}, |
| 90 |
{ |
| 91 |
"CNI_NETNS", |
| 92 |
&netns, |
| 93 |
reqForCmdEntry{ |
| 94 |
"ADD": true, |
| 95 |
"CHECK": true, |
| 96 |
"DEL": false, |
| 97 |
}, |
| 98 |
nil, |
| 99 |
}, |
| 100 |
{ |
| 101 |
"CNI_IFNAME", |
| 102 |
&ifName, |
| 103 |
reqForCmdEntry{ |
| 104 |
"ADD": true, |
| 105 |
"CHECK": true, |
| 106 |
"DEL": true, |
| 107 |
}, |
| 108 |
utils.ValidateInterfaceName, |
| 109 |
}, |
| 110 |
{ |
| 111 |
"CNI_ARGS", |
| 112 |
&args, |
| 113 |
reqForCmdEntry{ |
| 114 |
"ADD": false, |
| 115 |
"CHECK": false, |
| 116 |
"DEL": false, |
| 117 |
}, |
| 118 |
nil, |
| 119 |
}, |
| 120 |
{ |
| 121 |
"CNI_PATH", |
| 122 |
&path, |
| 123 |
reqForCmdEntry{ |
| 124 |
"ADD": true, |
| 125 |
"CHECK": true, |
| 126 |
"DEL": true, |
| 127 |
"GC": true, |
| 128 |
"STATUS": true, |
| 129 |
}, |
| 130 |
nil, |
| 131 |
}, |
| 132 |
{ |
| 133 |
"CNI_NETNS_OVERRIDE", |
| 134 |
&netnsOverride, |
| 135 |
reqForCmdEntry{ |
| 136 |
"ADD": false, |
| 137 |
"CHECK": false, |
| 138 |
"DEL": false, |
| 139 |
}, |
| 140 |
nil, |
| 141 |
}, |
| 142 |
} |
| 143 |
|
| 144 |
argsMissing := make([]string, 0) |
| 145 |
for _, v := range vars { |
| 146 |
*v.val = t.Getenv(v.name) |
| 147 |
if *v.val == "" { |
| 148 |
if v.reqForCmd[cmd] || v.name == "CNI_COMMAND" { |
| 149 |
argsMissing = append(argsMissing, v.name) |
| 150 |
} |
| 151 |
} else if v.reqForCmd[cmd] && v.validateFn != nil { |
| 152 |
if err := v.validateFn(*v.val); err != nil { |
| 153 |
return "", nil, err |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
if len(argsMissing) > 0 { |
| 159 |
joined := strings.Join(argsMissing, ",") |
| 160 |
return "", nil, types.NewError(types.ErrInvalidEnvironmentVariables, fmt.Sprintf("required env variables [%s] missing", joined), "") |
| 161 |
} |
| 162 |
|
| 163 |
if cmd == "VERSION" { |
| 164 |
t.Stdin = bytes.NewReader(nil) |
| 165 |
} |
| 166 |
|
| 167 |
stdinData, err := io.ReadAll(t.Stdin) |
| 168 |
if err != nil { |
| 169 |
return "", nil, types.NewError(types.ErrIOFailure, fmt.Sprintf("error reading from stdin: %v", err), "") |
| 170 |
} |
| 171 |
|
| 172 |
if cmd != "VERSION" { |
| 173 |
if err := validateConfig(stdinData); err != nil { |
| 174 |
return "", nil, err |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
cmdArgs := &CmdArgs{ |
| 179 |
ContainerID: contID, |
| 180 |
Netns: netns, |
| 181 |
IfName: ifName, |
| 182 |
Args: args, |
| 183 |
Path: path, |
| 184 |
StdinData: stdinData, |
| 185 |
NetnsOverride: netnsOverride, |
| 186 |
} |
| 187 |
return cmd, cmdArgs, nil |
| 188 |
} |
| 189 |
|
| 190 |
func (t *dispatcher) checkVersionAndCall(cmdArgs *CmdArgs, pluginVersionInfo version.PluginInfo, toCall func(*CmdArgs) error) *types.Error { |
| 191 |
configVersion, err := t.ConfVersionDecoder.Decode(cmdArgs.StdinData) |
| 192 |
if err != nil { |
| 193 |
return types.NewError(types.ErrDecodingFailure, err.Error(), "") |
| 194 |
} |
| 195 |
verErr := t.VersionReconciler.Check(configVersion, pluginVersionInfo) |
| 196 |
if verErr != nil { |
| 197 |
return types.NewError(types.ErrIncompatibleCNIVersion, "incompatible CNI versions", verErr.Details()) |
| 198 |
} |
| 199 |
|
| 200 |
if toCall == nil { |
| 201 |
return nil |
| 202 |
} |
| 203 |
|
| 204 |
if err = toCall(cmdArgs); err != nil { |
| 205 |
var e *types.Error |
| 206 |
if errors.As(err, &e) { |
| 207 |
// don't wrap Error in Error |
| 208 |
return e |
| 209 |
} |
| 210 |
return types.NewError(types.ErrInternal, err.Error(), "") |
| 211 |
} |
| 212 |
|
| 213 |
return nil |
| 214 |
} |
| 215 |
|
| 216 |
func validateConfig(jsonBytes []byte) *types.Error { |
| 217 |
var conf struct { |
| 218 |
Name string `json:"name"` |
| 219 |
} |
| 220 |
if err := json.Unmarshal(jsonBytes, &conf); err != nil { |
| 221 |
return types.NewError(types.ErrDecodingFailure, fmt.Sprintf("error unmarshall network config: %v", err), "") |
| 222 |
} |
| 223 |
if conf.Name == "" { |
| 224 |
return types.NewError(types.ErrInvalidNetworkConfig, "missing network name", "") |
| 225 |
} |
| 226 |
if err := utils.ValidateNetworkName(conf.Name); err != nil { |
| 227 |
return err |
| 228 |
} |
| 229 |
return nil |
| 230 |
} |
| 231 |
|
| 232 |
func (t *dispatcher) pluginMain(funcs CNIFuncs, versionInfo version.PluginInfo, about string) *types.Error { |
| 233 |
cmd, cmdArgs, err := t.getCmdArgsFromEnv() |
| 234 |
if err != nil { |
| 235 |
// Print the about string to stderr when no command is set |
| 236 |
if err.Code == types.ErrInvalidEnvironmentVariables && t.Getenv("CNI_COMMAND") == "" && about != "" { |
| 237 |
_, _ = fmt.Fprintln(t.Stderr, about) |
| 238 |
_, _ = fmt.Fprintf(t.Stderr, "CNI protocol versions supported: %s\n", strings.Join(versionInfo.SupportedVersions(), ", ")) |
| 239 |
return nil |
| 240 |
} |
| 241 |
return err |
| 242 |
} |
| 243 |
|
| 244 |
switch cmd { |
| 245 |
case "ADD": |
| 246 |
err = t.checkVersionAndCall(cmdArgs, versionInfo, funcs.Add) |
| 247 |
if err != nil { |
| 248 |
return err |
| 249 |
} |
| 250 |
if strings.ToUpper(cmdArgs.NetnsOverride) != "TRUE" && cmdArgs.NetnsOverride != "1" { |
| 251 |
isPluginNetNS, checkErr := ns.CheckNetNS(cmdArgs.Netns) |
| 252 |
if checkErr != nil { |
| 253 |
return checkErr |
| 254 |
} else if isPluginNetNS { |
| 255 |
return types.NewError(types.ErrInvalidNetNS, "plugin's netns and netns from CNI_NETNS should not be the same", "") |
| 256 |
} |
| 257 |
} |
| 258 |
case "CHECK": |
| 259 |
configVersion, err := t.ConfVersionDecoder.Decode(cmdArgs.StdinData) |
| 260 |
if err != nil { |
| 261 |
return types.NewError(types.ErrDecodingFailure, err.Error(), "") |
| 262 |
} |
| 263 |
if gtet, err := version.GreaterThanOrEqualTo(configVersion, "0.4.0"); err != nil { |
| 264 |
return types.NewError(types.ErrDecodingFailure, err.Error(), "") |
| 265 |
} else if !gtet { |
| 266 |
return types.NewError(types.ErrIncompatibleCNIVersion, "config version does not allow CHECK", "") |
| 267 |
} |
| 268 |
for _, pluginVersion := range versionInfo.SupportedVersions() { |
| 269 |
gtet, err := version.GreaterThanOrEqualTo(pluginVersion, configVersion) |
| 270 |
if err != nil { |
| 271 |
return types.NewError(types.ErrDecodingFailure, err.Error(), "") |
| 272 |
} else if gtet { |
| 273 |
if err := t.checkVersionAndCall(cmdArgs, versionInfo, funcs.Check); err != nil { |
| 274 |
return err |
| 275 |
} |
| 276 |
return nil |
| 277 |
} |
| 278 |
} |
| 279 |
return types.NewError(types.ErrIncompatibleCNIVersion, "plugin version does not allow CHECK", "") |
| 280 |
case "DEL": |
| 281 |
err = t.checkVersionAndCall(cmdArgs, versionInfo, funcs.Del) |
| 282 |
if err != nil { |
| 283 |
return err |
| 284 |
} |
| 285 |
if strings.ToUpper(cmdArgs.NetnsOverride) != "TRUE" && cmdArgs.NetnsOverride != "1" { |
| 286 |
isPluginNetNS, checkErr := ns.CheckNetNS(cmdArgs.Netns) |
| 287 |
if checkErr != nil { |
| 288 |
return checkErr |
| 289 |
} else if isPluginNetNS { |
| 290 |
return types.NewError(types.ErrInvalidNetNS, "plugin's netns and netns from CNI_NETNS should not be the same", "") |
| 291 |
} |
| 292 |
} |
| 293 |
case "GC": |
| 294 |
configVersion, err := t.ConfVersionDecoder.Decode(cmdArgs.StdinData) |
| 295 |
if err != nil { |
| 296 |
return types.NewError(types.ErrDecodingFailure, err.Error(), "") |
| 297 |
} |
| 298 |
if gtet, err := version.GreaterThanOrEqualTo(configVersion, "1.1.0"); err != nil { |
| 299 |
return types.NewError(types.ErrDecodingFailure, err.Error(), "") |
| 300 |
} else if !gtet { |
| 301 |
return types.NewError(types.ErrIncompatibleCNIVersion, "config version does not allow GC", "") |
| 302 |
} |
| 303 |
for _, pluginVersion := range versionInfo.SupportedVersions() { |
| 304 |
gtet, err := version.GreaterThanOrEqualTo(pluginVersion, configVersion) |
| 305 |
if err != nil { |
| 306 |
return types.NewError(types.ErrDecodingFailure, err.Error(), "") |
| 307 |
} else if gtet { |
| 308 |
if err := t.checkVersionAndCall(cmdArgs, versionInfo, funcs.GC); err != nil { |
| 309 |
return err |
| 310 |
} |
| 311 |
return nil |
| 312 |
} |
| 313 |
} |
| 314 |
return types.NewError(types.ErrIncompatibleCNIVersion, "plugin version does not allow GC", "") |
| 315 |
case "STATUS": |
| 316 |
configVersion, err := t.ConfVersionDecoder.Decode(cmdArgs.StdinData) |
| 317 |
if err != nil { |
| 318 |
return types.NewError(types.ErrDecodingFailure, err.Error(), "") |
| 319 |
} |
| 320 |
if gtet, err := version.GreaterThanOrEqualTo(configVersion, "1.1.0"); err != nil { |
| 321 |
return types.NewError(types.ErrDecodingFailure, err.Error(), "") |
| 322 |
} else if !gtet { |
| 323 |
return types.NewError(types.ErrIncompatibleCNIVersion, "config version does not allow STATUS", "") |
| 324 |
} |
| 325 |
for _, pluginVersion := range versionInfo.SupportedVersions() { |
| 326 |
gtet, err := version.GreaterThanOrEqualTo(pluginVersion, configVersion) |
| 327 |
if err != nil { |
| 328 |
return types.NewError(types.ErrDecodingFailure, err.Error(), "") |
| 329 |
} else if gtet { |
| 330 |
if err := t.checkVersionAndCall(cmdArgs, versionInfo, funcs.Status); err != nil { |
| 331 |
return err |
| 332 |
} |
| 333 |
return nil |
| 334 |
} |
| 335 |
} |
| 336 |
return types.NewError(types.ErrIncompatibleCNIVersion, "plugin version does not allow STATUS", "") |
| 337 |
case "VERSION": |
| 338 |
if err := versionInfo.Encode(t.Stdout); err != nil { |
| 339 |
return types.NewError(types.ErrIOFailure, err.Error(), "") |
| 340 |
} |
| 341 |
default: |
| 342 |
return types.NewError(types.ErrInvalidEnvironmentVariables, fmt.Sprintf("unknown CNI_COMMAND: %v", cmd), "") |
| 343 |
} |
| 344 |
|
| 345 |
return err |
| 346 |
} |
| 347 |
|
| 348 |
// PluginMainWithError is the core "main" for a plugin. It accepts |
| 349 |
// callback functions for add, check, and del CNI commands and returns an error. |
| 350 |
// |
| 351 |
// The caller must also specify what CNI spec versions the plugin supports. |
| 352 |
// |
| 353 |
// It is the responsibility of the caller to check for non-nil error return. |
| 354 |
// |
| 355 |
// For a plugin to comply with the CNI spec, it must print any error to stdout |
| 356 |
// as JSON and then exit with nonzero status code. |
| 357 |
// |
| 358 |
// To let this package automatically handle errors and call os.Exit(1) for you, |
| 359 |
// use PluginMain() instead. |
| 360 |
// |
| 361 |
// Deprecated: Use github.com/containernetworking/cni/pkg/skel.PluginMainFuncsWithError instead. |
| 362 |
func PluginMainWithError(cmdAdd, cmdCheck, cmdDel func(_ *CmdArgs) error, versionInfo version.PluginInfo, about string) *types.Error { |
| 363 |
return PluginMainFuncsWithError(CNIFuncs{Add: cmdAdd, Check: cmdCheck, Del: cmdDel}, versionInfo, about) |
| 364 |
} |
| 365 |
|
| 366 |
// CNIFuncs contains a group of callback command funcs to be passed in as |
| 367 |
// parameters to the core "main" for a plugin. |
| 368 |
type CNIFuncs struct { |
| 369 |
Add func(_ *CmdArgs) error |
| 370 |
Del func(_ *CmdArgs) error |
| 371 |
Check func(_ *CmdArgs) error |
| 372 |
GC func(_ *CmdArgs) error |
| 373 |
Status func(_ *CmdArgs) error |
| 374 |
} |
| 375 |
|
| 376 |
// PluginMainFuncsWithError is the core "main" for a plugin. It accepts |
| 377 |
// callback functions defined within CNIFuncs and returns an error. |
| 378 |
// |
| 379 |
// The caller must also specify what CNI spec versions the plugin supports. |
| 380 |
// |
| 381 |
// It is the responsibility of the caller to check for non-nil error return. |
| 382 |
// |
| 383 |
// For a plugin to comply with the CNI spec, it must print any error to stdout |
| 384 |
// as JSON and then exit with nonzero status code. |
| 385 |
// |
| 386 |
// To let this package automatically handle errors and call os.Exit(1) for you, |
| 387 |
// use PluginMainFuncs() instead. |
| 388 |
func PluginMainFuncsWithError(funcs CNIFuncs, versionInfo version.PluginInfo, about string) *types.Error { |
| 389 |
return (&dispatcher{ |
| 390 |
Getenv: os.Getenv, |
| 391 |
Stdin: os.Stdin, |
| 392 |
Stdout: os.Stdout, |
| 393 |
Stderr: os.Stderr, |
| 394 |
}).pluginMain(funcs, versionInfo, about) |
| 395 |
} |
| 396 |
|
| 397 |
// PluginMainFuncs is the core "main" for a plugin which includes automatic error handling. |
| 398 |
// This is a newer alternative func to PluginMain which abstracts CNI commands within a |
| 399 |
// CNIFuncs interface. |
| 400 |
// |
| 401 |
// The caller must also specify what CNI spec versions the plugin supports. |
| 402 |
// |
| 403 |
// The caller can specify an "about" string, which is printed on stderr |
| 404 |
// when no CNI_COMMAND is specified. The recommended output is "CNI plugin <foo> v<version>" |
| 405 |
// |
| 406 |
// When an error occurs in any func in CNIFuncs, PluginMainFuncs will print the error |
| 407 |
// as JSON to stdout and call os.Exit(1). |
| 408 |
// |
| 409 |
// To have more control over error handling, use PluginMainFuncsWithError() instead. |
| 410 |
func PluginMainFuncs(funcs CNIFuncs, versionInfo version.PluginInfo, about string) { |
| 411 |
if e := PluginMainFuncsWithError(funcs, versionInfo, about); e != nil { |
| 412 |
if err := e.Print(); err != nil { |
| 413 |
log.Print("Error writing error JSON to stdout: ", err) |
| 414 |
} |
| 415 |
os.Exit(1) |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
// PluginMain is the core "main" for a plugin which includes automatic error handling. |
| 420 |
// |
| 421 |
// The caller must also specify what CNI spec versions the plugin supports. |
| 422 |
// |
| 423 |
// The caller can specify an "about" string, which is printed on stderr |
| 424 |
// when no CNI_COMMAND is specified. The recommended output is "CNI plugin <foo> v<version>" |
| 425 |
// |
| 426 |
// When an error occurs in either cmdAdd, cmdCheck, or cmdDel, PluginMain will print the error |
| 427 |
// as JSON to stdout and call os.Exit(1). |
| 428 |
// |
| 429 |
// To have more control over error handling, use PluginMainWithError() instead. |
| 430 |
// |
| 431 |
// Deprecated: Use github.com/containernetworking/cni/pkg/skel.PluginMainFuncs instead. |
| 432 |
func PluginMain(cmdAdd, cmdCheck, cmdDel func(_ *CmdArgs) error, versionInfo version.PluginInfo, about string) { |
| 433 |
if e := PluginMainWithError(cmdAdd, cmdCheck, cmdDel, versionInfo, about); e != nil { |
| 434 |
if err := e.Print(); err != nil { |
| 435 |
log.Print("Error writing error JSON to stdout: ", err) |
| 436 |
} |
| 437 |
os.Exit(1) |
| 438 |
} |
| 439 |
} |