Skip to content

Commit

Permalink
Update machine.Spec.ProviderID field
Browse files Browse the repository at this point in the history
  • Loading branch information
vikaschoudhary16 committed May 24, 2019
1 parent 0e9a428 commit f92f2f4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
33 changes: 32 additions & 1 deletion pkg/actuators/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
apierrors "github.com/openshift/cluster-api/pkg/errors"
providerconfigv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsproviderconfig/v1beta1"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"

awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/client"
Expand Down Expand Up @@ -116,7 +117,37 @@ func (a *Actuator) Create(context context.Context, cluster *clusterv1.Cluster, m
}
return err
}
return a.updateStatus(machine, instance)
updatedMachine, err := a.updateProviderID(machine, instance)
if err != nil {
return fmt.Errorf("failed to update machine object with providerID: %v", err)
}
return a.updateStatus(updatedMachine, instance)
}

// updateProviderID adds providerID in the machine spec
func (a *Actuator) updateProviderID(machine *machinev1.Machine, instance *ec2.Instance) (*machinev1.Machine, error) {
existingProviderID := machine.Spec.ProviderID
machineCopy := machine.DeepCopy()
if instance != nil {
providerID := fmt.Sprintf("aws:///%s/%s", aws.StringValue(instance.Placement.AvailabilityZone), aws.StringValue(instance.InstanceId))

if existingProviderID != nil && *existingProviderID == providerID {
glog.Infof("ProviderID already set in the machine Spec with value:%s", *existingProviderID)
return machine, nil
}
machineCopy.Spec.ProviderID = &providerID
if err := a.client.Update(context.Background(), machineCopy); err != nil {
return nil, fmt.Errorf("error updating machine spec ProviderID: %v", err)
}
glog.Infof("ProviderID updated at machine spec: %s", providerID)
} else {
machineCopy.Spec.ProviderID = nil
if err := a.client.Update(context.Background(), machineCopy); err != nil {
return nil, fmt.Errorf("error updating ProviderID in machine spec: %v", err)
}
glog.Infof("No instance found so clearing ProviderID field in the machine spec")
}
return machineCopy, nil
}

func (a *Actuator) updateMachineStatus(machine *machinev1.Machine, awsStatus *providerconfigv1.AWSMachineProviderStatus, networkAddresses []corev1.NodeAddress) error {
Expand Down
5 changes: 4 additions & 1 deletion pkg/actuators/machine/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ func TestAvailabiltyZone(t *testing.T) {
if tc.availabilityZone != "" && tc.subnet == "" {
placement = &ec2.Placement{AvailabilityZone: aws.String(tc.availabilityZone)}
}

az := "us-east-1a"
mockAWSClient.EXPECT().RunInstances(placementMatcher{placement}).Return(
&ec2.Reservation{
Instances: []*ec2.Instance{
Expand All @@ -653,6 +653,9 @@ func TestAvailabiltyZone(t *testing.T) {
Name: aws.String("Running"),
},
LaunchTime: aws.Time(time.Now()),
Placement: &ec2.Placement{
AvailabilityZone: &az,
},
},
},
}, nil)
Expand Down
4 changes: 4 additions & 0 deletions pkg/actuators/machine/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ func stubDescribeTargetGroupsOutput() *elbv2.DescribeTargetGroupsOutput {
}

func stubReservation(imageID, instanceID string) *ec2.Reservation {
az := defaultAvailabilityZone
return &ec2.Reservation{
Instances: []*ec2.Instance{
{
Expand All @@ -251,6 +252,9 @@ func stubReservation(imageID, instanceID string) *ec2.Reservation {
Code: aws.Int64(16),
},
LaunchTime: aws.Time(time.Now()),
Placement: &ec2.Placement{
AvailabilityZone: &az,
},
},
},
}
Expand Down

0 comments on commit f92f2f4

Please sign in to comment.