Skip to content

Commit e49d03f

Browse files
committed
Fix some minor issues in ipam.go
- Fix flawed conditional in createOrPatchDNSData() - Rmove nested for loop when deleting ipsets - break when ctlplane network is found in reserveIPs() - Fix SA1006 and other staticcheck linter issues Signed-off-by: rabi <[email protected]>
1 parent aaf6236 commit e49d03f

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

pkg/dataplane/ipam.go

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ func createOrPatchDNSData(ctx context.Context, helper *helper.Helper,
107107
allIPSets map[string]infranetworkv1.IPSet, dnsDetails *DNSDetails,
108108
) error {
109109
var allDNSRecords []infranetworkv1.DNSHost
110-
var ctlplaneSearchDomain string
111110
dnsDetails.Hostnames = map[string]map[infranetworkv1.NetNameStr]string{}
112111
dnsDetails.AllIPs = map[string]map[infranetworkv1.NetNameStr]string{}
113112

@@ -155,9 +154,8 @@ func createOrPatchDNSData(ctx context.Context, helper *helper.Helper,
155154
dnsDetails.AllIPs[hostName][infranetworkv1.NetNameStr(netLower)] = res.Address
156155
dnsRecord.Hostnames = fqdnNames
157156
allDNSRecords = append(allDNSRecords, dnsRecord)
158-
// Adding only ctlplane domain for ansibleee.
159-
// TODO (rabi) This is not very efficient.
160-
if netLower == dataplanev1.CtlPlaneNetwork && ctlplaneSearchDomain == "" {
157+
158+
if dnsDetails.CtlplaneSearchDomain == "" && netLower == dataplanev1.CtlPlaneNetwork {
161159
dnsDetails.CtlplaneSearchDomain = res.DNSDomain
162160
}
163161
}
@@ -309,27 +307,19 @@ func cleanupStaleReservations(ctx context.Context, helper *helper.Helper,
309307
return err
310308
}
311309

312-
ipSetsToRemove := []infranetworkv1.IPSet{}
313-
// Delete all IPSet for nodes that are not in nodeset
310+
currentNodes := make(map[string]bool)
311+
for _, node := range instance.Spec.Nodes {
312+
currentNodes[node.HostName] = true
313+
}
314+
314315
for _, ipSet := range ipSetList.Items {
315-
found := false
316-
for _, node := range instance.Spec.Nodes {
317-
if ipSet.Name == node.HostName {
318-
found = true
319-
break
316+
if _, ok := currentNodes[ipSet.Name]; !ok {
317+
if err := helper.GetClient().Delete(ctx, &ipSet); err != nil {
318+
return err
320319
}
321320
}
322-
if !found {
323-
ipSetsToRemove = append(ipSetsToRemove, ipSet)
324-
}
325-
}
326-
for _, ipSet := range ipSetsToRemove {
327-
if err := helper.GetClient().Delete(ctx, &ipSet); err != nil {
328-
return err
329-
}
330321
}
331322
return nil
332-
333323
}
334324

335325
// reserveIPs Reserves IPs by creating IPSets
@@ -348,41 +338,41 @@ func reserveIPs(ctx context.Context, helper *helper.Helper,
348338
if len(netConfigList.Items) == 0 {
349339
errMsg := "no NetConfig CR exists yet"
350340
util.LogForObject(helper, errMsg, instance)
351-
return nil, nil, fmt.Errorf(errMsg)
341+
return nil, nil, fmt.Errorf("%s", errMsg)
352342
}
353343
netServiceNetMap := BuildNetServiceNetMap(netConfigList.Items[0])
354344
allIPSets := make(map[string]infranetworkv1.IPSet)
355345

356346
// CreateOrPatch IPSets
357347
for nodeName, node := range instance.Spec.Nodes {
358348
foundCtlPlane := false
359-
nets := node.Networks
360-
hostName := node.HostName
361-
if len(nets) == 0 {
362-
nets = instance.Spec.NodeTemplate.Networks
349+
if len(node.Networks) == 0 {
350+
node.Networks = instance.Spec.NodeTemplate.Networks
363351
}
364352

365-
if len(nets) > 0 {
366-
for _, net := range nets {
353+
if len(node.Networks) > 0 {
354+
for _, net := range node.Networks {
367355
if strings.EqualFold(string(net.Name), dataplanev1.CtlPlaneNetwork) ||
368356
netServiceNetMap[strings.ToLower(string(net.Name))] == dataplanev1.CtlPlaneNetwork {
369357
foundCtlPlane = true
358+
break
370359
}
371360
}
372361
if !foundCtlPlane {
373-
msg := fmt.Sprintf("ctlplane network should be defined for node %s", nodeName)
374-
return nil, netServiceNetMap, fmt.Errorf(msg)
362+
return nil, netServiceNetMap,
363+
fmt.Errorf("ctlplane network should be defined for node %s", nodeName)
375364
}
376365
ipSet := &infranetworkv1.IPSet{
377366
ObjectMeta: metav1.ObjectMeta{
378367
Namespace: instance.Namespace,
379-
Name: hostName,
368+
Name: node.HostName,
380369
},
381370
}
382371
_, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), ipSet, func() error {
383-
ownerLabels := labels.GetLabels(instance, labels.GetGroupLabel(NodeSetLabel), map[string]string{})
372+
ownerLabels := labels.GetLabels(
373+
instance, labels.GetGroupLabel(NodeSetLabel), map[string]string{})
384374
ipSet.Labels = util.MergeStringMaps(ipSet.GetLabels(), ownerLabels)
385-
ipSet.Spec.Networks = nets
375+
ipSet.Spec.Networks = node.Networks
386376
// Set controller reference to the DataPlaneNode object
387377
err := controllerutil.SetControllerReference(
388378
helper.GetBeforeObject(), ipSet, helper.GetScheme())
@@ -391,11 +381,11 @@ func reserveIPs(ctx context.Context, helper *helper.Helper,
391381
if err != nil {
392382
return nil, netServiceNetMap, err
393383
}
394-
allIPSets[hostName] = *ipSet
384+
allIPSets[node.HostName] = *ipSet
395385
} else {
396386
msg := fmt.Sprintf("No Networks defined for node %s or template", nodeName)
397387
util.LogForObject(helper, msg, instance)
398-
return nil, netServiceNetMap, fmt.Errorf(msg)
388+
return nil, netServiceNetMap, fmt.Errorf("%s", msg)
399389
}
400390
}
401391
return allIPSets, netServiceNetMap, nil

0 commit comments

Comments
 (0)