From: Stefan Junker Date: Sun, 16 Aug 2015 00:30:04 +0000 (+0200) Subject: host-local: allow ip request via CNI_ARGS X-Git-Url: https://git.halfball.org/?a=commitdiff_plain;h=d1a09053456f6a9f075d2f4c4dfc0e79144c0153;p=plugins.git host-local: allow ip request via CNI_ARGS A specific IP can now be requested via the environment variable CNI_ARGS, e.g. `CNI_ARGS=ip=1.2.3.4`. The plugin will try to reserve the specified IP. If this is not successful the execution will fail. --- diff --git a/plugin/args.go b/plugin/args.go new file mode 100644 index 0000000..8489579 --- /dev/null +++ b/plugin/args.go @@ -0,0 +1,36 @@ +package plugin + +import ( + "encoding" + "fmt" + "reflect" + "strings" +) + +func LoadArgs(args string, container interface{}) error { + if args == "" { + return nil + } + + containerValue := reflect.ValueOf(container) + + pairs := strings.Split(args, ",") + for _, pair := range pairs { + kv := strings.Split(pair, "=") + if len(kv) != 2 { + return fmt.Errorf("ARGS: invalid pair %q", pair) + } + keyString := kv[0] + valueString := kv[1] + keyField := containerValue.Elem().FieldByName(keyString) + if !keyField.IsValid() { + return fmt.Errorf("ARGS: invalid key %q", keyString) + } + u := keyField.Addr().Interface().(encoding.TextUnmarshaler) + err := u.UnmarshalText([]byte(valueString)) + if err != nil { + return fmt.Errorf("ARGS: error parsing value of pair %q: %v)", pair, err) + } + } + return nil +}