Skip to content

Commit

Permalink
test: proxmox-api
Browse files Browse the repository at this point in the history
Mock proxmox api calls.
  • Loading branch information
sergelogvinov committed May 4, 2023
1 parent bc135db commit 8ed6376
Show file tree
Hide file tree
Showing 10 changed files with 655 additions and 240 deletions.
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ RUN make build-all-archs

########################################

FROM --platform=${TARGETARCH} gcr.io/distroless/static-debian11:nonroot AS controller
FROM --platform=${TARGETARCH} scratch AS controller
LABEL org.opencontainers.image.source = "https://github.com/sergelogvinov/proxmox-csi-plugin"

COPY --from=gcr.io/distroless/static-debian11:nonroot . .
ARG TARGETARCH
COPY --from=builder /src/bin/proxmox-csi-controller-${TARGETARCH} /proxmox-csi-controller
COPY --from=builder /src/bin/proxmox-csi-controller-${TARGETARCH} /bin/proxmox-csi-controller

ENTRYPOINT ["/proxmox-csi-controller"]
ENTRYPOINT ["/bin/proxmox-csi-controller"]

########################################

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/Telmate/proxmox-api-go v0.0.0-20230418175406-5c7733d8993f
github.com/container-storage-interface/spec v1.8.0
github.com/golang/protobuf v1.5.3
github.com/jarcoal/httpmock v1.3.0
github.com/kubernetes-csi/csi-lib-utils v0.13.0
github.com/sergelogvinov/proxmox-cloud-controller-manager v0.0.2-0.20230501134902-b776e54e4f32
github.com/stretchr/testify v1.8.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFb
github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM=
github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/jarcoal/httpmock v1.3.0 h1:2RJ8GP0IIaWwcC9Fp2BmVi8Kog3v2Hn7VXM3fTd+nuc=
github.com/jarcoal/httpmock v1.3.0/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand All @@ -99,6 +100,7 @@ github.com/kubernetes-csi/csi-lib-utils v0.13.0 h1:QrTdZVZbHlaSUBN9ReayBPnnF1N0e
github.com/kubernetes-csi/csi-lib-utils v0.13.0/go.mod h1:JS9eDIZmSjx4F9o0bLTVK/qfhIIOifdjEfVXzxWapfE=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g=
github.com/moby/sys/mountinfo v0.6.2 h1:BzJjoreD5BMFNmD9Rus6gdd1pLuecOFPt8wC+Vygl78=
github.com/moby/sys/mountinfo v0.6.2/go.mod h1:IJb6JQeOklcdMU9F5xQ8ZALD+CUr5VlGpwtX+VE0rpI=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
43 changes: 23 additions & 20 deletions pkg/csi/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ var controllerCaps = []csi.ControllerServiceCapability_RPC_Type{

// ControllerService is the controller service for the CSI driver
type ControllerService struct {
cluster proxmox.Cluster
Cluster *proxmox.Cluster

volumeLocks sync.Mutex
}

Expand All @@ -68,7 +69,7 @@ func NewControllerService(cloudConfig string) (*ControllerService, error) {
}

return &ControllerService{
cluster: *cluster,
Cluster: cluster,
}, nil
}

Expand Down Expand Up @@ -114,11 +115,11 @@ func (d *ControllerService) CreateVolume(_ context.Context, request *csi.CreateV
return nil, status.Error(codes.InvalidArgument, "cannot find best region and zone")
}

cl, err := d.cluster.GetProxmoxCluster(region)
cl, err := d.Cluster.GetProxmoxCluster(region)
if err != nil {
klog.Errorf("failed to get proxmox cluster: %v", err)

return nil, status.Error(codes.InvalidArgument, err.Error())
return nil, status.Error(codes.Internal, err.Error())
}

vol := volume.NewVolume(region, zone, params[StorageIDKey], fmt.Sprintf("vm-%d-%s", vmID, pvc))
Expand Down Expand Up @@ -160,7 +161,7 @@ func (d *ControllerService) DeleteVolume(_ context.Context, request *csi.DeleteV
return nil, status.Error(codes.InvalidArgument, err.Error())
}

cl, err := d.cluster.GetProxmoxCluster(vol.Cluster())
cl, err := d.Cluster.GetProxmoxCluster(vol.Cluster())
if err != nil {
klog.Errorf("failed to get proxmox cluster: %v", err)

Expand All @@ -184,11 +185,10 @@ func (d *ControllerService) DeleteVolume(_ context.Context, request *csi.DeleteV
vmr.SetNode(vol.Node())
vmr.SetVmType("qemu")

_, err = cl.DeleteVolume(vmr, vol.Storage(), vol.Disk())
if err != nil {
klog.Errorf("failed to delete volume: %v", err)
if _, err := cl.DeleteVolume(vmr, vol.Storage(), vol.Disk()); err != nil {
klog.Errorf("failed to delete volume: %s", vol.Disk())

return nil, status.Error(codes.Internal, err.Error())
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to delete volume: %s", vol.Disk()))
}

klog.V(4).Infof("DeleteVolume: successfully deleted volume %s", vol.Disk())
Expand Down Expand Up @@ -244,7 +244,7 @@ func (d *ControllerService) ControllerPublishVolume(_ context.Context, request *
return nil, status.Error(codes.InvalidArgument, err.Error())
}

cl, err := d.cluster.GetProxmoxCluster(vol.Cluster())
cl, err := d.Cluster.GetProxmoxCluster(vol.Cluster())
if err != nil {
klog.Errorf("failed to get proxmox cluster: %v", err)

Expand All @@ -258,6 +258,10 @@ func (d *ControllerService) ControllerPublishVolume(_ context.Context, request *
return nil, status.Error(codes.Internal, err.Error())
}

if vm.Node() != vol.Node() {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("volume %s does not exist on the node %s", volumeID, nodeID))
}

options := map[string]string{
"backup": "0",
"iothread": "1",
Expand All @@ -267,13 +271,13 @@ func (d *ControllerService) ControllerPublishVolume(_ context.Context, request *
options["ro"] = "1"
}

if volCtx["ssd"] == "true" {
if volCtx[StorageSSDKey] == "true" {
options["ssd"] = "1"
options["discard"] = "on"
}

if volCtx["cache"] != "" {
options["cache"] = volCtx["cache"]
if volCtx[StorageCacheKey] != "" {
options["cache"] = volCtx[StorageCacheKey]
}

d.volumeLocks.Lock()
Expand Down Expand Up @@ -308,7 +312,7 @@ func (d *ControllerService) ControllerUnpublishVolume(_ context.Context, request
return nil, status.Error(codes.InvalidArgument, err.Error())
}

cl, err := d.cluster.GetProxmoxCluster(vol.Cluster())
cl, err := d.Cluster.GetProxmoxCluster(vol.Cluster())
if err != nil {
klog.Errorf("failed to get proxmox cluster: %v", err)

Expand Down Expand Up @@ -361,7 +365,7 @@ func (d *ControllerService) GetCapacity(_ context.Context, request *csi.GetCapac

klog.V(4).Infof("GetCapacity: region=%s, zone=%s, storageName=%s", region, zone, storageName)

cl, err := d.cluster.GetProxmoxCluster(region)
cl, err := d.Cluster.GetProxmoxCluster(region)
if err != nil {
klog.Errorf("failed to get proxmox cluster: %v", err)

Expand All @@ -379,7 +383,7 @@ func (d *ControllerService) GetCapacity(_ context.Context, request *csi.GetCapac
klog.Errorf("GetCapacity: failed to get storage status: %v", err)

if !strings.Contains(err.Error(), "Parameter verification failed") {
return nil, status.Error(codes.InvalidArgument, err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
} else {
availableCapacity = int64(storage["avail"].(float64))
Expand Down Expand Up @@ -444,7 +448,7 @@ func (d *ControllerService) ControllerExpandVolume(_ context.Context, request *c
return nil, status.Error(codes.InvalidArgument, err.Error())
}

cl, err := d.cluster.GetProxmoxCluster(vol.Cluster())
cl, err := d.Cluster.GetProxmoxCluster(vol.Cluster())
if err != nil {
klog.Errorf("failed to get proxmox cluster: %v", err)

Expand All @@ -455,7 +459,7 @@ func (d *ControllerService) ControllerExpandVolume(_ context.Context, request *c
if err != nil {
klog.Errorf("failed to check if pvc exists: %v", err)

return nil, status.Error(codes.Internal, err.Error())
return nil, status.Error(codes.NotFound, err.Error())
}

if !exist {
Expand Down Expand Up @@ -505,8 +509,7 @@ func (d *ControllerService) ControllerExpandVolume(_ context.Context, request *c

device := "scsi" + strconv.Itoa(lun)

_, err = cl.ResizeQemuDiskRaw(vmr, device, fmt.Sprintf("%dG", volSizeGB))
if err != nil {
if _, err := cl.ResizeQemuDiskRaw(vmr, device, fmt.Sprintf("%dG", volSizeGB)); err != nil {
klog.Errorf("failed to resize vm disk: %s, %v", vol.Disk(), err)

return nil, status.Error(codes.Internal, err.Error())
Expand Down
Loading

0 comments on commit 8ed6376

Please sign in to comment.