{"record":{"id":"718bb5ec29b324c0","repo":"kubernetes/kops","slug":"unable-to-calculate-subnet-cidr-for-q-d-d","errorCode":null,"errorMessage":"unable to calculate subnet CIDR for %q -> /%d#%d : %v","messagePattern":"unable to calculate subnet CIDR for %q -> /(.+?)#(.+?) : (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"upup/pkg/fi/utils/net.go","lineNumber":117,"sourceCode":"}\n\n// CIDRSubnet calculates a subnet address within given IP network address prefix.\n// Inspired by the Terraform implementation of the \"cidrsubnet\" function\n// https://www.terraform.io/docs/language/functions/cidrsubnet.html\nfunc CIDRSubnet(prefix string, newSize int, netNum int64) (string, error) {\n\t_, baseCIDR, err := net.ParseCIDR(prefix)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"unable to parse CIDR for %q: %v\", prefix, err)\n\t}\n\n\toldSize, totalSize := baseCIDR.Mask.Size()\n\tif oldSize == 0 && totalSize == 0 {\n\t\treturn \"\", fmt.Errorf(\"unable to calculate CIDR mask size for %q: %q\", prefix, baseCIDR.Mask)\n\t}\n\n\tnewNetwork, err := cidr.SubnetBig(baseCIDR, newSize-oldSize, big.NewInt(netNum))\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"unable to calculate subnet CIDR for %q -> /%d#%d : %v\", prefix, newSize, netNum, err)\n\t}\n\n\treturn newNetwork.String(), nil\n}\n","sourceCodeStart":99,"sourceCodeEnd":122,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/upup/pkg/fi/utils/net.go#L99-L122","documentation":"CIDRSubnet delegates the actual subnet computation to cidr.SubnetBig(baseCIDR, newSize-oldSize, netNum). If the requested new-bit expansion is invalid — e.g. newSize is smaller than or equal to the existing prefix size, or the expansion exceeds the address space — SubnetBig fails and the error is wrapped with the prefix, newSize, and netNum for diagnosis.","triggerScenarios":"Calling CIDRSubnet with newSize <= oldSize of the prefix (e.g. requesting /8 inside a /16), or newSize so large the subnet bits exceed the address family's total bits, or a netNum outside the range of addressable subnets (via calculateSubnetCIDR).","commonSituations":"Mistyped subnet-mask constants in cluster spec; asking for a subnet smaller than the parent prefix; offset/netnum exceeding the number of available subnets; IPv4 config values accidentally used in the IPv6 expansion path.","solutions":["Ensure newSize is greater than the base prefix size and within the address space (e.g. /16 base, newSize 24 is valid; 16 is not).","Keep netNum within [0, 2^(newSize-oldSize)); e.g. for an 8-bit expansion, netNum must be 0-255.","Check calculateSubnetCIDR inputs (zone index, subnet count) for out-of-range arithmetic.","Compute the expansion bits explicitly and assert 0 < newSize-oldSize <= totalSize-oldSize before calling."],"exampleFix":"// before\nsubnet, err := utils.CIDRSubnet(\"172.20.0.0/16\", 8, 1) // newSize smaller than oldSize\n// after\nsubnet, err := utils.CIDRSubnet(\"172.20.0.0/16\", 24, 1)","handlingStrategy":"validation","validationCode":"func validSubnetRequest(prefix string, newSize int, netNum int64) error {\n\t_, cidrNet, err := net.ParseCIDR(prefix)\n\tif err != nil {\n\t\treturn err\n\t}\n\told, total := cidrNet.Mask.Size()\n\tif newSize <= old || newSize > total {\n\t\treturn fmt.Errorf(\"newSize /%d must satisfy /%d < /%d <= /%d\", newSize, old, newSize, total)\n\t}\n\tmaxNet := int64(1) << uint(newSize-old)\n\tif netNum < 0 || netNum >= maxNet {\n\t\treturn fmt.Errorf(\"netNum %d out of range [0,%d)\", netNum, maxNet)\n\t}\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Assert newSize > existing prefix bits and within total address bits before expanding.","Keep netnum below 2^(newSize-oldSize); compute zone-index based netnums defensively.","Add table-driven tests covering edge sizes (newSize == oldSize, newSize == totalSize)."],"tags":["network","cidr","subnet","range-check"],"backgroundTag":"invalid-subnet-expansion","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}