update with new logic

pull/9294/head
Medya Gh 2020-09-23 20:03:11 -07:00
parent 2766235a5c
commit f163cf9e15
3 changed files with 26 additions and 22 deletions

View File

@ -94,13 +94,11 @@ func (d *Driver) Create() error {
APIServerPort: d.NodeConfig.APIServerPort, APIServerPort: d.NodeConfig.APIServerPort,
} }
// one network bridge per cluster. if gateway, err := oci.CreateNetwork(d.OCIBinary, d.NodeConfig.ClusterName); err != nil {
defaultNetwork := d.NodeConfig.ClusterName
if gateway, err := oci.CreateNetwork(d.OCIBinary, defaultNetwork); err != nil {
glog.Warningf("failed to create network: %v", err) glog.Warningf("failed to create network: %v", err)
out.WarningT("Unable to create dedicated network, This might result in cluster IP change after restart.") out.WarningT("Unable to create dedicated network, This might result in cluster IP change after restart.")
} else { } else {
params.Network = defaultNetwork params.Network = d.NodeConfig.ClusterName
ip := gateway.To4() ip := gateway.To4()
// calculate the container IP based on its machine order // calculate the container IP based on its machine order
ip[3] += byte(machineOrder(d.NodeConfig.MachineName)) ip[3] += byte(machineOrder(d.NodeConfig.MachineName))

View File

@ -34,6 +34,11 @@ func TestMachineOrder(t *testing.T) {
Name: "second-node", Name: "second-node",
MachineName: "minikube-m02", MachineName: "minikube-m02",
Want: 2}, Want: 2},
{
Name: "funny",
MachineName: "hahaha",
Want: 1},
{ {
Name: "dash-profile", Name: "dash-profile",
MachineName: "my-dashy-minikube", MachineName: "my-dashy-minikube",

View File

@ -29,7 +29,10 @@ import (
) )
// DefaultSubnet subnet to be used on first cluster // DefaultSubnet subnet to be used on first cluster
const defaultSubnet = "192.168.39.0/24" const defaultSubnetAddr = "192.168.39.0"
// big enough for a cluster of 256 nodes
const defaultSubnetRange = 24
// CreateNetwork creates a network returns gateway and error, minikube creates one network per cluster // CreateNetwork creates a network returns gateway and error, minikube creates one network per cluster
func CreateNetwork(ociBin string, name string) (net.IP, error) { func CreateNetwork(ociBin string, name string) (net.IP, error) {
@ -39,21 +42,17 @@ func CreateNetwork(ociBin string, name string) (net.IP, error) {
return createDockerNetwork(name) return createDockerNetwork(name)
} }
func createDockerNetwork(name string) (net.IP, error) { func createDockerNetwork(clusterName string) (net.IP, error) {
// check if the network already exists // check if the network already exists
subnet, gateway, err := dockerNetworkInspect(name) subnet, gateway, err := dockerNetworkInspect(clusterName)
if err == nil { if err == nil {
glog.Infof("Found existing network with subnet %s and gateway %s.", subnet, gateway) glog.Infof("Found existing network with subnet %s and gateway %s.", subnet, gateway)
return gateway, nil return gateway, nil
} }
// simple way to create networks, subnet is taken, try one bigger // simple way to create networks, subnet is taken, try one bigger
attempt := 0 attempt := 0
_, subnet, err = net.ParseCIDR(defaultSubnet) subnetAddr := defaultSubnetAddr
if err != nil { gateway, err = tryCreateDockerNetwork(subnetAddr, defaultSubnetRange, clusterName)
return nil, errors.Wrapf(err, "parse default subnet %s", defaultSubnet)
}
gateway, err = tryCreateDockerNetwork(subnet, name)
if err != nil { if err != nil {
if err != ErrNetworkSubnetTaken { if err != ErrNetworkSubnetTaken {
return nil, errors.Wrapf(err, "error creating network") return nil, errors.Wrapf(err, "error creating network")
@ -62,18 +61,20 @@ func createDockerNetwork(name string) (net.IP, error) {
// we can try up to 255 // we can try up to 255
for attempt < 13 { for attempt < 13 {
attempt++ attempt++
glog.Infof("Couldn't create network %q at %q subnet will try again with a new subnet ...", name, subnet) glog.Infof("Couldn't create network %q at %q subnet will try again with a new subnet ...", clusterName, subnetAddr)
// increase 3nd digit by 10 each time // increase 3nd digit by 10 each time
// 13 times adding 10 defaultSubnet "192.168.39.0/24" // 13 times adding 10 defaultSubnet "192.168.39.0/24"
// at most it will add up to 169 which is still less than max allowed 255 // at most it will add up to 169 which is still less than max allowed 255
// this is large enough to try more and not too small to not try enough // this is large enough to try more and not too small to not try enough
// can be tuned in the next iterations // can be tuned in the next iterations
subnet.IP.To4()[2] += 10 ip := net.ParseIP(subnetAddr).To4()
gateway, err := tryCreateDockerNetwork(subnet, name) ip[2] += byte(9 + attempt)
gateway, err = tryCreateDockerNetwork(ip.String(), defaultSubnetRange, clusterName)
if err == nil { if err == nil {
return gateway, nil return gateway, nil
} }
if err == ErrNetworkSubnetTaken { if errors.Is(err, ErrNetworkSubnetTaken) || errors.Is(err, ErrNetworkGatewayTaken) {
continue continue
} }
} }
@ -82,12 +83,12 @@ func createDockerNetwork(name string) (net.IP, error) {
return gateway, nil return gateway, nil
} }
func tryCreateDockerNetwork(subnet *net.IPNet, name string) (net.IP, error) { func tryCreateDockerNetwork(subnetAddr string, subnetRange int, name string) (net.IP, error) {
gateway := subnet.IP.To4() gateway := net.ParseIP(subnetAddr)
gateway[3]++ // first ip for gateway gateway.To4()[3]++ // first ip for gateway
glog.Infof("attempt to create network %q with subnet: %s and gateway %s...", subnet, name, gateway) glog.Infof("attempt to create network %q with subnet: %s and gateway %s...", subnetAddr, name, gateway)
// options documentation https://docs.docker.com/engine/reference/commandline/network_create/#bridge-driver-options // options documentation https://docs.docker.com/engine/reference/commandline/network_create/#bridge-driver-options
rr, err := runCmd(exec.Command(Docker, "network", "create", "--driver=bridge", fmt.Sprintf("--subnet=%s", subnet), fmt.Sprintf("--gateway=%s", gateway), "-o", "--ip-masq", "-o", "--icc", fmt.Sprintf("--label=%s=%s", CreatedByLabelKey, "true"), name)) rr, err := runCmd(exec.Command(Docker, "network", "create", "--driver=bridge", fmt.Sprintf("--subnet=%s", fmt.Sprintf("%s/%d", subnetAddr, subnetRange)), fmt.Sprintf("--gateway=%s", gateway), "-o", "--ip-masq", "-o", "--icc", fmt.Sprintf("--label=%s=%s", CreatedByLabelKey, "true"), name))
if err != nil { if err != nil {
if strings.Contains(rr.Output(), "Pool overlaps with other one on this address space") { if strings.Contains(rr.Output(), "Pool overlaps with other one on this address space") {
return nil, ErrNetworkSubnetTaken return nil, ErrNetworkSubnetTaken