Split snapshot controller using beta APIs

This commit is contained in:
xing-yang
2019-10-19 14:48:38 +00:00
parent 89889f005c
commit 6308420635
32 changed files with 3050 additions and 1817 deletions

View File

@@ -27,14 +27,13 @@ import (
"google.golang.org/grpc"
"k8s.io/api/core/v1"
"k8s.io/klog"
)
// Snapshotter implements CreateSnapshot/DeleteSnapshot operations against a remote CSI driver.
type Snapshotter interface {
// CreateSnapshot creates a snapshot for a volume
CreateSnapshot(ctx context.Context, snapshotName string, volume *v1.PersistentVolume, parameters map[string]string, snapshotterCredentials map[string]string) (driverName string, snapshotId string, timestamp time.Time, size int64, readyToUse bool, err error)
CreateSnapshot(ctx context.Context, snapshotName string, volumeHandle string, parameters map[string]string, snapshotterCredentials map[string]string) (driverName string, snapshotId string, timestamp time.Time, size int64, readyToUse bool, err error)
// DeleteSnapshot deletes a snapshot from a volume
DeleteSnapshot(ctx context.Context, snapshotID string, snapshotterCredentials map[string]string) (err error)
@@ -53,12 +52,8 @@ func NewSnapshotter(conn *grpc.ClientConn) Snapshotter {
}
}
func (s *snapshot) CreateSnapshot(ctx context.Context, snapshotName string, volume *v1.PersistentVolume, parameters map[string]string, snapshotterCredentials map[string]string) (string, string, time.Time, int64, bool, error) {
func (s *snapshot) CreateSnapshot(ctx context.Context, snapshotName string, volumeHandle string, parameters map[string]string, snapshotterCredentials map[string]string) (string, string, time.Time, int64, bool, error) {
klog.V(5).Infof("CSI CreateSnapshot: %s", snapshotName)
if volume.Spec.CSI == nil {
return "", "", time.Time{}, 0, false, fmt.Errorf("CSIPersistentVolumeSource not defined in spec")
}
client := csi.NewControllerClient(s.conn)
driverName, err := csirpc.GetDriverName(ctx, s.conn)
@@ -67,7 +62,7 @@ func (s *snapshot) CreateSnapshot(ctx context.Context, snapshotName string, volu
}
req := csi.CreateSnapshotRequest{
SourceVolumeId: volume.Spec.CSI.VolumeHandle,
SourceVolumeId: volumeHandle,
Name: snapshotName,
Parameters: parameters,
Secrets: snapshotterCredentials,
@@ -118,6 +113,8 @@ func (s *snapshot) isListSnapshotsSupported(ctx context.Context) (bool, error) {
}
func (s *snapshot) GetSnapshotStatus(ctx context.Context, snapshotID string) (bool, time.Time, int64, error) {
klog.V(5).Infof("GetSnapshotStatus: %s", snapshotID)
client := csi.NewControllerClient(s.conn)
// If the driver does not support ListSnapshots, assume the snapshot ID is valid.

View File

@@ -79,7 +79,6 @@ func TestCreateSnapshot(t *testing.T) {
}
csiVolume := FakeCSIVolume()
volumeWithoutCSI := FakeVolume()
defaultRequest := &csi.CreateSnapshotRequest{
Name: defaultName,
@@ -135,7 +134,7 @@ func TestCreateSnapshot(t *testing.T) {
tests := []struct {
name string
snapshotName string
volume *v1.PersistentVolume
volumeHandle string
parameters map[string]string
secrets map[string]string
input *csi.CreateSnapshotRequest
@@ -147,7 +146,7 @@ func TestCreateSnapshot(t *testing.T) {
{
name: "success",
snapshotName: defaultName,
volume: csiVolume,
volumeHandle: csiVolume.Spec.CSI.VolumeHandle,
input: defaultRequest,
output: defaultResponse,
expectError: false,
@@ -156,7 +155,7 @@ func TestCreateSnapshot(t *testing.T) {
{
name: "attributes",
snapshotName: defaultName,
volume: csiVolume,
volumeHandle: csiVolume.Spec.CSI.VolumeHandle,
parameters: defaultParameter,
input: attributesRequest,
output: defaultResponse,
@@ -166,25 +165,17 @@ func TestCreateSnapshot(t *testing.T) {
{
name: "secrets",
snapshotName: defaultName,
volume: csiVolume,
volumeHandle: csiVolume.Spec.CSI.VolumeHandle,
secrets: createSecrets,
input: secretsRequest,
output: defaultResponse,
expectError: false,
expectResult: result,
},
{
name: "fail for volume without csi source",
snapshotName: defaultName,
volume: volumeWithoutCSI,
input: nil,
output: nil,
expectError: true,
},
{
name: "gRPC transient error",
snapshotName: defaultName,
volume: csiVolume,
volumeHandle: csiVolume.Spec.CSI.VolumeHandle,
input: defaultRequest,
output: nil,
injectError: codes.DeadlineExceeded,
@@ -193,7 +184,7 @@ func TestCreateSnapshot(t *testing.T) {
{
name: "gRPC final error",
snapshotName: defaultName,
volume: csiVolume,
volumeHandle: csiVolume.Spec.CSI.VolumeHandle,
input: defaultRequest,
output: nil,
injectError: codes.NotFound,
@@ -224,7 +215,7 @@ func TestCreateSnapshot(t *testing.T) {
}
s := NewSnapshotter(csiConn)
driverName, snapshotId, timestamp, size, readyToUse, err := s.CreateSnapshot(context.Background(), test.snapshotName, test.volume, test.parameters, test.secrets)
driverName, snapshotId, timestamp, size, readyToUse, err := s.CreateSnapshot(context.Background(), test.snapshotName, test.volumeHandle, test.parameters, test.secrets)
if test.expectError && err == nil {
t.Errorf("test %q: Expected error, got none", test.name)
}
@@ -509,29 +500,3 @@ func FakeCSIVolume() *v1.PersistentVolume {
return &volume
}
func FakeVolume() *v1.PersistentVolume {
volume := v1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-csi-volume",
},
Spec: v1.PersistentVolumeSpec{
ClaimRef: &v1.ObjectReference{
Kind: "PersistentVolumeClaim",
APIVersion: "v1",
UID: types.UID("uid123"),
Namespace: "default",
Name: "test-claim",
},
PersistentVolumeSource: v1.PersistentVolumeSource{
GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{},
},
StorageClassName: "default",
},
Status: v1.PersistentVolumeStatus{
Phase: v1.VolumeBound,
},
}
return &volume
}