Skip to content

Commit

Permalink
Add explicit error logging in actuator.
Browse files Browse the repository at this point in the history
Errors returned to the controller do not seem to automatically be
logged. Added logging where necessary to make sure we stop swallowing
errors silently.
  • Loading branch information
dgoodwin committed Aug 24, 2018
1 parent 8858754 commit bf388b0
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions cloud/aws/actuators/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (a *Actuator) Create(cluster *clusterv1.Cluster, machine *clusterv1.Machine
func (a *Actuator) removeStoppedMachine(machine *clusterv1.Machine, client awsclient.Client, mLog log.FieldLogger) error {
instances, err := GetStoppedInstances(machine, client)
if err != nil {
mLog.Errorf("Error getting stopped instances: %v", err)
return fmt.Errorf("Error getting stopped instances: %v", err)
}

Expand All @@ -108,6 +109,7 @@ func (a *Actuator) CreateMachine(cluster *clusterv1.Cluster, machine *clusterv1.

machineProviderConfig, err := MachineProviderConfigFromClusterAPIMachineSpec(&machine.Spec)
if err != nil {
mLog.Errorf("error decoding MachineProviderConfig: %v", err)
return nil, err
}

Expand All @@ -117,6 +119,7 @@ func (a *Actuator) CreateMachine(cluster *clusterv1.Cluster, machine *clusterv1.
}
client, err := a.awsClientBuilder(a.kubeClient, credentialsSecretName, machine.Namespace, machineProviderConfig.Placement.Region)
if err != nil {
mLog.Errorf("unable to obtain AWS client: %v", err)
return nil, fmt.Errorf("unable to obtain AWS client: %v", err)
}

Expand All @@ -125,6 +128,7 @@ func (a *Actuator) CreateMachine(cluster *clusterv1.Cluster, machine *clusterv1.
// Prevent having a lot of stopped nodes sitting around.
err = a.removeStoppedMachine(machine, client, mLog)
if err != nil {
mLog.Errorf("unable to remove stopped machines: %v", err)
return nil, fmt.Errorf("unable to remove stopped nodes: %v", err)
}
}
Expand All @@ -144,9 +148,11 @@ func (a *Actuator) CreateMachine(cluster *clusterv1.Cluster, machine *clusterv1.
}
describeAMIResult, err := client.DescribeImages(&describeImagesRequest)
if err != nil {
mLog.Errorf("error describing AMI %s: %v", amiName, err)
return nil, fmt.Errorf("error describing AMI %s: %v", amiName, err)
}
if len(describeAMIResult.Images) != 1 {
mLog.Errorf("Unexpected number of images returned: %d", len(describeAMIResult.Images))
return nil, fmt.Errorf("Unexpected number of images returned: %d", len(describeAMIResult.Images))
}

Expand Down Expand Up @@ -190,6 +196,7 @@ func (a *Actuator) CreateMachine(cluster *clusterv1.Cluster, machine *clusterv1.
if machineProviderConfig.UserDataSecret != nil {
userDataSecret, err := a.kubeClient.CoreV1().Secrets(machine.Namespace).Get(machineProviderConfig.UserDataSecret.Name, metav1.GetOptions{})
if err != nil {
mLog.Errorf("error getting user data secret %s: %v", machineProviderConfig.UserDataSecret.Name, err)
return nil, err
}
if data, exists := userDataSecret.Data[userDataSecretKey]; exists {
Expand Down Expand Up @@ -218,7 +225,8 @@ func (a *Actuator) CreateMachine(cluster *clusterv1.Cluster, machine *clusterv1.

runResult, err := client.RunInstances(&inputConfig)
if err != nil {
return nil, fmt.Errorf("cannot create EC2 instance: %v", err)
mLog.Errorf("error creating EC2 instance: %v", err)
return nil, fmt.Errorf("error creating EC2 instance: %v", err)
}

if runResult == nil || len(runResult.Instances) != 1 {
Expand Down Expand Up @@ -253,6 +261,7 @@ func (a *Actuator) DeleteMachine(cluster *clusterv1.Cluster, machine *clusterv1.

machineProviderConfig, err := MachineProviderConfigFromClusterAPIMachineSpec(&machine.Spec)
if err != nil {
mLog.Errorf("error decoding MachineProviderConfig: %v", err)
return err
}

Expand All @@ -263,11 +272,13 @@ func (a *Actuator) DeleteMachine(cluster *clusterv1.Cluster, machine *clusterv1.
}
client, err := a.awsClientBuilder(a.kubeClient, credentialsSecretName, machine.Namespace, region)
if err != nil {
mLog.Errorf("error getting EC2 client: %v", err)
return fmt.Errorf("error getting EC2 client: %v", err)
}

instances, err := GetRunningInstances(machine, client)
if err != nil {
mLog.Errorf("error getting running instances: %v", err)
return err
}
if len(instances) == 0 {
Expand All @@ -287,6 +298,7 @@ func (a *Actuator) Update(cluster *clusterv1.Cluster, machine *clusterv1.Machine

machineProviderConfig, err := MachineProviderConfigFromClusterAPIMachineSpec(&machine.Spec)
if err != nil {
mLog.Errorf("error decoding MachineProviderConfig: %v", err)
return err
}

Expand All @@ -298,14 +310,16 @@ func (a *Actuator) Update(cluster *clusterv1.Cluster, machine *clusterv1.Machine
}
client, err := a.awsClientBuilder(a.kubeClient, credentialsSecretName, machine.Namespace, region)
if err != nil {
mLog.Errorf("error getting EC2 client: %v", err)
return fmt.Errorf("unable to obtain EC2 client: %v", err)
}

instances, err := GetRunningInstances(machine, client)
mLog.Debugf("found %d instances for machine", len(instances))
if err != nil {
mLog.Errorf("error getting running instances: %v", err)
return err
}
mLog.Debugf("found %d instances for machine", len(instances))

// Parent controller should prevent this from ever happening by calling Exists and then Create,
// but instance could be deleted between the two calls.
Expand All @@ -316,6 +330,7 @@ func (a *Actuator) Update(cluster *clusterv1.Cluster, machine *clusterv1.Machine
if err != nil {
return err
}
mLog.Errorf("attempted to update machine but no instances found")
return fmt.Errorf("attempted to update machine but no instances found")
}
newestInstance, terminateInstances := SortInstances(instances)
Expand Down Expand Up @@ -345,6 +360,7 @@ func (a *Actuator) Exists(cluster *clusterv1.Cluster, machine *clusterv1.Machine

machineProviderConfig, err := MachineProviderConfigFromClusterAPIMachineSpec(&machine.Spec)
if err != nil {
mLog.Errorf("error decoding MachineProviderConfig: %v", err)
return false, err
}

Expand All @@ -355,11 +371,13 @@ func (a *Actuator) Exists(cluster *clusterv1.Cluster, machine *clusterv1.Machine
}
client, err := a.awsClientBuilder(a.kubeClient, credentialsSecretName, machine.Namespace, region)
if err != nil {
mLog.Errorf("error getting EC2 client: %v", err)
return false, fmt.Errorf("error getting EC2 client: %v", err)
}

instances, err := GetRunningInstances(machine, client)
if err != nil {
mLog.Errorf("error getting running instances: %v", err)
return false, err
}
if len(instances) == 0 {
Expand All @@ -380,6 +398,7 @@ func (a *Actuator) updateStatus(machine *clusterv1.Machine, instance *ec2.Instan
// Starting with a fresh status as we assume full control of it here.
awsStatus, err := AWSMachineProviderStatusFromClusterAPIMachine(machine)
if err != nil {
mLog.Errorf("error decoding machine provider status: %v", err)
return err
}
// Save this, we need to check if it changed later.
Expand Down

0 comments on commit bf388b0

Please sign in to comment.