| 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 main |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"crypto/sha512" |
| 20 |
"encoding/json" |
| 21 |
"fmt" |
| 22 |
"os" |
| 23 |
"path/filepath" |
| 24 |
"strings" |
| 25 |
|
| 26 |
"github.com/containernetworking/cni/libcni" |
| 27 |
) |
| 28 |
|
| 29 |
// Protocol parameters are passed to the plugins via OS environment variables. |
| 30 |
const ( |
| 31 |
EnvCNIPath = "CNI_PATH" |
| 32 |
EnvNetDir = "NETCONFPATH" |
| 33 |
EnvCapabilityArgs = "CAP_ARGS" |
| 34 |
EnvCNIArgs = "CNI_ARGS" |
| 35 |
EnvCNIIfname = "CNI_IFNAME" |
| 36 |
|
| 37 |
DefaultNetDir = "/etc/cni/net.d" |
| 38 |
|
| 39 |
CmdAdd = "add" |
| 40 |
CmdCheck = "check" |
| 41 |
CmdDel = "del" |
| 42 |
CmdGC = "gc" |
| 43 |
CmdStatus = "status" |
| 44 |
) |
| 45 |
|
| 46 |
func parseArgs(args string) ([][2]string, error) { |
| 47 |
var result [][2]string |
| 48 |
|
| 49 |
pairs := strings.Split(args, ";") |
| 50 |
for _, pair := range pairs { |
| 51 |
kv := strings.Split(pair, "=") |
| 52 |
if len(kv) != 2 || kv[0] == "" || kv[1] == "" { |
| 53 |
return nil, fmt.Errorf("invalid CNI_ARGS pair %q", pair) |
| 54 |
} |
| 55 |
|
| 56 |
result = append(result, [2]string{kv[0], kv[1]}) |
| 57 |
} |
| 58 |
|
| 59 |
return result, nil |
| 60 |
} |
| 61 |
|
| 62 |
func main() { |
| 63 |
if len(os.Args) < 4 { |
| 64 |
usage() |
| 65 |
} |
| 66 |
|
| 67 |
netdir := os.Getenv(EnvNetDir) |
| 68 |
if netdir == "" { |
| 69 |
netdir = DefaultNetDir |
| 70 |
} |
| 71 |
netconf, err := libcni.LoadNetworkConf(netdir, os.Args[2]) |
| 72 |
if err != nil { |
| 73 |
exit(err) |
| 74 |
} |
| 75 |
|
| 76 |
var capabilityArgs map[string]interface{} |
| 77 |
capabilityArgsValue := os.Getenv(EnvCapabilityArgs) |
| 78 |
if len(capabilityArgsValue) > 0 { |
| 79 |
if err = json.Unmarshal([]byte(capabilityArgsValue), &capabilityArgs); err != nil { |
| 80 |
exit(err) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
var cniArgs [][2]string |
| 85 |
args := os.Getenv(EnvCNIArgs) |
| 86 |
if len(args) > 0 { |
| 87 |
cniArgs, err = parseArgs(args) |
| 88 |
if err != nil { |
| 89 |
exit(err) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
ifName, ok := os.LookupEnv(EnvCNIIfname) |
| 94 |
if !ok { |
| 95 |
ifName = "eth0" |
| 96 |
} |
| 97 |
|
| 98 |
netns := os.Args[3] |
| 99 |
netns, err = filepath.Abs(netns) |
| 100 |
if err != nil { |
| 101 |
exit(err) |
| 102 |
} |
| 103 |
|
| 104 |
// Generate the containerid by hashing the netns path |
| 105 |
s := sha512.Sum512([]byte(netns)) |
| 106 |
containerID := fmt.Sprintf("cnitool-%x", s[:10]) |
| 107 |
|
| 108 |
cninet := libcni.NewCNIConfig(filepath.SplitList(os.Getenv(EnvCNIPath)), nil) |
| 109 |
|
| 110 |
rt := &libcni.RuntimeConf{ |
| 111 |
ContainerID: containerID, |
| 112 |
NetNS: netns, |
| 113 |
IfName: ifName, |
| 114 |
Args: cniArgs, |
| 115 |
CapabilityArgs: capabilityArgs, |
| 116 |
} |
| 117 |
|
| 118 |
switch os.Args[1] { |
| 119 |
case CmdAdd: |
| 120 |
result, err := cninet.AddNetworkList(context.TODO(), netconf, rt) |
| 121 |
if result != nil { |
| 122 |
_ = result.Print() |
| 123 |
} |
| 124 |
exit(err) |
| 125 |
case CmdCheck: |
| 126 |
err := cninet.CheckNetworkList(context.TODO(), netconf, rt) |
| 127 |
exit(err) |
| 128 |
case CmdDel: |
| 129 |
exit(cninet.DelNetworkList(context.TODO(), netconf, rt)) |
| 130 |
case CmdGC: |
| 131 |
// Currently just invoke GC without args, hence all network interface should be GC'ed! |
| 132 |
exit(cninet.GCNetworkList(context.TODO(), netconf, nil)) |
| 133 |
case CmdStatus: |
| 134 |
exit(cninet.GetStatusNetworkList(context.TODO(), netconf)) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
func usage() { |
| 139 |
exe := filepath.Base(os.Args[0]) |
| 140 |
|
| 141 |
fmt.Fprintf(os.Stderr, "%s: Add, check, remove, gc or status network interfaces from a network namespace\n", exe) |
| 142 |
fmt.Fprintf(os.Stderr, " %s add <net> <netns>\n", exe) |
| 143 |
fmt.Fprintf(os.Stderr, " %s check <net> <netns>\n", exe) |
| 144 |
fmt.Fprintf(os.Stderr, " %s del <net> <netns>\n", exe) |
| 145 |
fmt.Fprintf(os.Stderr, " %s gc <net> <netns>\n", exe) |
| 146 |
fmt.Fprintf(os.Stderr, " %s status <net> <netns>\n", exe) |
| 147 |
os.Exit(1) |
| 148 |
} |
| 149 |
|
| 150 |
func exit(err error) { |
| 151 |
if err != nil { |
| 152 |
fmt.Fprintf(os.Stderr, "%s\n", err) |
| 153 |
os.Exit(1) |
| 154 |
} |
| 155 |
os.Exit(0) |
| 156 |
} |