Skip to content

Commit 20dca67

Browse files
authored
Merge pull request kubernetes#87453 from aojea/kubeadm_dual
kubeadm: dual-stack validation allow single stack
2 parents 0c64701 + 6dda7ad commit 20dca67

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

cmd/kubeadm/app/apis/kubeadm/validation/validation.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -372,33 +372,32 @@ func ValidateHostPort(endpoint string, fldPath *field.Path) field.ErrorList {
372372
// ValidateIPNetFromString validates network portion of ip address
373373
func ValidateIPNetFromString(subnetStr string, minAddrs int64, isDualStack bool, fldPath *field.Path) field.ErrorList {
374374
allErrs := field.ErrorList{}
375-
if isDualStack {
376-
subnets, err := utilnet.ParseCIDRs(strings.Split(subnetStr, ","))
375+
subnets, err := utilnet.ParseCIDRs(strings.Split(subnetStr, ","))
376+
if err != nil {
377+
allErrs = append(allErrs, field.Invalid(fldPath, subnetStr, "couldn't parse subnet"))
378+
return allErrs
379+
}
380+
switch {
381+
// if DualStack only 2 CIDRs allowed
382+
case isDualStack && len(subnets) > 2:
383+
allErrs = append(allErrs, field.Invalid(fldPath, subnetStr, "expected one (IPv4 or IPv6) CIDR or two CIDRs from each family for dual-stack networking"))
384+
// if DualStack and there are 2 CIDRs validate if there is at least one of each IP family
385+
case isDualStack && len(subnets) == 2:
386+
areDualStackCIDRs, err := utilnet.IsDualStackCIDRs(subnets)
377387
if err != nil {
378388
allErrs = append(allErrs, field.Invalid(fldPath, subnetStr, err.Error()))
379-
} else {
380-
areDualStackCIDRs, err := utilnet.IsDualStackCIDRs(subnets)
381-
if err != nil {
382-
allErrs = append(allErrs, field.Invalid(fldPath, subnets, err.Error()))
383-
} else if !areDualStackCIDRs {
384-
allErrs = append(allErrs, field.Invalid(fldPath, subnetStr, "expected at least one IP from each family (v4 or v6) for dual-stack networking"))
385-
}
386-
for _, s := range subnets {
387-
numAddresses := utilnet.RangeSize(s)
388-
if numAddresses < minAddrs {
389-
allErrs = append(allErrs, field.Invalid(fldPath, s, "subnet is too small"))
390-
}
391-
}
389+
} else if !areDualStackCIDRs {
390+
allErrs = append(allErrs, field.Invalid(fldPath, subnetStr, "expected one (IPv4 or IPv6) CIDR or two CIDRs from each family for dual-stack networking"))
392391
}
393-
} else {
394-
_, svcSubnet, err := net.ParseCIDR(subnetStr)
395-
if err != nil {
396-
allErrs = append(allErrs, field.Invalid(fldPath, subnetStr, "couldn't parse subnet"))
397-
return allErrs
398-
}
399-
numAddresses := utilnet.RangeSize(svcSubnet)
392+
// if not DualStack only one CIDR allowed
393+
case !isDualStack && len(subnets) > 1:
394+
allErrs = append(allErrs, field.Invalid(fldPath, subnetStr, "only one CIDR allowed for single-stack networking"))
395+
}
396+
// validate the subnet/s
397+
for _, s := range subnets {
398+
numAddresses := utilnet.RangeSize(s)
400399
if numAddresses < minAddrs {
401-
allErrs = append(allErrs, field.Invalid(fldPath, subnetStr, "subnet is too small"))
400+
allErrs = append(allErrs, field.Invalid(fldPath, s, "subnet is too small"))
402401
}
403402
}
404403
return allErrs

cmd/kubeadm/app/apis/kubeadm/validation/validation_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,21 @@ func TestValidateIPNetFromString(t *testing.T) {
197197
expected bool
198198
}{
199199
{"invalid missing CIDR", "", 0, false, false},
200+
{"invalid CIDR", "a", 0, false, false},
200201
{"invalid CIDR missing decimal points in IPv4 address and / mask", "1234", 0, false, false},
201202
{"invalid CIDR use of letters instead of numbers and / mask", "abc", 0, false, false},
202203
{"invalid IPv4 address provided instead of CIDR representation", "1.2.3.4", 0, false, false},
203204
{"invalid IPv6 address provided instead of CIDR representation", "2001:db8::1", 0, false, false},
205+
{"invalid multiple CIDR provided in a single stack cluster", "2001:db8::1/64,1.2.3.4/24", 0, false, false},
206+
{"invalid multiple CIDR provided in a single stack cluster and one invalid subnet", "2001:db8::1/64,a", 0, false, false},
204207
{"valid, but IPv4 CIDR too small. At least 10 addresses needed", "10.0.0.16/29", 10, false, false},
205208
{"valid, but IPv6 CIDR too small. At least 10 addresses needed", "2001:db8::/125", 10, false, false},
206209
{"valid IPv4 CIDR", "10.0.0.16/12", 10, false, true},
207210
{"valid IPv6 CIDR", "2001:db8::/98", 10, false, true},
208211
// dual-stack:
209212
{"invalid missing CIDR", "", 0, true, false},
210-
{"invalid only an IPv4 CIDR specified", "10.0.0.16/12", 10, true, false},
211-
{"invalid only an IPv6 CIDR specified", "2001:db8::/98", 10, true, false},
213+
{"valid dual-stack enabled but only an IPv4 CIDR specified", "10.0.0.16/12", 10, true, true},
214+
{"valid dual-stack enabled but only an IPv6 CIDR specified", "2001:db8::/98", 10, true, true},
212215
{"invalid IPv4 address provided instead of CIDR representation", "1.2.3.4,2001:db8::/98", 0, true, false},
213216
{"invalid IPv6 address provided instead of CIDR representation", "2001:db8::1,10.0.0.16/12", 0, true, false},
214217
{"valid, but IPv4 CIDR too small. At least 10 addresses needed", "10.0.0.16/29,2001:db8::/98", 10, true, false},
@@ -217,6 +220,8 @@ func TestValidateIPNetFromString(t *testing.T) {
217220
{"valid, but only IPv6 family addresses specified. IPv4 CIDR is necessary.", "2001:db8::/98,2005:db8::/98", 10, true, false},
218221
{"valid IPv4 and IPv6 CIDR", "10.0.0.16/12,2001:db8::/98", 10, true, true},
219222
{"valid IPv6 and IPv4 CIDR", "10.0.0.16/12,2001:db8::/98", 10, true, true},
223+
{"invalid IPv6 and IPv4 CIDR with more than 2 subnets", "10.0.0.16/12,2001:db8::/98,192.168.0.0/16", 10, true, false},
224+
{"invalid IPv6 and IPv4 CIDR with more than 2 subnets", "10.0.0.16/12,2001:db8::/98,192.168.0.0/16,a.b.c.d/24", 10, true, false},
220225
}
221226
for _, rt := range tests {
222227
actual := ValidateIPNetFromString(rt.subnet, rt.minaddrs, rt.checkDualStack, nil)

0 commit comments

Comments
 (0)