Skip to content

Commit aabf9a7

Browse files
authored
Some minor fixes for batchattach API (#3740)
1 parent 7a6acd9 commit aabf9a7

File tree

5 files changed

+101
-59
lines changed

5 files changed

+101
-59
lines changed

pkg/apis/cnsoperator/cnsnodevmbatchattachment/v1alpha1/cnsnodebatchvmattachment_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ type PersistentVolumeClaimStatus struct {
138138
// +kubebuilder:subresource:status
139139
// +kubebuilder:object:root=true
140140
// +kubebuilder:resource:shortName=batchattach
141-
// +kubebuilder:printcolumn:name="NodeUUID",type="string",JSONPath=".spec.nodeUUID"
141+
// +kubebuilder:printcolumn:name="InstanceUUID",type="string",JSONPath=".spec.instanceUUID"
142142

143143
// CnsNodeVMBatchAttachment is the Schema for the cnsnodevmbatchattachments API
144144
type CnsNodeVMBatchAttachment struct {

pkg/apis/cnsoperator/config/cns.vmware.com_cnsnodevmbatchattachments.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ spec:
1717
scope: Namespaced
1818
versions:
1919
- additionalPrinterColumns:
20-
- jsonPath: .spec.nodeUUID
21-
name: NodeUUID
20+
- jsonPath: .spec.instanceUUID
21+
name: InstanceUUID
2222
type: string
2323
name: v1alpha1
2424
schema:

pkg/syncer/cnsoperator/controller/cnsnodevmbatchattachment/cnsnodevmbatchattachment_controller.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,8 @@ func (r *Reconciler) Reconcile(ctx context.Context,
305305

306306
patchErr := removeFinalizerFromCRDInstance(batchAttachCtx, instance, r.client)
307307
if patchErr != nil {
308-
recordEvent(batchAttachCtx, r, instance, v1.EventTypeWarning, patchErr.Error())
309308
log.Errorf("failed to update CnsNodeVMBatchAttachment %s. Err: +%v", instance.Name, patchErr)
310-
return reconcile.Result{RequeueAfter: timeout}, nil
309+
return r.completeReconciliationWithError(batchAttachCtx, instance, request.NamespacedName, timeout, err)
311310
}
312311
log.Infof("Successfully removed finalizer %s from instance %s",
313312
cnsoperatortypes.CNSFinalizer, request.NamespacedName.String())
@@ -316,10 +315,7 @@ func (r *Reconciler) Reconcile(ctx context.Context,
316315
delete(backOffDuration, request.NamespacedName)
317316
backOffDurationMapMutex.Unlock()
318317

319-
msg := fmt.Sprintf("ReconcileCnsNodeVMBatchAttachment: Successfully processed instance %s",
320-
request.NamespacedName.String())
321-
recordEvent(batchAttachCtx, r, instance, v1.EventTypeNormal, msg)
322-
return reconcile.Result{}, nil
318+
return r.completeReconciliationWithSuccess(batchAttachCtx, instance, request.NamespacedName, timeout)
323319
}
324320

325321
// The CR is not being deleted, so call attach and detach for volumes.

pkg/syncer/cnsoperator/controller/cnsnodevmbatchattachment/cnsnodevmbatchattachment_helper.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ func updatePvcStatusEntryName(ctx context.Context,
252252
if _, ok := pvcsToDetach[volume.PersistentVolumeClaim.ClaimName]; !ok {
253253
continue
254254
}
255+
if strings.HasSuffix(instance.Status.VolumeStatus[i].Name, detachSuffix) {
256+
log.Infof("VolumeName %s for PVC %s already contains suffix %s. Skipping.",
257+
instance.Status.VolumeStatus[i].Name, volume.PersistentVolumeClaim.ClaimName,
258+
detachSuffix)
259+
continue
260+
}
255261
newVolumeName := instance.Status.VolumeStatus[i].Name + detachSuffix
256262
instance.Status.VolumeStatus[i].Name = newVolumeName
257263
log.Infof("Updating status name entry to %s for detaching PVC %s",

pkg/syncer/cnsoperator/controller/cnsnodevmbatchattachment/cnsnodevmbatchattachment_test.go

Lines changed: 90 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/stretchr/testify/assert"
2727
"github.com/vmware/govmomi/object"
2828
v1 "k8s.io/api/core/v1"
29-
"k8s.io/apimachinery/pkg/api/errors"
3029
"k8s.io/apimachinery/pkg/api/resource"
3130
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3231
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -233,55 +232,6 @@ func TestCnsNodeVMBatchAttachmentWhenVmOnVcenterReturnsError(t *testing.T) {
233232
})
234233
}
235234

236-
func TestCnsNodeVMBatchAttachmentWhenVmOnVcenterReturnsNotFoundError(t *testing.T) {
237-
238-
t.Run("TestCnsNodeVMBatchAttachmentWhenVmOnVcenterReturnsNotFoundError", func(t *testing.T) {
239-
testCnsNodeVMBatchAttachment := setupTestCnsNodeVMBatchAttachment()
240-
testCnsNodeVMBatchAttachment.Spec.InstanceUUID = "test-3"
241-
242-
r := setTestEnvironment(&testCnsNodeVMBatchAttachment, true)
243-
244-
req := reconcile.Request{
245-
NamespacedName: types.NamespacedName{
246-
Name: testCnsNodeVMBatchAttachmentName,
247-
Namespace: testNamespace,
248-
},
249-
}
250-
251-
GetVMFromVcenter = MockGetVMFromVcenter
252-
commonco.ContainerOrchestratorUtility = &unittestcommon.FakeK8SOrchestrator{}
253-
254-
// Override with fake client
255-
newClientFunc = func(ctx context.Context) (kubernetes.Interface, error) {
256-
fakeK8sClient := getClientSetWithPvc()
257-
return fakeK8sClient, nil
258-
}
259-
260-
res, err := r.Reconcile(context.TODO(), req)
261-
if err != nil {
262-
t.Fatal("Unexpected reconcile error")
263-
}
264-
expectedReconcileResult := reconcile.Result{}
265-
var expectedReconcileError error
266-
267-
assert.Equal(t, expectedReconcileResult, res)
268-
assert.Equal(t, expectedReconcileError, err)
269-
270-
updatedCnsNodeVMBatchAttachment := &v1alpha1.CnsNodeVMBatchAttachment{}
271-
err = r.client.Get(context.TODO(), req.NamespacedName, updatedCnsNodeVMBatchAttachment)
272-
if err == nil {
273-
t.Fatalf("failed to get cnsnodevmbatchattachemnt instance")
274-
}
275-
276-
// Error should be not found
277-
if statusErr, ok := err.(*errors.StatusError); ok {
278-
assert.Equal(t, metav1.StatusReasonNotFound, statusErr.Status().Reason)
279-
} else {
280-
t.Fatalf("Unable to verify CnsNodeVMBatchAttachment error")
281-
}
282-
})
283-
}
284-
285235
func TestCnsNodeVMBatchAttachmentWhenVmOnVcenterReturnsNotFoundErrorAndInstanceIsNotDeleted(t *testing.T) {
286236
t.Run("TestCnsNodeVMBatchAttachmentWhenVmOnVcenterReturnsNotFoundErrorAndInstanceIsNotDeleted",
287237
func(t *testing.T) {
@@ -740,6 +690,96 @@ func TestIsSharedPvc(t *testing.T) {
740690
}
741691
}
742692

693+
func TestUpdatePvcStatusEntryName_AddsSuffix(t *testing.T) {
694+
ctx := context.TODO()
695+
696+
instance := &v1alpha1.CnsNodeVMBatchAttachment{
697+
Status: v1alpha1.CnsNodeVMBatchAttachmentStatus{
698+
VolumeStatus: []v1alpha1.VolumeStatus{
699+
{
700+
Name: "vol1",
701+
PersistentVolumeClaim: v1alpha1.PersistentVolumeClaimStatus{
702+
ClaimName: "pvc-1",
703+
},
704+
},
705+
},
706+
},
707+
}
708+
709+
pvcsToDetach := map[string]string{
710+
"pvc-1": "",
711+
}
712+
713+
updatePvcStatusEntryName(ctx, instance, pvcsToDetach)
714+
715+
got := instance.Status.VolumeStatus[0].Name
716+
want := "vol1" + detachSuffix
717+
718+
if got != want {
719+
t.Fatalf("expected volume name %q, got %q", want, got)
720+
}
721+
}
722+
723+
func TestUpdatePvcStatusEntryName_SkipsIfAlreadyHasSuffix(t *testing.T) {
724+
ctx := context.TODO()
725+
726+
instance := &v1alpha1.CnsNodeVMBatchAttachment{
727+
Status: v1alpha1.CnsNodeVMBatchAttachmentStatus{
728+
VolumeStatus: []v1alpha1.VolumeStatus{
729+
{
730+
Name: "vol1" + detachSuffix,
731+
PersistentVolumeClaim: v1alpha1.PersistentVolumeClaimStatus{
732+
ClaimName: "pvc-1",
733+
},
734+
},
735+
},
736+
},
737+
}
738+
739+
pvcsToDetach := map[string]string{
740+
"pvc-1": "",
741+
}
742+
743+
updatePvcStatusEntryName(ctx, instance, pvcsToDetach)
744+
745+
got := instance.Status.VolumeStatus[0].Name
746+
want := "vol1" + detachSuffix // should remain unchanged
747+
748+
if got != want {
749+
t.Fatalf("expected volume name to remain %q, got %q", want, got)
750+
}
751+
}
752+
753+
func TestUpdatePvcStatusEntryName_SkipsNonTargetPVC(t *testing.T) {
754+
ctx := context.TODO()
755+
756+
instance := &v1alpha1.CnsNodeVMBatchAttachment{
757+
Status: v1alpha1.CnsNodeVMBatchAttachmentStatus{
758+
VolumeStatus: []v1alpha1.VolumeStatus{
759+
{
760+
Name: "vol1",
761+
PersistentVolumeClaim: v1alpha1.PersistentVolumeClaimStatus{
762+
ClaimName: "pvc-1",
763+
},
764+
},
765+
},
766+
},
767+
}
768+
769+
pvcsToDetach := map[string]string{
770+
"some-other-pvc": "",
771+
}
772+
773+
updatePvcStatusEntryName(ctx, instance, pvcsToDetach)
774+
775+
got := instance.Status.VolumeStatus[0].Name
776+
want := "vol1" // unchanged
777+
778+
if got != want {
779+
t.Fatalf("expected volume name %q (unchanged), got %q", want, got)
780+
}
781+
}
782+
743783
func MockGetVMFromVcenter(ctx context.Context, nodeUUID string,
744784
configInfo config.ConfigurationInfo) (*cnsvsphere.VirtualMachine, error) {
745785
var vm *cnsvsphere.VirtualMachine

0 commit comments

Comments
 (0)