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:
1 parent
debf2afed2
commit
aff8fe38b5
61 files changed
+5833
-536
No files matched your search
@@ -1,9 +1,15 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"cloudipvalidator/internal/db"
|
||||
"cloudipvalidator/internal/orchestrator"
|
||||
)
|
||||
|
||||
func (s *Server) handleHealthz(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -11,7 +17,13 @@ func (s *Server) handleHealthz(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (s *Server) handleAdminStatus(w http.ResponseWriter, r *http.Request) {
|
||||
ips, err := s.DB.ListIPs(r.Context())
|
||||
// GROUP BY counts instead of loading every row: the dashboard polls this.
|
||||
byState, total, err := s.DB.CountIPsByState(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
byResult, err := s.DB.CountIPsByResult(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
@@ -21,24 +33,117 @@ func (s *Server) handleAdminStatus(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
byState := map[string]int{}
|
||||
for _, ip := range ips {
|
||||
byState[ip.State]++
|
||||
overall := map[string]int{
|
||||
db.ResultPass: 0, db.ResultPartial: 0, db.ResultFail: 0, db.ResultCancelled: 0,
|
||||
}
|
||||
for res, n := range byResult {
|
||||
overall[res] = n
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{
|
||||
"total_ips": len(ips),
|
||||
"ips_by_state": byState,
|
||||
"total_validators": len(validators),
|
||||
"total_ips": total,
|
||||
"ips_by_state": byState,
|
||||
"total_validators": len(validators),
|
||||
"results_by_overall": overall,
|
||||
})
|
||||
}
|
||||
|
||||
// handleAdminIPs lists the queue. Without `limit` it returns the bare array of
|
||||
// every row (the original contract); with `limit` (1..1000) it returns the
|
||||
// envelope {items,total,limit,offset}. Filters: state (csv of valid ip
|
||||
// states), q (substring of the address), result (pass|partial|fail|
|
||||
// cancelled), order (sequence|aggregated_at_desc); offset >= 0 (needs limit).
|
||||
func (s *Server) handleAdminIPs(w http.ResponseWriter, r *http.Request) {
|
||||
ips, err := s.DB.ListIPs(r.Context())
|
||||
q := r.URL.Query()
|
||||
limit, offset, paged, err := parsePaging(q)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
filter := db.IPFilter{Query: strings.TrimSpace(q.Get("q"))}
|
||||
for _, st := range strings.Split(q.Get("state"), ",") {
|
||||
st = strings.TrimSpace(st)
|
||||
if st == "" {
|
||||
continue
|
||||
}
|
||||
if !db.IsValidIPState(st) {
|
||||
writeError(w, http.StatusBadRequest, "invalid state "+strconv.Quote(st)+" (valid: "+strings.Join(db.IPStates, ", ")+")")
|
||||
return
|
||||
}
|
||||
filter.States = append(filter.States, st)
|
||||
}
|
||||
if res := q.Get("result"); res != "" {
|
||||
if !db.IsValidResult(res) {
|
||||
writeError(w, http.StatusBadRequest, "invalid result "+strconv.Quote(res)+" (valid: pass, partial, fail, cancelled)")
|
||||
return
|
||||
}
|
||||
filter.Result = res
|
||||
}
|
||||
switch order := q.Get("order"); order {
|
||||
case "", db.IPOrderSequence:
|
||||
filter.Order = db.IPOrderSequence
|
||||
case db.IPOrderAggregatedAtDesc:
|
||||
filter.Order = order
|
||||
default:
|
||||
writeError(w, http.StatusBadRequest, "invalid order "+strconv.Quote(order)+" (valid: sequence, aggregated_at_desc)")
|
||||
return
|
||||
}
|
||||
|
||||
if len(q) == 0 {
|
||||
ips, err := s.DB.ListIPs(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, ips)
|
||||
return
|
||||
}
|
||||
items, total, err := s.DB.ListIPsPage(r.Context(), filter, limit, offset)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, ips)
|
||||
if !paged {
|
||||
writeJSON(w, http.StatusOK, items)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, ipsPageResponse{Items: items, Total: total, Limit: limit, Offset: offset})
|
||||
}
|
||||
|
||||
// maxPageLimit is the largest allowed `limit` of the paginated list endpoints.
|
||||
const maxPageLimit = 1000
|
||||
|
||||
// parsePaging reads limit/offset. paged is true when `limit` was given (the
|
||||
// envelope form); `offset` without `limit` is rejected.
|
||||
func parsePaging(q url.Values) (limit, offset int, paged bool, err error) {
|
||||
if v := q.Get("limit"); v != "" {
|
||||
limit, err = strconv.Atoi(v)
|
||||
if err != nil || limit < 1 || limit > maxPageLimit {
|
||||
return 0, 0, false, fmt.Errorf("limit must be an integer in 1..%d", maxPageLimit)
|
||||
}
|
||||
paged = true
|
||||
}
|
||||
if v := q.Get("offset"); v != "" {
|
||||
if !paged {
|
||||
return 0, 0, false, errors.New("offset requires limit")
|
||||
}
|
||||
offset, err = strconv.Atoi(v)
|
||||
if err != nil || offset < 0 {
|
||||
return 0, 0, false, errors.New("offset must be an integer >= 0")
|
||||
}
|
||||
}
|
||||
return limit, offset, paged, nil
|
||||
}
|
||||
|
||||
// parseBoolParam reads an optional boolean query parameter.
|
||||
func parseBoolParam(q url.Values, name string) (bool, error) {
|
||||
v := strings.ToLower(q.Get(name))
|
||||
switch v {
|
||||
case "", "0", "false":
|
||||
return false, nil
|
||||
case "1", "true":
|
||||
return true, nil
|
||||
}
|
||||
return false, fmt.Errorf("%s must be true or false", name)
|
||||
}
|
||||
|
||||
func (s *Server) handleAdminIPDetail(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -99,13 +204,34 @@ func (s *Server) handleAdminSubmitIPs(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
// handleAdminScanFloatingIPs lists every floating IP in the configured
|
||||
// OpenStack project, filters to the free (unassociated) pool, and submits
|
||||
// that address list to the check queue — see orchestrator.ScanFloatingIPs.
|
||||
// Takes no body; POST is used (rather than GET) because it mutates the
|
||||
// queue, matching handleAdminSubmitIPs.
|
||||
// handleAdminScanFloatingIPs starts the background floating-IP scan (see
|
||||
// orchestrator.StartScan) and answers 202 with its status at once; a scan that
|
||||
// is already running is joined (202 with the running job's status, no second
|
||||
// job). Query: dry_run=true only discovers and counts, leaving the queue
|
||||
// untouched; wait=true blocks until the job finishes and answers 200 with the
|
||||
// classic synchronous body {scanned_free, added, requeued, reordered,
|
||||
// skipped_in_progress} (502 if the scan failed). Takes no body; POST is used
|
||||
// (rather than GET) because it mutates the queue, matching handleAdminSubmitIPs.
|
||||
func (s *Server) handleAdminScanFloatingIPs(w http.ResponseWriter, r *http.Request) {
|
||||
result, scannedFree, err := s.Orch.ScanFloatingIPs(r.Context())
|
||||
q := r.URL.Query()
|
||||
dryRun, err := parseBoolParam(q, "dry_run")
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
wait, err := parseBoolParam(q, "wait")
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
opts := orchestrator.ScanOptions{DryRun: dryRun}
|
||||
|
||||
if !wait {
|
||||
st, _ := s.Orch.StartScan(opts)
|
||||
writeJSON(w, http.StatusAccepted, toScanStatusDTO(st))
|
||||
return
|
||||
}
|
||||
result, scannedFree, err := s.Orch.ScanAndWait(r.Context(), opts)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadGateway, err.Error())
|
||||
return
|
||||
@@ -119,6 +245,29 @@ func (s *Server) handleAdminScanFloatingIPs(w http.ResponseWriter, r *http.Reque
|
||||
})
|
||||
}
|
||||
|
||||
// handleAdminScanStatus returns the current status/progress of the scan job.
|
||||
func (s *Server) handleAdminScanStatus(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, toScanStatusDTO(s.Orch.ScanStatus()))
|
||||
}
|
||||
|
||||
func toScanStatusDTO(st orchestrator.ScanStatus) scanStatusDTO {
|
||||
return scanStatusDTO{
|
||||
State: string(st.State),
|
||||
Running: st.Running,
|
||||
DryRun: st.DryRun,
|
||||
Pages: st.Pages,
|
||||
Discovered: st.Discovered,
|
||||
Free: st.Free,
|
||||
Added: st.Added,
|
||||
Requeued: st.Requeued,
|
||||
Reordered: st.Reordered,
|
||||
SkippedInProgress: st.SkippedInProgress,
|
||||
StartedAt: st.StartedAt,
|
||||
FinishedAt: st.FinishedAt,
|
||||
Error: st.Error,
|
||||
}
|
||||
}
|
||||
|
||||
// handleAdminCancelIP force-stops a check in progress (or still-queued) for
|
||||
// the given address. Requires the orchestrator, since a floating IP may
|
||||
// need to be disassociated in OpenStack.
|
||||
@@ -176,7 +325,7 @@ func (s *Server) handleAdminClearQueue(w http.ResponseWriter, r *http.Request) {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, clearQueueResponse{Deleted: emptyIfNil(result.Deleted)})
|
||||
writeJSON(w, http.StatusOK, clearQueueResponse{Deleted: emptyIfNil(result.Deleted), Count: len(result.Deleted)})
|
||||
}
|
||||
|
||||
// emptyIfNil turns a nil slice into an empty one so these fields always
|
||||
|
||||
Reference in new issue
Block a user