@@ -137,7 +137,11 @@ func (oc *OpenstackClient) Create(ctx context.Context, machine *machinev1.Machin
137137 "Cannot unmarshal providerSpec field: %v" , err ), createEventAction )
138138 }
139139
140- if verr := oc .validateMachine (machine , providerSpec ); verr != nil {
140+ if err = oc .validateMachine (machine ); err != nil {
141+ verr := & apierrors.MachineError {
142+ Reason : machinev1 .CreateMachineError ,
143+ Message : err .Error (),
144+ }
141145 return oc .handleMachineError (machine , verr , createEventAction )
142146 }
143147
@@ -321,6 +325,14 @@ func (oc *OpenstackClient) Delete(ctx context.Context, machine *machinev1.Machin
321325}
322326
323327func (oc * OpenstackClient ) Update (ctx context.Context , machine * machinev1.Machine ) error {
328+ if err := oc .validateMachine (machine ); err != nil {
329+ verr := & apierrors.MachineError {
330+ Reason : machinev1 .UpdateMachineError ,
331+ Message : err .Error (),
332+ }
333+ return oc .handleMachineError (machine , verr , createEventAction )
334+ }
335+
324336 clusterInfra , err := oc .params .ConfigClient .Infrastructures ().Get (context .TODO (), "cluster" , metav1.GetOptions {})
325337 if err != nil {
326338 return fmt .Errorf ("Failed to retrieve cluster Infrastructure object: %v" , err )
@@ -619,7 +631,7 @@ func (oc *OpenstackClient) instanceExists(machine *machinev1.Machine) (instance
619631
620632 instanceList , err := machineService .GetInstanceList (opts )
621633 if err != nil {
622- return nil , fmt .Errorf ("\n Error listing the instances (machine/actuator.go 472) : %v" , err )
634+ return nil , fmt .Errorf ("\n Error listing the instances: %v" , err )
623635 }
624636 if len (instanceList ) == 0 {
625637 return nil , nil
@@ -650,7 +662,24 @@ func (oc *OpenstackClient) createBootstrapToken() (string, error) {
650662 ), nil
651663}
652664
653- func (oc * OpenstackClient ) validateMachine (machine * machinev1.Machine , config * openstackconfigv1.OpenstackProviderSpec ) * apierrors.MachineError {
654- // TODO: other validate of openstackCloud
665+ func (oc * OpenstackClient ) validateMachine (machine * machinev1.Machine ) error {
666+ machineSpec , err := openstackconfigv1 .MachineSpecFromProviderSpec (machine .Spec .ProviderSpec )
667+ if err != nil {
668+ return fmt .Errorf ("\n Error getting the machine spec from the provider spec: %v" , err )
669+ }
670+
671+ machineService , err := clients .NewInstanceServiceFromMachine (oc .params .KubeClient , machine )
672+ if err != nil {
673+ return fmt .Errorf ("\n Error getting a new instance service from the machine: %v" , err )
674+ }
675+
676+ // TODO(mfedosin): add more validations here
677+
678+ // Validate that flavor exists
679+ err = machineService .DoesFlavorExist (machineSpec .Flavor )
680+ if err != nil {
681+ return fmt .Errorf ("Can't find a flavor with name %v: %v" , machineSpec .Flavor , err )
682+ }
683+
655684 return nil
656685}
0 commit comments