pkg/skel: allow arg requriements specified by CMD

This commit is contained in:
Stefan Junker 2016-05-27 10:56:24 +02:00
parent 2d47b0396d
commit d7de8d4f98

View File

@ -36,28 +36,72 @@ type CmdArgs struct {
StdinData []byte StdinData []byte
} }
type reqForCmdEntry map[string]bool
// PluginMain is the "main" for a plugin. It accepts // PluginMain is the "main" for a plugin. It accepts
// two callback functions for add and del commands. // two callback functions for add and del commands.
func PluginMain(cmdAdd, cmdDel func(_ *CmdArgs) error) { func PluginMain(cmdAdd, cmdDel func(_ *CmdArgs) error) {
var cmd, contID, netns, ifName, args, path string var cmd, contID, netns, ifName, args, path string
vars := []struct { vars := []struct {
name string name string
val *string val *string
req bool reqForCmd reqForCmdEntry
}{ }{
{"CNI_COMMAND", &cmd, true}, {
{"CNI_CONTAINERID", &contID, false}, "CNI_COMMAND",
{"CNI_NETNS", &netns, true}, &cmd,
{"CNI_IFNAME", &ifName, true}, reqForCmdEntry{
{"CNI_ARGS", &args, false}, "ADD": true,
{"CNI_PATH", &path, true}, "DEL": true,
},
},
{
"CNI_CONTAINERID",
&contID,
reqForCmdEntry{
"ADD": false,
"DEL": false,
},
},
{
"CNI_NETNS",
&netns,
reqForCmdEntry{
"ADD": true,
"DEL": true,
},
},
{
"CNI_IFNAME",
&ifName,
reqForCmdEntry{
"ADD": true,
"DEL": true,
},
},
{
"CNI_ARGS",
&args,
reqForCmdEntry{
"ADD": false,
"DEL": false,
},
},
{
"CNI_PATH",
&path,
reqForCmdEntry{
"ADD": true,
"DEL": true,
},
},
} }
argsMissing := false argsMissing := false
for _, v := range vars { for _, v := range vars {
*v.val = os.Getenv(v.name) *v.val = os.Getenv(v.name)
if v.req && *v.val == "" { if v.reqForCmd[cmd] && *v.val == "" {
log.Printf("%v env variable missing", v.name) log.Printf("%v env variable missing", v.name)
argsMissing = true argsMissing = true
} }