From fc469ef7d564ec060af667a4c8b3af424b932aec Mon Sep 17 00:00:00 2001 From: maelk Date: Fri, 12 Jun 2020 16:47:04 +0300 Subject: [PATCH] Trim trailing - in IPAddress object names This fixes a bug when the ip address ends with 0 in ipv6, leading to a name ending with "--". Those are now trimed --- ipam/ippool_manager.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/ipam/ippool_manager.go b/ipam/ippool_manager.go index ccba50df..f5971a36 100644 --- a/ipam/ippool_manager.go +++ b/ipam/ippool_manager.go @@ -278,11 +278,8 @@ func (m *IPPoolManager) createAddress(ctx context.Context, } if allocatedAddress, ok := m.IPPool.Status.Allocations[addressClaim.Name]; ok { - formatedAddress := strings.Replace( - strings.Replace(string(allocatedAddress), ":", "-", -1), ".", "-", -1, - ) addressClaim.Status.Address = &corev1.ObjectReference{ - Name: m.IPPool.Spec.NamePrefix + "-" + formatedAddress, + Name: m.formatAddressName(allocatedAddress), Namespace: m.IPPool.Namespace, } return addresses, nil @@ -295,12 +292,9 @@ func (m *IPPoolManager) createAddress(ctx context.Context, if err != nil { return addresses, err } - formatedAddress := strings.Replace( - strings.Replace(string(allocatedAddress), ":", "-", -1), ".", "-", -1, - ) // Set the index and IPAddress names - addressName := m.IPPool.Spec.NamePrefix + "-" + formatedAddress + addressName := m.formatAddressName(allocatedAddress) m.Log.Info("Address allocated", "Claim", addressClaim.Name, "address", allocatedAddress) @@ -380,11 +374,8 @@ func (m *IPPoolManager) deleteAddress(ctx context.Context, if ok { // Try to get the IPAddress. if it succeeds, delete it tmpM3Data := &ipamv1.IPAddress{} - formatedAddress := strings.Replace( - strings.Replace(string(allocatedAddress), ":", "-", -1), ".", "-", -1, - ) key := client.ObjectKey{ - Name: m.IPPool.Spec.NamePrefix + "-" + formatedAddress, + Name: m.formatAddressName(allocatedAddress), Namespace: m.IPPool.Namespace, } err := m.client.Get(ctx, key, tmpM3Data) @@ -518,3 +509,10 @@ func addOffsetToIP(ip, endIP net.IP, offset int) (net.IP, error) { copy(ip[16-IPBytesLen:], IPBytes) return ip, nil } + +// formatAddressName renders the name of the IPAddress objects +func (m *IPPoolManager) formatAddressName(address ipamv1.IPAddressStr) string { + return strings.TrimRight(m.IPPool.Spec.NamePrefix+"-"+strings.Replace( + strings.Replace(string(address), ":", "-", -1), ".", "-", -1, + ), "-") +}