From 0f4c27e2bf4ba28707f96df18486165688341e79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Taavi=20V=C3=A4=C3=A4n=C3=A4nen?= Date: Fri, 30 Jun 2023 23:12:24 +0300 Subject: [PATCH] discovery/openstack: Include instance image ID in labels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a new label to include the ID of the image that an instance is using. This can be used for example to filter a job to only include instances using a certain image as that image includes some exporter. Sometimes the image information isn't available, such as when the image is private and the user doesn't have the roles required to see it. In those cases we just don't set the label, as the rest of the information from the discovery provider can still be used. Signed-off-by: Taavi Väänänen --- discovery/openstack/instance.go | 11 +++++++++-- discovery/openstack/instance_test.go | 2 ++ docs/configuration/configuration.md | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/discovery/openstack/instance.go b/discovery/openstack/instance.go index 2f7e99a07..b2fe1e787 100644 --- a/discovery/openstack/instance.go +++ b/discovery/openstack/instance.go @@ -36,6 +36,7 @@ const ( openstackLabelAddressPool = openstackLabelPrefix + "address_pool" openstackLabelInstanceFlavor = openstackLabelPrefix + "instance_flavor" openstackLabelInstanceID = openstackLabelPrefix + "instance_id" + openstackLabelInstanceImage = openstackLabelPrefix + "instance_image" openstackLabelInstanceName = openstackLabelPrefix + "instance_name" openstackLabelInstanceStatus = openstackLabelPrefix + "instance_status" openstackLabelPrivateIP = openstackLabelPrefix + "private_ip" @@ -144,12 +145,18 @@ func (i *InstanceDiscovery) refresh(ctx context.Context) ([]*targetgroup.Group, openstackLabelUserID: model.LabelValue(s.UserID), } - id, ok := s.Flavor["id"].(string) + flavorId, ok := s.Flavor["id"].(string) if !ok { level.Warn(i.logger).Log("msg", "Invalid type for flavor id, expected string") continue } - labels[openstackLabelInstanceFlavor] = model.LabelValue(id) + labels[openstackLabelInstanceFlavor] = model.LabelValue(flavorId) + + imageId, ok := s.Image["id"].(string) + if ok { + labels[openstackLabelInstanceImage] = model.LabelValue(imageId) + } + for k, v := range s.Metadata { name := strutil.SanitizeLabelName(k) labels[openstackLabelTagPrefix+model.LabelName(name)] = model.LabelValue(v) diff --git a/discovery/openstack/instance_test.go b/discovery/openstack/instance_test.go index d47cb0020..d2da5d968 100644 --- a/discovery/openstack/instance_test.go +++ b/discovery/openstack/instance_test.go @@ -73,6 +73,7 @@ func TestOpenstackSDInstanceRefresh(t *testing.T) { "__address__": model.LabelValue("10.0.0.32:0"), "__meta_openstack_instance_flavor": model.LabelValue("1"), "__meta_openstack_instance_id": model.LabelValue("ef079b0c-e610-4dfb-b1aa-b49f07ac48e5"), + "__meta_openstack_instance_image": model.LabelValue("f90f6034-2570-4974-8351-6b49732ef2eb"), "__meta_openstack_instance_status": model.LabelValue("ACTIVE"), "__meta_openstack_instance_name": model.LabelValue("herp"), "__meta_openstack_private_ip": model.LabelValue("10.0.0.32"), @@ -85,6 +86,7 @@ func TestOpenstackSDInstanceRefresh(t *testing.T) { "__address__": model.LabelValue("10.0.0.31:0"), "__meta_openstack_instance_flavor": model.LabelValue("1"), "__meta_openstack_instance_id": model.LabelValue("9e5476bd-a4ec-4653-93d6-72c93aa682ba"), + "__meta_openstack_instance_image": model.LabelValue("f90f6034-2570-4974-8351-6b49732ef2eb"), "__meta_openstack_instance_status": model.LabelValue("ACTIVE"), "__meta_openstack_instance_name": model.LabelValue("derp"), "__meta_openstack_private_ip": model.LabelValue("10.0.0.31"), diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index b094bb4ec..30bb07a8c 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -1276,6 +1276,7 @@ The following meta labels are available on targets during [relabeling](#relabel_ * `__meta_openstack_address_pool`: the pool of the private IP. * `__meta_openstack_instance_flavor`: the flavor of the OpenStack instance. * `__meta_openstack_instance_id`: the OpenStack instance ID. +* `__meta_openstack_instance_image`: the ID of the image the OpenStack instance is using. * `__meta_openstack_instance_name`: the OpenStack instance name. * `__meta_openstack_instance_status`: the status of the OpenStack instance. * `__meta_openstack_private_ip`: the private IP of the OpenStack instance.