added comments and improved error message for validateSubnet

pull/15530/head
Shubh Bapna 2022-12-23 08:44:57 -05:00
parent 7466acb3e8
commit 474a56b50c
2 changed files with 6 additions and 4 deletions

View File

@ -1752,19 +1752,21 @@ func validateDockerStorageDriver(drvName string) {
viper.Set(preload, false) viper.Set(preload, false)
} }
// validateSubnet checks that the subnet provided has a private IP
// and does not have a mask of more that /30
func validateSubnet(subnet string) error { func validateSubnet(subnet string) error {
ip, cidr, err := netutil.ParseAddr(subnet) ip, cidr, err := netutil.ParseAddr(subnet)
if err != nil { if err != nil {
return err return err
} }
if !ip.IsPrivate() { if !ip.IsPrivate() {
return errors.Errorf("Sorry, %s is not a private IP", ip) return errors.Errorf("Sorry, the subnet %s is not a private IP", ip)
} }
if cidr != nil { if cidr != nil {
mask, _ := cidr.Mask.Size() mask, _ := cidr.Mask.Size()
if mask > 30 { if mask > 30 {
return errors.Errorf("Sorry, mask must be less than /30") return errors.Errorf("Sorry, the subnet provided does not have a mask less than or equal to /30")
} }
} }
return nil return nil

View File

@ -641,7 +641,7 @@ func TestValidateSubnet(t *testing.T) {
}, },
{ {
subnet: "193.169.1.1", subnet: "193.169.1.1",
errorMsg: "Sorry, 193.169.1.1 is not a private IP", errorMsg: "Sorry, the subnet 193.169.1.1 is not a private IP",
}, },
{ {
subnet: "192.168.1.1/24", subnet: "192.168.1.1/24",
@ -649,7 +649,7 @@ func TestValidateSubnet(t *testing.T) {
}, },
{ {
subnet: "192.168.1.1/32", subnet: "192.168.1.1/32",
errorMsg: "Sorry, mask must be less than /30", errorMsg: "Sorry, the subnet provided does not have a mask less than or equal to /30",
}, },
} }
for _, test := range tests { for _, test := range tests {