Skip to content

Commit 03c9e2d

Browse files
committed
Add checks to prevent potential panics
- Use two-return type assertions to check we have correct type - Check for a variable not being nil before dereferencing - Check for map item existence before accessing - Fix log message grammar This shouldn't be a problem unless there is a change in how inventory secrets look, but this is still an improvement, which doesn't hurt anything. Generated-By: cursor with claude-4-sonnet model
1 parent 1c10700 commit 03c9e2d

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

controllers/metricstorage_controller.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,11 +1131,23 @@ func getComputeNodesConnectionInfo(
11311131
if err != nil {
11321132
return []ConnectionInfo{}, err
11331133
}
1134-
nodeSetGroup := inventory.Groups[secret.Labels["openstackdataplanenodeset"]]
1134+
nodeSetName, exists := secret.Labels["openstackdataplanenodeset"]
1135+
if !exists {
1136+
continue // Skip this secret if it doesn't have the required label
1137+
}
1138+
nodeSetGroup, exists := inventory.Groups[nodeSetName]
1139+
if !exists {
1140+
continue // Skip if the group doesn't exist in inventory
1141+
}
11351142
containsTargetService := false
1136-
for _, svc := range nodeSetGroup.Vars["edpm_services"].([]interface{}) {
1137-
if svc.(string) == telemetryServiceName {
1138-
containsTargetService = true
1143+
if services, ok := nodeSetGroup.Vars["edpm_services"].([]interface{}); ok {
1144+
for _, svc := range services {
1145+
if svcStr, ok := svc.(string); ok {
1146+
if svcStr == telemetryServiceName {
1147+
containsTargetService = true
1148+
break
1149+
}
1150+
}
11391151
}
11401152
}
11411153
if !containsTargetService {
@@ -1209,7 +1221,11 @@ func getAddressFromIPSet(
12091221
item *ansible.Host,
12101222
helper *helper.Helper,
12111223
) (string, discoveryv1.AddressType) {
1212-
ansibleHost := item.Vars["ansible_host"].(string)
1224+
ansibleHost, ok := item.Vars["ansible_host"].(string)
1225+
if !ok {
1226+
helper.GetLogger().Info("ansible_host is not a string or is missing")
1227+
return "", ""
1228+
}
12131229
canonicalHostname, _ := getCanonicalHostname(item)
12141230
ctlplaneDNSDomain := ""
12151231

@@ -1242,15 +1258,15 @@ func getAddressFromIPSet(
12421258
return ansibleHost, discoveryv1.AddressTypeFQDN
12431259
}
12441260
// No IP address or valid hostname found anywhere
1245-
helper.GetLogger().Info("Did not found a valid hostname or IP address")
1261+
helper.GetLogger().Info("Did not find a valid hostname or IP address")
12461262
return "", ""
12471263
}
12481264
}
12491265
// check that the reservations list is not empty
12501266
if len(ipset.Status.Reservation) > 0 {
12511267
// search for the network specified in the Spec
12521268
for _, reservation := range ipset.Status.Reservation {
1253-
if reservation.Network == *instance.Spec.DataplaneNetwork {
1269+
if instance.Spec.DataplaneNetwork != nil && reservation.Network == *instance.Spec.DataplaneNetwork {
12541270
return reservation.Address, discoveryv1.AddressTypeIPv4
12551271
}
12561272
}
@@ -1260,7 +1276,10 @@ func getAddressFromIPSet(
12601276
}
12611277

12621278
func getAddressFromAnsibleHost(item *ansible.Host) (string, discoveryv1.AddressType) {
1263-
ansibleHost := item.Vars["ansible_host"].(string)
1279+
ansibleHost, ok := item.Vars["ansible_host"].(string)
1280+
if !ok {
1281+
return "", ""
1282+
}
12641283
// check if ansiblehost is an IP
12651284
addr := net.ParseIP(ansibleHost)
12661285
if addr != nil {

0 commit comments

Comments
 (0)