Skip to content

Commit 4e14d1b

Browse files
author
Antonio Ojea
committed
kubeadm: validate node-cidr-mask are correct
1 parent 7fc6b41 commit 4e14d1b

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,11 @@ func ValidatePodSubnetNodeMask(subnetStr string, c *kubeadm.ClusterConfiguration
432432
mask := podSubnet.Mask
433433
maskSize, _ := mask.Size()
434434
// obtain node-cidr-mask
435-
nodeMask := getClusterNodeMask(c, utilnet.IsIPv6(podSubnet.IP))
435+
nodeMask, err := getClusterNodeMask(c, utilnet.IsIPv6(podSubnet.IP))
436+
if err != nil {
437+
allErrs = append(allErrs, field.Invalid(fldPath, podSubnet.String(), err.Error()))
438+
continue
439+
}
436440
// the pod subnet mask needs to allow one or multiple node-masks
437441
// i.e. if it has a /24 the node mask must be between 24 and 32 for ipv4
438442
if maskSize > nodeMask {
@@ -447,13 +451,14 @@ func ValidatePodSubnetNodeMask(subnetStr string, c *kubeadm.ClusterConfiguration
447451
// getClusterNodeMask returns the corresponding node-cidr-mask
448452
// based on the Cluster configuration and the IP family
449453
// Default is 24 for IPv4 and 64 for IPv6
450-
func getClusterNodeMask(c *kubeadm.ClusterConfiguration, isIPv6 bool) int {
454+
func getClusterNodeMask(c *kubeadm.ClusterConfiguration, isIPv6 bool) (int, error) {
451455
// defaultNodeMaskCIDRIPv4 is default mask size for IPv4 node cidr for use by the controller manager
452456
const defaultNodeMaskCIDRIPv4 = 24
453457
// DefaultNodeMaskCIDRIPv6 is default mask size for IPv6 node cidr for use by the controller manager
454458
const defaultNodeMaskCIDRIPv6 = 64
455459
var maskSize int
456460
var maskArg string
461+
var err error
457462
isDualStack := features.Enabled(c.FeatureGates, features.IPv6DualStack)
458463

459464
if isDualStack && isIPv6 {
@@ -466,13 +471,17 @@ func getClusterNodeMask(c *kubeadm.ClusterConfiguration, isIPv6 bool) int {
466471

467472
if v, ok := c.ControllerManager.ExtraArgs[maskArg]; ok && v != "" {
468473
// assume it is an integer, if not it will fail later
469-
maskSize, _ = strconv.Atoi(v)
474+
maskSize, err = strconv.Atoi(v)
475+
if err != nil {
476+
errors.Wrapf(err, "could not parse the value of the kube-controller-manager flag %s as an integer: %v", maskArg, err)
477+
return 0, err
478+
}
470479
} else if isIPv6 {
471480
maskSize = defaultNodeMaskCIDRIPv6
472481
} else {
473482
maskSize = defaultNodeMaskCIDRIPv4
474483
}
475-
return maskSize
484+
return maskSize, nil
476485
}
477486

478487
// ValidateNetworking validates networking configuration

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,10 +1130,11 @@ func TestValidateEtcd(t *testing.T) {
11301130

11311131
func TestGetClusterNodeMask(t *testing.T) {
11321132
tests := []struct {
1133-
name string
1134-
cfg *kubeadmapi.ClusterConfiguration
1135-
isIPv6 bool
1136-
expectedMask int
1133+
name string
1134+
cfg *kubeadmapi.ClusterConfiguration
1135+
isIPv6 bool
1136+
expectedMask int
1137+
expectedError bool
11371138
}{
11381139
{
11391140
name: "ipv4 default mask",
@@ -1151,6 +1152,16 @@ func TestGetClusterNodeMask(t *testing.T) {
11511152
isIPv6: false,
11521153
expectedMask: 23,
11531154
},
1155+
{
1156+
name: "ipv4 wrong mask",
1157+
cfg: &kubeadmapi.ClusterConfiguration{
1158+
ControllerManager: kubeadmapi.ControlPlaneComponent{
1159+
ExtraArgs: map[string]string{"node-cidr-mask-size": "aa23"},
1160+
},
1161+
},
1162+
isIPv6: false,
1163+
expectedError: true,
1164+
},
11541165
{
11551166
name: "ipv6 default mask",
11561167
cfg: &kubeadmapi.ClusterConfiguration{},
@@ -1216,6 +1227,17 @@ func TestGetClusterNodeMask(t *testing.T) {
12161227
isIPv6: false,
12171228
expectedMask: 23,
12181229
},
1230+
{
1231+
name: "dual ipv4 wrong mask",
1232+
cfg: &kubeadmapi.ClusterConfiguration{
1233+
FeatureGates: map[string]bool{features.IPv6DualStack: true},
1234+
ControllerManager: kubeadmapi.ControlPlaneComponent{
1235+
ExtraArgs: map[string]string{"node-cidr-mask-size-ipv4": "aa"},
1236+
},
1237+
},
1238+
isIPv6: false,
1239+
expectedError: true,
1240+
},
12191241
{
12201242
name: "dual ipv6 default mask and legacy flag",
12211243
cfg: &kubeadmapi.ClusterConfiguration{
@@ -1238,10 +1260,25 @@ func TestGetClusterNodeMask(t *testing.T) {
12381260
isIPv6: true,
12391261
expectedMask: 83,
12401262
},
1263+
{
1264+
name: "dual ipv6 custom mask and wrong flag",
1265+
cfg: &kubeadmapi.ClusterConfiguration{
1266+
FeatureGates: map[string]bool{features.IPv6DualStack: true},
1267+
ControllerManager: kubeadmapi.ControlPlaneComponent{
1268+
ExtraArgs: map[string]string{"node-cidr-mask-size": "23", "node-cidr-mask-size-ipv6": "a83"},
1269+
},
1270+
},
1271+
isIPv6: true,
1272+
expectedError: true,
1273+
},
12411274
}
12421275
for _, test := range tests {
12431276
t.Run(test.name, func(t *testing.T) {
1244-
if mask := getClusterNodeMask(test.cfg, test.isIPv6); mask != test.expectedMask {
1277+
mask, err := getClusterNodeMask(test.cfg, test.isIPv6)
1278+
if (err == nil) == test.expectedError {
1279+
t.Errorf("expected error: %v, got %v", test.expectedError, err)
1280+
}
1281+
if mask != test.expectedMask {
12451282
t.Errorf("expected mask: %d, got %d", test.expectedMask, mask)
12461283
}
12471284
})

0 commit comments

Comments
 (0)