Skip to content

Commit

Permalink
cnitool: add support for CNI_ARGS
Browse files Browse the repository at this point in the history
Signed-off-by: Pengfei Ni <[email protected]>
  • Loading branch information
feiskyer committed Jul 11, 2017
1 parent f197c01 commit feeb3d4
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions cnitool/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/containernetworking/cni/libcni"
)
Expand All @@ -27,13 +28,30 @@ const (
EnvCNIPath = "CNI_PATH"
EnvNetDir = "NETCONFPATH"
EnvCapabilityArgs = "CAP_ARGS"
EnvCNIArgs = "CNI_ARGS"

DefaultNetDir = "/etc/cni/net.d"

CmdAdd = "add"
CmdDel = "del"
)

func parseArgs(args string) ([][2]string, error) {
var result [][2]string

pairs := strings.Split(args, ";")
for _, pair := range pairs {
kv := strings.Split(pair, "=")
if len(kv) != 2 || kv[0] == "" || kv[1] == "" {
return nil, fmt.Errorf("invalid CNI_ARGS pair %q", pair)
}

result = append(result, [2]string{kv[0], kv[1]})
}

return result, nil
}

func main() {
if len(os.Args) < 3 {
usage()
Expand All @@ -50,9 +68,18 @@ func main() {
}

var capabilityArgs map[string]interface{}
args := os.Getenv(EnvCapabilityArgs)
capabilityArgsValue := os.Getenv(EnvCapabilityArgs)
if len(capabilityArgsValue) > 0 {
if err = json.Unmarshal([]byte(capabilityArgsValue), &capabilityArgs); err != nil {
exit(err)
}
}

var cniArgs [][2]string
args := os.Getenv(EnvCNIArgs)
if len(args) > 0 {
if err = json.Unmarshal([]byte(args), &capabilityArgs); err != nil {
cniArgs, err = parseArgs(args)
if err != nil {
exit(err)
}
}
Expand All @@ -67,6 +94,7 @@ func main() {
ContainerID: "cni",
NetNS: netns,
IfName: "eth0",
Args: cniArgs,
CapabilityArgs: capabilityArgs,
}

Expand Down

0 comments on commit feeb3d4

Please sign in to comment.