Skip to content

Commit 48d0322

Browse files
committed
Update NICs and providerID on create
1 parent 17ec858 commit 48d0322

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

pkg/cloud/azure/actuators/machine/actuator_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,9 @@ func TestReconcileVMSuceededState(t *testing.T) {
343343
t.Errorf("failed to create machine: %+v", err)
344344
}
345345

346-
if fakeVMService.GetCallCount != 1 {
347-
t.Errorf("expected get to be called just once")
346+
if fakeVMService.GetCallCount != 2 {
347+
// One call for create, and one for the update that happens when the create is successful.
348+
t.Errorf("expected get to be called exactly twice")
348349
}
349350

350351
if fakeVMService.DeleteCallCount != 0 {
@@ -359,11 +360,18 @@ func TestReconcileVMSuceededState(t *testing.T) {
359360
// FakeVMCheckZonesService generic fake vm zone service
360361
type FakeVMCheckZonesService struct {
361362
checkZones []string
363+
created bool
362364
}
363365

364366
// Get returns fake success.
365367
func (s *FakeVMCheckZonesService) Get(ctx context.Context, spec azure.Spec) (interface{}, error) {
366-
return nil, errors.New("vm not found")
368+
if !s.created {
369+
return nil, errors.New("vm not found")
370+
}
371+
372+
return &compute.VirtualMachine{
373+
VirtualMachineProperties: &compute.VirtualMachineProperties{},
374+
}, nil
367375
}
368376

369377
// CreateOrUpdate returns fake success.
@@ -374,10 +382,12 @@ func (s *FakeVMCheckZonesService) CreateOrUpdate(ctx context.Context, spec azure
374382
}
375383

376384
if len(s.checkZones) <= 0 {
385+
s.created = true
377386
return nil
378387
}
379388
for _, zone := range s.checkZones {
380389
if strings.EqualFold(zone, vmSpec.Zone) {
390+
s.created = true
381391
return nil
382392
}
383393
}

pkg/cloud/azure/actuators/machine/reconciler.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
2929
"github.com/Azure/go-autorest/autorest"
30+
autorestazure "github.com/Azure/go-autorest/autorest/azure"
3031
machinev1 "github.com/openshift/api/machine/v1beta1"
3132
machinecontroller "github.com/openshift/machine-api-operator/pkg/controller/machine"
3233
"github.com/openshift/machine-api-operator/pkg/metrics"
@@ -139,6 +140,13 @@ func (s *Reconciler) CreateMachine(ctx context.Context) error {
139140
return fmt.Errorf("failed to create vm %s: %w", s.scope.Machine.Name, err)
140141
}
141142

143+
// Once we have created the machine, attempt to update it.
144+
// This should set the network addresses and provider ID which should be set as soon
145+
// as the machine is created, moving it to the provisioned phase.
146+
if err := s.Update(ctx); err != nil {
147+
return fmt.Errorf("failed to update machine %s: %w", s.scope.Machine.Name, err)
148+
}
149+
142150
return nil
143151
}
144152

@@ -700,8 +708,8 @@ func (s *Reconciler) createVirtualMachine(ctx context.Context, nicName, asName s
700708
vmSpec.CustomData = userData
701709
}
702710

703-
err = s.virtualMachinesSvc.CreateOrUpdate(ctx, vmSpec)
704-
if err != nil {
711+
// If we get an AsynOpIncompleteError, this means the VM is being created and we completed the request successfully.
712+
if err := s.virtualMachinesSvc.CreateOrUpdate(ctx, vmSpec); err != nil && !errors.Is(err, autorestazure.NewAsyncOpIncompleteError("compute.VirtualMachinesCreateOrUpdateFuture")) {
705713
metrics.RegisterFailedInstanceCreate(&metrics.MachineLabels{
706714
Name: s.scope.Machine.Name,
707715
Namespace: s.scope.Machine.Namespace,
@@ -712,6 +720,7 @@ func (s *Reconciler) createVirtualMachine(ctx context.Context, nicName, asName s
712720
if errors.As(err, &detailedError) && detailedError.Message == "Failure sending request" {
713721
return machinecontroller.InvalidMachineConfiguration("failure sending request for machine %s: %v", s.scope.Machine.Name, err)
714722
}
723+
715724
return fmt.Errorf("failed to create VM: %w", err)
716725
}
717726
} else if err != nil {

0 commit comments

Comments
 (0)