refactor external snapshotter to use csi-lib-utils/rpc
This commit is contained in:
committed by
Andrew Sy Kim
parent
c20ded872e
commit
06a4bf2a05
@@ -23,7 +23,8 @@ import (
|
||||
"time"
|
||||
|
||||
crdv1 "github.com/kubernetes-csi/external-snapshotter/pkg/apis/volumesnapshot/v1alpha1"
|
||||
"github.com/kubernetes-csi/external-snapshotter/pkg/connection"
|
||||
"github.com/kubernetes-csi/external-snapshotter/pkg/snapshotter"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
)
|
||||
|
||||
@@ -36,7 +37,7 @@ type Handler interface {
|
||||
|
||||
// csiHandler is a handler that calls CSI to create/delete volume snapshot.
|
||||
type csiHandler struct {
|
||||
csiConnection connection.CSIConnection
|
||||
snapshotter snapshotter.Snapshotter
|
||||
timeout time.Duration
|
||||
snapshotNamePrefix string
|
||||
snapshotNameUUIDLength int
|
||||
@@ -44,13 +45,13 @@ type csiHandler struct {
|
||||
|
||||
// NewCSIHandler returns a handler which includes the csi connection and Snapshot name details
|
||||
func NewCSIHandler(
|
||||
csiConnection connection.CSIConnection,
|
||||
snapshotter snapshotter.Snapshotter,
|
||||
timeout time.Duration,
|
||||
snapshotNamePrefix string,
|
||||
snapshotNameUUIDLength int,
|
||||
) Handler {
|
||||
return &csiHandler{
|
||||
csiConnection: csiConnection,
|
||||
snapshotter: snapshotter,
|
||||
timeout: timeout,
|
||||
snapshotNamePrefix: snapshotNamePrefix,
|
||||
snapshotNameUUIDLength: snapshotNameUUIDLength,
|
||||
@@ -70,7 +71,7 @@ func (handler *csiHandler) CreateSnapshot(snapshot *crdv1.VolumeSnapshot, volume
|
||||
if err != nil {
|
||||
return "", "", 0, 0, false, fmt.Errorf("failed to remove CSI Parameters of prefixed keys: %v", err)
|
||||
}
|
||||
return handler.csiConnection.CreateSnapshot(ctx, snapshotName, volume, newParameters, snapshotterCredentials)
|
||||
return handler.snapshotter.CreateSnapshot(ctx, snapshotName, volume, newParameters, snapshotterCredentials)
|
||||
}
|
||||
|
||||
func (handler *csiHandler) DeleteSnapshot(content *crdv1.VolumeSnapshotContent, snapshotterCredentials map[string]string) error {
|
||||
@@ -80,7 +81,7 @@ func (handler *csiHandler) DeleteSnapshot(content *crdv1.VolumeSnapshotContent,
|
||||
ctx, cancel := context.WithTimeout(context.Background(), handler.timeout)
|
||||
defer cancel()
|
||||
|
||||
err := handler.csiConnection.DeleteSnapshot(ctx, content.Spec.CSI.SnapshotHandle, snapshotterCredentials)
|
||||
err := handler.snapshotter.DeleteSnapshot(ctx, content.Spec.CSI.SnapshotHandle, snapshotterCredentials)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete snapshot content %s: %q", content.Name, err)
|
||||
}
|
||||
@@ -95,12 +96,12 @@ func (handler *csiHandler) GetSnapshotStatus(content *crdv1.VolumeSnapshotConten
|
||||
ctx, cancel := context.WithTimeout(context.Background(), handler.timeout)
|
||||
defer cancel()
|
||||
|
||||
csiSnapshotStatus, timestamp, size, err := handler.csiConnection.GetSnapshotStatus(ctx, content.Spec.CSI.SnapshotHandle)
|
||||
csiSnapshotStatus, timestamp, size, err := handler.snapshotter.GetSnapshotStatus(ctx, content.Spec.CSI.SnapshotHandle)
|
||||
if err != nil {
|
||||
return false, 0, 0, fmt.Errorf("failed to list snapshot content %s: %q", content.Name, err)
|
||||
}
|
||||
return csiSnapshotStatus, timestamp, size, nil
|
||||
|
||||
return csiSnapshotStatus, timestamp, size, nil
|
||||
}
|
||||
|
||||
func makeSnapshotName(prefix, snapshotUID string, snapshotNameUUIDLength int) (string, error) {
|
||||
|
@@ -717,7 +717,7 @@ func newTestController(kubeClient kubernetes.Interface, clientset clientset.Inte
|
||||
coreFactory := coreinformers.NewSharedInformerFactory(kubeClient, NoResyncPeriodFunc())
|
||||
|
||||
// Construct controller
|
||||
csiConnection := &fakeCSIConnection{
|
||||
fakeSnapshot := &fakeSnapshotter{
|
||||
t: t,
|
||||
listCalls: test.expectedListCalls,
|
||||
createCalls: test.expectedCreateCalls,
|
||||
@@ -734,7 +734,7 @@ func newTestController(kubeClient kubernetes.Interface, clientset clientset.Inte
|
||||
coreFactory.Core().V1().PersistentVolumeClaims(),
|
||||
3,
|
||||
5*time.Millisecond,
|
||||
csiConnection,
|
||||
fakeSnapshot,
|
||||
5*time.Millisecond,
|
||||
60*time.Second,
|
||||
"snapshot",
|
||||
@@ -1152,9 +1152,9 @@ type createCall struct {
|
||||
err error
|
||||
}
|
||||
|
||||
// Fake CSIConnection implementation that check that Attach/Detach is called
|
||||
// Fake SnapShotter implementation that check that Attach/Detach is called
|
||||
// with the right parameters and it returns proper error code and metadata.
|
||||
type fakeCSIConnection struct {
|
||||
type fakeSnapshotter struct {
|
||||
createCalls []createCall
|
||||
createCallCounter int
|
||||
deleteCalls []deleteCall
|
||||
@@ -1164,19 +1164,7 @@ type fakeCSIConnection struct {
|
||||
t *testing.T
|
||||
}
|
||||
|
||||
func (f *fakeCSIConnection) GetDriverName(ctx context.Context) (string, error) {
|
||||
return mockDriverName, nil
|
||||
}
|
||||
|
||||
func (f *fakeCSIConnection) SupportsControllerCreateSnapshot(ctx context.Context) (bool, error) {
|
||||
return false, fmt.Errorf("Not implemented")
|
||||
}
|
||||
|
||||
func (f *fakeCSIConnection) SupportsControllerListSnapshots(ctx context.Context) (bool, error) {
|
||||
return false, fmt.Errorf("Not implemented")
|
||||
}
|
||||
|
||||
func (f *fakeCSIConnection) CreateSnapshot(ctx context.Context, snapshotName string, volume *v1.PersistentVolume, parameters map[string]string, snapshotterCredentials map[string]string) (string, string, int64, int64, bool, error) {
|
||||
func (f *fakeSnapshotter) CreateSnapshot(ctx context.Context, snapshotName string, volume *v1.PersistentVolume, parameters map[string]string, snapshotterCredentials map[string]string) (string, string, int64, int64, bool, error) {
|
||||
if f.createCallCounter >= len(f.createCalls) {
|
||||
f.t.Errorf("Unexpected CSI Create Snapshot call: snapshotName=%s, volume=%v, index: %d, calls: %+v", snapshotName, volume.Name, f.createCallCounter, f.createCalls)
|
||||
return "", "", 0, 0, false, fmt.Errorf("unexpected call")
|
||||
@@ -1212,7 +1200,7 @@ func (f *fakeCSIConnection) CreateSnapshot(ctx context.Context, snapshotName str
|
||||
return call.driverName, call.snapshotId, call.timestamp, call.size, call.readyToUse, call.err
|
||||
}
|
||||
|
||||
func (f *fakeCSIConnection) DeleteSnapshot(ctx context.Context, snapshotID string, snapshotterCredentials map[string]string) error {
|
||||
func (f *fakeSnapshotter) DeleteSnapshot(ctx context.Context, snapshotID string, snapshotterCredentials map[string]string) error {
|
||||
if f.deleteCallCounter >= len(f.deleteCalls) {
|
||||
f.t.Errorf("Unexpected CSI Delete Snapshot call: snapshotID=%s, index: %d, calls: %+v", snapshotID, f.createCallCounter, f.createCalls)
|
||||
return fmt.Errorf("unexpected call")
|
||||
@@ -1238,7 +1226,7 @@ func (f *fakeCSIConnection) DeleteSnapshot(ctx context.Context, snapshotID strin
|
||||
return call.err
|
||||
}
|
||||
|
||||
func (f *fakeCSIConnection) GetSnapshotStatus(ctx context.Context, snapshotID string) (bool, int64, int64, error) {
|
||||
func (f *fakeSnapshotter) GetSnapshotStatus(ctx context.Context, snapshotID string) (bool, int64, int64, error) {
|
||||
if f.listCallCounter >= len(f.listCalls) {
|
||||
f.t.Errorf("Unexpected CSI list Snapshot call: snapshotID=%s, index: %d, calls: %+v", snapshotID, f.createCallCounter, f.createCalls)
|
||||
return false, 0, 0, fmt.Errorf("unexpected call")
|
||||
@@ -1258,11 +1246,3 @@ func (f *fakeCSIConnection) GetSnapshotStatus(ctx context.Context, snapshotID st
|
||||
|
||||
return call.readyToUse, call.createTime, call.size, call.err
|
||||
}
|
||||
|
||||
func (f *fakeCSIConnection) Close() error {
|
||||
return fmt.Errorf("Not implemented")
|
||||
}
|
||||
|
||||
func (f *fakeCSIConnection) Probe(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
@@ -24,8 +24,9 @@ import (
|
||||
clientset "github.com/kubernetes-csi/external-snapshotter/pkg/client/clientset/versioned"
|
||||
storageinformers "github.com/kubernetes-csi/external-snapshotter/pkg/client/informers/externalversions/volumesnapshot/v1alpha1"
|
||||
storagelisters "github.com/kubernetes-csi/external-snapshotter/pkg/client/listers/volumesnapshot/v1alpha1"
|
||||
"github.com/kubernetes-csi/external-snapshotter/pkg/connection"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"github.com/kubernetes-csi/external-snapshotter/pkg/snapshotter"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
@@ -81,7 +82,7 @@ func NewCSISnapshotController(
|
||||
pvcInformer coreinformers.PersistentVolumeClaimInformer,
|
||||
createSnapshotContentRetryCount int,
|
||||
createSnapshotContentInterval time.Duration,
|
||||
conn connection.CSIConnection,
|
||||
snapshotter snapshotter.Snapshotter,
|
||||
timeout time.Duration,
|
||||
resyncPeriod time.Duration,
|
||||
snapshotNamePrefix string,
|
||||
@@ -98,7 +99,7 @@ func NewCSISnapshotController(
|
||||
client: client,
|
||||
snapshotterName: snapshotterName,
|
||||
eventRecorder: eventRecorder,
|
||||
handler: NewCSIHandler(conn, timeout, snapshotNamePrefix, snapshotNameUUIDLength),
|
||||
handler: NewCSIHandler(snapshotter, timeout, snapshotNamePrefix, snapshotNameUUIDLength),
|
||||
runningOperations: goroutinemap.NewGoRoutineMap(true),
|
||||
createSnapshotContentRetryCount: createSnapshotContentRetryCount,
|
||||
createSnapshotContentInterval: createSnapshotContentInterval,
|
||||
|
Reference in New Issue
Block a user