Skip to content

Commit

Permalink
Put AMI ID retrieval under its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
ingvagabund committed Nov 27, 2018
1 parent c52d350 commit 3948ae6
Showing 1 changed file with 43 additions and 37 deletions.
80 changes: 43 additions & 37 deletions pkg/cloud/aws/actuators/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,43 +271,16 @@ func getSubnetIDs(subnet providerconfigv1.AWSResourceReference, availabilityZone
return subnetIDs, nil
}

// CreateMachine starts a new AWS instance as described by the cluster and machine resources
func (a *Actuator) CreateMachine(cluster *clusterv1.Cluster, machine *clusterv1.Machine) (*ec2.Instance, error) {
machineProviderConfig, err := ProviderConfigFromMachine(machine)
if err != nil {
glog.Errorf("error decoding MachineProviderConfig: %v", err)
return nil, err
}

credentialsSecretName := ""
if machineProviderConfig.CredentialsSecret != nil {
credentialsSecretName = machineProviderConfig.CredentialsSecret.Name
}
client, err := a.awsClientBuilder(a.kubeClient, credentialsSecretName, machine.Namespace, machineProviderConfig.Placement.Region)
if err != nil {
glog.Errorf("unable to obtain AWS client: %v", err)
return nil, fmt.Errorf("unable to obtain AWS client: %v", err)
}

// We explicitly do NOT want to remove stopped masters.
if !IsMaster(machine) {
// Prevent having a lot of stopped nodes sitting around.
err = a.removeStoppedMachine(machine, client)
if err != nil {
glog.Errorf("unable to remove stopped machines: %v", err)
return nil, fmt.Errorf("unable to remove stopped nodes: %v", err)
}
}

// Get AMI to use
var amiID *string
if machineProviderConfig.AMI.ID != nil {
amiID = machineProviderConfig.AMI.ID
func getAMI(AMI providerconfigv1.AWSResourceReference, client awsclient.Client) (*string, error) {
if AMI.ID != nil {
amiID := AMI.ID
glog.Infof("Using AMI %s", *amiID)
} else if len(machineProviderConfig.AMI.Filters) > 0 {
return amiID, nil
}
if len(AMI.Filters) > 0 {
glog.Info("Describing AMI based on filters")
describeImagesRequest := ec2.DescribeImagesInput{
Filters: buildEC2Filters(machineProviderConfig.AMI.Filters),
Filters: buildEC2Filters(AMI.Filters),
}
describeAMIResult, err := client.DescribeImages(&describeImagesRequest)
if err != nil {
Expand Down Expand Up @@ -335,9 +308,42 @@ func (a *Actuator) CreateMachine(cluster *clusterv1.Cluster, machine *clusterv1.
latestTime = imageTime
}
}
amiID = latestImage.ImageId
} else {
return nil, fmt.Errorf("AMI ID or AMI filters need to be specified")
return latestImage.ImageId, nil
}
return nil, fmt.Errorf("AMI ID or AMI filters need to be specified")
}

// CreateMachine starts a new AWS instance as described by the cluster and machine resources
func (a *Actuator) CreateMachine(cluster *clusterv1.Cluster, machine *clusterv1.Machine) (*ec2.Instance, error) {
machineProviderConfig, err := ProviderConfigFromMachine(machine)
if err != nil {
glog.Errorf("error decoding MachineProviderConfig: %v", err)
return nil, err
}

credentialsSecretName := ""
if machineProviderConfig.CredentialsSecret != nil {
credentialsSecretName = machineProviderConfig.CredentialsSecret.Name
}
client, err := a.awsClientBuilder(a.kubeClient, credentialsSecretName, machine.Namespace, machineProviderConfig.Placement.Region)
if err != nil {
glog.Errorf("unable to obtain AWS client: %v", err)
return nil, fmt.Errorf("unable to obtain AWS client: %v", err)
}

// We explicitly do NOT want to remove stopped masters.
if !IsMaster(machine) {
// Prevent having a lot of stopped nodes sitting around.
err = a.removeStoppedMachine(machine, client)
if err != nil {
glog.Errorf("unable to remove stopped machines: %v", err)
return nil, fmt.Errorf("unable to remove stopped nodes: %v", err)
}
}

amiID, err := getAMI(machineProviderConfig.AMI, client)
if err != nil {
return nil, fmt.Errorf("error getting AMI: %v,", err)
}

securityGroupsIDs, err := getSecurityGroupsIDs(machineProviderConfig.SecurityGroups, client)
Expand Down

0 comments on commit 3948ae6

Please sign in to comment.