Skip to content

Commit

Permalink
Fixing nits
Browse files Browse the repository at this point in the history
  • Loading branch information
krunaljain committed Aug 1, 2018
1 parent 95a2f1b commit b726b99
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
3 changes: 1 addition & 2 deletions pkg/csi_driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ func (s *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolu
} else {
// If we are creating a new instance, we need to evaluate the reservedIPRange range if reserved-ipv4-cidr is provided
// If the param was not provided, we default reservedIPRange to "" and cloud provider takes care of the allocation
reservedIPV4CIDR, ok := req.GetParameters()[paramReservedIPV4CIDR]
if ok {
if reservedIPV4CIDR, ok := req.GetParameters()[paramReservedIPV4CIDR]; ok {
validCIDR := s.config.ipAllocator.ValidateCIDR(reservedIPV4CIDR)
if !validCIDR {
return nil, fmt.Errorf("The provided reserved-ipv4-cidr %s evaluated to be invalid after parsing", reservedIPV4CIDR)
Expand Down
15 changes: 8 additions & 7 deletions pkg/util/ip_reservation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import (
)

const (
incrementStep29IPBlock = byte(8)
incrementStep29IPBlock = 8
reservationBlockSize = "/29"
byteMax = 255
)

// IPAllocator struct consists of shared resources that are used to keep track of the /29 IPRanges currently reserved by service instances
Expand All @@ -45,7 +46,7 @@ func NewIPAllocator(hold map[string]bool) *IPAllocator {
}
}

// holdIPV4Range function adds a particular IP range in the hold set
// holdIPV4Range adds a particular IP range in the hold set
// Argument ipBlock string is an IPV4 range which needs put on hold
func (ipAllocator *IPAllocator) holdIPV4Range(ipV4Range string) {
ipAllocator.hold[ipV4Range] = true
Expand Down Expand Up @@ -134,24 +135,24 @@ func isOverlap(cidr1 string, cidr2 string) (bool, error) {
return ipnet1.Contains(ipnet2.IP) || ipnet2.Contains(ipnet1.IP), nil
}

// Increment the given IP value by the provided step. The step is a byte with maximum value 255
// Increment the given IP value by the provided step. The step is a byte with maximum value maximum byte value
func incrementIP(ip net.IP, step byte) (net.IP, error) {
incrementedIP := cloneIP(ip)
incrementedIP = incrementedIP.To4()

// Step can be added directly to the Least Significant Byte and we can return the result
if incrementedIP[3] < 255-step {
if incrementedIP[3] < byteMax-step {
incrementedIP[3] += step
return incrementedIP, nil
}

// Step addition in the Least Significant Byte resulted in overflow
// Propogating the carry addition to the higher order bytes and calculating value of the current byte
incrementedIP[3] = incrementedIP[3] - 255 + step - 1
incrementedIP[3] = incrementedIP[3] - byteMax + step - 1

for ipByte := 2; ipByte >= 0; ipByte-- {
// Rollover occurs when value changes from 255 to 0 as maximum propagated carry is 1
if incrementedIP[ipByte] != 255 {
// Rollover occurs when value changes from maximum byte value to 0 as maximum propagated carry is 1
if incrementedIP[ipByte] != byteMax {
incrementedIP[ipByte]++
return incrementedIP, nil
}
Expand Down

0 comments on commit b726b99

Please sign in to comment.