示例#1
0
func (driver *driver) createNetwork(w http.ResponseWriter, r *http.Request) {
	var create networkCreate
	err := json.NewDecoder(r.Body).Decode(&create)
	if err != nil {
		sendError(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
		return
	}

	if driver.network != "" {
		errorResponsef(w, "You get just one network, and you already made %s", driver.network)
		return
	}
	driver.network = create.NetworkID
	containerCidr := driver.pluginConfig.containerSubnet
	driver.cidr = containerCidr
	// Todo: check for ipam errors
	driver.ipAllocator.RequestIP(containerCidr, nil)

	emptyResponse(w)

	// TODO: sort out what rules are in place by default vs. plugin
	err = driver.natOut()
	if err != nil {
		log.Warnf("error setting up outboud nat: %s", err)
	}

	if ipVlanMode == ipVlanL3 {
		log.Debugf("Adding route for the local ipvlan subnet [ %s ] in the default namespace using the specified host interface [ %s]", containerCidr.String(), ipVlanEthIface)
		ipvlanParent, err := netlink.LinkByName(ipVlanEthIface)
		// Add a route in the default NS to point to the IPVlan namespace subnet
		addRouteIface(containerCidr, ipvlanParent)
		if err != nil {
			log.Debugf("a problem occurred adding the container subnet default namespace route", err)
		}
	} else if ipVlanMode == ipVlanL3Routing {
		log.Debugf("Adding route for the local ipvlan subnet [ %s ] in the default namespace using the specified host interface [ %s]", containerCidr.String(), ipVlanEthIface)
		ipvlanParent, err := netlink.LinkByName(ipVlanEthIface)
		// Add a route in the default NS to point to the IPVlan namespace subnet
		addRouteIface(containerCidr, ipvlanParent)
		if err != nil {
			log.Debugf("a problem occurred adding the container subnet default namespace route", err)
		}

		// Announce the local IPVLAN network to the other peers in the BGP cluster
		log.Infof("New Docker network: [ %s ]", containerCidr.String())
		err = routing.AdvertizeNewRoute(containerCidr)
		if err != nil {
			log.Fatalf("Error installing container route : %s", err)
		}
	}
}
示例#2
0
func (driver *driver) createNetwork(w http.ResponseWriter, r *http.Request) {
	var create networkCreate
	err := json.NewDecoder(r.Body).Decode(&create)
	if err != nil {
		sendError(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
		return
	}
	log.Debugf("Network Create Called: [ %+v ]", create)
	for _, v4 := range create.IpV4Data {
		driver.gateway = v4.Gateway.IP.String()
		driver.cidr = v4.Pool
	}
	// TODO: replace driver options with docker network options
	for k, v := range create.Options {
		log.Debugf("Libnetwork Opts Sent: [ %s ] Value: [ %s ]", k, v)
	}
	driver.networkID = create.NetworkID
	containerCidr := driver.cidr
	emptyResponse(w)

	if ipVlanMode == ipVlanL3 {
		log.Debugf("Adding route for the local ipvlan subnet [ %s ] in the default namespace using the specified host interface [ %s]", containerCidr.String(), ipVlanEthIface)
		ipvlanParent, err := netlink.LinkByName(ipVlanEthIface)
		// Add a route in the default NS to point to the IPVlan namespace subnet
		addRouteIface(containerCidr, ipvlanParent)
		if err != nil {
			log.Debugf("a problem occurred adding the container subnet default namespace route", err)
		}
	} else if ipVlanMode == ipVlanL3Routing {
		log.Debugf("Adding route for the local ipvlan subnet [ %s ] in the default namespace using the specified host interface [ %s]", containerCidr.String(), ipVlanEthIface)
		ipvlanParent, err := netlink.LinkByName(ipVlanEthIface)
		// Add a route in the default NS to point to the IPVlan namespace subnet
		addRouteIface(containerCidr, ipvlanParent)
		if err != nil {
			log.Debugf("a problem occurred adding the container subnet default namespace route", err)
		}

		// Announce the local IPVLAN network to the other peers in the BGP cluster
		log.Infof("New Docker network: [ %s ]", containerCidr.String())
		err = routing.AdvertizeNewRoute(containerCidr)
		if err != nil {
			log.Fatalf("Error installing container route : %s", err)
		}
	}
}
示例#3
0
func (driver *driver) createNetwork(w http.ResponseWriter, r *http.Request) {
	var create networkCreate
	err := json.NewDecoder(r.Body).Decode(&create)
	if err != nil {
		sendError(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
		return
	}
	var netCidr *net.IPNet
	var netGw string
	log.Debugf("Network Create Called: [ %+v ]", create)
	for _, v4 := range create.IpV4Data {
		netGw = v4.Gateway.IP.String()
		netCidr = v4.Pool
	}
	n := &network{
		id:        create.NetworkID,
		endpoints: endpointTable{},
		cidr:      netCidr,
		gateway:   netGw,
	}
	// Parse docker network -o opts
	for k, v := range create.Options {
		if k == "com.docker.network.generic" {
			if genericOpts, ok := v.(map[string]interface{}); ok {
				for key, val := range genericOpts {
					log.Debugf("Libnetwork Opts Sent: [ %s ] Value: [ %s ]", key, val)
					// Parse -o mode from libnetwork generic opts
					if key == "mode" {
						switch val {
						case ipVlanL2:
							log.Debugf("Ipvlan mode is L2 [ %s ]", val)
							// backward compatable
							ipVlanMode = ipVlanL2
							// new API
							n.modeOpt = ipVlanL2
						case ipVlanL3:
							log.Debugf("Ipvlan mode is L3 [ %s ]", val)
							// IPVlan simply needs the container interface for its
							// default route target since only unicast is allowed <3
							// backward compatable
							ipVlanMode = ipVlanL3
							// new API
							n.modeOpt = ipVlanL3
						case ipVlanL3Routing:
							// IPVlan simply needs the container interface for its
							// default route target since only unicast is allowed <3
							ipVlanMode = ipVlanL3Routing
							n.modeOpt = ipVlanL3Routing
						}
					}
					// Parse -o host_iface from libnetwork generic opts
					if key == "host_iface" {
						n.ifaceOpt = val.(string)
					}
				}
			}
		}
	}
	if n.ifaceOpt == "" {
		n.ifaceOpt = ipVlanEthIface
	}
	driver.addNetwork(n)
	emptyResponse(w)

	if ipVlanMode == ipVlanL3 {
		log.Debugf("Adding route for the local ipvlan subnet [ %s ] in the default namespace using the specified host interface [ %s]", netCidr.String(), n.ifaceOpt)
		ipvlanParent, err := netlink.LinkByName(n.ifaceOpt)
		// Add a route in the default NS to point to the IPVlan namespace subnet
		addRouteIface(netCidr, ipvlanParent)
		if err != nil {
			log.Debugf("a problem occurred adding the container subnet default namespace route", err)
		}
	} else if ipVlanMode == ipVlanL3Routing {
		log.Debugf("Adding route for the local ipvlan subnet [ %s ] in the default namespace using the specified host interface [ %s]", netCidr.String(), n.ifaceOpt)
		ipvlanParent, err := netlink.LinkByName(n.ifaceOpt)
		// Add a route in the default NS to point to the IPVlan namespace subnet
		addRouteIface(netCidr, ipvlanParent)
		if err != nil {
			log.Debugf("a problem occurred adding the container subnet default namespace route", err)
		}

		// Announce the local IPVLAN network to the other peers in the BGP cluster
		log.Infof("New Docker network: [ %s ]", netCidr.String())
		err = routing.AdvertizeNewRoute(netCidr)
		if err != nil {
			log.Fatalf("Error installing container route : %s", err)
		}
	}
}