Exemplo n.º 1
0
Arquivo: main.go Projeto: samarvin/rkt
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 = plugin.LoadArgs(args.Args, &ipamArgs)
	if err != nil {
		return err
	}
	ipamConf.Args = &ipamArgs

	allocator, err := NewIPAllocator(ipamConf, store)
	if err != nil {
		return err
	}

	var ipConf *plugin.IPConfig

	switch ipamConf.Type {
	case "host-local":
		ipConf, err = allocator.Get(args.ContainerID)
	case "host-local-ptp":
		ipConf, err = allocator.GetPtP(args.ContainerID)
	default:
		return errors.New("Unsupported IPAM plugin type")
	}

	if err != nil {
		return err
	}

	r := &plugin.Result{
		IP4: ipConf,
	}
	return r.Print()
}
Exemplo n.º 2
0
// 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 != "" {
		ipamArgs := IPAMArgs{}
		err := plugin.LoadArgs(args, &ipamArgs)
		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
}