kubernetes/kops · error

unable to parse CIDR for %q: %v

Error message

unable to parse CIDR for %q: %v

What it means

CIDRSubnet computes a subnet address within a given IP network prefix (Terraform cidrsubnet-style). It first parses the base prefix with net.ParseCIDR; if the prefix is not valid CIDR syntax, the parse error is wrapped as this error and returned.

Source

Thrown at upup/pkg/fi/utils/net.go:107

	if err != nil {
		return 0, 0, fmt.Errorf("unable to convert CIDR subnet new bits to int: %q: %v", s[1], err)
	}

	netNum, err := strconv.ParseInt(s[2], 16, 64)
	if err != nil {
		return 0, 0, fmt.Errorf("unable to convert CIDR subnet net num to int: %q: %v", s[2], err)
	}

	return newSize, netNum, nil
}

// CIDRSubnet calculates a subnet address within given IP network address prefix.
// Inspired by the Terraform implementation of the "cidrsubnet" function
// https://www.terraform.io/docs/language/functions/cidrsubnet.html
func CIDRSubnet(prefix string, newSize int, netNum int64) (string, error) {
	_, baseCIDR, err := net.ParseCIDR(prefix)
	if err != nil {
		return "", fmt.Errorf("unable to parse CIDR for %q: %v", prefix, err)
	}

	oldSize, totalSize := baseCIDR.Mask.Size()
	if oldSize == 0 && totalSize == 0 {
		return "", fmt.Errorf("unable to calculate CIDR mask size for %q: %q", prefix, baseCIDR.Mask)
	}

	newNetwork, err := cidr.SubnetBig(baseCIDR, newSize-oldSize, big.NewInt(netNum))
	if err != nil {
		return "", fmt.Errorf("unable to calculate subnet CIDR for %q -> /%d#%d : %v", prefix, newSize, netNum, err)
	}

	return newNetwork.String(), nil
}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Fix the prefix to include a valid mask, e.g. "10.0.0.0/16" instead of "10.0.0.0".
  2. Verify each address component is a valid IP (octets 0-255 for IPv4).
  3. Check that config/template substitution did not leave the prefix empty or malformed.
  4. Pre-validate with net.ParseCIDR(prefix) in the caller and log a clear config error.

Example fix

// before
subnet, err := utils.CIDRSubnet("172.20.0.0", 8, 1) // missing /mask
// after
subnet, err := utils.CIDRSubnet("172.20.0.0/16", 8, 1)
Defensive patterns

Strategy: validation

Validate before calling

func validCIDRPrefix(prefix string) error {
	_, _, err := net.ParseCIDR(prefix)
	return err
}

Prevention

When it happens

Trigger: Calling CIDRSubnet (via calculateSubnetCIDR) with a prefix string that net.ParseCIDR rejects: missing '/' and mask size ("10.0.0.0"), invalid octets ("300.0.0.0/8"), host bits set incorrectly, or non-IP text.

Common situations: Cluster spec networkCIDR/nonMasqueradeCIDR values hand-edited to omit the /mask; region or env-config substitution producing an empty string; mixing IPv6 shorthand with the IPv4-only expectation.

Understand the failure class

Related errors


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/5118e8764ed7f121. Report an issue: GitHub.