From 0c5ec73d606ec44e49ecfdb13622b96a61660c87 Mon Sep 17 00:00:00 2001 From: Vasilev Andrew Date: Wed, 16 Aug 2023 15:00:46 +0200 Subject: [PATCH 1/3] Deprecated netaddr replaced with default netip library --- api/v1alpha1/cidr_types.go | 104 ++++++++++++++++-------------- api/v1alpha1/cidr_types_test.go | 19 +++--- api/v1alpha1/ipaddr_types.go | 11 ++-- api/v1alpha1/ipaddr_types_test.go | 11 ++-- api/v1alpha1/subnet_types.go | 4 +- go.mod | 1 + go.sum | 2 + 7 files changed, 80 insertions(+), 72 deletions(-) diff --git a/api/v1alpha1/cidr_types.go b/api/v1alpha1/cidr_types.go index d31981a..d389fee 100644 --- a/api/v1alpha1/cidr_types.go +++ b/api/v1alpha1/cidr_types.go @@ -17,30 +17,34 @@ package v1alpha1 import ( "encoding/json" "math/big" + "net/netip" - "inet.af/netaddr" + "go4.org/netipx" ) // +kubebuilder:validation:Type=string type CIDR struct { - Net netaddr.IPPrefix `json:"-"` + Net netip.Prefix `json:"-"` + IPRange netipx.IPRange `json:"-"` } func CIDRFromString(cidrString string) (*CIDR, error) { - cidr, err := netaddr.ParseIPPrefix(cidrString) + cidr, err := netip.ParsePrefix(cidrString) if err != nil { return nil, err } + ipRange := netipx.RangeOfPrefix(cidr) return &CIDR{ - Net: cidr, + Net: cidr, + IPRange: ipRange, }, nil } -func CIDRFromNet(n netaddr.IPPrefix) *CIDR { +func CIDRFromNet(n netip.Prefix) *CIDR { return &CIDR{Net: n} } -func (in CIDR) MarshalJSON() ([]byte, error) { +func (in *CIDR) MarshalJSON() ([]byte, error) { return json.Marshal(in.String()) } @@ -52,7 +56,7 @@ func (in *CIDR) UnmarshalJSON(b []byte) error { if err := json.Unmarshal(b, &stringVal); err != nil { return err } - pIP, err := netaddr.ParseIPPrefix(stringVal) + pIP, err := netip.ParsePrefix(stringVal) if err != nil { return err } @@ -61,22 +65,22 @@ func (in *CIDR) UnmarshalJSON(b []byte) error { } func (in *CIDR) MaskBits() byte { - return in.Net.IP().BitLen() + return byte(in.Net.Addr().BitLen()) } func (in *CIDR) MaskOnes() byte { - return in.Net.Bits() + return byte(in.Net.Bits()) } func (in *CIDR) MaskZeroes() byte { ones := in.Net.Bits() - bits := in.Net.IP().BitLen() - return bits - ones + bits := in.Net.Addr().BitLen() + return byte(bits - ones) } func (in *CIDR) AddressCapacity() *big.Int { ones := in.Net.Bits() - bits := in.Net.IP().BitLen() + bits := in.Net.Addr().BitLen() ac := big.Int{} ac.Exp(big.NewInt(2), big.NewInt(int64(bits-ones)), nil) @@ -91,16 +95,16 @@ func (in *CIDR) MaskCapacity() *big.Int { return count } -func (in *CIDR) ToAddressRange() (netaddr.IP, netaddr.IP) { - return in.Net.Range().From(), in.Net.Range().To() +func (in *CIDR) ToAddressRange() (netip.Addr, netip.Addr) { + return in.IPRange.From(), in.IPRange.To() } func (in *CIDR) Equal(cidr *CIDR) bool { ourOnes := in.Net.Bits() - ourBits := in.Net.IP().BitLen() + ourBits := in.Net.Addr().BitLen() theirOnes := cidr.Net.Bits() - theirBits := cidr.Net.IP().BitLen() + theirBits := cidr.Net.Addr().BitLen() firstOurIP, _ := in.ToAddressRange() firstTheirIP, _ := cidr.ToAddressRange() @@ -111,32 +115,32 @@ func (in *CIDR) Equal(cidr *CIDR) bool { func (in *CIDR) IsLeft() bool { ones := in.Net.Bits() - bits := in.Net.IP().BitLen() + bits := in.Net.Addr().BitLen() if ones == 0 { return false } return in.isLeft(ones, bits) } -func (in *CIDR) isLeft(ones, bits uint8) bool { +func (in *CIDR) isLeft(ones, bits int) bool { var ipBytes []byte - if in.Net.IP().Is4() { - ipv4 := in.Net.IP().As4() + if in.Net.Addr().Is4() { + ipv4 := in.Net.Addr().As4() ipBytes = ipv4[:] } else { - ipv6 := in.Net.IP().As16() + ipv6 := in.Net.Addr().As16() ipBytes = ipv6[:] } ipLen := len(ipBytes) bitsDiff := bits - ones - ipIdx := uint8(ipLen) - bitsDiff/8 - 1 + ipIdx := ipLen - bitsDiff/8 - 1 ipBit := bitsDiff % 8 return ipBytes[ipIdx]&(1< Date: Wed, 16 Aug 2023 15:12:59 +0200 Subject: [PATCH 2/3] missed range added --- api/v1alpha1/cidr_types.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/api/v1alpha1/cidr_types.go b/api/v1alpha1/cidr_types.go index d389fee..08d7fe4 100644 --- a/api/v1alpha1/cidr_types.go +++ b/api/v1alpha1/cidr_types.go @@ -41,7 +41,11 @@ func CIDRFromString(cidrString string) (*CIDR, error) { } func CIDRFromNet(n netip.Prefix) *CIDR { - return &CIDR{Net: n} + ipRange := netipx.RangeOfPrefix(n) + return &CIDR{ + Net: n, + IPRange: ipRange, + } } func (in *CIDR) MarshalJSON() ([]byte, error) { From 63a487b30460372315df0e9db9fae713c36b7c4d Mon Sep 17 00:00:00 2001 From: Vasilev Andrew Date: Wed, 16 Aug 2023 15:52:31 +0200 Subject: [PATCH 3/3] range calculation added --- api/v1alpha1/cidr_types.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/api/v1alpha1/cidr_types.go b/api/v1alpha1/cidr_types.go index 08d7fe4..294453c 100644 --- a/api/v1alpha1/cidr_types.go +++ b/api/v1alpha1/cidr_types.go @@ -24,8 +24,7 @@ import ( // +kubebuilder:validation:Type=string type CIDR struct { - Net netip.Prefix `json:"-"` - IPRange netipx.IPRange `json:"-"` + Net netip.Prefix `json:"-"` } func CIDRFromString(cidrString string) (*CIDR, error) { @@ -33,18 +32,14 @@ func CIDRFromString(cidrString string) (*CIDR, error) { if err != nil { return nil, err } - ipRange := netipx.RangeOfPrefix(cidr) return &CIDR{ - Net: cidr, - IPRange: ipRange, + Net: cidr, }, nil } func CIDRFromNet(n netip.Prefix) *CIDR { - ipRange := netipx.RangeOfPrefix(n) return &CIDR{ - Net: n, - IPRange: ipRange, + Net: n, } } @@ -100,7 +95,8 @@ func (in *CIDR) MaskCapacity() *big.Int { } func (in *CIDR) ToAddressRange() (netip.Addr, netip.Addr) { - return in.IPRange.From(), in.IPRange.To() + ipRange := netipx.RangeOfPrefix(in.Net) + return ipRange.From(), ipRange.To() } func (in *CIDR) Equal(cidr *CIDR) bool {