From 7d36f105463f4512118fdd13d8fa55822e3f23c4 Mon Sep 17 00:00:00 2001 From: Marvin Beckers Date: Thu, 17 Feb 2022 16:37:44 +0100 Subject: [PATCH] Improve nil pointer checks and consider VPC overlay networks Signed-off-by: Marvin Beckers --- pkg/cloudprovider/provider/nutanix/client.go | 24 ++++++++----------- .../provider/nutanix/provider.go | 4 ++++ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/pkg/cloudprovider/provider/nutanix/client.go b/pkg/cloudprovider/provider/nutanix/client.go index 760dab46b..c57a07187 100644 --- a/pkg/cloudprovider/provider/nutanix/client.go +++ b/pkg/cloudprovider/provider/nutanix/client.go @@ -232,8 +232,11 @@ func getSubnetByName(client *ClientSet, name, clusterID string) (*nutanixv3.Subn } for _, subnet := range subnets.Entities { - if *subnet.Status.Name == name && *subnet.Status.ClusterReference.UUID == clusterID { - return subnet, nil + if subnet != nil && subnet.Status != nil && subnet.Status.Name != nil && *subnet.Status.Name == name { + // some subnet types (e.g. VPC overlays) do not come with a cluster reference; we don't need to check them + if subnet.Status.ClusterReference == nil || (subnet.Status.ClusterReference.UUID != nil && *subnet.Status.ClusterReference.UUID == clusterID) { + return subnet, nil + } } } @@ -260,15 +263,7 @@ func getProjectByName(client *ClientSet, name string) (*nutanixv3.Project, error } for _, project := range projects.Entities { - if project == nil { - return nil, errors.New("project is nil") - } - - if project.Status == nil { - return nil, errors.New("project status is nil") - } - - if project.Status.Name == name { + if project != nil && project.Status != nil && project.Status.Name == name { return project, nil } } @@ -295,7 +290,7 @@ func getClusterByName(client *ClientSet, name string) (*nutanixv3.ClusterIntentR } for _, cluster := range clusters.Entities { - if *cluster.Status.Name == name { + if cluster.Status != nil && cluster.Status.Name != nil && *cluster.Status.Name == name { return cluster, nil } } @@ -322,7 +317,7 @@ func getImageByName(client *ClientSet, name string) (*nutanixv3.ImageIntentRespo } for _, image := range images.Entities { - if *image.Status.Name == name { + if image.Status != nil && image.Status.Name != nil && *image.Status.Name == name { return image, nil } } @@ -343,7 +338,8 @@ func getVMByName(client *ClientSet, name string, projectID *string) (*nutanixv3. for _, vm := range vms.Entities { if *vm.Status.Name == name { - if projectID != nil && *vm.Metadata.ProjectReference.UUID != *projectID { + if projectID != nil && vm.Metadata != nil && vm.Metadata.ProjectReference != nil && + vm.Metadata.ProjectReference.UUID != nil && *vm.Metadata.ProjectReference.UUID != *projectID { continue } return vm, nil diff --git a/pkg/cloudprovider/provider/nutanix/provider.go b/pkg/cloudprovider/provider/nutanix/provider.go index 6e2a9f2b1..5ad6b0212 100644 --- a/pkg/cloudprovider/provider/nutanix/provider.go +++ b/pkg/cloudprovider/provider/nutanix/provider.go @@ -315,6 +315,10 @@ func (p *provider) cleanup(machine *clusterv1alpha1.Machine, data *cloudprovider return false, err } + if vm.Metadata == nil || vm.Metadata.UUID == nil { + return false, fmt.Errorf("failed to get valid VM metadata for machine '%s'", machine.Name) + } + // TODO: figure out if VM is already in deleting state resp, err := client.Prism.V3.DeleteVM(*vm.Metadata.UUID)