Skip to content

Commit 2b93e00

Browse files
authored
Improve nil pointer checks and consider VPC overlay networks (#1193)
Signed-off-by: Marvin Beckers <[email protected]>
1 parent c713902 commit 2b93e00

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

pkg/cloudprovider/provider/nutanix/client.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,11 @@ func getSubnetByName(client *ClientSet, name, clusterID string) (*nutanixv3.Subn
232232
}
233233

234234
for _, subnet := range subnets.Entities {
235-
if *subnet.Status.Name == name && *subnet.Status.ClusterReference.UUID == clusterID {
236-
return subnet, nil
235+
if subnet != nil && subnet.Status != nil && subnet.Status.Name != nil && *subnet.Status.Name == name {
236+
// some subnet types (e.g. VPC overlays) do not come with a cluster reference; we don't need to check them
237+
if subnet.Status.ClusterReference == nil || (subnet.Status.ClusterReference.UUID != nil && *subnet.Status.ClusterReference.UUID == clusterID) {
238+
return subnet, nil
239+
}
237240
}
238241
}
239242

@@ -260,15 +263,7 @@ func getProjectByName(client *ClientSet, name string) (*nutanixv3.Project, error
260263
}
261264

262265
for _, project := range projects.Entities {
263-
if project == nil {
264-
return nil, errors.New("project is nil")
265-
}
266-
267-
if project.Status == nil {
268-
return nil, errors.New("project status is nil")
269-
}
270-
271-
if project.Status.Name == name {
266+
if project != nil && project.Status != nil && project.Status.Name == name {
272267
return project, nil
273268
}
274269
}
@@ -295,7 +290,7 @@ func getClusterByName(client *ClientSet, name string) (*nutanixv3.ClusterIntentR
295290
}
296291

297292
for _, cluster := range clusters.Entities {
298-
if *cluster.Status.Name == name {
293+
if cluster.Status != nil && cluster.Status.Name != nil && *cluster.Status.Name == name {
299294
return cluster, nil
300295
}
301296
}
@@ -322,7 +317,7 @@ func getImageByName(client *ClientSet, name string) (*nutanixv3.ImageIntentRespo
322317
}
323318

324319
for _, image := range images.Entities {
325-
if *image.Status.Name == name {
320+
if image.Status != nil && image.Status.Name != nil && *image.Status.Name == name {
326321
return image, nil
327322
}
328323
}
@@ -343,7 +338,8 @@ func getVMByName(client *ClientSet, name string, projectID *string) (*nutanixv3.
343338

344339
for _, vm := range vms.Entities {
345340
if *vm.Status.Name == name {
346-
if projectID != nil && *vm.Metadata.ProjectReference.UUID != *projectID {
341+
if projectID != nil && vm.Metadata != nil && vm.Metadata.ProjectReference != nil &&
342+
vm.Metadata.ProjectReference.UUID != nil && *vm.Metadata.ProjectReference.UUID != *projectID {
347343
continue
348344
}
349345
return vm, nil

pkg/cloudprovider/provider/nutanix/provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ func (p *provider) cleanup(machine *clusterv1alpha1.Machine, data *cloudprovider
315315
return false, err
316316
}
317317

318+
if vm.Metadata == nil || vm.Metadata.UUID == nil {
319+
return false, fmt.Errorf("failed to get valid VM metadata for machine '%s'", machine.Name)
320+
}
321+
318322
// TODO: figure out if VM is already in deleting state
319323

320324
resp, err := client.Prism.V3.DeleteVM(*vm.Metadata.UUID)

0 commit comments

Comments
 (0)