← Back to team overview

cloud-init-dev team mailing list archive

Re: [Merge] ~smoser/cloud-init:cleanup/mask2cidr into cloud-init:master

 

responded to rharper's comments. i think he reviewed an older version though.


Diff comments:

> diff --git a/cloudinit/net/network_state.py b/cloudinit/net/network_state.py
> index db3c357..9def76d 100644
> --- a/cloudinit/net/network_state.py
> +++ b/cloudinit/net/network_state.py
> @@ -289,19 +289,16 @@ class NetworkStateInterpreter(object):
>              iface.update({param: val})
>  
>          # convert subnet ipv6 netmask to cidr as needed
> -        subnets = command.get('subnets')
> -        if subnets:
> +        subnets = _normalize_subnets(command.get('subnets'))
> +        print("normalized subnets: %s" % subnets)

ack

> +
> +        # automatically set 'use_ipv6' if any addresses are ipv6
> +        if not self.use_ipv6:
>              for subnet in subnets:
> -                if subnet['type'] == 'static':
> -                    if ':' in subnet['address']:
> -                        self.use_ipv6 = True
> -                    if 'netmask' in subnet and ':' in subnet['address']:
> -                        subnet['netmask'] = mask2cidr(subnet['netmask'])
> -                        for route in subnet.get('routes', []):
> -                            if 'netmask' in route:
> -                                route['netmask'] = mask2cidr(route['netmask'])
> -                elif subnet['type'].endswith('6'):
> +                if (subnet.get('type').endswith('6') or
> +                        is_ipv6_addr(subnet.get('address'))):
>                      self.use_ipv6 = True
> +                    break
>  
>          iface.update({
>              'name': command.get('name'),
> @@ -692,6 +689,122 @@ class NetworkStateInterpreter(object):
>          return subnets
>  
>  
> +def _normalize_subnet(subnet):
> +    # Prune all keys with None values.
> +    subnet = copy.deepcopy(subnet)
> +    normal_subnet = dict((k, v) for k, v in subnet.items() if v)
> +
> +    if subnet.get('type') == 'static':

if there is a static6, then yes.  why is there a 'static6'? i didnt know that.

> +        normal_subnet.update(
> +            _normalize_net_keys(normal_subnet, address_keys=('address',)))
> +    normal_subnet['routes'] = [_normalize_route(r)
> +                               for r in subnet.get('routes', [])]
> +    return normal_subnet
> +
> +
> +def _normalize_net_keys(network, address_keys=()):
> +
> +    """Normalize dictionary network keys returning prefix and address keys.
> +
> +    @param network: A dict of network-related definition containing prefix,
> +        netmask and address_keys.
> +    @param address_keys: A tuple of keys to search for representing the address
> +        or cidr. The first address_key discovered will be used for
> +        normalization.
> +
> +    @returns: A dict containing normalized prefix and matching addr_key.
> +    """
> +    network = copy.deepcopy(network)
> +    net = dict((k, v) for k, v in network.items() if v)
> +    addr_key = None
> +    for key in address_keys:
> +        if net.get(key):
> +            addr_key = key
> +            break
> +    if not addr_key:
> +        message = (
> +            'No config network address keys [%s] found in %s' %
> +            (','.join(address_keys), network))
> +        LOG.error(message)
> +        raise ValueError(message)
> +
> +    addr = net.get(addr_key)
> +    ipv6 = is_ipv6_addr(addr)
> +    netmask = net.get('netmask')
> +    if "/" in addr:
> +        toks = addr.split("/", 2)

ack.

> +        net[addr_key] = toks[0]
> +        # If prefix is defined by the user but addr_key is a CIDR, we
> +        # silently overwrite the user's original prefix here. We should
> +        # log a warning.

we can add a warn to warn if the value is *different*.

> +        net['prefix'] = toks[1]
> +        try:
> +            net['prefix'] = int(toks[1])
> +        except ValueError:
> +            # this supports input of <address>/255.255.255.0
> +            net['prefix'] = mask_to_net_prefix(toks[1])
> +
> +    elif netmask:
> +        net['prefix'] = mask_to_net_prefix(netmask)
> +    else:
> +        net['prefix'] = 64 if ipv6 else 24

yeah, its wierd. i agree.

> +
> +    if ipv6:
> +        if 'netmask' in net:
> +            del net['netmask']
> +    else:
> +        net['netmask'] = net_prefix_to_ipv4_mask(net['prefix'])
> +
> +    prefix = net['prefix']
> +    if prefix:
> +        try:
> +            net['prefix'] = int(prefix)
> +        except ValueError:
> +            raise TypeError(
> +                'Network config prefix {} is not an integer'.format(prefix))
> +    return net
> +
> +
> +def _normalize_route(route):
> +    """normalize a route.
> +    return a dictionary with only:
> +       'type': 'route' (only present if it was present in input)
> +       'network': the network portion of the route as a string.
> +       'prefix': the network prefix for address as an integer.
> +       'metric': integer metric (only if present in input).
> +       'netmask': netmask (string) equivalent to prefix if ipv4.
> +       """
> +    route = copy.deepcopy(route)
> +    print("normalizing %s" % route)

ack

> +    # Prune None-value keys
> +    normal_route = dict((k, v) for k, v in route.items() if v)
> +    normal_route.update(
> +        _normalize_net_keys(
> +            normal_route, address_keys=('network', 'destination')))
> +
> +    metric = normal_route.get('metric')
> +    if metric:
> +        try:
> +            normal_route['metric'] = int(metric)
> +        except ValueError:
> +            raise TypeError(
> +                'Route config metric {} is not an integer'.format(metric))
> +    print("normaled to %s" % normal_route)
> +    return normal_route
> +
> +
> +def _normalize_subnets(subnets):
> +    if not subnets:
> +        subnets = []
> +    return [_normalize_subnet(s) for s in subnets]
> +
> +
> +def is_ipv6_addr(address):
> +    if not address:
> +        return False
> +    return ":" in address
> +
> +
>  def subnet_is_ipv6(subnet):
>      """Common helper for checking network_state subnets for ipv6."""
>      # 'static6' or 'dhcp6'
> @@ -733,6 +869,28 @@ def ipv6mask2cidr(mask):
>      return cidr
>  
>  
> +def mask_to_net_prefix(mask):
> +    """Return the network prefix for the netmask provided.
> +
> +    Supports ipv4 or ipv6 netmasks."""
> +    if ':' in str(mask):
> +        return ipv6_mask_to_net_prefix(mask)
> +    elif '.' in str(mask):
> +        return ipv4_mask_to_net_prefix(mask)
> +

i made it so it will take a string that is an integer.
because some places (especially ipv6) *do* use the word 'netmask' to mean 'network prefix'

> +
> +def ipv4mask2cidr(mask):
> +    return ipv4_mask_to_net_prefix(mask, strict=False)
> +
> +
> +def cidr2mask(cidr):
> +    return net_prefix_to_ipv4_mask(cidr)
> +
> +
> +def ipv6mask2cidr(mask):
> +    return ipv6_mask_to_net_prefix(mask, strict=False)
> +
> +
>  def mask2cidr(mask):
>      if ':' in mask:
>          return ipv6mask2cidr(mask)


-- 
https://code.launchpad.net/~smoser/cloud-init/+git/cloud-init/+merge/324677
Your team cloud-init commiters is requested to review the proposed merge of ~smoser/cloud-init:cleanup/mask2cidr into cloud-init:master.


References