Repository: moby/moby
File: daemon/libnetwork/drivers/macvlan/macvlan_joinleave.go (Go)

Code before:
			s := getSubnetForIP(ep.addr, n.config.Ipv4Subnets)
			if s == nil {
				return fmt.Errorf("could not find a valid ipv4 subnet for endpoint %s", eid)
			}
			if s.GwIP == "" {
				// Can't set up a default gateway, but the network is not internal, so it should
				// be treated as having external connectivity. (This preserves old behavior,
				// where a gateway address was assigned from IPAM that did not necessarily
				// correspond with a working gateway.)
				jinfo.ForceGw4()
			} else {
				v4gw, _, err := net.ParseCIDR(s.GwIP)
				if err != nil {
					return fmt.Errorf("gateway %s is not a valid ipv4 address: %v", s.GwIP, err)
				}
				err = jinfo.SetGateway(v4gw)
				if err != nil {
					return err
				}
			}
			log.G(ctx).Debugf("Macvlan Endpoint Joined with IPv4_Addr: %s, Gateway: %s, MacVlan_Mode: %s, Parent: %s",
				ep.addr.IP.String(), s.GwIP, n.config.MacvlanMode, n.config.Parent)
		}
		// parse and correlate the endpoint v6 address with the available v6 subnets
		if ep.addrv6 != nil && len(n.config.Ipv6Subnets) > 0 {
			s := getSubnetForIP(ep.addr, n.config.Ipv6Subnets)
			if s == nil {
				return fmt.Errorf("could not find a valid ipv6 subnet for endpoint %s", eid)
			}
			if s.GwIP == "" {
				// Can't set up a default gateway, but the network is not internal, so it should
				// be treated as having external connectivity. (This preserves old behavior,
				// where a gateway address was assigned from IPAM that did not necessarily
				// correspond with a working gateway.)
				jinfo.ForceGw6()
			} else {
				v6gw, _, err := net.ParseCIDR(s.GwIP)
				if err != nil {
					return fmt.Errorf("gateway %s is not a valid ipv6 address: %v", s.GwIP, err)
				}
				err = jinfo.SetGatewayIPv6(v6gw)
				if err != nil {
					return err
				}
			}
			log.G(ctx).Debugf("Macvlan Endpoint Joined with IPv6_Addr: %s Gateway: %s MacVlan_Mode: %s, Parent: %s",
				ep.addrv6.IP.String(), s.GwIP, n.config.MacvlanMode, n.config.Parent)
		}
	} else {
		if len(n.config.Ipv4Subnets) > 0 {
			log.G(ctx).Debugf("Macvlan Endpoint Joined with IPv4_Addr: %s, MacVlan_Mode: %s, Parent: %s",

Review comment (suggestion):
_Still draft I see - but I realised I could "check (it) out any time I like"!_

This should fix the tests ...

```suggestion
			s := getSubnetForIP(ep.addrv6, n.config.Ipv6Subnets)
```

Code after:
			s := getSubnetForIP(ep.addr, n.config.Ipv4Subnets)
			if s == nil {
				return fmt.Errorf("could not find a valid ipv4 subnet for endpoint %s", eid)
			}
			if s.GwIP == "" {
				// Can't set up a default gateway, but the network is not internal, so it should
				// be treated as having external connectivity. (This preserves old behavior,
				// where a gateway address was assigned from IPAM that did not necessarily
				// correspond with a working gateway.)
				jinfo.ForceGw4()
			} else {
				v4gw, _, err := net.ParseCIDR(s.GwIP)
				if err != nil {
					return fmt.Errorf("gateway %s is not a valid ipv4 address: %v", s.GwIP, err)
				}
				err = jinfo.SetGateway(v4gw)
				if err != nil {
					return err
				}
			}
			log.G(ctx).Debugf("Macvlan Endpoint Joined with IPv4_Addr: %s, Gateway: %s, MacVlan_Mode: %s, Parent: %s",
				ep.addr.IP.String(), s.GwIP, n.config.MacvlanMode, n.config.Parent)
		}
		// parse and correlate the endpoint v6 address with the available v6 subnets
		if ep.addrv6 != nil && len(n.config.Ipv6Subnets) > 0 {
			s := getSubnetForIP(ep.addrv6, n.config.Ipv6Subnets)
			if s == nil {
				return fmt.Errorf("could not find a valid ipv6 subnet for endpoint %s", eid)
			}
			if s.GwIP == "" {
				// Can't set up a default gateway, but the network is not internal, so it should
				// be treated as having external connectivity. (This preserves old behavior,
				// where a gateway address was assigned from IPAM that did not necessarily
				// correspond with a working gateway.)
				jinfo.ForceGw6()
			} else {
				v6gw, _, err := net.ParseCIDR(s.GwIP)
				if err != nil {
					return fmt.Errorf("gateway %s is not a valid ipv6 address: %v", s.GwIP, err)
				}
				err = jinfo.SetGatewayIPv6(v6gw)
				if err != nil {
					return err
				}
			}
			log.G(ctx).Debugf("Macvlan Endpoint Joined with IPv6_Addr: %s Gateway: %s MacVlan_Mode: %s, Parent: %s",
				ep.addrv6.IP.String(), s.GwIP, n.config.MacvlanMode, n.config.Parent)
		}
	} else {
		if len(n.config.Ipv4Subnets) > 0 {
			log.G(ctx).Debugf("Macvlan Endpoint Joined with IPv4_Addr: %s, MacVlan_Mode: %s, Parent: %s",