discovery: move openstack floating ips function from deprecated Compute API /os-floating-ips to Network API /floatingips (#14367)
Some checks are pending
buf.build / lint and publish (push) Waiting to run
CI / Go tests (push) Waiting to run
CI / More Go tests (push) Waiting to run
CI / Go tests with previous Go version (push) Waiting to run
CI / UI tests (push) Waiting to run
CI / Go tests on Windows (push) Waiting to run
CI / Mixins tests (push) Waiting to run
CI / Build Prometheus for common architectures (0) (push) Waiting to run
CI / Build Prometheus for common architectures (1) (push) Waiting to run
CI / Build Prometheus for common architectures (2) (push) Waiting to run
CI / Build Prometheus for all architectures (0) (push) Waiting to run
CI / Build Prometheus for all architectures (1) (push) Waiting to run
CI / Build Prometheus for all architectures (10) (push) Waiting to run
CI / Build Prometheus for all architectures (11) (push) Waiting to run
CI / Build Prometheus for all architectures (2) (push) Waiting to run
CI / Build Prometheus for all architectures (3) (push) Waiting to run
CI / Build Prometheus for all architectures (4) (push) Waiting to run
CI / Build Prometheus for all architectures (5) (push) Waiting to run
CI / Build Prometheus for all architectures (6) (push) Waiting to run
CI / Build Prometheus for all architectures (7) (push) Waiting to run
CI / Build Prometheus for all architectures (8) (push) Waiting to run
CI / Build Prometheus for all architectures (9) (push) Waiting to run
CI / Report status of build Prometheus for all architectures (push) Blocked by required conditions
CI / Check generated parser (push) Waiting to run
CI / golangci-lint (push) Waiting to run
CI / fuzzing (push) Waiting to run
CI / codeql (push) Waiting to run
CI / Publish main branch artifacts (push) Blocked by required conditions
CI / Publish release artefacts (push) Blocked by required conditions
CI / Publish UI on npm Registry (push) Blocked by required conditions
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run

This commit is contained in:
Paulo Dias 2025-01-21 10:40:15 +00:00 committed by GitHub
parent 2a8ae586f4
commit e8fab32ca2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 446 additions and 109 deletions

View file

@ -22,8 +22,9 @@ import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
"github.com/gophercloud/gophercloud/pagination"
"github.com/prometheus/common/model"
"github.com/prometheus/common/promslog"
@ -72,7 +73,7 @@ func newInstanceDiscovery(provider *gophercloud.ProviderClient, opts *gopherclou
}
type floatingIPKey struct {
id string
deviceID string
fixed string
}
@ -90,9 +91,33 @@ func (i *InstanceDiscovery) refresh(ctx context.Context) ([]*targetgroup.Group,
return nil, fmt.Errorf("could not create OpenStack compute session: %w", err)
}
networkClient, err := openstack.NewNetworkV2(i.provider, gophercloud.EndpointOpts{
Region: i.region, Availability: i.availability,
})
if err != nil {
return nil, fmt.Errorf("could not create OpenStack network session: %w", err)
}
// OpenStack API reference
// https://developer.openstack.org/api-ref/compute/#list-floating-ips
pagerFIP := floatingips.List(client)
// https://docs.openstack.org/api-ref/network/v2/index.html#list-ports
portPages, err := ports.List(networkClient, ports.ListOpts{}).AllPages()
if err != nil {
return nil, fmt.Errorf("failed to list all ports: %w", err)
}
allPorts, err := ports.ExtractPorts(portPages)
if err != nil {
return nil, fmt.Errorf("failed to extract Ports: %w", err)
}
portList := make(map[string]string)
for _, port := range allPorts {
portList[port.ID] = port.DeviceID
}
// OpenStack API reference
// https://docs.openstack.org/api-ref/network/v2/index.html#list-floating-ips
pagerFIP := floatingips.List(networkClient, floatingips.ListOpts{})
floatingIPList := make(map[floatingIPKey]string)
floatingIPPresent := make(map[string]struct{})
err = pagerFIP.EachPage(func(page pagination.Page) (bool, error) {
@ -102,11 +127,24 @@ func (i *InstanceDiscovery) refresh(ctx context.Context) ([]*targetgroup.Group,
}
for _, ip := range result {
// Skip not associated ips
if ip.InstanceID == "" || ip.FixedIP == "" {
if ip.PortID == "" || ip.FixedIP == "" {
continue
}
floatingIPList[floatingIPKey{id: ip.InstanceID, fixed: ip.FixedIP}] = ip.IP
floatingIPPresent[ip.IP] = struct{}{}
// Fetch deviceID from portList
deviceID, ok := portList[ip.PortID]
if !ok {
i.logger.Warn("Floating IP PortID not found in portList", "PortID", ip.PortID)
continue
}
key := floatingIPKey{
deviceID: deviceID,
fixed: ip.FixedIP,
}
floatingIPList[key] = ip.FloatingIP
floatingIPPresent[ip.FloatingIP] = struct{}{}
}
return true, nil
})
@ -198,7 +236,7 @@ func (i *InstanceDiscovery) refresh(ctx context.Context) ([]*targetgroup.Group,
}
lbls[openstackLabelAddressPool] = model.LabelValue(pool)
lbls[openstackLabelPrivateIP] = model.LabelValue(addr)
if val, ok := floatingIPList[floatingIPKey{id: s.ID, fixed: addr}]; ok {
if val, ok := floatingIPList[floatingIPKey{deviceID: s.ID, fixed: addr}]; ok {
lbls[openstackLabelPublicIP] = model.LabelValue(val)
}
addr = net.JoinHostPort(addr, strconv.Itoa(i.port))

View file

@ -32,6 +32,7 @@ func (s *OpenstackSDInstanceTestSuite) SetupTest(t *testing.T) {
s.Mock.HandleServerListSuccessfully()
s.Mock.HandleFloatingIPListSuccessfully()
s.Mock.HandlePortsListSuccessfully()
s.Mock.HandleVersionsSuccessfully()
s.Mock.HandleAuthSuccessfully()
@ -66,7 +67,7 @@ func TestOpenstackSDInstanceRefresh(t *testing.T) {
tg := tgs[0]
require.NotNil(t, tg)
require.NotNil(t, tg.Targets)
require.Len(t, tg.Targets, 4)
require.Len(t, tg.Targets, 6)
for i, lbls := range []model.LabelSet{
{
@ -119,6 +120,31 @@ func TestOpenstackSDInstanceRefresh(t *testing.T) {
"__meta_openstack_project_id": model.LabelValue("fcad67a6189847c4aecfa3c81a05783b"),
"__meta_openstack_user_id": model.LabelValue("9349aff8be7545ac9d2f1d00999a23cd"),
},
{
"__address__": model.LabelValue("10.0.0.33:0"),
"__meta_openstack_instance_flavor": model.LabelValue("m1.small"),
"__meta_openstack_instance_id": model.LabelValue("87caf8ed-d92a-41f6-9dcd-d1399e39899f"),
"__meta_openstack_instance_status": model.LabelValue("ACTIVE"),
"__meta_openstack_instance_name": model.LabelValue("merp-project2"),
"__meta_openstack_private_ip": model.LabelValue("10.0.0.33"),
"__meta_openstack_address_pool": model.LabelValue("private"),
"__meta_openstack_tag_env": model.LabelValue("prod"),
"__meta_openstack_project_id": model.LabelValue("b78fef2305934dbbbeb9a10b4c326f7a"),
"__meta_openstack_user_id": model.LabelValue("9349aff8be7545ac9d2f1d00999a23cd"),
},
{
"__address__": model.LabelValue("10.0.0.34:0"),
"__meta_openstack_instance_flavor": model.LabelValue("m1.small"),
"__meta_openstack_instance_id": model.LabelValue("87caf8ed-d92a-41f6-9dcd-d1399e39899f"),
"__meta_openstack_instance_status": model.LabelValue("ACTIVE"),
"__meta_openstack_instance_name": model.LabelValue("merp-project2"),
"__meta_openstack_private_ip": model.LabelValue("10.0.0.34"),
"__meta_openstack_address_pool": model.LabelValue("private"),
"__meta_openstack_tag_env": model.LabelValue("prod"),
"__meta_openstack_public_ip": model.LabelValue("10.10.10.24"),
"__meta_openstack_project_id": model.LabelValue("b78fef2305934dbbbeb9a10b4c326f7a"),
"__meta_openstack_user_id": model.LabelValue("9349aff8be7545ac9d2f1d00999a23cd"),
},
} {
t.Run(fmt.Sprintf("item %d", i), func(t *testing.T) {
require.Equal(t, lbls, tg.Targets[i])

View file

@ -136,8 +136,20 @@ func (m *SDMock) HandleAuthSuccessfully() {
],
"id": "b7f2a5b1a019459cb956e43a8cb41e31",
"type": "compute"
},
{
"endpoints": [
{
"id": "5448e46679564d7d95466c2bef54c296",
"interface": "public",
"region": "RegionOne",
"region_id": "RegionOne",
"url": "%s"
}
],
"id": "589f3d99a3d94f5f871e9f5cf206d2e8",
"type": "network"
}
],
"expires_at": "2013-02-27T18:30:59.999999Z",
"is_domain": false,
@ -174,7 +186,7 @@ func (m *SDMock) HandleAuthSuccessfully() {
}
}
}
`, m.Endpoint())
`, m.Endpoint(), m.Endpoint())
})
}
@ -536,6 +548,83 @@ const serverListBody = `
"metadata": {
"env": "prod"
}
},
{
"status": "ACTIVE",
"updated": "2014-09-25T13:04:49Z",
"hostId": "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362",
"OS-EXT-SRV-ATTR:host": "devstack",
"addresses": {
"private": [
{
"version": 4,
"addr": "10.0.0.33",
"OS-EXT-IPS:type": "fixed"
},
{
"version": 4,
"addr": "10.0.0.34",
"OS-EXT-IPS:type": "fixed"
},
{
"version": 4,
"addr": "10.10.10.24",
"OS-EXT-IPS:type": "floating"
}
]
},
"links": [
{
"href": "http://104.130.131.164:8774/v2/b78fef2305934dbbbeb9a10b4c326f7a/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
"rel": "self"
},
{
"href": "http://104.130.131.164:8774/b78fef2305934dbbbeb9a10b4c326f7a/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
"rel": "bookmark"
}
],
"key_name": null,
"image": "",
"OS-EXT-STS:task_state": null,
"OS-EXT-STS:vm_state": "active",
"OS-EXT-SRV-ATTR:instance_name": "instance-0000002d",
"OS-SRV-USG:launched_at": "2014-09-25T13:04:49.000000",
"OS-EXT-SRV-ATTR:hypervisor_hostname": "devstack",
"flavor": {
"vcpus": 2,
"ram": 4096,
"disk": 0,
"ephemeral": 0,
"swap": 0,
"original_name": "m1.small",
"extra_specs": {
"aggregate_instance_extra_specs:general": "true",
"hw:mem_page_size": "large",
"hw:vif_multiqueue_enabled": "true"
}
},
"id": "87caf8ed-d92a-41f6-9dcd-d1399e39899f",
"security_groups": [
{
"name": "default"
}
],
"OS-SRV-USG:terminated_at": null,
"OS-EXT-AZ:availability_zone": "nova",
"user_id": "9349aff8be7545ac9d2f1d00999a23cd",
"name": "merp-project2",
"created": "2014-09-25T13:04:41Z",
"tenant_id": "b78fef2305934dbbbeb9a10b4c326f7a",
"OS-DCF:diskConfig": "MANUAL",
"os-extended-volumes:volumes_attached": [],
"accessIPv4": "",
"accessIPv6": "",
"progress": 0,
"OS-EXT-STS:power_state": 1,
"config_drive": "",
"metadata": {
"env": "prod"
}
}
]
}
@ -554,27 +643,74 @@ func (m *SDMock) HandleServerListSuccessfully() {
const listOutput = `
{
"floating_ips": [
"floatingips": [
{
"fixed_ip": null,
"id": "1",
"instance_id": null,
"ip": "10.10.10.1",
"pool": "nova"
"id": "03a77860-ae03-46c4-b502-caea11467a79",
"tenant_id": "fcad67a6189847c4aecfa3c81a05783b",
"floating_ip_address": "10.10.10.1",
"floating_network_id": "d02c4f18-d606-4864-b12a-1c9b39a46be2",
"router_id": "f03af93b-4e8f-4f55-adcf-a0317782ede2",
"port_id": "d5597901-48c8-4a69-a041-cfc5be158a04",
"fixed_ip_address": null,
"status": "ACTIVE",
"description": "",
"dns_domain": "",
"dns_name": "",
"port_forwardings": [],
"tags": [],
"created_at": "2023-08-30T16:30:27Z",
"updated_at": "2023-08-30T16:30:28Z"
},
{
"fixed_ip": "10.0.0.32",
"id": "2",
"instance_id": "ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
"ip": "10.10.10.2",
"pool": "nova"
"id": "03e28c79-5a4c-491e-a4fe-3ff6bba830c6",
"tenant_id": "fcad67a6189847c4aecfa3c81a05783b",
"floating_ip_address": "10.10.10.2",
"floating_network_id": "d02c4f18-d606-4864-b12a-1c9b39a46be2",
"router_id": "f03af93b-4e8f-4f55-adcf-a0317782ede2",
"port_id": "4a45b012-0478-484d-8cf3-c8abdb194d08",
"fixed_ip_address": "10.0.0.32",
"status": "ACTIVE",
"description": "",
"dns_domain": "",
"dns_name": "",
"port_forwardings": [],
"tags": [],
"created_at": "2023-09-06T15:45:36Z",
"updated_at": "2023-09-06T15:45:36Z"
},
{
"fixed_ip": "10.0.0.34",
"id": "3",
"instance_id": "9e5476bd-a4ec-4653-93d6-72c93aa682bb",
"ip": "10.10.10.4",
"pool": "nova"
"id": "087fcdd2-1d13-4f72-9c0e-c759e796d558",
"tenant_id": "fcad67a6189847c4aecfa3c81a05783b",
"floating_ip_address": "10.10.10.4",
"floating_network_id": "d02c4f18-d606-4864-b12a-1c9b39a46be2",
"router_id": "f03af93b-4e8f-4f55-adcf-a0317782ede2",
"port_id": "a0e244e8-7910-4427-b8d1-20470cad4f8a",
"fixed_ip_address": "10.0.0.34",
"status": "ACTIVE",
"description": "",
"dns_domain": "",
"dns_name": "",
"port_forwardings": [],
"tags": [],
"created_at": "2024-01-24T13:30:50Z",
"updated_at": "2024-01-24T13:30:51Z"
},
{
"id": "b23df91a-a74a-4f75-b252-750aff4a5a0c",
"tenant_id": "b78fef2305934dbbbeb9a10b4c326f7a",
"floating_ip_address": "10.10.10.24",
"floating_network_id": "b19ff5bc-a49a-46cc-8d14-ca5f1e94791f",
"router_id": "65a5e5af-17f0-4124-9a81-c08b44f5b8a7",
"port_id": "b926ab68-ec54-46d8-8c50-1c07aafd5ae9",
"fixed_ip_address": "10.0.0.34",
"status": "ACTIVE",
"description": "",
"dns_domain": "",
"dns_name": "",
"port_forwardings": [],
"tags": [],
"created_at": "2024-01-24T13:30:50Z",
"updated_at": "2024-01-24T13:30:51Z"
}
]
}
@ -582,7 +718,7 @@ const listOutput = `
// HandleFloatingIPListSuccessfully mocks floating ips call.
func (m *SDMock) HandleFloatingIPListSuccessfully() {
m.Mux.HandleFunc("/os-floating-ips", func(w http.ResponseWriter, r *http.Request) {
m.Mux.HandleFunc("/v2.0/floatingips", func(w http.ResponseWriter, r *http.Request) {
testMethod(m.t, r, http.MethodGet)
testHeader(m.t, r, "X-Auth-Token", tokenID)
@ -590,3 +726,140 @@ func (m *SDMock) HandleFloatingIPListSuccessfully() {
fmt.Fprint(w, listOutput)
})
}
const portsListBody = `
{
"ports": [
{
"id": "d5597901-48c8-4a69-a041-cfc5be158a04",
"name": "",
"network_id": "d02c4f18-d606-4864-b12a-1c9b39a46be2",
"tenant_id": "fcad67a6189847c4aecfa3c81a05783b",
"mac_address": "",
"admin_state_up": true,
"status": "DOWN",
"device_id": "",
"device_owner": "",
"fixed_ips": [],
"allowed_address_pairs": [],
"extra_dhcp_opts": [],
"security_groups": [],
"description": "",
"binding:vnic_type": "normal",
"port_security_enabled": true,
"dns_name": "",
"dns_assignment": [],
"dns_domain": "",
"tags": [],
"created_at": "2023-08-30T16:30:27Z",
"updated_at": "2023-08-30T16:30:28Z",
"revision_number": 0,
"project_id": "fcad67a6189847c4aecfa3c81a05783b"
},
{
"id": "4a45b012-0478-484d-8cf3-c8abdb194d08",
"name": "ovn-lb-vip-0980c8de-58c3-481d-89e3-ed81f44286c0",
"network_id": "03200a39-b399-44f3-a778-6dbb93343a31",
"tenant_id": "fcad67a6189847c4aecfa3c81a05783b",
"mac_address": "fa:16:3e:23:12:a3",
"admin_state_up": true,
"status": "ACTIVE",
"device_id": "ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
"device_owner": "",
"fixed_ips": [
{
"subnet_id": "",
"ip_address": "10.10.10.2"
}
],
"allowed_address_pairs": [],
"extra_dhcp_opts": [],
"security_groups": [],
"description": "",
"binding:vnic_type": "normal",
"port_security_enabled": true,
"dns_name": "",
"dns_assignment": [],
"dns_domain": "",
"tags": [],
"created_at": "2023-09-06T15:45:36Z",
"updated_at": "2023-09-06T15:45:36Z",
"revision_number": 0,
"project_id": "fcad67a6189847c4aecfa3c81a05783b"
},
{
"id": "a0e244e8-7910-4427-b8d1-20470cad4f8a",
"name": "ovn-lb-vip-26c0ccb1-3036-4345-99e8-d8f34a8ba6b2",
"network_id": "03200a39-b399-44f3-a778-6dbb93343a31",
"tenant_id": "fcad67a6189847c4aecfa3c81a05783b",
"mac_address": "fa:16:3e:5f:43:10",
"admin_state_up": true,
"status": "ACTIVE",
"device_id": "9e5476bd-a4ec-4653-93d6-72c93aa682bb",
"device_owner": "",
"fixed_ips": [
{
"subnet_id": "",
"ip_address": "10.10.10.4"
}
],
"allowed_address_pairs": [],
"extra_dhcp_opts": [],
"security_groups": [],
"description": "",
"binding:vnic_type": "normal",
"port_security_enabled": true,
"dns_name": "",
"dns_assignment": [],
"dns_domain": "",
"tags": [],
"created_at": "2024-01-24T13:30:50Z",
"updated_at": "2024-01-24T13:30:51Z",
"revision_number": 0,
"project_id": "fcad67a6189847c4aecfa3c81a05783b"
},
{
"id": "b926ab68-ec54-46d8-8c50-1c07aafd5ae9",
"name": "dummy-port",
"network_id": "03200a39-b399-44f3-a778-6dbb93343a31",
"tenant_id": "b78fef2305934dbbbeb9a10b4c326f7a",
"mac_address": "fa:16:3e:5f:12:10",
"admin_state_up": true,
"status": "ACTIVE",
"device_id": "87caf8ed-d92a-41f6-9dcd-d1399e39899f",
"device_owner": "",
"fixed_ips": [
{
"subnet_id": "",
"ip_address": "10.10.10.24"
}
],
"allowed_address_pairs": [],
"extra_dhcp_opts": [],
"security_groups": [],
"description": "",
"binding:vnic_type": "normal",
"port_security_enabled": true,
"dns_name": "",
"dns_assignment": [],
"dns_domain": "",
"tags": [],
"created_at": "2024-01-24T13:30:50Z",
"updated_at": "2024-01-24T13:30:51Z",
"revision_number": 0,
"project_id": "b78fef2305934dbbbeb9a10b4c326f7a"
}
]
}
`
// HandlePortsListSuccessfully mocks the ports list API.
func (m *SDMock) HandlePortsListSuccessfully() {
m.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) {
testMethod(m.t, r, http.MethodGet)
testHeader(m.t, r, "X-Auth-Token", tokenID)
w.Header().Add("Content-Type", "application/json")
fmt.Fprint(w, portsListBody)
})
}