github.com/mccv1r0/cni@v0.7.0-alpha1/pkg/invoke/args.go (about) 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 invoke 16 17 import ( 18 "os" 19 "strings" 20 ) 21 22 type CNIArgs interface { 23 // For use with os/exec; i.e., return nil to inherit the 24 // environment from this process 25 AsEnv() []string 26 } 27 28 type inherited struct{} 29 30 var inheritArgsFromEnv inherited 31 32 func (_ *inherited) AsEnv() []string { 33 return nil 34 } 35 36 func ArgsFromEnv() CNIArgs { 37 return &inheritArgsFromEnv 38 } 39 40 type Args struct { 41 Command string 42 ContainerID string 43 NetNS string 44 PluginArgs [][2]string 45 PluginArgsStr string 46 IfName string 47 Path string 48 } 49 50 // Args implements the CNIArgs interface 51 var _ CNIArgs = &Args{} 52 53 func (args *Args) AsEnv() []string { 54 env := os.Environ() 55 pluginArgsStr := args.PluginArgsStr 56 if pluginArgsStr == "" { 57 pluginArgsStr = stringify(args.PluginArgs) 58 } 59 60 // Ensure that the custom values are first, so any value present in 61 // the process environment won't override them. 62 env = append([]string{ 63 "CNI_COMMAND=" + args.Command, 64 "CNI_CONTAINERID=" + args.ContainerID, 65 "CNI_NETNS=" + args.NetNS, 66 "CNI_ARGS=" + pluginArgsStr, 67 "CNI_IFNAME=" + args.IfName, 68 "CNI_PATH=" + args.Path, 69 }, env...) 70 return env 71 } 72 73 // taken from rkt/networking/net_plugin.go 74 func stringify(pluginArgs [][2]string) string { 75 entries := make([]string, len(pluginArgs)) 76 77 for i, kv := range pluginArgs { 78 entries[i] = strings.Join(kv[:], "=") 79 } 80 81 return strings.Join(entries, ";") 82 }