From d1a09053456f6a9f075d2f4c4dfc0e79144c0153 Mon Sep 17 00:00:00 2001 From: Stefan Junker Date: Sun, 16 Aug 2015 02:30:04 +0200 Subject: [PATCH] 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. --- plugin/args.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 plugin/args.go diff --git a/plugin/args.go b/plugin/args.go new file mode 100644 index 00000000..84895793 --- /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 +}