Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions cmd/stackit-csi-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import (
)

var (
endpoint string
cloudConfig string
cluster string
metricsAddress string
provideControllerService bool
provideNodeService bool
legacyStorageMode bool
legacyVolumeCreation bool
endpoint string
cloudConfig string
cluster string
metricsAddress string
provideControllerService bool
provideNodeService bool
legacyStorageMode bool
legacyVolumeCreation bool
deleteVolumesInErrorState bool
)

func main() {
Expand Down Expand Up @@ -85,6 +86,7 @@ func main() {
cmd.PersistentFlags().BoolVar(&legacyStorageMode, "legacy-storage-mode", false,
"Configures the CSI to listen to the legacy storage driverName cinder.csi.openstack.org instead")
cmd.PersistentFlags().BoolVar(&legacyVolumeCreation, "legacy-volume-creation", true, "Enable or disable support for creating volumes with the old driverName (cinder.csi.openstack.org)")
cmd.PersistentFlags().BoolVar(&deleteVolumesInErrorState, "delete-volumes-in-error", false, "Delete volumes in error state when creating")

stackitclient.AddExtraFlags(pflag.CommandLine)

Expand Down Expand Up @@ -117,6 +119,10 @@ func handle(ctx context.Context) {
driverOpts.BlockVolumeCreation = true
}

if deleteVolumesInErrorState {
driverOpts.DeleteVolumesInErrorState = true
}

d := blockstorage.NewDriver(driverOpts)

if provideControllerService {
Expand Down
19 changes: 17 additions & 2 deletions pkg/csi/blockstorage/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
return nil, status.Error(codes.AlreadyExists, "Volume Already exists with same name and different capacity")
}
if *vols[0].Status != stackitclient.VolumeAvailableStatus {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if *vols[0].Status != stackitclient.VolumeAvailableStatus {
if vols[0].GetStatus() != stackitclient.VolumeAvailableStatus {
if cs.Driver.deleteVolumesInErrorState {
cs.deleteVolumeInError(ctx, &vols[0])
}
return nil, status.Errorf(codes.Internal, "Volume %s is not in available state", vols[0].GetId())
}

Rationale: Use getter methods GetStatus() and GetId() (or status.Errorf) to safely prevent potential nil pointer dereferences when inspecting vols[0].

if cs.Driver.deleteVolumesInErrorState {
cs.deleteVolumeInError(ctx, &vols[0])
}
return nil, status.Error(codes.Internal, fmt.Sprintf("Volume %s is not in available state", *vols[0].Id))
}
klog.V(4).Infof("Volume %s already exists in Availability Zone: %s of size %d GiB", *vols[0].Id, vols[0].AvailabilityZone, *vols[0].Size)
Expand Down Expand Up @@ -265,14 +268,14 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol

targetStatus := []string{stackitclient.VolumeAvailableStatus}
// Recheck after: 0s (immediate), 20s, 45.6s, 78.36s, 120.31s
err = cloud.WaitVolumeTargetStatusWithCustomBackoff(ctx, *vol.Id, targetStatus,
err = cloud.WaitVolumeTargetStatusWithCustomBackoff(ctx, &vol, targetStatus,
&wait.Backoff{
Duration: 20 * time.Second,
Steps: 5,
Factor: 1.28,
})
if err != nil {
klog.Errorf("Failed to WaitVolumeTargetStatus of volume %s: %v", *vol.Id, err)
klog.Errorf("Failed to WaitVolumeTargetStatus of volume %s: %v", vol.GetId(), err)
return nil, status.Error(codes.Internal, fmt.Sprintf("CreateVolume Volume %s failed getting available in time: %v", *vol.Id, err))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return nil, status.Error(codes.Internal, fmt.Sprintf("CreateVolume Volume %s failed getting available in time: %v", *vol.Id, err))
return nil, status.Errorf(codes.Internal, "CreateVolume Volume %s failed getting available in time: %v", vol.GetId(), err)

Rationale: Use vol.GetId() rather than direct pointer dereference *vol.Id to maintain nil-safety and consistency with line 278.

}

Expand All @@ -281,6 +284,18 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
return cs.getCreateVolumeResponse(vol), nil
}

func (cs *controllerServer) deleteVolumeInError(ctx context.Context, vol *iaas.Volume) {
cloud := cs.Instance

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cloud := cs.Instance
func (cs *controllerServer) deleteVolumeInError(ctx context.Context, vol *iaas.Volume) {
if vol == nil {
return
}
cloud := cs.Instance

Rationale: Add a defensive nil check on vol before accessing vol.GetStatus() and vol.GetId().

if vol.GetStatus() == stackitclient.VolumeErrorStatus {
klog.Warningf("Volume %s entered ERROR status, attempting cleanup deletion...", vol.GetId())
if deleteErr := cloud.DeleteVolume(ctx, vol.GetId()); deleteErr != nil {
klog.Errorf("Failed to delete erroneous volume %s: %v", vol.GetId(), deleteErr)
} else {
klog.Infof("Successfully deleted erroneous volume %s", vol.GetId())
}
}
}

func setVolumeEncryptionParameters(opts *iaas.CreateVolumePayload, volParams *stackitParameterConfig) error {
err := validateEncryptionConfig(volParams)
if err != nil {
Expand Down
120 changes: 75 additions & 45 deletions pkg/csi/blockstorage/controllerserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ var _ = Describe("ControllerServer test", Ordered, func() {

iaasClient.EXPECT().GetVolumesByName(gomock.Any(), "new volume").Return([]iaas.Volume{}, nil)

iaasClient.EXPECT().CreateVolume(gomock.Any(), gomock.Any()).Return(&iaas.Volume{
vol := &iaas.Volume{
Id: new("volume-id"),
Name: new("new volume"),
AvailabilityZone: "eu01",
Size: new(int64(20)),
}, nil)
iaasClient.EXPECT().WaitVolumeTargetStatusWithCustomBackoff(gomock.Any(), "volume-id", gomock.Any(), gomock.Any()).Return(nil)
}

iaasClient.EXPECT().CreateVolume(gomock.Any(), gomock.Any()).Return(vol, nil)
iaasClient.EXPECT().WaitVolumeTargetStatusWithCustomBackoff(gomock.Any(), &vol, gomock.Any(), gomock.Any()).Return(nil)

resp, err := fakeCs.CreateVolume(context.Background(), req)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -122,13 +124,15 @@ var _ = Describe("ControllerServer test", Ordered, func() {

iaasClient.EXPECT().GetVolumesByName(gomock.Any(), "volume name").Return([]iaas.Volume{}, nil)

iaasClient.EXPECT().CreateVolume(gomock.Any(), gomock.Any()).Return(&iaas.Volume{
vol := &iaas.Volume{
Id: new("volume-id"),
Name: new("volume name"),
AvailabilityZone: "zone-from-parameters",
Size: new(int64(20)),
}, nil)
iaasClient.EXPECT().WaitVolumeTargetStatusWithCustomBackoff(gomock.Any(), "volume-id", gomock.Any(), gomock.Any()).Return(nil)
}

iaasClient.EXPECT().CreateVolume(gomock.Any(), gomock.Any()).Return(vol, nil)
iaasClient.EXPECT().WaitVolumeTargetStatusWithCustomBackoff(gomock.Any(), &vol, gomock.Any(), gomock.Any()).Return(nil)

_, err := fakeCs.CreateVolume(context.Background(), req)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -150,13 +154,15 @@ var _ = Describe("ControllerServer test", Ordered, func() {

iaasClient.EXPECT().GetVolumesByName(gomock.Any(), "volume name").Return([]iaas.Volume{}, nil)

iaasClient.EXPECT().CreateVolume(gomock.Any(), gomock.Any()).Return(&iaas.Volume{
vol := &iaas.Volume{
Id: new("volume-id"),
Name: new("volume name"),
AvailabilityZone: "zone-from-accessibility-reqs",
Size: new(int64(20)),
}, nil)
iaasClient.EXPECT().WaitVolumeTargetStatusWithCustomBackoff(gomock.Any(), "volume-id", gomock.Any(), gomock.Any()).Return(nil)
}

iaasClient.EXPECT().CreateVolume(gomock.Any(), gomock.Any()).Return(vol, nil)
iaasClient.EXPECT().WaitVolumeTargetStatusWithCustomBackoff(gomock.Any(), &vol, gomock.Any(), gomock.Any()).Return(nil)

_, err := fakeCs.CreateVolume(context.Background(), req)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -246,6 +252,31 @@ var _ = Describe("ControllerServer test", Ordered, func() {
Expect(err.Error()).To(ContainSubstring("is not in available state"))
})

It("should delete an existing volume in error state when cleanup is enabled", func() {
req := &csi.CreateVolumeRequest{
Name: "new volume",
VolumeCapabilities: stdVolCaps,
CapacityRange: stdCapRange,
}
fakeCs.Driver.deleteVolumesInErrorState = true

iaasClient.EXPECT().GetVolumesByName(gomock.Any(), "new volume").Return([]iaas.Volume{
{
Id: new("existing-error-volume-id"),
Name: new("new volume"),
Size: new(int64(20)),
Status: new(stackitclient.VolumeErrorStatus),
AvailabilityZone: "eu01",
},
}, nil)
iaasClient.EXPECT().DeleteVolume(gomock.Any(), "existing-error-volume-id").Return(nil)

_, err := fakeCs.CreateVolume(context.Background(), req)
Expect(err).To(HaveOccurred())
Expect(status.Code(err)).To(Equal(codes.Internal))
Expect(err.Error()).To(ContainSubstring("is not in available state"))
})

It("should fail if more than one volume with the same name are available", func() {
req := &csi.CreateVolumeRequest{
Name: "new volume",
Expand Down Expand Up @@ -303,24 +334,23 @@ var _ = Describe("ControllerServer test", Ordered, func() {
VolumeId: "snapshot-volume-id",
AvailabilityZone: new("eu01"),
}, nil)

vol := &iaas.Volume{
Id: new("volume-id"),
Name: new("new volume"),
AvailabilityZone: "eu01",
Size: new(int64(20)),
}

iaasClient.EXPECT().
CreateVolume(gomock.Any(), gomock.Any()).
DoAndReturn(func(_ context.Context, opts iaas.CreateVolumePayload) (*iaas.Volume, error) {
Expect(opts.Source.Id).To(Equal("snapshot-id"))
Expect(opts.Source.Type).To(Equal("snapshot"))

volumeID := "volume-id"
name := "new volume"
size := int64(20)

return &iaas.Volume{
Id: &volumeID,
Name: &name,
AvailabilityZone: "eu01",
Size: &size,
}, nil
return vol, nil
})
iaasClient.EXPECT().WaitVolumeTargetStatusWithCustomBackoff(gomock.Any(), "volume-id", gomock.Any(), gomock.Any()).Return(nil)
iaasClient.EXPECT().WaitVolumeTargetStatusWithCustomBackoff(gomock.Any(), &vol, gomock.Any(), gomock.Any()).Return(nil)

_, err := fakeCs.CreateVolume(context.Background(), req)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -379,24 +409,23 @@ var _ = Describe("ControllerServer test", Ordered, func() {
Status: new("AVAILABLE"),
AvailabilityZone: new("eu01"),
}, nil)

vol := &iaas.Volume{
Id: new("volume-id"),
Name: new("new volume"),
AvailabilityZone: "eu01",
Size: new(int64(20)),
}

iaasClient.EXPECT().
CreateVolume(gomock.Any(), gomock.Any()).
DoAndReturn(func(_ context.Context, opts iaas.CreateVolumePayload) (*iaas.Volume, error) {
Expect(opts.Source.Id).To(Equal("snapshot-id"))
Expect(opts.Source.Type).To(Equal("backup"))

volumeID := "volume-id"
name := "new volume"
size := int64(20)

return &iaas.Volume{
Id: &volumeID,
Name: &name,
AvailabilityZone: "eu01",
Size: &size,
}, nil
return vol, nil
})
iaasClient.EXPECT().WaitVolumeTargetStatusWithCustomBackoff(gomock.Any(), "volume-id", gomock.Any(), gomock.Any()).Return(nil)
iaasClient.EXPECT().WaitVolumeTargetStatusWithCustomBackoff(gomock.Any(), &vol, gomock.Any(), gomock.Any()).Return(nil)

_, err := fakeCs.CreateVolume(context.Background(), req)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -490,24 +519,23 @@ var _ = Describe("ControllerServer test", Ordered, func() {
Status: new("AVAILABLE"),
AvailabilityZone: "eu01",
}, nil)

vol := &iaas.Volume{
Id: new("volume-id"),
Name: new("new volume"),
AvailabilityZone: "eu01",
Size: new(int64(20)),
}

iaasClient.EXPECT().
CreateVolume(gomock.Any(), gomock.Any()).
DoAndReturn(func(_ context.Context, opts iaas.CreateVolumePayload) (*iaas.Volume, error) {
Expect(opts.Source.Id).To(Equal("volume-source-id"))
Expect(opts.Source.Type).To(Equal("volume"))

name := "new volume"
volumeID := "volume-id"
size := int64(20)

return &iaas.Volume{
Id: &volumeID,
Name: &name,
AvailabilityZone: "eu01",
Size: &size,
}, nil
return vol, nil
})
iaasClient.EXPECT().WaitVolumeTargetStatusWithCustomBackoff(gomock.Any(), "volume-id", gomock.Any(), gomock.Any()).Return(nil)
iaasClient.EXPECT().WaitVolumeTargetStatusWithCustomBackoff(gomock.Any(), &vol, gomock.Any(), gomock.Any()).Return(nil)

_, err := fakeCs.CreateVolume(context.Background(), req)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -578,13 +606,15 @@ var _ = Describe("ControllerServer test", Ordered, func() {

iaasClient.EXPECT().GetVolumesByName(gomock.Any(), "new volume").Return([]iaas.Volume{}, nil)

iaasClient.EXPECT().CreateVolume(gomock.Any(), gomock.Any()).Return(&iaas.Volume{
vol := &iaas.Volume{
Id: new("volume-id"),
Name: new("new volume"),
AvailabilityZone: "eu01",
Size: new(int64(20)),
}, nil)
iaasClient.EXPECT().WaitVolumeTargetStatusWithCustomBackoff(gomock.Any(), "volume-id", gomock.Any(), gomock.Any()).
}

iaasClient.EXPECT().CreateVolume(gomock.Any(), gomock.Any()).Return(vol, nil)
iaasClient.EXPECT().WaitVolumeTargetStatusWithCustomBackoff(gomock.Any(), &vol, gomock.Any(), gomock.Any()).
Return(fmt.Errorf("injected error"))

_, err := fakeCs.CreateVolume(context.Background(), req)
Expand Down
26 changes: 16 additions & 10 deletions pkg/csi/blockstorage/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ var (
)

type Driver struct {
name string
fqVersion string // Fully qualified version in format {Version}@{CPO version}
endpoint string
clusterID string
legacyDriver bool
blockVolumeCreation bool
name string
fqVersion string // Fully qualified version in format {Version}@{CPO version}
endpoint string
clusterID string
legacyDriver bool
blockVolumeCreation bool
deleteVolumesInErrorState bool

ids *identityServer
cs *controllerServer
Expand All @@ -51,10 +52,11 @@ type Driver struct {
}

type DriverOpts struct {
ClusterID string
Endpoint string
LegacyDriverName bool
BlockVolumeCreation bool
ClusterID string
Endpoint string
LegacyDriverName bool
BlockVolumeCreation bool
DeleteVolumesInErrorState bool

PVCLister corev1.PersistentVolumeClaimLister
}
Expand All @@ -73,6 +75,10 @@ func NewDriver(o *DriverOpts) *Driver {
d.legacyDriver = true
}

if o.DeleteVolumesInErrorState {
d.deleteVolumesInErrorState = true
}

if o.BlockVolumeCreation {
d.blockVolumeCreation = true
}
Expand Down
Loading