mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 02:33:34 +00:00
rbd: migration of replication service to csi-addon
this commit removes grpc import from replication.go and replaced it with usual errors and passed gRPC responses in csi-addons Signed-off-by: riya-singhal31 <rsinghal@redhat.com>
This commit is contained in:
committed by
mergify[bot]
parent
66785c3bba
commit
cdaa9264eb
@ -326,7 +326,12 @@ func (rs *ReplicationServer) DisableVolumeReplication(ctx context.Context,
|
||||
case librbd.MirrorImageDisabling:
|
||||
return nil, status.Errorf(codes.Aborted, "%s is in disabling state", volumeID)
|
||||
case librbd.MirrorImageEnabled:
|
||||
return corerbd.DisableVolumeReplication(rbdVol, mirroringInfo, force)
|
||||
err = rbdVol.DisableVolumeReplication(mirroringInfo, force)
|
||||
if err != nil {
|
||||
return nil, getGRPCError(err)
|
||||
}
|
||||
|
||||
return &replication.DisableVolumeReplicationResponse{}, nil
|
||||
default:
|
||||
return nil, status.Errorf(codes.InvalidArgument, "image is in %s Mode", mirroringInfo.State)
|
||||
}
|
||||
@ -627,9 +632,7 @@ func (rs *ReplicationServer) ResyncVolume(ctx context.Context,
|
||||
|
||||
err = rbdVol.ResyncVol(localStatus, req.Force)
|
||||
if err != nil {
|
||||
log.ErrorLog(ctx, err.Error())
|
||||
|
||||
return nil, err
|
||||
return nil, getGRPCError(err)
|
||||
}
|
||||
|
||||
err = checkVolumeResyncStatus(localStatus)
|
||||
@ -649,6 +652,32 @@ func (rs *ReplicationServer) ResyncVolume(ctx context.Context,
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func getGRPCError(err error) error {
|
||||
if err == nil {
|
||||
return status.Error(codes.OK, codes.OK.String())
|
||||
}
|
||||
|
||||
errorStatusMap := map[error]codes.Code{
|
||||
corerbd.ErrFetchingLocalState: codes.Internal,
|
||||
corerbd.ErrResyncImageFailed: codes.Internal,
|
||||
corerbd.ErrDisableImageMirroringFailed: codes.Internal,
|
||||
corerbd.ErrFetchingMirroringInfo: codes.Internal,
|
||||
corerbd.ErrInvalidArgument: codes.InvalidArgument,
|
||||
corerbd.ErrAborted: codes.Aborted,
|
||||
corerbd.ErrFailedPrecondition: codes.FailedPrecondition,
|
||||
corerbd.ErrUnavailable: codes.Unavailable,
|
||||
}
|
||||
|
||||
for e, code := range errorStatusMap {
|
||||
if errors.Is(err, e) {
|
||||
return status.Error(code, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Handle any other non nil error not listed in the map
|
||||
return status.Error(codes.Unknown, err.Error())
|
||||
}
|
||||
|
||||
// GetVolumeReplicationInfo extracts the RBD volume information from the volumeID, If the
|
||||
// image is present, mirroring is enabled and the image is in primary state.
|
||||
func (rs *ReplicationServer) GetVolumeReplicationInfo(ctx context.Context,
|
||||
|
@ -18,6 +18,7 @@ package rbd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
@ -27,6 +28,9 @@ import (
|
||||
|
||||
librbd "github.com/ceph/go-ceph/rbd"
|
||||
"github.com/ceph/go-ceph/rbd/admin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
@ -494,3 +498,72 @@ func TestValidateLastSyncTime(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetGRPCError(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
name string
|
||||
err error
|
||||
expectedErr error
|
||||
}{
|
||||
{
|
||||
name: "FetchingLocalStateFailed",
|
||||
err: corerbd.ErrFetchingLocalState,
|
||||
expectedErr: status.Error(codes.Internal, corerbd.ErrFetchingLocalState.Error()),
|
||||
},
|
||||
{
|
||||
name: "ResyncImageFailed",
|
||||
err: corerbd.ErrResyncImageFailed,
|
||||
expectedErr: status.Error(codes.Internal, corerbd.ErrResyncImageFailed.Error()),
|
||||
},
|
||||
{
|
||||
name: "DisableImageMirroringFailed",
|
||||
err: corerbd.ErrDisableImageMirroringFailed,
|
||||
expectedErr: status.Error(codes.Internal, corerbd.ErrDisableImageMirroringFailed.Error()),
|
||||
},
|
||||
{
|
||||
name: "FetchingMirroringInfoFailed",
|
||||
err: corerbd.ErrFetchingMirroringInfo,
|
||||
expectedErr: status.Error(codes.Internal, corerbd.ErrFetchingMirroringInfo.Error()),
|
||||
},
|
||||
{
|
||||
name: "InvalidArgument",
|
||||
err: corerbd.ErrInvalidArgument,
|
||||
expectedErr: status.Error(codes.InvalidArgument, corerbd.ErrInvalidArgument.Error()),
|
||||
},
|
||||
{
|
||||
name: "Aborted",
|
||||
err: corerbd.ErrAborted,
|
||||
expectedErr: status.Error(codes.Aborted, corerbd.ErrAborted.Error()),
|
||||
},
|
||||
{
|
||||
name: "FailedPrecondition",
|
||||
err: corerbd.ErrFailedPrecondition,
|
||||
expectedErr: status.Error(codes.FailedPrecondition, corerbd.ErrFailedPrecondition.Error()),
|
||||
},
|
||||
{
|
||||
name: "Unavailable",
|
||||
err: corerbd.ErrUnavailable,
|
||||
expectedErr: status.Error(codes.Unavailable, corerbd.ErrUnavailable.Error()),
|
||||
},
|
||||
{
|
||||
name: "InvalidError",
|
||||
err: errors.New("some error"),
|
||||
expectedErr: status.Error(codes.Unknown, "some error"),
|
||||
},
|
||||
{
|
||||
name: "NilError",
|
||||
err: nil,
|
||||
expectedErr: status.Error(codes.OK, "ok string"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
result := getGRPCError(tt.err)
|
||||
assert.Equal(t, tt.expectedErr, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user