Skip to content

Commit 889648d

Browse files
committed
Improve the error message for the service cidr check
1 parent b19de7f commit 889648d

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

cmd/kube-apiserver/app/options/validation.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import (
3636
// validateClusterIPFlags is expected to be called after Complete()
3737
func validateClusterIPFlags(options *ServerRunOptions) []error {
3838
var errs []error
39+
// maxCIDRbits is used to define the maximum CIDR size for the cluster ip(s)
40+
const maxCIDRbits = 20
3941

4042
// validate that primary has been processed by user provided values or it has been defaulted
4143
if options.PrimaryServiceClusterIPRange.IP == nil {
@@ -50,8 +52,8 @@ func validateClusterIPFlags(options *ServerRunOptions) []error {
5052
// Complete() expected to have set Primary* and Secondary*
5153
// primary CIDR validation
5254
var ones, bits = options.PrimaryServiceClusterIPRange.Mask.Size()
53-
if bits-ones > 20 {
54-
errs = append(errs, errors.New("specified --service-cluster-ip-range is too large"))
55+
if bits-ones > maxCIDRbits {
56+
errs = append(errs, fmt.Errorf("specified --service-cluster-ip-range is too large. Network CIDR should not be bigger than /%d", bits-maxCIDRbits))
5557
}
5658

5759
// Secondary IP validation
@@ -79,8 +81,8 @@ func validateClusterIPFlags(options *ServerRunOptions) []error {
7981
// bigger cidr (specially those offered by IPv6) will add no value
8082
// significantly increase snapshotting time.
8183
var ones, bits = options.SecondaryServiceClusterIPRange.Mask.Size()
82-
if bits-ones > 20 {
83-
errs = append(errs, errors.New("specified --secondary-service-cluster-ip-range is too large"))
84+
if bits-ones > maxCIDRbits {
85+
errs = append(errs, fmt.Errorf("specified --service-cluster-ip-range is too large. Network CIDR should not be bigger than /%d", bits-maxCIDRbits))
8486
}
8587
}
8688

cmd/kube-apiserver/app/options/validation_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ func TestClusterSerivceIPRange(t *testing.T) {
9595
options: makeOptionsWithCIDRs("10.0.0.0/16", "3000::/108"),
9696
enableDualStack: false,
9797
},
98+
{
99+
name: "service cidr to big",
100+
expectErrors: true,
101+
options: makeOptionsWithCIDRs("10.0.0.0/8", ""),
102+
enableDualStack: true,
103+
},
104+
{
105+
name: "dual-stack secondary cidr to big",
106+
expectErrors: true,
107+
options: makeOptionsWithCIDRs("10.0.0.0/16", "3000::/64"),
108+
enableDualStack: true,
109+
},
98110
/* success cases */
99111
{
100112
name: "valid primary",

0 commit comments

Comments
 (0)