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

Ensure only one VR per PVC #213

Merged
merged 1 commit into from
Aug 25, 2022
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
4 changes: 4 additions & 0 deletions apis/replication.storage/v1alpha1/volumereplication_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
VolumeReplicationNameAnnotation = "replication.storage.openshift.io/volume-replication-name"
)

// ReplicationState represents the replication operations to be performed on the volume.
// +kubebuilder:validation:Enum=primary;secondary;resync
type ReplicationState string
Expand Down
35 changes: 35 additions & 0 deletions controllers/replication.storage/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

replicationv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/apis/replication.storage/v1alpha1"
)

// getPVCDataSource get pvc, pv object from the request.
Expand Down Expand Up @@ -56,3 +58,36 @@ func (r VolumeReplicationReconciler) getPVCDataSource(logger logr.Logger, req ty

return pvc, pv, nil
}

// annotatePVCWithOwner will add the VolumeReplication details to the PVC annotations.
func (r *VolumeReplicationReconciler) annotatePVCWithOwner(ctx context.Context, logger logr.Logger, req types.NamespacedName, pvc *corev1.PersistentVolumeClaim) error {
if pvc.ObjectMeta.Annotations == nil {
pvc.ObjectMeta.Annotations = map[string]string{}
}

ownerName := pvc.ObjectMeta.Annotations[replicationv1alpha1.VolumeReplicationNameAnnotation]
if ownerName == "" {
pvc.ObjectMeta.Annotations[replicationv1alpha1.VolumeReplicationNameAnnotation] = req.Name
err := r.Update(ctx, pvc)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 separate VR reconciles could update the annotation at the same time. (2 go routines etc.). This may need some rudimentary locking such that there is only one updater (fetch and update sequence) for a given PVC.

This should be quite rare, but something to keep track of.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree but am thinking if 2 VR acts on the same PVC to add annotation, either one will fail and one will pass and should be fine. what do you think? as its a quite rare condition we can see how to fix it.

if err != nil {
logger.Error(err, "Failed to update PVC annotation", "Name", pvc.Name)

return fmt.Errorf("failed to update PVC %q annotation for VolumeReplication: %w",
pvc.Name, err)
}

return nil
}

if ownerName != req.Name {
logger.Info("cannot change the owner of PVC",
"PVC name", pvc.Name,
"current owner", ownerName,
"requested owner", req.Name)

return fmt.Errorf("PVC %q not owned by VolumeReplication %q",
pvc.Name, req.Name)
}

return nil
}
65 changes: 64 additions & 1 deletion controllers/replication.storage/pvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"testing"

replicationv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/apis/replication.storage/v1alpha1"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -175,3 +174,67 @@ func TestGetVolumeHandle(t *testing.T) {
}
}
}

func TestVolumeReplicationReconciler_annotatePVCWithOwner(t *testing.T) {
t.Parallel()
vrName := "test-vr"

testcases := []struct {
name string
pvc *corev1.PersistentVolumeClaim
errorExpected bool
}{
{
name: "case 1: no VR is owning the PVC",
pvc: mockPersistentVolumeClaim,
errorExpected: false,
},
{
name: "case 2: pvc is already owned by same VR",
pvc: &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "pvc-name",
Namespace: mockNamespace,
Annotations: map[string]string{
replicationv1alpha1.VolumeReplicationNameAnnotation: vrName,
},
},
},
errorExpected: false,
},
{
name: "case 2: pvc is owned by different VR",
pvc: &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "pvc-name",
Namespace: mockNamespace,
Annotations: map[string]string{
replicationv1alpha1.VolumeReplicationNameAnnotation: "test-vr-1",
},
},
},
errorExpected: true,
},
}

for _, tc := range testcases {
volumeReplication := &replicationv1alpha1.VolumeReplication{}
mockVolumeReplicationObj.DeepCopyInto(volumeReplication)

testPVC := &corev1.PersistentVolumeClaim{}
tc.pvc.DeepCopyInto(testPVC)

namespacedName := types.NamespacedName{
Name: vrName,
Namespace: mockNamespace,
}

reconciler := createFakeVolumeReplicationReconciler(t, testPVC, volumeReplication)
err := reconciler.annotatePVCWithOwner(context.TODO(), log.FromContext(context.TODO()), namespacedName, testPVC)
if tc.errorExpected {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ func (r *VolumeReplicationReconciler) Reconcile(ctx context.Context, req ctrl.Re
logger.Info("Replication handle", "ReplicationHandleName", replicationHandle)
}

err = r.annotatePVCWithOwner(ctx, logger, nameSpacedName, pvc)
if err != nil {
logger.Error(err, "Failed to annotate PVC owner")
return ctrl.Result{}, err
}

replicationClient, err := r.getReplicationClient(vrcObj.Spec.Provisioner)
if err != nil {
logger.Error(err, "Failed to get ReplicationClient")
Expand Down