Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set additional machine annotations/labels to get pretty machine output #242

Merged
merged 2 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 66 additions & 1 deletion pkg/actuators/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import (

awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client"

machinecontroller "github.com/openshift/cluster-api/pkg/controller/machine"
)

const (
Expand Down Expand Up @@ -122,9 +124,58 @@ func (a *Actuator) Create(context context.Context, cluster *clusterv1.Cluster, m
if err != nil {
return fmt.Errorf("%s: failed to update machine object with providerID: %v", machine.Name, err)
}

updatedMachine, err = a.setMachineCloudProviderSpecifics(updatedMachine, instance)
if err != nil {
return fmt.Errorf("%s: failed to set machine cloud provider specifics: %v", machine.Name, err)
}

return a.updateStatus(updatedMachine, instance)
}

// updateProviderID adds providerID in the machine spec
func (a *Actuator) setMachineCloudProviderSpecifics(machine *machinev1.Machine, instance *ec2.Instance) (*machinev1.Machine, error) {
if instance == nil {
return machine, nil
}
machineCopy := machine.DeepCopy()

if machineCopy.Labels == nil {
machineCopy.Labels = make(map[string]string)
}

if machineCopy.Annotations == nil {
machineCopy.Annotations = make(map[string]string)
}

// Reaching to machine provider config since the region is not directly
// providing by *ec2.Instance object
machineProviderConfig, err := providerConfigFromMachine(machine, a.codec)
if err != nil {
return nil, fmt.Errorf("error decoding MachineProviderConfig: %v", err)
}

machineCopy.Labels[machinecontroller.MachineRegionLabelName] = machineProviderConfig.Placement.Region

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These labels should live in the pkg/api section of cluster-api.


if instance.Placement != nil {
machineCopy.Labels[machinecontroller.MachineAZLabelName] = aws.StringValue(instance.Placement.AvailabilityZone)
}

if instance.InstanceType != nil {
machineCopy.Labels[machinecontroller.MachineInstanceTypeLabelName] = aws.StringValue(instance.InstanceType)
}

if instance.State != nil && instance.State.Name != nil {
machineCopy.Annotations[machinecontroller.MachineInstanceStateAnnotationName] = aws.StringValue(instance.State.Name)
}

if err := a.client.Update(context.Background(), machineCopy); err != nil {
return nil, fmt.Errorf("%s: error updating machine spec: %v", machine.Name, err)
}

return machineCopy, nil
}

// updateProviderID adds providerID in the machine spec
func (a *Actuator) updateProviderID(machine *machinev1.Machine, instance *ec2.Instance) (*machinev1.Machine, error) {
existingProviderID := machine.Spec.ProviderID
Expand Down Expand Up @@ -325,10 +376,19 @@ func (a *Actuator) DeleteMachine(cluster *clusterv1.Cluster, machine *machinev1.
return nil
}

err = terminateInstances(client, instances)
terminatingInstances, err := terminateInstances(client, instances)
if err != nil {
return a.handleMachineError(machine, apierrors.DeleteMachine(err.Error()), noEventAction)
}

if len(terminatingInstances) == 1 {
if terminatingInstances[0] != nil && terminatingInstances[0].CurrentState != nil && terminatingInstances[0].CurrentState.Name != nil {
machineCopy := machine.DeepCopy()
machineCopy.Annotations[machinecontroller.MachineInstanceStateAnnotationName] = aws.StringValue(terminatingInstances[0].CurrentState.Name)
a.client.Update(context.Background(), machineCopy)
}
}

a.eventRecorder.Eventf(machine, corev1.EventTypeNormal, "Deleted", "Deleted machine %v", machine.Name)

return nil
Expand Down Expand Up @@ -404,6 +464,11 @@ func (a *Actuator) Update(context context.Context, cluster *clusterv1.Cluster, m

a.eventRecorder.Eventf(machine, corev1.EventTypeNormal, "Updated", "Updated machine %v", machine.Name)

machine, err = a.setMachineCloudProviderSpecifics(machine, newestInstance)
if err != nil {
return fmt.Errorf("%s: failed to set machine cloud provider specifics: %v", machine.Name, err)
}

// We do not support making changes to pre-existing instances, just update status.
return a.updateStatus(machine, newestInstance)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/actuators/machine/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func removeStoppedMachine(machine *machinev1.Machine, client awsclient.Client) e
return nil
}

return terminateInstances(client, instances)
_, err = terminateInstances(client, instances)
return err
}

func buildEC2Filters(inputFilters []providerconfigv1.Filter) []*ec2.Filter {
Expand Down
13 changes: 9 additions & 4 deletions pkg/actuators/machine/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func getVolume(client awsclient.Client, volumeID string) (*ec2.Volume, error) {
}

// terminateInstances terminates all provided instances with a single EC2 request.
func terminateInstances(client awsclient.Client, instances []*ec2.Instance) error {
func terminateInstances(client awsclient.Client, instances []*ec2.Instance) ([]*ec2.InstanceStateChange, error) {
instanceIDs := []*string{}
// Cleanup all older instances:
for _, instance := range instances {
Expand All @@ -158,12 +158,17 @@ func terminateInstances(client awsclient.Client, instances []*ec2.Instance) erro
terminateInstancesRequest := &ec2.TerminateInstancesInput{
InstanceIds: instanceIDs,
}
_, err := client.TerminateInstances(terminateInstancesRequest)
output, err := client.TerminateInstances(terminateInstancesRequest)
if err != nil {
glog.Errorf("Error terminating instances: %v", err)
return fmt.Errorf("error terminating instances: %v", err)
return nil, fmt.Errorf("error terminating instances: %v", err)
}
return nil

if output == nil {
return nil, nil
}

return output.TerminatingInstances, nil
}

// providerConfigFromMachine gets the machine provider config MachineSetSpec from the
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.