コード例 #1
0
ファイル: create.go プロジェクト: vmware/vic
// processNetwork parses network args if present
func (c *Create) processNetwork(network *data.NetworkConfig, netName, pgName, staticIP, gateway string) error {
	network.Name = pgName

	var err error

	i := staticIP != ""
	g := gateway != ""
	if !i && !g {
		return nil
	}
	if i != g {
		return fmt.Errorf("%s network IP and gateway must both be specified", netName)
	}

	defer func(net *data.NetworkConfig) {
		if err == nil {
			log.Debugf("%s network: IP %q gateway %q", netName, net.IP, net.Gateway)
		}
	}(network)

	network.Gateway, err = ip.ParseIPandMask(gateway)
	if err != nil {
		return fmt.Errorf("Invalid %s network gateway: %s", netName, err)
	}

	network.IP, err = ip.ParseIPandMask(staticIP)
	if err == nil {
		return nil
	}

	// try treating it as a name, using the mask from the gateway
	ips, err := net.LookupIP(staticIP)
	if err != nil {
		return fmt.Errorf("Invalid %s network address - neither IP nor resolvable hostname", netName)
	}

	for _, ip := range ips {
		if !network.Gateway.Contains(ip) {
			log.Debugf("Skipping %s as value for %s because it's not in the network specified by gateway", ip.String(), staticIP)
			continue
		}

		if ip.String() != staticIP {
			log.Infof("Assigning %s based on %s", ip.String(), staticIP)
		}

		network.IP = net.IPNet{
			IP:   ip,
			Mask: network.Gateway.Mask,
		}

		return nil
	}

	return fmt.Errorf("Invalid %s network address: %s does not resolve to a gateway compatible IP", netName, staticIP)
}
コード例 #2
0
ファイル: network.go プロジェクト: vmware/vic
func (v *Validator) getEndpoint(ctx context.Context, conf *config.VirtualContainerHostConfigSpec, network data.NetworkConfig, epName, contNetName string, def bool, ns []net.IP) (*executor.NetworkEndpoint, error) {
	defer trace.End(trace.Begin(""))
	var gw net.IPNet
	var staticIP *net.IPNet

	if !network.Empty() {
		log.Debugf("Setting static IP for %q on port group %q", contNetName, network.Name)
		gw = network.Gateway
		staticIP = &network.IP
	}

	moid, err := v.networkHelper(ctx, network.Name)
	if err != nil {
		return nil, err
	}

	e := &executor.NetworkEndpoint{
		Common: executor.Common{
			Name: epName,
		},
		Network: executor.ContainerNetwork{
			Common: executor.Common{
				Name: contNetName,
				ID:   moid,
			},
			Default:     def,
			Gateway:     gw,
			Nameservers: ns,
		},
		IP: staticIP,
	}
	if staticIP != nil {
		e.Static = true
	}
	log.Debugf("NetworkEndpoint: %v", e)

	return e, nil
}