Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(controller): Use v3 VM VG Reference for v4 detach VG From VM #473

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
prismclientv4 "github.com/nutanix-cloud-native/prism-go-client/v4"
prismcommonconfig "github.com/nutanix/ntnx-api-golang-clients/prism-go-client/v4/models/common/v1/config"
prismapi "github.com/nutanix/ntnx-api-golang-clients/prism-go-client/v4/models/prism/v4/config"
vmconfig "github.com/nutanix/ntnx-api-golang-clients/vmm-go-client/v4/models/vmm/v4/ahv/config"
prismconfig "github.com/nutanix/ntnx-api-golang-clients/volumes-go-client/v4/models/prism/v4/config"
volumesconfig "github.com/nutanix/ntnx-api-golang-clients/volumes-go-client/v4/models/volumes/v4/config"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -878,38 +877,26 @@
return *pcInfo.Resources.Version, nil
}

func detachVolumeGroupsFromVM(ctx context.Context, v4Client *prismclientv4.Client, vmUUID string) error {
func detachVolumeGroupsFromVM(ctx context.Context, v4Client *prismclientv4.Client, vm *prismclientv3.VMIntentResponse) error {
log := ctrl.LoggerFrom(ctx)
vmResp, err := v4Client.VmApiInstance.GetVmById(&vmUUID)
if err != nil {
return fmt.Errorf("failed to get virtual machine: %w", err)
}

data := vmResp.GetData()
vm, ok := data.(vmconfig.Vm)
if !ok {
return fmt.Errorf("failed to cast response to VM")
}

volumeGroupsToDetach := make([]string, 0)
for _, disk := range vm.Disks {
backingInfo, ok := disk.GetBackingInfo().(vmconfig.ADSFVolumeGroupReference)
if !ok {
for _, disk := range vm.Spec.Resources.DiskList {
if disk.VolumeGroupReference == nil {

Check warning on line 884 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L883-L884

Added lines #L883 - L884 were not covered by tests
continue
}

volumeGroupsToDetach = append(volumeGroupsToDetach, *backingInfo.VolumeGroupExtId)
volumeGroupsToDetach = append(volumeGroupsToDetach, *disk.VolumeGroupReference.UUID)

Check warning on line 888 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L888

Added line #L888 was not covered by tests
}

// Detach the volume groups from the virtual machine
for _, volumeGroup := range volumeGroupsToDetach {
log.Info(fmt.Sprintf("detaching volume group %s from virtual machine %s", volumeGroup, vmUUID))
log.Info(fmt.Sprintf("detaching volume group %s from virtual machine %s", volumeGroup, *vm.Status.Name))

Check warning on line 893 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L893

Added line #L893 was not covered by tests
body := &volumesconfig.VmAttachment{
ExtId: &vmUUID,
ExtId: vm.Metadata.UUID,
}
resp, err := v4Client.VolumeGroupsApiInstance.DetachVm(&volumeGroup, body)
if err != nil {
return fmt.Errorf("failed to detach volume group %s from virtual machine %s: %w", volumeGroup, vmUUID, err)
return fmt.Errorf("failed to detach volume group %s from virtual machine %s: %w", volumeGroup, *vm.Metadata.UUID, err)

Check warning on line 899 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L899

Added line #L899 was not covered by tests
}

data := resp.GetData()
Expand Down
8 changes: 4 additions & 4 deletions controllers/nutanixmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@
}

if vgDetachNeeded {
if err := r.detachVolumeGroups(rctx, vmUUID); err != nil {
if err := r.detachVolumeGroups(rctx, vm); err != nil {

Check warning on line 382 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L382

Added line #L382 was not covered by tests
err := fmt.Errorf("failed to detach volume groups from VM %s with UUID %s: %v", vmName, vmUUID, err)
log.Error(err, "failed to detach volume groups from VM")
conditions.MarkFalse(rctx.NutanixMachine, infrav1.VMProvisionedCondition, infrav1.VolumeGroupDetachFailed, capiv1.ConditionSeverityWarning, err.Error())
Expand All @@ -401,7 +401,7 @@
return reconcile.Result{RequeueAfter: 5 * time.Second}, nil
}

func (r *NutanixMachineReconciler) detachVolumeGroups(rctx *nctx.MachineContext, vmUUID string) error {
func (r *NutanixMachineReconciler) detachVolumeGroups(rctx *nctx.MachineContext, vm *prismclientv3.VMIntentResponse) error {
createV4Client, err := isPrismCentralV4Compatible(rctx.Context, rctx.NutanixClient)
if err != nil {
return fmt.Errorf("error occurred while checking compatibility for Prism Central v4 APIs: %w", err)
Expand All @@ -416,8 +416,8 @@
return fmt.Errorf("error occurred while fetching Prism Central v4 client: %w", err)
}

if err := detachVolumeGroupsFromVM(rctx.Context, v4Client, vmUUID); err != nil {
return fmt.Errorf("failed to detach volume groups from VM %s with UUID %s: %w", rctx.Machine.Name, vmUUID, err)
if err := detachVolumeGroupsFromVM(rctx.Context, v4Client, vm); err != nil {
return fmt.Errorf("failed to detach volume groups from VM %s with UUID %s: %w", rctx.Machine.Name, *vm.Metadata.UUID, err)

Check warning on line 420 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L419-L420

Added lines #L419 - L420 were not covered by tests
}

return nil
Expand Down
42 changes: 31 additions & 11 deletions controllers/nutanixmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

"github.com/golang/mock/gomock"
credentialTypes "github.com/nutanix-cloud-native/prism-go-client/environment/credentials"
v3 "github.com/nutanix-cloud-native/prism-go-client/v3"
prismclientv3 "github.com/nutanix-cloud-native/prism-go-client/v3"
"github.com/nutanix-cloud-native/prism-go-client/v3/models"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -246,12 +246,16 @@ func TestNutanixMachineReconciler(t *testing.T) {
mockv3Service := mocknutanixv3.NewMockService(mockctrl)
mockv3Service.EXPECT().GetPrismCentral(gomock.Any()).Return(nil, errors.New("error"))

v3Client := &v3.Client{
v3Client := &prismclientv3.Client{
V3: mockv3Service,
}
err := reconciler.detachVolumeGroups(&nctx.MachineContext{
NutanixClient: v3Client,
}, ntnxMachine.Status.VmUUID)
}, &prismclientv3.VMIntentResponse{
Metadata: &prismclientv3.Metadata{
UUID: &ntnxMachine.Status.VmUUID,
},
})
g.Expect(err).To(HaveOccurred())
})

Expand All @@ -262,12 +266,16 @@ func TestNutanixMachineReconciler(t *testing.T) {
Version: ptr.To(""),
}}, nil)

v3Client := &v3.Client{
v3Client := &prismclientv3.Client{
V3: mockv3Service,
}
err := reconciler.detachVolumeGroups(&nctx.MachineContext{
NutanixClient: v3Client,
}, ntnxMachine.Status.VmUUID)
}, &prismclientv3.VMIntentResponse{
Metadata: &prismclientv3.Metadata{
UUID: &ntnxMachine.Status.VmUUID,
},
})
g.Expect(err).To(HaveOccurred())
})

Expand All @@ -278,12 +286,16 @@ func TestNutanixMachineReconciler(t *testing.T) {
Version: ptr.To("pc."),
}}, nil)

v3Client := &v3.Client{
v3Client := &prismclientv3.Client{
V3: mockv3Service,
}
err := reconciler.detachVolumeGroups(&nctx.MachineContext{
NutanixClient: v3Client,
}, ntnxMachine.Status.VmUUID)
}, &prismclientv3.VMIntentResponse{
Metadata: &prismclientv3.Metadata{
UUID: &ntnxMachine.Status.VmUUID,
},
})
g.Expect(err).To(HaveOccurred())
})

Expand All @@ -294,12 +306,16 @@ func TestNutanixMachineReconciler(t *testing.T) {
Version: ptr.To("not.a.valid.version"),
}}, nil)

v3Client := &v3.Client{
v3Client := &prismclientv3.Client{
V3: mockv3Service,
}
err := reconciler.detachVolumeGroups(&nctx.MachineContext{
NutanixClient: v3Client,
}, ntnxMachine.Status.VmUUID)
}, &prismclientv3.VMIntentResponse{
Metadata: &prismclientv3.Metadata{
UUID: &ntnxMachine.Status.VmUUID,
},
})
g.Expect(err).To(HaveOccurred())
})

Expand All @@ -310,12 +326,16 @@ func TestNutanixMachineReconciler(t *testing.T) {
Version: ptr.To("pc.2023.4.0.1"),
}}, nil)

v3Client := &v3.Client{
v3Client := &prismclientv3.Client{
V3: mockv3Service,
}
err := reconciler.detachVolumeGroups(&nctx.MachineContext{
NutanixClient: v3Client,
}, ntnxMachine.Status.VmUUID)
}, &prismclientv3.VMIntentResponse{
Metadata: &prismclientv3.Metadata{
UUID: &ntnxMachine.Status.VmUUID,
},
})
g.Expect(err).To(Not(HaveOccurred()))
})
})
Expand Down
Loading