Skip to content

Commit

Permalink
convert ip to string
Browse files Browse the repository at this point in the history
  • Loading branch information
Icedroid committed Dec 6, 2020
1 parent 2aecb3d commit 7246037
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
12 changes: 12 additions & 0 deletions pkg/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,15 @@ func GenerateRandomV4IP(cidr string) string {
t := big.NewInt(0).Add(Ip2BigInt(ip), add)
return fmt.Sprintf("%s/%d", BigInt2Ip(t), netMask)
}

func IPToString(ip string) string {
ipNet, _, err := net.ParseCIDR(ip)
if err == nil {
return ipNet.String()
}
ipNet = net.ParseIP(ip)
if ipNet != nil {
return ipNet.String()
}
return ""
}
11 changes: 6 additions & 5 deletions pkg/util/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,13 @@ func ValidatePodNetwork(annotations map[string]string) error {
return nil
}

func ValidatePodCidr(cidrStr, ipStr string) error {
if SubnetBroadCast(cidrStr) == ipStr {
return fmt.Errorf("%s is the broadcast ip in cidr %s", ipStr, cidrStr)
func ValidatePodCidr(cidr, ip string) error {
ipStr := IPToString(ip)
if SubnetBroadCast(cidr) == ipStr {
return fmt.Errorf("%s is the broadcast ip in cidr %s", ipStr, cidr)
}
if SubnetNumber(cidrStr) == ipStr {
return fmt.Errorf("%s is the network number ip in cidr %s", ipStr, cidrStr)
if SubnetNumber(cidr) == ipStr {
return fmt.Errorf("%s is the network number ip in cidr %s", ipStr, cidr)
}

return nil
Expand Down

0 comments on commit 7246037

Please sign in to comment.