func cmdAdd(args *skel.CmdArgs) error { ipamConf, err := LoadIPAMConfig(args.StdinData, args.Args) if err != nil { return err } store, err := disk.New(ipamConf.Name) if err != nil { return err } defer store.Close() ipamArgs := IPAMArgs{} err = types.LoadArgs(args.Args, &ipamArgs) if err != nil { return err } ipamConf.Args = &ipamArgs allocator, err := NewIPAllocator(ipamConf, store) if err != nil { return err } ipConf, err := allocator.Get(args.ContainerID) if err != nil { return err } r := &types.Result{ IP4: ipConf, } return r.Print() }
// NewIPAMConfig creates a NetworkConfig from the given network name. func LoadIPAMConfig(bytes []byte, args string) (*IPAMConfig, error) { n := Net{} if err := json.Unmarshal(bytes, &n); err != nil { return nil, err } if args != "" { n.IPAM.Args = &IPAMArgs{} err := types.LoadArgs(args, n.IPAM.Args) if err != nil { return nil, err } } if n.IPAM == nil { return nil, fmt.Errorf("%q missing 'ipam' key") } // Copy net name into IPAM so not to drag Net struct around n.IPAM.Name = n.Name return n.IPAM, nil }