Skip to content

Commit b9f902b

Browse files
Merge pull request #7 from Nordix/bugfix-mael
🐛 Trim trailing - in IPAddress object names
2 parents 2da124b + fc469ef commit b9f902b

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

ipam/ippool_manager.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,8 @@ func (m *IPPoolManager) createAddress(ctx context.Context,
278278
}
279279

280280
if allocatedAddress, ok := m.IPPool.Status.Allocations[addressClaim.Name]; ok {
281-
formatedAddress := strings.Replace(
282-
strings.Replace(string(allocatedAddress), ":", "-", -1), ".", "-", -1,
283-
)
284281
addressClaim.Status.Address = &corev1.ObjectReference{
285-
Name: m.IPPool.Spec.NamePrefix + "-" + formatedAddress,
282+
Name: m.formatAddressName(allocatedAddress),
286283
Namespace: m.IPPool.Namespace,
287284
}
288285
return addresses, nil
@@ -295,12 +292,9 @@ func (m *IPPoolManager) createAddress(ctx context.Context,
295292
if err != nil {
296293
return addresses, err
297294
}
298-
formatedAddress := strings.Replace(
299-
strings.Replace(string(allocatedAddress), ":", "-", -1), ".", "-", -1,
300-
)
301295

302296
// Set the index and IPAddress names
303-
addressName := m.IPPool.Spec.NamePrefix + "-" + formatedAddress
297+
addressName := m.formatAddressName(allocatedAddress)
304298

305299
m.Log.Info("Address allocated", "Claim", addressClaim.Name, "address", allocatedAddress)
306300

@@ -380,11 +374,8 @@ func (m *IPPoolManager) deleteAddress(ctx context.Context,
380374
if ok {
381375
// Try to get the IPAddress. if it succeeds, delete it
382376
tmpM3Data := &ipamv1.IPAddress{}
383-
formatedAddress := strings.Replace(
384-
strings.Replace(string(allocatedAddress), ":", "-", -1), ".", "-", -1,
385-
)
386377
key := client.ObjectKey{
387-
Name: m.IPPool.Spec.NamePrefix + "-" + formatedAddress,
378+
Name: m.formatAddressName(allocatedAddress),
388379
Namespace: m.IPPool.Namespace,
389380
}
390381
err := m.client.Get(ctx, key, tmpM3Data)
@@ -518,3 +509,10 @@ func addOffsetToIP(ip, endIP net.IP, offset int) (net.IP, error) {
518509
copy(ip[16-IPBytesLen:], IPBytes)
519510
return ip, nil
520511
}
512+
513+
// formatAddressName renders the name of the IPAddress objects
514+
func (m *IPPoolManager) formatAddressName(address ipamv1.IPAddressStr) string {
515+
return strings.TrimRight(m.IPPool.Spec.NamePrefix+"-"+strings.Replace(
516+
strings.Replace(string(address), ":", "-", -1), ".", "-", -1,
517+
), "-")
518+
}

0 commit comments

Comments
 (0)