vSphere: Return useful errors from parameter validation

Returning "VolumeOptions verification failed" when StorageClass parameter
validation failed is not really useful. It does not tell which parameter is
wrong and what is a VolumeOption at all.

Trying to return something more useful here.
This commit is contained in:
Jan Safranek
2021-04-14 17:19:37 +02:00
parent 318db993c8
commit 6cf84f819e
3 changed files with 18 additions and 19 deletions

View File

@@ -20,20 +20,18 @@ import "errors"
// Error Messages // Error Messages
const ( const (
FileAlreadyExistErrMsg = "File requested already exist" FileAlreadyExistErrMsg = "File requested already exist"
NoDiskUUIDFoundErrMsg = "No disk UUID found" NoDiskUUIDFoundErrMsg = "No disk UUID found"
NoDevicesFoundErrMsg = "No devices found" NoDevicesFoundErrMsg = "No devices found"
DiskNotFoundErrMsg = "No vSphere disk ID found" DiskNotFoundErrMsg = "No vSphere disk ID found"
InvalidVolumeOptionsErrMsg = "VolumeOptions verification failed" NoVMFoundErrMsg = "No VM found"
NoVMFoundErrMsg = "No VM found"
) )
// Error constants // Error constants
var ( var (
ErrFileAlreadyExist = errors.New(FileAlreadyExistErrMsg) ErrFileAlreadyExist = errors.New(FileAlreadyExistErrMsg)
ErrNoDiskUUIDFound = errors.New(NoDiskUUIDFoundErrMsg) ErrNoDiskUUIDFound = errors.New(NoDiskUUIDFoundErrMsg)
ErrNoDevicesFound = errors.New(NoDevicesFoundErrMsg) ErrNoDevicesFound = errors.New(NoDevicesFoundErrMsg)
ErrNoDiskIDFound = errors.New(DiskNotFoundErrMsg) ErrNoDiskIDFound = errors.New(DiskNotFoundErrMsg)
ErrInvalidVolumeOptions = errors.New(InvalidVolumeOptionsErrMsg) ErrNoVMFound = errors.New(NoVMFoundErrMsg)
ErrNoVMFound = errors.New(NoVMFoundErrMsg)
) )

View File

@@ -64,9 +64,9 @@ func (virtualDisk *VirtualDisk) Create(ctx context.Context, datastore *vclib.Dat
if virtualDisk.VolumeOptions.DiskFormat == "" { if virtualDisk.VolumeOptions.DiskFormat == "" {
virtualDisk.VolumeOptions.DiskFormat = vclib.ThinDiskType virtualDisk.VolumeOptions.DiskFormat = vclib.ThinDiskType
} }
if !virtualDisk.VolumeOptions.VerifyVolumeOptions() { if err := virtualDisk.VolumeOptions.VerifyVolumeOptions(); err != nil {
klog.Error("VolumeOptions verification failed. volumeOptions: ", virtualDisk.VolumeOptions) klog.Errorf("VolumeOptions verification failed: %s (options: %+v)", err, virtualDisk.VolumeOptions)
return "", vclib.ErrInvalidVolumeOptions return "", fmt.Errorf("validation of parameters failed: %s", err)
} }
if virtualDisk.VolumeOptions.StoragePolicyID != "" && virtualDisk.VolumeOptions.StoragePolicyName != "" { if virtualDisk.VolumeOptions.StoragePolicyID != "" && virtualDisk.VolumeOptions.StoragePolicyName != "" {
return "", fmt.Errorf("Storage Policy ID and Storage Policy Name both set, Please set only one parameter") return "", fmt.Errorf("Storage Policy ID and Storage Policy Name both set, Please set only one parameter")

View File

@@ -17,6 +17,7 @@ limitations under the License.
package vclib package vclib
import ( import (
"fmt"
"strings" "strings"
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
@@ -90,21 +91,21 @@ func CheckControllerSupported(ctrlType string) bool {
} }
// VerifyVolumeOptions checks if volumeOptions.SCIControllerType is valid controller type // VerifyVolumeOptions checks if volumeOptions.SCIControllerType is valid controller type
func (volumeOptions VolumeOptions) VerifyVolumeOptions() bool { func (volumeOptions VolumeOptions) VerifyVolumeOptions() error {
// Validate only if SCSIControllerType is set by user. // Validate only if SCSIControllerType is set by user.
// Default value is set later in virtualDiskManager.Create and vmDiskManager.Create // Default value is set later in virtualDiskManager.Create and vmDiskManager.Create
if volumeOptions.SCSIControllerType != "" { if volumeOptions.SCSIControllerType != "" {
isValid := CheckControllerSupported(volumeOptions.SCSIControllerType) isValid := CheckControllerSupported(volumeOptions.SCSIControllerType)
if !isValid { if !isValid {
return false return fmt.Errorf("invalid scsiControllerType: %s", volumeOptions.SCSIControllerType)
} }
} }
// ThinDiskType is the default, so skip the validation. // ThinDiskType is the default, so skip the validation.
if volumeOptions.DiskFormat != ThinDiskType { if volumeOptions.DiskFormat != ThinDiskType {
isValid := CheckDiskFormatSupported(volumeOptions.DiskFormat) isValid := CheckDiskFormatSupported(volumeOptions.DiskFormat)
if !isValid { if !isValid {
return false return fmt.Errorf("invalid diskFormat: %s", volumeOptions.DiskFormat)
} }
} }
return true return nil
} }