Exemplo n.º 1
0
func New(version string, ctx *cli.Context) (Driver, error) {
	docker, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
	if err != nil {
		return nil, fmt.Errorf("could not connect to docker: %s", err)
	}
	ipVlanEthIface = ctx.String("host-interface")
	// bind CLI opts to the user config struct
	if ok := validateHostIface(ctx.String("host-interface")); !ok {
		log.Debugf("Field [ host-interface ] not detected. Assuming it will be passed via docker network -o (opts)")
	}
	// lower bound of v4 MTU is 68-bytes per rfc791
	if ctx.Int("mtu") <= 0 {
		cliMTU = defaultMTU
	} else if ctx.Int("mtu") >= minMTU {
		cliMTU = ctx.Int("mtu")
	} else {
		log.Fatalf("The MTU value passed [ %d ] must be greater then [ %d ] bytes per rfc791", ctx.Int("mtu"), minMTU)
	}

	switch ctx.String("mode") {
	case ipVlanL2:
		ipVlanMode = ipVlanL2
	case ipVlanL3:
		// IPVlan simply needs the container interface for its
		// default route target since only unicast is allowed <3
		ipVlanMode = ipVlanL3
	case ipVlanL3Routing:
		// IPVlan simply needs the container interface for its
		// default route target since only unicast is allowed <3
		ipVlanMode = ipVlanL3Routing
		//containerGW = nil
		managermode := ""
		as := "65000"
		if ctx.String("routemng") != "" {
			managermode = ctx.String("routemng")
		}
		if ctx.String("as") != "" {
			as = ctx.String("as")
		}
		// Initialize Routing monitoring
		go routing.InitRoutingMonitering(ipVlanEthIface, managermode, as)

	default:
		log.Debugf("Field [ mode ] not detected. Assuming it will be passed via docker network -o (opts)")
	}

	pluginOpts := &pluginConfig{
		mtu:       cliMTU,
		mode:      ipVlanMode,
		hostIface: ipVlanEthIface,
	}

	d := &driver{
		networks: networkTable{},
		dockerer: dockerer{
			client: docker,
		},
		pluginConfig: *pluginOpts,
	}
	return d, nil
}
Exemplo n.º 2
0
func New(version string, ctx *cli.Context) (Driver, error) {
	docker, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
	if err != nil {
		return nil, fmt.Errorf("could not connect to docker: %s", err)
	}
	if ctx.String("host-interface") == "" {
		log.Fatalf("Required flag [ host-interface ] that is used for off box communication was not defined. Example: --host-interface=eth1")
	}
	ipVlanEthIface = ctx.String("host-interface")

	// bind CLI opts to the user config struct
	if ok := validateHostIface(ctx.String("host-interface")); !ok {
		log.Fatalf("Requird field [ host-interface ] ethernet interface [ %s ] was not found. Exiting since this is required for both l2 and l3 modes.", ctx.String("host-interface"))
	}

	// lower bound of v4 MTU is 68-bytes per rfc791
	if ctx.Int("mtu") <= 0 {
		cliMTU = defaultMTU
	} else if ctx.Int("mtu") >= minMTU {
		cliMTU = ctx.Int("mtu")
	} else {
		log.Fatalf("The MTU value passed [ %d ] must be greater then [ %d ] bytes per rfc791", ctx.Int("mtu"), minMTU)
	}

	// Parse the container IP subnet
	containerGW, cidr, err := net.ParseCIDR(ctx.String("ipvlan-subnet"))
	if err != nil {
		log.Fatalf("Error parsing cidr from the subnet flag provided [ %s ]: %s", FlagSubnet, err)
	}

	// For ipvlan L2 mode a gateway IP address is used just like any other
	// normal L2 domain. If no gateway is specified, we attempt to guess using
	// the first usable IP on the container subnet from the CLI argument or from
	// the defaultSubnet "192.168.1.0/24" which results in a gatway of "192.168.1.1".
	switch ctx.String("mode") {
	case ipVlanL2:
		ipVlanMode = ipVlanL2
		if ctx.String("gateway") != "" {
			// bind the container gateway to the IP passed from the CLI
			cliGateway := net.ParseIP(ctx.String("gateway"))
			if err != nil {
				log.Fatalf("The IP passed with the [ gateway ] flag [ %s ] was not a valid address: %s", FlagGateway.Value, err)
			}
			containerGW = cliGateway
		} else {
			// if no gateway was passed, guess the first valid address on the container subnet
			containerGW = ipIncrement(containerGW)
		}
	case ipVlanL3:
		// IPVlan simply needs the container interface for its
		// default route target since only unicast is allowed <3
		ipVlanMode = ipVlanL3
		containerGW = nil

	case ipVlanL3Routing:
		// IPVlan simply needs the container interface for its
		// default route target since only unicast is allowed <3
		ipVlanMode = ipVlanL3Routing
		containerGW = nil
		managermode := ""
		if ctx.String("routemng") != "" {
			managermode = ctx.String("routemng")
		}

		// Initialize Routing monitoring
		go routing.InitRoutingMonitering(ipVlanEthIface, managermode)

	default:
		log.Fatalf("Invalid IPVlan mode supplied [ %s ]. IPVlan has two modes: [ %s ] or [%s ]", ctx.String("mode"), ipVlanL2, ipVlanL3)
	}

	pluginOpts := &pluginConfig{
		mtu:             cliMTU,
		mode:            ipVlanMode,
		containerSubnet: cidr,
		gatewayIP:       containerGW,
		hostIface:       ipVlanEthIface,
	}
	// Leaving as info for now to stdout the plugin config
	log.Infof("Plugin configuration options are: \n %s", pluginOpts)

	ipAllocator := ipallocator.New()
	d := &driver{
		dockerer: dockerer{
			client: docker,
		},
		ipAllocator:  ipAllocator,
		version:      version,
		pluginConfig: *pluginOpts,
	}
	return d, nil
}
Exemplo n.º 3
0
func New(version string, ctx *cli.Context) (Driver, error) {
	docker, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
	if err != nil {
		return nil, fmt.Errorf("could not connect to docker: %s", err)
	}
	if ctx.String("host-interface") == "" {
		log.Fatalf("Required flag [ host-interface ] that is used for off box communication was not defined. Example: --host-interface=eth1")
	}
	ipVlanEthIface = ctx.String("host-interface")

	// bind CLI opts to the user config struct
	if ok := validateHostIface(ctx.String("host-interface")); !ok {
		log.Fatalf("Requird field [ host-interface ] ethernet interface [ %s ] was not found. Exiting since this is required for both l2 and l3 modes.", ctx.String("host-interface"))
	}

	// lower bound of v4 MTU is 68-bytes per rfc791
	if ctx.Int("mtu") <= 0 {
		cliMTU = defaultMTU
	} else if ctx.Int("mtu") >= minMTU {
		cliMTU = ctx.Int("mtu")
	} else {
		log.Fatalf("The MTU value passed [ %d ] must be greater then [ %d ] bytes per rfc791", ctx.Int("mtu"), minMTU)
	}

	switch ctx.String("mode") {
	case ipVlanL2:
		ipVlanMode = ipVlanL2
	case ipVlanL3:
		// IPVlan simply needs the container interface for its
		// default route target since only unicast is allowed <3
		ipVlanMode = ipVlanL3
	case ipVlanL3Routing:
		// IPVlan simply needs the container interface for its
		// default route target since only unicast is allowed <3
		ipVlanMode = ipVlanL3Routing
		//containerGW = nil
		managermode := ""
		serveraddr := net.ParseIP("127.0.0.1")
		as := "65000"
		if ctx.String("routemng") != "" {
			managermode = ctx.String("routemng")
		}
		if ctx.String("serveraddr") != "" {
			serveraddr = net.ParseIP(ctx.String("serveraddr"))
		}
		if ctx.String("as") != "" {
			as = ctx.String("as")
		}
		// Initialize Routing monitoring
		go routing.InitRoutingMonitering(ipVlanEthIface, managermode, serveraddr, as)

	default:
		log.Fatalf("Invalid IPVlan mode supplied [ %s ]. IPVlan has two modes: [ %s ] or [%s ]", ctx.String("mode"), ipVlanL2, ipVlanL3)
	}

	pluginOpts := &pluginConfig{
		mtu:       cliMTU,
		mode:      ipVlanMode,
		hostIface: ipVlanEthIface,
	}

	d := &driver{
		dockerer: dockerer{
			client: docker,
		},
		pluginConfig: *pluginOpts,
	}
	return d, nil
}