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 10, 2017
1 parent f197c01 commit c412df4
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 @@ -18,6 +18,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"
"path/filepath"

"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 {
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 c412df4

Please sign in to comment.