Skip to content

Commit

Permalink
[vSphere] Fail machine on invalid provider spec value
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Demichev committed May 19, 2020
1 parent 3fa1f70 commit 6672ec4
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 111 deletions.
40 changes: 26 additions & 14 deletions pkg/controller/vsphere/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,28 +432,26 @@ func clone(s *machineScope) (string, error) {

folder, err := s.GetSession().Finder.FolderOrDefault(s, folderPath)
if err != nil {
return "", fmt.Errorf("unable to get folder for %q: %w", folderPath, err)
multipleFoundMsg := "multiple folders found, specify one in config"
notFoundMsg := "folder not found, specify valid value"
defaultError := fmt.Errorf("unable to get folder for %q: %w", folderPath, err)
return "", handleVsphereError(multipleFoundMsg, notFoundMsg, defaultError, err)
}

datastore, err := s.GetSession().Finder.DatastoreOrDefault(s, datastorePath)
if err != nil {
return "", fmt.Errorf("unable to get datastore for %q: %w", datastorePath, err)
multipleFoundMsg := "multiple datastores found, specify one in config"
notFoundMsg := "datastore not found, specify valid value"
defaultError := fmt.Errorf("unable to get datastore for %q: %w", datastorePath, err)
return "", handleVsphereError(multipleFoundMsg, notFoundMsg, defaultError, err)
}

resourcepool, err := s.GetSession().Finder.ResourcePoolOrDefault(s, resourcepoolPath)
if err != nil {
// TODO: move error checks to provider spec validation
var multipleFoundError *find.MultipleFoundError
var notFoundError *find.NotFoundError
if errors.As(err, &multipleFoundError) {
return "", machinecontroller.InvalidMachineConfiguration("multiple resource pools found, specify one in config")
}

if errors.As(err, &notFoundError) {
return "", machinecontroller.InvalidMachineConfiguration("resource pool not found, specify valid value")
}

return "", fmt.Errorf("unable to get resource pool for %q: %w", resourcepool, err)
multipleFoundMsg := "multiple resource pools found, specify one in config"
notFoundMsg := "resource pool not found, specify valid value"
defaultError := fmt.Errorf("unable to get resource pool for %q: %w", resourcepool, err)
return "", handleVsphereError(multipleFoundMsg, notFoundMsg, defaultError, err)
}

numCPUs := s.providerSpec.NumCPUs
Expand Down Expand Up @@ -653,6 +651,20 @@ func setProviderStatus(taskRef string, condition vspherev1.VSphereMachineProvide
return nil
}

func handleVsphereError(multipleFoundMsg, notFoundMsg string, defaultError, vsphereError error) error {
var multipleFoundError *find.MultipleFoundError
var notFoundError *find.NotFoundError
if errors.As(vsphereError, &multipleFoundError) {
return machinecontroller.InvalidMachineConfiguration(multipleFoundMsg)
}

if errors.As(vsphereError, &notFoundError) {
return machinecontroller.InvalidMachineConfiguration(notFoundMsg)
}

return defaultError
}

type virtualMachine struct {
context.Context
Ref types.ManagedObjectReference
Expand Down

0 comments on commit 6672ec4

Please sign in to comment.