Scan floating IPs in the background, page by page, so thousands of addresses work

The "Scan Floating IP" button failed with a client timeout: the project now
holds ~6.4k floating IPs and the scan listed them all in one unpaginated,
timeout-less Neutron request on the HTTP request context.

openstack: ListFreeFloatingIPs reads marker-based pages (fields= keeps them
small) with per-page retry/backoff on transport errors, 5xx and 429, and every
request now has a timeout (also ends hangs inside the orchestrator tick).

orchestrator: the scan is a single-flight background job on the process
context with progress (clearing/listing/enqueuing/done/error), dry_run, full
discovery before anything is enqueued, then SubmitIPs in chunks of 500 in
ascending IP order; a failed read leaves the queue untouched. The auto-cycle
gets a "scanning" phase that polls the job, so the control loop and
autoCycleMu are never held across OpenStack/DB work; it recovers after a
restart and waits for (instead of adopting) a scan started by someone else.

db: migration 0009 (indexes), paged ListIPsPage/ListRegistryPage, GROUP BY
counters, EXISTS completion check, set-based ClearAllIPs.

API: POST /admin/ips/scan -> 202 (dry_run, wait), GET /admin/ips/scan, paging
and filters on /admin/ips and /admin/registry (bare arrays without limit),
results_by_overall in /admin/status.

dashboard: scan progress panel and dry-run button, paginated /ips and
/registry with server-side filters, Overview on counters and capped lists
with progress/ETA, "select all N by filter", hx-params fix for per-row
buttons, real counts in confirmations.

Also: docs (API, USAGE, DASHBOARD, README), plan and review under
docs/changes/, bin/ rebuilt with new SHA256SUMS.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ayurishchevandClaude Sonnet 5.5 committed 2026-10-01 19:31:11 +03:00
1 parent debf2afed2
commit aff8fe38b5
61 files changed
+5833 -536

No files matched your search

+88 -11
View File
@@ -3,10 +3,13 @@ package openstack
import (
"context"
"fmt"
"net/url"
"time"
"github.com/gophercloud/gophercloud/v2"
osauth "github.com/gophercloud/gophercloud/v2/openstack"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/gophercloud/gophercloud/v2/pagination"
)
// AuthMethod selects how the client obtains the token it uses for Neutron
@@ -51,10 +54,20 @@ type ClientConfig struct {
ProjectID string
Region string
Interface string // "public" | "internal" | "admin"; "" defaults to "public"
// RequestTimeout bounds every single HTTP request to Keystone/Neutron
// (provider.HTTPClient.Timeout). Zero means no timeout (not recommended:
// a hung Neutron call would otherwise block the caller forever).
RequestTimeout time.Duration
// ListPageRetries is how many times one failed page of the floating-IP
// listing is retried (exponential backoff 1s,2s,4s,...). Zero disables
// retries; negative values mean "use DefaultListPageRetries".
ListPageRetries int
}
type Client struct {
networking *gophercloud.ServiceClient
retry pageRetry
}
// buildAuthOptions translates ClientConfig into gophercloud.AuthOptions. It
@@ -119,6 +132,10 @@ func NewClient(ctx context.Context, cfg ClientConfig) (*Client, error) {
return nil, fmt.Errorf("openstack: authenticate: %w", err)
}
if cfg.RequestTimeout > 0 {
provider.HTTPClient.Timeout = cfg.RequestTimeout
}
iface := cfg.Interface
if iface == "" {
iface = string(gophercloud.AvailabilityPublic)
@@ -131,7 +148,11 @@ func NewClient(ctx context.Context, cfg ClientConfig) (*Client, error) {
return nil, fmt.Errorf("openstack: networking client: %w", err)
}
return &Client{networking: networking}, nil
retries := cfg.ListPageRetries
if retries < 0 {
retries = DefaultListPageRetries
}
return &Client{networking: networking, retry: pageRetry{Retries: retries}}, nil
}
func (c *Client) GetFloatingIPByAddress(ctx context.Context, address string) (*FloatingIP, error) {
@@ -150,22 +171,78 @@ func (c *Client) GetFloatingIPByAddress(ctx context.Context, address string) (*F
return &FloatingIP{ID: f.ID, Address: f.FloatingIP, PortID: f.PortID, ProjectID: f.TenantID}, nil
}
func (c *Client) ListFloatingIPs(ctx context.Context) ([]FloatingIP, error) {
pages, err := floatingips.List(c.networking, floatingips.ListOpts{}).AllPages(ctx)
// fipListFields is the set of attributes requested from Neutron when listing:
// everything FloatingIP needs and nothing more (the full resource is several
// times larger, which matters with thousands of floating IPs).
var fipListFields = []string{"id", "floating_ip_address", "port_id", "project_id"}
// pagedListOpts wraps floatingips.ListOpts to add the `fields` query
// parameter, which gophercloud's ListOpts does not expose.
type pagedListOpts struct {
floatingips.ListOpts
fields []string
}
func (o pagedListOpts) ToFloatingIPListQuery() (string, error) {
q, err := o.ListOpts.ToFloatingIPListQuery()
if err != nil {
return "", err
}
if len(o.fields) == 0 {
return q, nil
}
v := url.Values{}
for _, f := range o.fields {
v.Add("fields", f)
}
if q == "" {
return "?" + v.Encode(), nil
}
return q + "&" + v.Encode(), nil
}
// fetchPage requests exactly one page (limit entries after marker). It uses
// marker pagination driven by us rather than gophercloud's `next` link: behind
// a proxy that link may point at an internal host.
func (c *Client) fetchPage(ctx context.Context, marker string, limit int) ([]FloatingIP, error) {
opts := pagedListOpts{
ListOpts: floatingips.ListOpts{Limit: limit, Marker: marker},
fields: fipListFields,
}
var out []FloatingIP
err := floatingips.List(c.networking, opts).EachPage(ctx, func(_ context.Context, page pagination.Page) (bool, error) {
list, err := floatingips.ExtractFloatingIPs(page)
if err != nil {
return false, fmt.Errorf("openstack: extract floating ips: %w", err)
}
out = make([]FloatingIP, 0, len(list))
for _, f := range list {
proj := f.TenantID
if proj == "" {
proj = f.ProjectID // Neutron may return only project_id when `fields` is used
}
out = append(out, FloatingIP{ID: f.ID, Address: f.FloatingIP, PortID: f.PortID, ProjectID: proj})
}
return false, nil // one page per request
})
if err != nil {
return nil, fmt.Errorf("openstack: list floating ips: %w", err)
}
list, err := floatingips.ExtractFloatingIPs(pages)
if err != nil {
return nil, fmt.Errorf("openstack: extract floating ips: %w", err)
}
out := make([]FloatingIP, 0, len(list))
for _, f := range list {
out = append(out, FloatingIP{ID: f.ID, Address: f.FloatingIP, PortID: f.PortID, ProjectID: f.TenantID})
}
return out, nil
}
// ListFreeFloatingIPs pages through every floating IP of the project (page by
// page, retrying transient failures per page) and hands each page to onPage in
// server order. All entries of a page are passed — free and associated; the
// caller filters. It returns the number of non-empty pages read.
func (c *Client) ListFreeFloatingIPs(ctx context.Context, pageSize int, onPage func([]FloatingIP) error) (int, error) {
return paginate(ctx, pageSize, c.retry, c.fetchPage, onPage)
}
func (c *Client) ListFloatingIPs(ctx context.Context) ([]FloatingIP, error) {
return listAll(ctx, c)
}
func (c *Client) AssociateFloatingIP(ctx context.Context, fipID, portID string) error {
_, err := floatingips.Update(ctx, c.networking, fipID, floatingips.UpdateOpts{
PortID: &portID,