Skip to content

Commit

Permalink
Bug 1724204: Set additional machine annotations/labels
Browse files Browse the repository at this point in the history
This commit sets additional machine annotations/labels to
get pretty machine output.
  • Loading branch information
Fedosin committed Aug 15, 2019
1 parent 076f2c3 commit e67f5b0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
54 changes: 54 additions & 0 deletions pkg/cloud/openstack/clients/machineservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/bootfromvolume"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
Expand Down Expand Up @@ -60,6 +61,15 @@ const (

TimeoutPortDelete = 3 * time.Minute
RetryIntervalPortDelete = 5 * time.Second

// MachineRegionLabelName as annotation name for a machine region
MachineRegionLabelName = "machine.openshift.io/region"

// MachineAZLabelName as annotation name for a machine AZ
MachineAZLabelName = "machine.openshift.io/zone"

// MachineInstanceTypeLabelName as annotation name for a machine instance type
MachineInstanceTypeLabelName = "machine.openshift.io/instance-type"
)

type InstanceService struct {
Expand All @@ -68,6 +78,8 @@ type InstanceService struct {
identityClient *gophercloud.ServiceClient
networkClient *gophercloud.ServiceClient
imagesClient *gophercloud.ServiceClient

regionName string
}

type Instance struct {
Expand All @@ -92,6 +104,19 @@ type InstanceListOpts struct {
Name string `q:"name"`
}

type serverMetadata struct {
// AZ contains name of the server's availability zone
AZ string `json:"OS-EXT-AZ:availability_zone"`

// Flavor refers to a JSON object, which itself indicates the hardware
// configuration of the deployed server.
Flavor map[string]interface{} `json:"flavor"`

// Status contains the current operational status of the server,
// such as IN_PROGRESS or ACTIVE.
Status string `json:"status"`
}

func GetCloudFromSecret(kubeClient kubernetes.Interface, namespace string, secretName string, cloudName string) (clientconfig.Cloud, error) {
emptyCloud := clientconfig.Cloud{}

Expand Down Expand Up @@ -205,6 +230,7 @@ func NewInstanceServiceFromCloud(cloud clientconfig.Cloud) (*InstanceService, er
computeClient: serverClient,
networkClient: networkingClient,
imagesClient: imagesClient,
regionName: clientOpts.RegionName,
}, nil
}

Expand Down Expand Up @@ -729,6 +755,34 @@ func (is *InstanceService) GetInstance(resourceId string) (instance *Instance, e
return serverToInstance(server), err
}

// SetMachineLabels set labels describing the machine
func (is *InstanceService) SetMachineLabels(machine *machinev1.Machine, instanceID string) error {
var sm serverMetadata
err := servers.Get(is.computeClient, instanceID).ExtractInto(&sm)
if err != nil {
return err
}

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

// Set the region
machine.Labels[MachineRegionLabelName] = is.regionName

// Set the availability zone
machine.Labels[MachineAZLabelName] = sm.AZ

// Set the flavor name
flavor, err := flavors.Get(is.computeClient, sm.Flavor["id"].(string)).Extract()
if err != nil {
return err
}
machine.Labels[MachineInstanceTypeLabelName] = flavor.Name

return nil
}

func serverToInstance(server *servers.Server) *Instance {
return &Instance{*server}
}
10 changes: 10 additions & 0 deletions pkg/cloud/openstack/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ const (
TimeoutInstanceCreate = 5
TimeoutInstanceDelete = 5
RetryIntervalInstanceStatus = 10 * time.Second

// MachineInstanceStateAnnotationName as annotation name for a machine instance state
MachineInstanceStateAnnotationName = "machine.openshift.io/instance-state"
)

type OpenstackClient struct {
Expand Down Expand Up @@ -244,6 +247,11 @@ func (oc *OpenstackClient) Create(ctx context.Context, cluster *clusterv1.Cluste

}

err = machineService.SetMachineLabels(machine, instance.ID)
if err != nil {
return nil
}

return oc.updateAnnotation(machine, instance.ID)
}

Expand Down Expand Up @@ -406,6 +414,8 @@ func (oc *OpenstackClient) updateAnnotation(machine *machinev1.Machine, id strin
return err
}
machine.ObjectMeta.Annotations[openstack.OpenstackIPAnnotationKey] = ip
machine.ObjectMeta.Annotations[MachineInstanceStateAnnotationName] = instance.Status

if err := oc.client.Update(nil, machine); err != nil {
return err
}
Expand Down

0 comments on commit e67f5b0

Please sign in to comment.