Skip to content

Commit

Permalink
Merge pull request #30 from civo/feat/add-error-info
Browse files Browse the repository at this point in the history
Add additional information to the error message
  • Loading branch information
hlts2 authored Nov 19, 2024
2 parents 50f8c13 + 6822501 commit 418cece
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
22 changes: 11 additions & 11 deletions pkg/driver/controller_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const BytesInGigabyte int64 = 1024 * 1024 * 1024
const CivoVolumeAvailableRetries int = 20

var supportedAccessModes = map[csi.VolumeCapability_AccessMode_Mode]struct{}{
csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER: struct{}{},
csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY: struct{}{},
csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER: {},
csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY: {},
}

// CreateVolume is the first step when a PVC tries to create a dynamic volume
Expand Down Expand Up @@ -105,10 +105,10 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
availableSize := int64(quota.DiskGigabytesLimit - quota.DiskGigabytesUsage)
if availableSize < desiredSize {
log.Error().Msg("Requested volume would exceed storage quota available")
return nil, status.Error(codes.OutOfRange, fmt.Sprintf("Requested volume would exceed volume space quota by %d GB", desiredSize-availableSize))
return nil, status.Errorf(codes.OutOfRange, "Requested volume would exceed volume space quota by %d GB", desiredSize-availableSize)
} else if quota.DiskVolumeCountUsage >= quota.DiskVolumeCountLimit {
log.Error().Msg("Requested volume would exceed volume quota available")
return nil, status.Error(codes.OutOfRange, fmt.Sprintf("Requested volume would exceed volume count limit quota of %d", quota.DiskVolumeCountLimit))
return nil, status.Errorf(codes.OutOfRange, "Requested volume would exceed volume count limit quota of %d", quota.DiskVolumeCountLimit)
}

log.Debug().Int("disk_gb_limit", quota.DiskGigabytesLimit).Int("disk_gb_usage", quota.DiskGigabytesUsage).Msg("Quota has sufficient capacity remaining")
Expand Down Expand Up @@ -152,7 +152,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
}

log.Error().Err(err).Msg("Civo Volume is not 'available'")
return nil, status.Errorf(codes.Unavailable, "Civo Volume is not 'available'")
return nil, status.Errorf(codes.Unavailable, "Civo Volume %q is not \"available\", state currently is %q", volume.ID, volume.Status)
}

// waitForVolumeAvailable will just sleep/loop waiting for Civo's API to report it's available, or hit a defined
Expand Down Expand Up @@ -226,7 +226,7 @@ func (d *Driver) ControllerPublishVolume(ctx context.Context, req *csi.Controlle
log.Debug().Msg("Check if Node exits")
cluster, err := d.CivoClient.GetKubernetesCluster(d.ClusterID)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to connect to Civo Api. error: %s", err.Error()))
return nil, status.Errorf(codes.Internal, "unable to connect to Civo Api. error: %s", err)
}
found := false
for _, instance := range cluster.Instances {
Expand Down Expand Up @@ -261,7 +261,7 @@ func (d *Driver) ControllerPublishVolume(ctx context.Context, req *csi.Controlle
Str("requested_instance_id", req.NodeId).
Str("current_instance_id", volume.InstanceID).
Msg("Volume is not available to be attached")
return nil, status.Errorf(codes.Unavailable, "Volume is not available to be attached")
return nil, status.Errorf(codes.Unavailable, "Volume %q is not available to be attached, state is currently %q", volume.ID, volume.Status)
}

// Check if the volume is attaching to this node
Expand Down Expand Up @@ -293,12 +293,12 @@ func (d *Driver) ControllerPublishVolume(ctx context.Context, req *csi.Controlle
}
if volume.Status != "attached" {
log.Error().Str("volume_id", volume.ID).Str("status", volume.Status).Msg("Volume is not in the attached state")
return nil, status.Errorf(codes.Unavailable, "Volume is not attached to the requested instance")
return nil, status.Errorf(codes.Unavailable, "Volume %q is not attached to the requested instance, state is currently %q", volume.ID, volume.Status)
}

if volume.InstanceID != req.NodeId {
log.Error().Str("volume_id", volume.ID).Str("instance_id", req.NodeId).Msg("Volume is not attached to the requested instance")
return nil, status.Errorf(codes.Unavailable, "Volume is not attached to the requested instance")
return nil, status.Errorf(codes.Unavailable, "Volume %q is not attached to the requested instance %q, instance id is currently %q", volume.ID, req.NodeId, volume.InstanceID)
}

log.Debug().Str("volume_id", volume.ID).Msg("Volume successfully attached in Civo API")
Expand Down Expand Up @@ -374,7 +374,7 @@ func (d *Driver) ControllerUnpublishVolume(ctx context.Context, req *csi.Control

// err that the the volume is not available
log.Error().Msg("Civo Volume did not go back to 'available' status")
return nil, status.Errorf(codes.Unavailable, "Civo Volume did not go back to 'available'")
return nil, status.Errorf(codes.Unavailable, "Civo Volume %q did not go back to \"available\", state is currently %q", req.VolumeId, volume.Status)
}

// ControllerExpandVolume allows for offline expansion of Volumes
Expand Down Expand Up @@ -461,7 +461,7 @@ func (d *Driver) ValidateVolumeCapabilities(ctx context.Context, req *csi.Valida

_, err := d.CivoClient.GetVolume(req.VolumeId)
if err != nil {
return nil, status.Error(codes.NotFound, "Unable to fetch volume from Civo API")
return nil, status.Errorf(codes.NotFound, "Unable to fetch volume from Civo API: %s", err)
}

accessModeSupported := false
Expand Down
2 changes: 1 addition & 1 deletion pkg/driver/identity_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (d *Driver) GetPluginCapabilities(context.Context, *csi.GetPluginCapabiliti
func (d *Driver) Probe(context.Context, *csi.ProbeRequest) (*csi.ProbeResponse, error) {
err := d.CivoClient.Ping()
if err != nil {
return nil, status.Error(codes.Unavailable, "unable to connect to Civo API")
return nil, status.Errorf(codes.Unavailable, "unable to connect to Civo API: %s", err)
}

return &csi.ProbeResponse{
Expand Down
21 changes: 9 additions & 12 deletions pkg/driver/node_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
// Find the disk attachment location
attachedDiskPath := d.DiskHotPlugger.PathForVolume(req.VolumeId)
if attachedDiskPath == "" {
err := status.Error(codes.NotFound, "path to volume (/dev/disk/by-id/VOLUME_ID) not found")
log.Error().Str("volume_id", req.VolumeId).Err(err)
return nil, err
log.Error().Str("volume_id", req.VolumeId).Msg("path to volume (/dev/disk/by-id/VOLUME_ID) not found")
return nil, status.Errorf(codes.NotFound, "path to volume (/dev/disk/by-id/%s) not found", req.VolumeId)
}

// Format the volume if not already formatted
Expand Down Expand Up @@ -196,7 +195,7 @@ func (d *Driver) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublish
if !mounted {
if err = os.RemoveAll(targetPath); err != nil {
log.Error().Str("targetPath", targetPath).Err(err).Msg("Failed to remove target path")
return nil, status.Error(codes.Internal, err.Error())
return nil, status.Errorf(codes.Internal, "failed to remove target path %q: %s", targetPath, err)
}

return &csi.NodeUnpublishVolumeResponse{}, nil
Expand All @@ -212,7 +211,7 @@ func (d *Driver) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublish
err = os.Remove(targetPath)
if err != nil && !os.IsNotExist(err) {
log.Error().Str("targetPath", targetPath).Err(err).Msg("Failed to remove target path")
return nil, status.Error(codes.Internal, err.Error())
return nil, status.Errorf(codes.Internal, "failed to remove target path %q: %s", targetPath, err)
}

return &csi.NodeUnpublishVolumeResponse{}, nil
Expand All @@ -225,7 +224,7 @@ func (d *Driver) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (
nodeInstanceID, region, err := d.currentNodeDetails()
if err != nil {
log.Error().Err(err).Msg("Failed to get current node details")
return nil, status.Error(codes.Internal, err.Error())
return nil, status.Errorf(codes.Internal, "failed to get current node details: %s", err)
}

log.Debug().Str("node_id", nodeInstanceID).Str("region", region).Msg("Requested information about node")
Expand Down Expand Up @@ -312,22 +311,20 @@ func (d *Driver) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVolume
_, err := d.CivoClient.GetVolume(req.VolumeId)
if err != nil {
log.Error().Str("volume_id", req.VolumeId).Err(err).Msg("Failed to find VolumeID to NodeExpandVolume")
return nil, status.Error(codes.NotFound, "unable to fund VolumeID to NodeExpandVolume")

return nil, status.Errorf(codes.NotFound, "unable to find VolumeID %q to NodeExpandVolume: %s", req.VolumeId, err)
}
// Find the disk attachment location
attachedDiskPath := d.DiskHotPlugger.PathForVolume(req.VolumeId)
if attachedDiskPath == "" {
err := status.Error(codes.NotFound, "path to volume (/dev/disk/by-id/VOLUME_ID) not found")
log.Error().Str("volume_id", req.VolumeId).Err(err)
return nil, err
log.Error().Str("volume_id", req.VolumeId).Msg("path to volume (/dev/disk/by-id/VOLUME_ID) not found")
return nil, status.Errorf(codes.NotFound, "path to volume (/dev/disk/by-id/%s) not found", req.VolumeId)
}

log.Info().Str("volume_id", req.VolumeId).Str("path", attachedDiskPath).Msg("Expanding Volume")
err = d.DiskHotPlugger.ExpandFilesystem(d.DiskHotPlugger.PathForVolume(req.VolumeId))
if err != nil {
log.Error().Str("volume_id", req.VolumeId).Err(err).Msg("Failed to expand filesystem")
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to expand file system: %s", err.Error()))
return nil, status.Errorf(codes.Internal, "failed to expand file system: %s", err)
}

// TODO: Get new size for resposne
Expand Down

0 comments on commit 418cece

Please sign in to comment.