feat: add immediate and deferred provisioning modes
This commit is contained in:
@@ -3,6 +3,7 @@ package api
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@@ -40,6 +41,17 @@ func TestClientCreateVolume(t *testing.T) {
|
||||
if r.Method != http.MethodPost || r.URL.Path != "/v1/volumes" {
|
||||
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
||||
}
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("read request body: %v", err)
|
||||
}
|
||||
var got provisioner.CreateVolumeRequest
|
||||
if err := json.Unmarshal(body, &got); err != nil {
|
||||
t.Fatalf("decode request body: %v", err)
|
||||
}
|
||||
if got.Parameters["kks.kloud/provisioning-mode"] != "immediate" {
|
||||
t.Fatalf("unexpected parameters: %#v", got.Parameters)
|
||||
}
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
_ = json.NewEncoder(w).Encode(provisioner.Volume{
|
||||
VolumeID: "abc123/k8s-volumes/pvc-1",
|
||||
@@ -52,6 +64,9 @@ func TestClientCreateVolume(t *testing.T) {
|
||||
vol, err := client.CreateVolume(context.Background(), provisioner.CreateVolumeRequest{
|
||||
Name: "pvc-1",
|
||||
SizeBytes: 1024,
|
||||
Parameters: map[string]string{
|
||||
"kks.kloud/provisioning-mode": "immediate",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateVolume failed: %v", err)
|
||||
|
||||
@@ -3,8 +3,8 @@ package driver
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/container-storage-interface/spec/lib/go/csi"
|
||||
"github.com/KubelanCloud/kks-csi-plugin/pkg/csi/provisioner"
|
||||
"github.com/container-storage-interface/spec/lib/go/csi"
|
||||
)
|
||||
|
||||
type ControllerServer struct {
|
||||
@@ -33,8 +33,9 @@ func (s *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolu
|
||||
}
|
||||
|
||||
vol, err := s.d.backend.CreateVolume(ctx, provisioner.CreateVolumeRequest{
|
||||
Name: sanitizeVolumeName(req.GetName()),
|
||||
SizeBytes: capacity,
|
||||
Name: sanitizeVolumeName(req.GetName()),
|
||||
SizeBytes: capacity,
|
||||
Parameters: req.GetParameters(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, internalError(err)
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package driver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/KubelanCloud/kks-csi-plugin/pkg/csi/provisioner"
|
||||
"github.com/container-storage-interface/spec/lib/go/csi"
|
||||
)
|
||||
|
||||
type fakeBackend struct {
|
||||
createReq provisioner.CreateVolumeRequest
|
||||
}
|
||||
|
||||
func (f *fakeBackend) ClusterInfo(context.Context) (provisioner.ClusterInfo, error) {
|
||||
return provisioner.ClusterInfo{}, nil
|
||||
}
|
||||
|
||||
func (f *fakeBackend) CreateVolume(_ context.Context, req provisioner.CreateVolumeRequest) (provisioner.Volume, error) {
|
||||
f.createReq = req
|
||||
return provisioner.Volume{VolumeID: "vol-1", SizeBytes: req.SizeBytes}, nil
|
||||
}
|
||||
|
||||
func (f *fakeBackend) DeleteVolume(context.Context, string) error { return nil }
|
||||
|
||||
func (f *fakeBackend) VolumeExists(context.Context, string) (bool, error) { return false, nil }
|
||||
|
||||
func (f *fakeBackend) PublishVolume(context.Context, string, string) (provisioner.PublishVolumeResponse, error) {
|
||||
return provisioner.PublishVolumeResponse{}, nil
|
||||
}
|
||||
|
||||
func (f *fakeBackend) UnpublishVolume(context.Context, string, string) error { return nil }
|
||||
|
||||
func (f *fakeBackend) Close() error { return nil }
|
||||
|
||||
func TestControllerCreateVolumeForwardsStorageClassParameters(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
backend := &fakeBackend{}
|
||||
s := &ControllerServer{d: &Driver{backend: backend}}
|
||||
|
||||
_, err := s.CreateVolume(context.Background(), &csi.CreateVolumeRequest{
|
||||
Name: "pvc-demo",
|
||||
CapacityRange: &csi.CapacityRange{
|
||||
RequiredBytes: 10,
|
||||
},
|
||||
VolumeCapabilities: []*csi.VolumeCapability{{
|
||||
AccessType: &csi.VolumeCapability_Mount{Mount: &csi.VolumeCapability_MountVolume{}},
|
||||
AccessMode: &csi.VolumeCapability_AccessMode{Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER},
|
||||
}},
|
||||
Parameters: map[string]string{
|
||||
"kks.kloud/provisioning-mode": "deferred",
|
||||
"example": "value",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateVolume returned error: %v", err)
|
||||
}
|
||||
|
||||
if backend.createReq.Parameters["kks.kloud/provisioning-mode"] != "deferred" {
|
||||
t.Fatalf("unexpected forwarded parameters: %#v", backend.createReq.Parameters)
|
||||
}
|
||||
}
|
||||
@@ -19,8 +19,9 @@ type Volume struct {
|
||||
}
|
||||
|
||||
type CreateVolumeRequest struct {
|
||||
Name string `json:"name"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
Name string `json:"name"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
Parameters map[string]string `json:"parameters,omitempty"`
|
||||
}
|
||||
|
||||
type PublishVolumeRequest struct {
|
||||
|
||||
Reference in New Issue
Block a user