Skip to content

Commit e979188

Browse files
committed
Allow CnsNodeVMBatchAttachment CRD to have empty list of volumes in its spec
1 parent f7d7663 commit e979188

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,11 @@ type CnsNodeVMBatchAttachmentSpec struct {
7474

7575
// InstanceUUID indicates the instance UUID of the node where the volume needs to be attached to.
7676
InstanceUUID string `json:"instanceUUID"`
77-
// +required
7877

7978
// +listType=map
8079
// +listMapKey=name
8180
// VolumeSpec reflects the desired state for each volume.
82-
Volumes []VolumeSpec `json:"volumes"`
81+
Volumes []VolumeSpec `json:"volumes,omitempty"`
8382
}
8483

8584
type VolumeSpec struct {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ spec:
107107
x-kubernetes-list-type: map
108108
required:
109109
- instanceUUID
110-
- volumes
111110
type: object
112111
status:
113112
description: CnsNodeVMBatchAttachmentStatus defines the observed state

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

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

327327
// The CR is not being deleted, so call attach and detach for volumes.
328328
if instance.DeletionTimestamp == nil {
329-
log.Infof("Starting reconciliation for instance %s", request.NamespacedName.String())
330-
331329
// Add finalizer to CR if it does not already exist.
332330
if !controllerutil.ContainsFinalizer(instance, cnsoperatortypes.CNSFinalizer) {
333331
log.Debugf("Finalizer %s not found on instance %s. Adding it now.",
@@ -521,6 +519,11 @@ func (r *Reconciler) processBatchAttach(ctx context.Context, k8sClient kubernete
521519
instance *v1alpha1.CnsNodeVMBatchAttachment) error {
522520
log := logger.GetLogger(ctx)
523521

522+
if len(instance.Spec.Volumes) == 0 {
523+
log.Infof("No volumes to attach to VM %q", instance.Spec.InstanceUUID)
524+
return nil
525+
}
526+
524527
// Construct batch attach request
525528
pvcsInSpec, volumeIdsInSpec, batchAttachRequest, err := constructBatchAttachRequest(ctx, instance)
526529
if err != nil {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,26 @@ func TestReconcileWithoutDeletionTimestamp(t *testing.T) {
356356
})
357357
}
358358

359+
func TestReconcileWithoutDeletionTimestampWithNoVolumestoAttach(t *testing.T) {
360+
361+
t.Run("TestReconcileWithoutDeletionTimestampWithNoVolumestoAttach", func(t *testing.T) {
362+
testCnsNodeVMBatchAttachment := setupTestCnsNodeVMBatchAttachment()
363+
testCnsNodeVMBatchAttachment.Spec.Volumes = []v1alpha1.VolumeSpec{}
364+
r := setTestEnvironment(&testCnsNodeVMBatchAttachment, false)
365+
mockVolumeManager := &unittestcommon.MockVolumeManager{}
366+
r.volumeManager = mockVolumeManager
367+
commonco.ContainerOrchestratorUtility = &unittestcommon.FakeK8SOrchestrator{}
368+
volumesToDetach := map[string]string{}
369+
vm := &cnsvsphere.VirtualMachine{}
370+
clientset := getClientSetWithPvc()
371+
372+
err := r.reconcileInstanceWithoutDeletionTimestamp(context.TODO(),
373+
clientset,
374+
&testCnsNodeVMBatchAttachment, volumesToDetach, vm)
375+
assert.NoError(t, err)
376+
})
377+
}
378+
359379
func TestReconcileWithoutDeletionTimestampWhenAttachFails(t *testing.T) {
360380

361381
t.Run("TestReconcileWithoutDeletionTimestamp", func(t *testing.T) {

0 commit comments

Comments
 (0)