added FIP Cooldown before start
This commit is contained in:
1 parent
291eea3eb8
commit
f22ad569b6
36 files changed
+1182
-25
No files matched your search
@@ -70,6 +70,14 @@ type OrchestratorConfig struct {
|
||||
MaxRetries int `yaml:"max_retries"`
|
||||
LeaseTTLSeconds int `yaml:"lease_ttl_seconds"`
|
||||
HeartbeatTimeoutSeconds int `yaml:"heartbeat_timeout_seconds"`
|
||||
// FIPSettleSeconds is only the one-time seed value used the first time
|
||||
// control-api starts against an empty database; after that it's
|
||||
// managed at runtime via PUT /api/v1/admin/config/orchestrator (or the
|
||||
// dashboard's /settings page) and this field is ignored. Zero (no
|
||||
// pause) is a valid, backward-compatible default — unlike every other
|
||||
// field in this struct, it deliberately gets no nonzero default in
|
||||
// LoadControlAPI below.
|
||||
FIPSettleSeconds int `yaml:"fip_settle_seconds"`
|
||||
}
|
||||
|
||||
type AggregationConfig struct {
|
||||
|
||||
@@ -200,3 +200,16 @@ func (c *client) PutCheckType(ctx context.Context, name string, enabled bool, ta
|
||||
func (c *client) DeleteCheckType(ctx context.Context, name string) error {
|
||||
return c.do(ctx, http.MethodDelete, "/api/v1/admin/config/check-types/"+url.PathEscape(name), nil, nil)
|
||||
}
|
||||
|
||||
func (c *client) GetOrchestratorSettings(ctx context.Context) (orchestratorSettingsDTO, error) {
|
||||
var out orchestratorSettingsDTO
|
||||
err := c.do(ctx, http.MethodGet, "/api/v1/admin/config/orchestrator", nil, &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
func (c *client) PutOrchestratorSettings(ctx context.Context, fipSettleSeconds int) (orchestratorSettingsDTO, error) {
|
||||
var out orchestratorSettingsDTO
|
||||
err := c.do(ctx, http.MethodPut, "/api/v1/admin/config/orchestrator",
|
||||
orchestratorSettingsDTO{FIPSettleSeconds: fipSettleSeconds}, &out)
|
||||
return out, err
|
||||
}
|
||||
@@ -22,12 +22,13 @@ import (
|
||||
// re-implementation of control-api's own business rules (that's already
|
||||
// covered by internal/httpapi's own tests).
|
||||
type fakeControlAPI struct {
|
||||
mu sync.Mutex
|
||||
ips []ipQueueItem
|
||||
validators []validatorDTO
|
||||
sites map[int]string
|
||||
groups map[string][]string
|
||||
checkTypes map[string]checkTypeDTO
|
||||
mu sync.Mutex
|
||||
ips []ipQueueItem
|
||||
validators []validatorDTO
|
||||
sites map[int]string
|
||||
groups map[string][]string
|
||||
checkTypes map[string]checkTypeDTO
|
||||
fipSettleSeconds int
|
||||
}
|
||||
|
||||
func newFakeControlAPI(t *testing.T) (*fakeControlAPI, string) {
|
||||
@@ -187,6 +188,24 @@ func (f *fakeControlAPI) handler() http.Handler {
|
||||
writeJSON(w, http.StatusOK, resp)
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /api/v1/admin/config/orchestrator", func(w http.ResponseWriter, r *http.Request) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
writeJSON(w, http.StatusOK, orchestratorSettingsDTO{FIPSettleSeconds: f.fipSettleSeconds})
|
||||
})
|
||||
mux.HandleFunc("PUT /api/v1/admin/config/orchestrator", func(w http.ResponseWriter, r *http.Request) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
var req orchestratorSettingsDTO
|
||||
_ = json.NewDecoder(r.Body).Decode(&req)
|
||||
if req.FIPSettleSeconds < 0 {
|
||||
writeAPIErr(w, http.StatusBadRequest, "fip_settle_seconds must be >= 0")
|
||||
return
|
||||
}
|
||||
f.fipSettleSeconds = req.FIPSettleSeconds
|
||||
writeJSON(w, http.StatusOK, orchestratorSettingsDTO{FIPSettleSeconds: f.fipSettleSeconds})
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /api/v1/admin/config/validators", func(w http.ResponseWriter, r *http.Request) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
@@ -35,6 +35,7 @@ type ipQueueItem struct {
|
||||
Site3Complete bool
|
||||
OverallResult string
|
||||
AssignedAt *time.Time
|
||||
FIPAssociatedAt *time.Time
|
||||
AggregatedAt *time.Time
|
||||
FIPReleasedAt *time.Time
|
||||
CreatedAt time.Time
|
||||
@@ -123,3 +124,7 @@ type checkTypeDTO struct {
|
||||
type errorResponse struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
type orchestratorSettingsDTO struct {
|
||||
FIPSettleSeconds int `json:"fip_settle_seconds"`
|
||||
}
|
||||
@@ -7,7 +7,8 @@ import (
|
||||
|
||||
type ipsPageData struct {
|
||||
PageData
|
||||
Items []ipQueueItem
|
||||
Items []ipQueueItem
|
||||
FIPSettleSeconds int
|
||||
}
|
||||
|
||||
type ipDetailData struct {
|
||||
@@ -17,7 +18,11 @@ type ipDetailData struct {
|
||||
|
||||
func (s *Server) handleIPsPage(w http.ResponseWriter, r *http.Request) {
|
||||
items, err := s.CA.ListIPs(r.Context())
|
||||
data := ipsPageData{Items: items}
|
||||
settings, settingsErr := s.CA.GetOrchestratorSettings(r.Context())
|
||||
if err == nil {
|
||||
err = settingsErr
|
||||
}
|
||||
data := ipsPageData{Items: items, FIPSettleSeconds: settings.FIPSettleSeconds}
|
||||
data.ActiveNav = "ips"
|
||||
data.Banner = bannerFor(err)
|
||||
s.renderPage(w, "ips_page", data)
|
||||
@@ -41,7 +46,11 @@ func (s *Server) renderIPsTable(w http.ResponseWriter, r *http.Request, actionEr
|
||||
if actionErr == nil {
|
||||
actionErr = listErr
|
||||
}
|
||||
s.renderFragment(w, "ips_table", ipsPageData{Items: items}, actionErr)
|
||||
settings, settingsErr := s.CA.GetOrchestratorSettings(r.Context())
|
||||
if actionErr == nil {
|
||||
actionErr = settingsErr
|
||||
}
|
||||
s.renderFragment(w, "ips_table", ipsPageData{Items: items, FIPSettleSeconds: settings.FIPSettleSeconds}, actionErr)
|
||||
}
|
||||
|
||||
func (s *Server) handleIPsSubmit(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package dashboard
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type settingsPageData struct {
|
||||
PageData
|
||||
Settings orchestratorSettingsDTO
|
||||
}
|
||||
|
||||
func (s *Server) handleSettingsPage(w http.ResponseWriter, r *http.Request) {
|
||||
settings, err := s.CA.GetOrchestratorSettings(r.Context())
|
||||
data := settingsPageData{Settings: settings}
|
||||
data.ActiveNav = "settings"
|
||||
data.Banner = bannerFor(err)
|
||||
s.renderPage(w, "settings_page", data)
|
||||
}
|
||||
|
||||
// renderSettingsForm re-fetches the current settings and renders the
|
||||
// settings_form fragment, tagging actionErr (if any) on the shared error
|
||||
// banner — same pattern as renderIPsTable: always reflect true current
|
||||
// state regardless of whether the mutation itself succeeded.
|
||||
func (s *Server) renderSettingsForm(w http.ResponseWriter, r *http.Request, actionErr error) {
|
||||
settings, getErr := s.CA.GetOrchestratorSettings(r.Context())
|
||||
if actionErr == nil {
|
||||
actionErr = getErr
|
||||
}
|
||||
s.renderFragment(w, "settings_form", settingsPageData{Settings: settings}, actionErr)
|
||||
}
|
||||
|
||||
func (s *Server) handleSettingsPut(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
s.renderSettingsForm(w, r, fmt.Errorf("invalid form: %w", err))
|
||||
return
|
||||
}
|
||||
seconds, err := strconv.Atoi(r.PostFormValue("fip_settle_seconds"))
|
||||
if err != nil {
|
||||
s.renderSettingsForm(w, r, &apiErr{Status: http.StatusBadRequest, Message: "пауза должна быть целым числом секунд"})
|
||||
return
|
||||
}
|
||||
_, err = s.CA.PutOrchestratorSettings(r.Context(), seconds)
|
||||
s.renderSettingsForm(w, r, err)
|
||||
}
|
||||
@@ -175,6 +175,61 @@ func TestIPsClear(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSettingsGetAndPut(t *testing.T) {
|
||||
fake, caURL := newFakeControlAPI(t)
|
||||
ts := newTestServer(t, caURL)
|
||||
|
||||
page := get(t, ts, "/settings")
|
||||
if !strings.Contains(page, `value="0"`) {
|
||||
t.Fatalf("expected default 0 in the form, got:\n%s", page)
|
||||
}
|
||||
|
||||
body := postForm(t, ts, "PUT", "/settings", map[string][]string{"fip_settle_seconds": {"15"}})
|
||||
if !strings.Contains(body, `value="15"`) {
|
||||
t.Fatalf("expected updated value 15 in re-rendered form, got:\n%s", body)
|
||||
}
|
||||
if fake.fipSettleSeconds != 15 {
|
||||
t.Fatalf("expected fake control-api settings updated, got %d", fake.fipSettleSeconds)
|
||||
}
|
||||
|
||||
// A control-api validation error (negative value here) surfaces via
|
||||
// the banner, not a crash.
|
||||
body = postForm(t, ts, "PUT", "/settings", map[string][]string{"fip_settle_seconds": {"-1"}})
|
||||
if !strings.Contains(body, "alert-warning") {
|
||||
t.Fatalf("expected client error banner for invalid value, got:\n%s", body)
|
||||
}
|
||||
|
||||
// A non-numeric value is caught by the dashboard itself before it ever
|
||||
// reaches control-api.
|
||||
body = postForm(t, ts, "PUT", "/settings", map[string][]string{"fip_settle_seconds": {"not-a-number"}})
|
||||
if !strings.Contains(body, "alert-warning") {
|
||||
t.Fatalf("expected client error banner for non-numeric value, got:\n%s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIPsPageShowsSettleBadge(t *testing.T) {
|
||||
fake, caURL := newFakeControlAPI(t)
|
||||
now := time.Now()
|
||||
fake.fipSettleSeconds = 60
|
||||
fake.ips = []ipQueueItem{
|
||||
{IPAddress: "1.1.1.1", State: "awaiting_self_check", FIPAssociatedAt: &now, UpdatedAt: now, CreatedAt: now},
|
||||
}
|
||||
ts := newTestServer(t, caURL)
|
||||
|
||||
body := get(t, ts, "/ips")
|
||||
if !strings.Contains(body, "прогрев FIP") {
|
||||
t.Fatalf("expected settle badge for a freshly-associated ip within the pause, got:\n%s", body)
|
||||
}
|
||||
|
||||
// Once fip_settle_seconds is 0, the same row falls back to the plain
|
||||
// state pill.
|
||||
fake.fipSettleSeconds = 0
|
||||
body = get(t, ts, "/ips")
|
||||
if strings.Contains(body, "прогрев FIP") {
|
||||
t.Fatalf("expected no settle badge once fip_settle_seconds is 0, got:\n%s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorsCRUD(t *testing.T) {
|
||||
_, caURL := newFakeControlAPI(t)
|
||||
ts := newTestServer(t, caURL)
|
||||
|
||||
@@ -13,7 +13,14 @@ import (
|
||||
// with "pill" in the template (e.g. `class="pill {{.Class}}"`).
|
||||
type Badge struct{ Class, Label string }
|
||||
|
||||
func ipBadge(state, result string) Badge {
|
||||
// ipBadge picks the status pill for a queue row. fipAssociatedAt/
|
||||
// settleSeconds are only consulted for state=="awaiting_self_check": if
|
||||
// the configured fip_settle_seconds pause (see docs/USAGE.md) hasn't
|
||||
// elapsed since the floating IP was associated, the row gets a distinct
|
||||
// "прогрев FIP" badge instead of looking identical to a row just waiting
|
||||
// on the agent's next poll. The time math happens here, not in the
|
||||
// template, same as fmtTime's existing precedent.
|
||||
func ipBadge(state, result string, fipAssociatedAt *time.Time, settleSeconds int) Badge {
|
||||
switch state {
|
||||
case "done", "failed":
|
||||
switch result {
|
||||
@@ -28,6 +35,11 @@ func ipBadge(state, result string) Badge {
|
||||
}
|
||||
case "queued":
|
||||
return Badge{"pill-neutral", "queued"}
|
||||
case "awaiting_self_check":
|
||||
if settleSeconds > 0 && fipAssociatedAt != nil && time.Now().Before(fipAssociatedAt.Add(time.Duration(settleSeconds)*time.Second)) {
|
||||
return Badge{"pill-warning", "прогрев FIP"}
|
||||
}
|
||||
return Badge{"pill-info", state}
|
||||
default:
|
||||
return Badge{"pill-info", state}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,9 @@ func (s *Server) routes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("PUT /check-types/{name}", s.handleCheckTypeUpdate)
|
||||
mux.HandleFunc("DELETE /check-types/{name}", s.handleCheckTypeDelete)
|
||||
|
||||
mux.HandleFunc("GET /settings", s.handleSettingsPage)
|
||||
mux.HandleFunc("PUT /settings", s.handleSettingsPut)
|
||||
|
||||
mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServerFS(staticSubFS())))
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
{{define "ip_detail_content"}}
|
||||
<p><a href="/ips">← К очереди</a></p>
|
||||
{{$b := ipBadge .Detail.IP.State .Detail.IP.OverallResult}}
|
||||
{{$b := ipBadge .Detail.IP.State .Detail.IP.OverallResult .Detail.IP.FIPAssociatedAt 0}}
|
||||
<div class="topbar">
|
||||
<h1 class="mono" style="display:flex;align-items:center;gap:10px">{{.Detail.IP.IPAddress}} <span class="pill {{$b.Class}}">{{$b.Label}}</span></h1>
|
||||
</div>
|
||||
|
||||
@@ -54,7 +54,7 @@
|
||||
<thead><tr><th><input type="checkbox" onclick="this.closest('table').querySelectorAll('input[name=addresses]').forEach(cb => cb.checked = this.checked)"></th><th>Адрес</th><th>Состояние</th><th>Валидатор</th><th>Попытка</th><th>Обновлено</th><th></th></tr></thead>
|
||||
<tbody>
|
||||
{{range .Items}}
|
||||
{{$b := ipBadge .State .OverallResult}}
|
||||
{{$b := ipBadge .State .OverallResult .FIPAssociatedAt $.FIPSettleSeconds}}
|
||||
{{$terminal := or (eq .State "done") (eq .State "failed")}}
|
||||
<tr>
|
||||
<td data-label=""><input type="checkbox" name="addresses" value="{{.IPAddress}}"></td>
|
||||
|
||||
@@ -52,6 +52,10 @@
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 11l3 3L22 4"/><path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11"/></svg>
|
||||
Типы проверок
|
||||
</a>
|
||||
<a class="nav-link{{if eq .ActiveNav "settings"}} active{{end}}" {{if eq .ActiveNav "settings"}}aria-current="page"{{end}} href="/settings">
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1Z"/></svg>
|
||||
Настройки
|
||||
</a>
|
||||
</nav>
|
||||
|
||||
<div class="sidebar-foot">
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<thead><tr><th>Адрес</th><th>Состояние</th><th>Валидатор</th><th>Попытка</th><th>Назначено</th></tr></thead>
|
||||
<tbody>
|
||||
{{range .CurrentItems}}
|
||||
{{$b := ipBadge .State .OverallResult}}
|
||||
{{$b := ipBadge .State .OverallResult .FIPAssociatedAt 0}}
|
||||
<tr>
|
||||
<td class="addr" data-label="Адрес"><a href="/ips/{{.IPAddress}}">{{.IPAddress}}</a></td>
|
||||
<td data-label="Состояние"><span class="pill {{$b.Class}}">{{$b.Label}}</span></td>
|
||||
@@ -46,7 +46,7 @@
|
||||
<thead><tr><th>Адрес</th><th>Итог</th><th>Завершено</th></tr></thead>
|
||||
<tbody>
|
||||
{{range .LastCompleted}}
|
||||
{{$b := ipBadge .State .OverallResult}}
|
||||
{{$b := ipBadge .State .OverallResult .FIPAssociatedAt 0}}
|
||||
<tr>
|
||||
<td class="addr" data-label="Адрес"><a href="/ips/{{.IPAddress}}">{{.IPAddress}}</a></td>
|
||||
<td data-label="Итог"><span class="pill {{$b.Class}}">{{$b.Label}}</span></td>
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
{{define "settings_page"}}
|
||||
<!doctype html>
|
||||
<html lang="ru">
|
||||
<head>{{template "html_head" .}}</head>
|
||||
<body>
|
||||
<input type="checkbox" id="nav-toggle" class="nav-toggle">
|
||||
<div class="shell">
|
||||
{{template "sidebar_nav" .}}
|
||||
<div>
|
||||
{{template "topbar_mobile" .}}
|
||||
<main class="main">
|
||||
<div id="error-banner">{{template "banner_inner" .Banner}}</div>
|
||||
{{template "settings_content" .}}
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
||||
|
||||
{{define "settings_content"}}
|
||||
<div class="topbar"><h1>Настройки</h1></div>
|
||||
|
||||
<div id="settings-form-wrap">
|
||||
{{template "settings_form" .}}
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
{{define "settings_form"}}
|
||||
<div class="panel">
|
||||
<div class="panel-body">
|
||||
<p class="muted" style="margin-bottom:16px">Пауза между привязкой Floating IP к валидатору и началом self-check —
|
||||
даёт data plane OpenStack время начать пропускать трафик через только что привязанный адрес, прежде чем
|
||||
проверять его. <code>0</code> — без паузы (поведение по умолчанию). Значение должно оставлять запас внутри лизинга
|
||||
адреса: <code>fip_settle_seconds + self_check_timeout_seconds < lease_ttl_seconds</code> — иначе адрес не успеет
|
||||
пройти self-check до истечения лизинга и будет возвращён в очередь.</p>
|
||||
<form hx-put="/settings" hx-target="#settings-form-wrap" hx-swap="innerHTML">
|
||||
<div class="field-row">
|
||||
<div class="field">
|
||||
<label for="fip_settle_seconds">Пауза перед self-check (сек)</label>
|
||||
<input type="number" id="fip_settle_seconds" name="fip_settle_seconds" min="0" step="1" value="{{.Settings.FIPSettleSeconds}}" required>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Сохранить</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
@@ -33,6 +33,9 @@ func (d *DB) BootstrapFromConfig(ctx context.Context, cfg *config.ControlAPI) er
|
||||
if err := d.bootstrapCheckTypes(ctx, cfg.CheckTypes); err != nil {
|
||||
return fmt.Errorf("bootstrap check types: %w", err)
|
||||
}
|
||||
if err := d.bootstrapSettings(ctx, cfg.Orchestrator.FIPSettleSeconds); err != nil {
|
||||
return fmt.Errorf("bootstrap settings: %w", err)
|
||||
}
|
||||
if err := d.SeedQueue(ctx, cfg.IPAddresses); err != nil {
|
||||
return fmt.Errorf("seed ip queue: %w", err)
|
||||
}
|
||||
@@ -102,3 +105,18 @@ func (d *DB) bootstrapCheckTypes(ctx context.Context, checkTypes []config.CheckT
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DB) bootstrapSettings(ctx context.Context, fipSettleSeconds int) error {
|
||||
var count int
|
||||
if err := d.QueryRowContext(ctx, `SELECT COUNT(*) FROM settings`).Scan(&count); err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return nil
|
||||
}
|
||||
now := timeToDB(Now())
|
||||
_, err := d.ExecContext(ctx, `
|
||||
INSERT INTO settings (id, fip_settle_seconds, created_at, updated_at) VALUES (1, ?, ?, ?)
|
||||
`, fipSettleSeconds, now, now)
|
||||
return err
|
||||
}
|
||||
@@ -19,6 +19,9 @@ var initSchema string
|
||||
//go:embed migrations/0002_dynamic_config.sql
|
||||
var dynamicConfigSchema string
|
||||
|
||||
//go:embed migrations/0003_fip_settle_delay.sql
|
||||
var fipSettleDelaySchema string
|
||||
|
||||
// migrations is the ordered list of schema versions. Each entry's SQL is
|
||||
// applied, in order, for any version greater than the database's current
|
||||
// PRAGMA user_version — so a fresh database walks the whole list and an
|
||||
@@ -29,6 +32,7 @@ var migrations = []struct {
|
||||
}{
|
||||
{1, initSchema},
|
||||
{2, dynamicConfigSchema},
|
||||
{3, fipSettleDelaySchema},
|
||||
}
|
||||
|
||||
type DB struct {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
-- Support for a configurable pause between FIP association and the start
|
||||
-- of self-check (see docs/USAGE.md).
|
||||
|
||||
ALTER TABLE ip_queue ADD COLUMN fip_associated_at TIMESTAMP;
|
||||
|
||||
-- Singleton row for the one dynamic scalar setting we have so far.
|
||||
-- Deliberately a typed single-row table, not a generic key-value settings
|
||||
-- table: overengineering for exactly one field. If more scalar knobs show
|
||||
-- up, add columns here first.
|
||||
CREATE TABLE settings (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
fip_settle_seconds INTEGER NOT NULL DEFAULT 0,
|
||||
created_at TIMESTAMP NOT NULL,
|
||||
updated_at TIMESTAMP NOT NULL
|
||||
);
|
||||
@@ -84,6 +84,7 @@ type IPQueueItem struct {
|
||||
Site3Complete bool
|
||||
OverallResult string
|
||||
AssignedAt *time.Time
|
||||
FIPAssociatedAt *time.Time
|
||||
AggregatedAt *time.Time
|
||||
FIPReleasedAt *time.Time
|
||||
CreatedAt time.Time
|
||||
@@ -162,3 +163,11 @@ type DeleteIPsResult struct {
|
||||
Deleted []string
|
||||
NotFound []string
|
||||
}
|
||||
|
||||
// Settings is the singleton row of orchestrator-wide scalar knobs that are
|
||||
// admin-configurable at runtime (see queries_settings.go).
|
||||
type Settings struct {
|
||||
FIPSettleSeconds int
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
@@ -110,9 +110,9 @@ func (d *DB) ClaimNextQueued(ctx context.Context, validatorID string, leaseTTL t
|
||||
func (d *DB) SetFIPAssociated(ctx context.Context, ipID int64, fipID string, leaseTTL time.Duration) error {
|
||||
now := Now()
|
||||
_, err := d.ExecContext(ctx, `
|
||||
UPDATE ip_queue SET state=?, fip_id=?, lease_expires_at=?, updated_at=?
|
||||
UPDATE ip_queue SET state=?, fip_id=?, fip_associated_at=?, lease_expires_at=?, updated_at=?
|
||||
WHERE id=?
|
||||
`, IPAwaitingSelfCheck, fipID, timeToDB(now.Add(leaseTTL)), timeToDB(now), ipID)
|
||||
`, IPAwaitingSelfCheck, fipID, timeToDB(now), timeToDB(now.Add(leaseTTL)), timeToDB(now), ipID)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -197,7 +197,7 @@ func (d *DB) RequeueOrFail(ctx context.Context, ipID int64, validatorID string,
|
||||
UPDATE ip_queue SET
|
||||
state=?, owner_validator_id=NULL, fip_id='', retry_count=?, attempt_number=attempt_number+1,
|
||||
lease_expires_at=NULL, egress_complete=0, site1_complete=0, site2_complete=0, site3_complete=0,
|
||||
overall_result='', assigned_at=NULL, updated_at=?
|
||||
overall_result='', assigned_at=NULL, fip_associated_at=NULL, updated_at=?
|
||||
WHERE id=?
|
||||
`, nextState, retryCount, now, ipID)
|
||||
} else {
|
||||
@@ -283,7 +283,7 @@ func (d *DB) SubmitIPs(ctx context.Context, addresses []string) (SubmitIPsResult
|
||||
state=?, sequence=?, owner_validator_id=NULL, fip_id='', retry_count=0,
|
||||
attempt_number=attempt_number+1, lease_expires_at=NULL, egress_complete=0,
|
||||
site1_complete=0, site2_complete=0, site3_complete=0, overall_result='',
|
||||
assigned_at=NULL, aggregated_at=NULL, fip_released_at=NULL, updated_at=?
|
||||
assigned_at=NULL, fip_associated_at=NULL, aggregated_at=NULL, fip_released_at=NULL, updated_at=?
|
||||
WHERE ip_address=?
|
||||
`, IPQueued, seq, now, addr); err != nil {
|
||||
return result, fmt.Errorf("requeue %s: %w", addr, err)
|
||||
@@ -480,7 +480,7 @@ func (d *DB) ListExpiredLeases(ctx context.Context, now time.Time) ([]IPQueueIte
|
||||
const ipQueueSelect = `
|
||||
SELECT id, ip_address, sequence, state, owner_validator_id, fip_id, attempt_number, retry_count,
|
||||
lease_expires_at, egress_complete, site1_complete, site2_complete, site3_complete, overall_result,
|
||||
assigned_at, aggregated_at, fip_released_at, created_at, updated_at
|
||||
assigned_at, fip_associated_at, aggregated_at, fip_released_at, created_at, updated_at
|
||||
FROM ip_queue
|
||||
`
|
||||
|
||||
@@ -499,13 +499,13 @@ func scanIPQueueItems(rows *sql.Rows) ([]IPQueueItem, error) {
|
||||
func scanIPQueueItem(row rowScanner) (*IPQueueItem, error) {
|
||||
var item IPQueueItem
|
||||
var owner sql.NullString
|
||||
var leaseExpires, assignedAt, aggregatedAt, fipReleasedAt sql.NullString
|
||||
var leaseExpires, assignedAt, fipAssociatedAt, aggregatedAt, fipReleasedAt sql.NullString
|
||||
var createdAt, updatedAt string
|
||||
if err := row.Scan(
|
||||
&item.ID, &item.IPAddress, &item.Sequence, &item.State, &owner, &item.FIPID,
|
||||
&item.AttemptNumber, &item.RetryCount, &leaseExpires,
|
||||
&item.EgressComplete, &item.Site1Complete, &item.Site2Complete, &item.Site3Complete,
|
||||
&item.OverallResult, &assignedAt, &aggregatedAt, &fipReleasedAt, &createdAt, &updatedAt,
|
||||
&item.OverallResult, &assignedAt, &fipAssociatedAt, &aggregatedAt, &fipReleasedAt, &createdAt, &updatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -519,6 +519,9 @@ func scanIPQueueItem(row rowScanner) (*IPQueueItem, error) {
|
||||
if item.AssignedAt, err = nullStringToTimePtr(assignedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if item.FIPAssociatedAt, err = nullStringToTimePtr(fipAssociatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if item.AggregatedAt, err = nullStringToTimePtr(aggregatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSetFIPAssociatedStampsTimestamp(t *testing.T) {
|
||||
d, ctx := newTestDB(t)
|
||||
|
||||
if err := d.SeedQueue(ctx, []string{"1.2.3.4"}); err != nil {
|
||||
t.Fatalf("seed queue: %v", err)
|
||||
}
|
||||
if err := d.AdminCreateValidator(ctx, "validator-1", "port-1"); err != nil {
|
||||
t.Fatalf("create validator: %v", err)
|
||||
}
|
||||
claimed, err := d.ClaimNextQueued(ctx, "validator-1", time.Minute)
|
||||
if err != nil || claimed == nil {
|
||||
t.Fatalf("claim: item=%+v err=%v", claimed, err)
|
||||
}
|
||||
if claimed.FIPAssociatedAt != nil {
|
||||
t.Fatalf("expected FIPAssociatedAt nil before association, got %v", claimed.FIPAssociatedAt)
|
||||
}
|
||||
|
||||
before := Now()
|
||||
if err := d.SetFIPAssociated(ctx, claimed.ID, "fip-1", time.Minute); err != nil {
|
||||
t.Fatalf("set fip associated: %v", err)
|
||||
}
|
||||
item, err := d.GetIP(ctx, claimed.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get ip: %v", err)
|
||||
}
|
||||
if item.FIPAssociatedAt == nil {
|
||||
t.Fatalf("expected FIPAssociatedAt to be set")
|
||||
}
|
||||
if item.FIPAssociatedAt.Before(before) {
|
||||
t.Fatalf("expected FIPAssociatedAt >= %v, got %v", before, *item.FIPAssociatedAt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequeueClearsFIPAssociatedAt(t *testing.T) {
|
||||
d, ctx := newTestDB(t)
|
||||
|
||||
if err := d.SeedQueue(ctx, []string{"1.2.3.4"}); err != nil {
|
||||
t.Fatalf("seed queue: %v", err)
|
||||
}
|
||||
if err := d.AdminCreateValidator(ctx, "validator-1", "port-1"); err != nil {
|
||||
t.Fatalf("create validator: %v", err)
|
||||
}
|
||||
claimed, err := d.ClaimNextQueued(ctx, "validator-1", time.Minute)
|
||||
if err != nil || claimed == nil {
|
||||
t.Fatalf("claim: item=%+v err=%v", claimed, err)
|
||||
}
|
||||
if err := d.SetFIPAssociated(ctx, claimed.ID, "fip-1", time.Minute); err != nil {
|
||||
t.Fatalf("set fip associated: %v", err)
|
||||
}
|
||||
|
||||
if err := d.RequeueOrFail(ctx, claimed.ID, "validator-1", 3); err != nil {
|
||||
t.Fatalf("requeue or fail: %v", err)
|
||||
}
|
||||
item, err := d.GetIP(ctx, claimed.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get ip after requeue: %v", err)
|
||||
}
|
||||
if item.State != IPQueued {
|
||||
t.Fatalf("expected requeued, got state=%s", item.State)
|
||||
}
|
||||
if item.FIPAssociatedAt != nil {
|
||||
t.Fatalf("expected FIPAssociatedAt cleared on requeue, got %v", item.FIPAssociatedAt)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// GetSettings returns the singleton settings row. BootstrapFromConfig
|
||||
// guarantees it exists before any other code path can observe it, so
|
||||
// sql.ErrNoRows here would indicate a bootstrap bug, not a normal
|
||||
// condition.
|
||||
func (d *DB) GetSettings(ctx context.Context) (Settings, error) {
|
||||
var s Settings
|
||||
var createdAt, updatedAt string
|
||||
err := d.QueryRowContext(ctx, `
|
||||
SELECT fip_settle_seconds, created_at, updated_at FROM settings WHERE id=1
|
||||
`).Scan(&s.FIPSettleSeconds, &createdAt, &updatedAt)
|
||||
if err != nil {
|
||||
return Settings{}, err
|
||||
}
|
||||
if s.CreatedAt, err = dbToTime(createdAt); err != nil {
|
||||
return Settings{}, err
|
||||
}
|
||||
if s.UpdatedAt, err = dbToTime(updatedAt); err != nil {
|
||||
return Settings{}, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// SetFIPSettleSeconds persists a new fip_settle_seconds value. Only
|
||||
// enforces seconds >= 0 — the cross-field rule against
|
||||
// lease_ttl_seconds/self_check_timeout_seconds lives in
|
||||
// orchestrator.Orchestrator.SetFIPSettleSeconds, which has access to the
|
||||
// static orchestrator config this package doesn't.
|
||||
func (d *DB) SetFIPSettleSeconds(ctx context.Context, seconds int) error {
|
||||
if seconds < 0 {
|
||||
return fmt.Errorf("fip_settle_seconds must be >= 0: %w", ErrValidation)
|
||||
}
|
||||
now := timeToDB(Now())
|
||||
_, err := d.ExecContext(ctx, `
|
||||
UPDATE settings SET fip_settle_seconds=?, updated_at=? WHERE id=1
|
||||
`, seconds, now)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"cloudipvalidator/internal/config"
|
||||
)
|
||||
|
||||
func TestBootstrapSettingsSeedsOnceFromConfig(t *testing.T) {
|
||||
d, ctx := newTestDB(t)
|
||||
|
||||
cfg := &config.ControlAPI{Orchestrator: config.OrchestratorConfig{FIPSettleSeconds: 30}}
|
||||
if err := d.BootstrapFromConfig(ctx, cfg); err != nil {
|
||||
t.Fatalf("first bootstrap: %v", err)
|
||||
}
|
||||
settings, err := d.GetSettings(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("get settings: %v", err)
|
||||
}
|
||||
if settings.FIPSettleSeconds != 30 {
|
||||
t.Fatalf("expected seeded value 30, got %d", settings.FIPSettleSeconds)
|
||||
}
|
||||
|
||||
// A second bootstrap with a different YAML value must not overwrite the
|
||||
// now-non-empty settings table — same "DB is source of truth once
|
||||
// seeded" semantics as validators/sites/targets/check_types.
|
||||
cfg2 := &config.ControlAPI{Orchestrator: config.OrchestratorConfig{FIPSettleSeconds: 99}}
|
||||
if err := d.BootstrapFromConfig(ctx, cfg2); err != nil {
|
||||
t.Fatalf("second bootstrap: %v", err)
|
||||
}
|
||||
settings, err = d.GetSettings(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("get settings after second bootstrap: %v", err)
|
||||
}
|
||||
if settings.FIPSettleSeconds != 30 {
|
||||
t.Fatalf("expected YAML to be ignored on non-empty settings table, got %d", settings.FIPSettleSeconds)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetFIPSettleSecondsRejectsNegative(t *testing.T) {
|
||||
d, ctx := newTestDB(t)
|
||||
if err := d.BootstrapFromConfig(ctx, &config.ControlAPI{}); err != nil {
|
||||
t.Fatalf("bootstrap: %v", err)
|
||||
}
|
||||
if err := d.SetFIPSettleSeconds(ctx, -1); !errors.Is(err, ErrValidation) {
|
||||
t.Fatalf("expected ErrValidation for negative seconds, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSetFIPSettleSecondsRoundTrip(t *testing.T) {
|
||||
d, ctx := newTestDB(t)
|
||||
if err := d.BootstrapFromConfig(ctx, &config.ControlAPI{}); err != nil {
|
||||
t.Fatalf("bootstrap: %v", err)
|
||||
}
|
||||
before, err := d.GetSettings(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("get settings: %v", err)
|
||||
}
|
||||
if before.FIPSettleSeconds != 0 {
|
||||
t.Fatalf("expected default 0, got %d", before.FIPSettleSeconds)
|
||||
}
|
||||
|
||||
if err := d.SetFIPSettleSeconds(ctx, 45); err != nil {
|
||||
t.Fatalf("set fip settle seconds: %v", err)
|
||||
}
|
||||
after, err := d.GetSettings(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("get settings after set: %v", err)
|
||||
}
|
||||
if after.FIPSettleSeconds != 45 {
|
||||
t.Fatalf("expected 45, got %d", after.FIPSettleSeconds)
|
||||
}
|
||||
if after.UpdatedAt.Before(before.UpdatedAt) {
|
||||
t.Fatalf("expected updated_at not to go backwards, before=%v after=%v", before.UpdatedAt, after.UpdatedAt)
|
||||
}
|
||||
}
|
||||
@@ -73,3 +73,10 @@ type putCheckTypeRequest struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Targets []string `json:"targets"`
|
||||
}
|
||||
|
||||
// orchestratorSettingsDTO doubles as both the GET response and the PUT
|
||||
// request body for /api/v1/admin/config/orchestrator — a single-field DTO,
|
||||
// same shape both ways, like putSiteRequest/siteDTO.
|
||||
type orchestratorSettingsDTO struct {
|
||||
FIPSettleSeconds int `json:"fip_settle_seconds"`
|
||||
}
|
||||
@@ -181,3 +181,31 @@ func (s *Server) handleConfigDeleteCheckType(w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
writeJSON(w, http.StatusOK, okResponse{OK: true})
|
||||
}
|
||||
|
||||
// --- orchestrator settings ---
|
||||
|
||||
func (s *Server) handleConfigGetOrchestratorSettings(w http.ResponseWriter, r *http.Request) {
|
||||
settings, err := s.DB.GetSettings(r.Context())
|
||||
if err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, orchestratorSettingsDTO{FIPSettleSeconds: settings.FIPSettleSeconds})
|
||||
}
|
||||
|
||||
// handleConfigPutOrchestratorSettings goes through the orchestrator (not a
|
||||
// direct DB call, unlike the read above) because setting fip_settle_seconds
|
||||
// needs cross-field validation against the static lease_ttl_seconds/
|
||||
// self_check_timeout_seconds config, which only the orchestrator has.
|
||||
func (s *Server) handleConfigPutOrchestratorSettings(w http.ResponseWriter, r *http.Request) {
|
||||
var req orchestratorSettingsDTO
|
||||
if err := readJSON(r, &req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid body: "+err.Error())
|
||||
return
|
||||
}
|
||||
if err := s.Orch.SetFIPSettleSeconds(r.Context(), req.FIPSettleSeconds); err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, orchestratorSettingsDTO{FIPSettleSeconds: req.FIPSettleSeconds})
|
||||
}
|
||||
@@ -410,3 +410,93 @@ func TestDeleteIPsAndClearQueue(t *testing.T) {
|
||||
t.Fatalf("expected validator freed after clear, got state=%s current_ip=%v", v.State, v.CurrentIPID)
|
||||
}
|
||||
}
|
||||
|
||||
// TestOrchestratorSettingsGetPut proves the settle-delay setting round-trips
|
||||
// through GET/PUT /api/v1/admin/config/orchestrator.
|
||||
func TestOrchestratorSettingsGetPut(t *testing.T) {
|
||||
fc, _, _, _ := newConfigTestHarness(t)
|
||||
|
||||
resp, body := fc.do(http.MethodGet, "/api/v1/admin/config/orchestrator", nil)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("get settings: status=%d body=%s", resp.StatusCode, body)
|
||||
}
|
||||
var got orchestratorSettingsDTO
|
||||
if err := json.Unmarshal(body, &got); err != nil {
|
||||
t.Fatalf("unmarshal get response: %v", err)
|
||||
}
|
||||
if got.FIPSettleSeconds != 0 {
|
||||
t.Fatalf("expected default 0, got %+v", got)
|
||||
}
|
||||
|
||||
resp, body = fc.do(http.MethodPut, "/api/v1/admin/config/orchestrator", orchestratorSettingsDTO{FIPSettleSeconds: 20})
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("put settings: status=%d body=%s", resp.StatusCode, body)
|
||||
}
|
||||
if err := json.Unmarshal(body, &got); err != nil {
|
||||
t.Fatalf("unmarshal put response: %v", err)
|
||||
}
|
||||
if got.FIPSettleSeconds != 20 {
|
||||
t.Fatalf("expected 20, got %+v", got)
|
||||
}
|
||||
|
||||
resp, body = fc.do(http.MethodGet, "/api/v1/admin/config/orchestrator", nil)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("get settings after put: status=%d body=%s", resp.StatusCode, body)
|
||||
}
|
||||
if err := json.Unmarshal(body, &got); err != nil {
|
||||
t.Fatalf("unmarshal get-after-put response: %v", err)
|
||||
}
|
||||
if got.FIPSettleSeconds != 20 {
|
||||
t.Fatalf("expected 20 to persist, got %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestOrchestratorSettingsPutValidation proves a value that would leave no
|
||||
// room for self-check inside the claim lease is rejected with 400.
|
||||
func TestOrchestratorSettingsPutValidation(t *testing.T) {
|
||||
fc, _, _, _ := newConfigTestHarness(t)
|
||||
// newConfigTestHarness: LeaseTTLSeconds=180, SelfCheckTimeoutSeconds=10.
|
||||
|
||||
resp, body := fc.do(http.MethodPut, "/api/v1/admin/config/orchestrator", orchestratorSettingsDTO{FIPSettleSeconds: 175})
|
||||
if resp.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400 for settle seconds too close to lease ttl, status=%d body=%s", resp.StatusCode, body)
|
||||
}
|
||||
|
||||
resp, body = fc.do(http.MethodPut, "/api/v1/admin/config/orchestrator", orchestratorSettingsDTO{FIPSettleSeconds: -1})
|
||||
if resp.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400 for negative settle seconds, status=%d body=%s", resp.StatusCode, body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFIPSettleDelayGatesAssignmentEndpoint proves GET
|
||||
// /api/v1/agents/{id}/assignment returns 204 while the settle window is
|
||||
// open and 200 once it has elapsed — the end-to-end proof of the whole
|
||||
// feature over the real HTTP wire contract.
|
||||
func TestFIPSettleDelayGatesAssignmentEndpoint(t *testing.T) {
|
||||
fc, _, orch, mock := newConfigTestHarness(t)
|
||||
ctx := context.Background()
|
||||
mock.Seed("fip-1", "9.9.9.9", "svc-project")
|
||||
|
||||
resp, body := fc.do(http.MethodPut, "/api/v1/admin/config/orchestrator", orchestratorSettingsDTO{FIPSettleSeconds: 1})
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("put settings: status=%d body=%s", resp.StatusCode, body)
|
||||
}
|
||||
|
||||
fc.do(http.MethodPost, "/api/v1/admin/config/validators", createValidatorRequest{ValidatorID: "validator-1", OSPortID: "port-1"})
|
||||
fc.do(http.MethodPost, "/api/v1/agents/register", registerAgentRequest{ValidatorID: "validator-1"})
|
||||
fc.do(http.MethodPost, "/api/v1/admin/ips", submitIPsRequest{Addresses: []string{"9.9.9.9"}})
|
||||
|
||||
orch.Tick(ctx) // claim + associate -> awaiting_self_check, fip attached
|
||||
|
||||
resp, body = fc.do(http.MethodGet, "/api/v1/agents/validator-1/assignment", nil)
|
||||
if resp.StatusCode != http.StatusNoContent {
|
||||
t.Fatalf("expected 204 during settle window, status=%d body=%s", resp.StatusCode, body)
|
||||
}
|
||||
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
|
||||
resp, body = fc.do(http.MethodGet, "/api/v1/agents/validator-1/assignment", nil)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("expected 200 after settle window elapsed, status=%d body=%s", resp.StatusCode, body)
|
||||
}
|
||||
}
|
||||
@@ -43,4 +43,7 @@ func (s *Server) routes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/v1/admin/config/check-types", s.handleConfigListCheckTypes)
|
||||
mux.HandleFunc("PUT /api/v1/admin/config/check-types/{name}", s.handleConfigPutCheckType)
|
||||
mux.HandleFunc("DELETE /api/v1/admin/config/check-types/{name}", s.handleConfigDeleteCheckType)
|
||||
|
||||
mux.HandleFunc("GET /api/v1/admin/config/orchestrator", s.handleConfigGetOrchestratorSettings)
|
||||
mux.HandleFunc("PUT /api/v1/admin/config/orchestrator", s.handleConfigPutOrchestratorSettings)
|
||||
}
|
||||
@@ -170,6 +170,15 @@ func (o *Orchestrator) AssignmentForValidator(ctx context.Context, validatorID s
|
||||
if item.State != db.IPAwaitingSelfCheck && item.State != db.IPChecking {
|
||||
return nil, nil, nil
|
||||
}
|
||||
if item.State == db.IPAwaitingSelfCheck {
|
||||
settled, err := o.isFIPSettled(ctx, item)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if !settled {
|
||||
return nil, nil, nil
|
||||
}
|
||||
}
|
||||
resolved, err := o.DB.ListResolvedCheckTypes(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
@@ -181,6 +190,47 @@ func (o *Orchestrator) AssignmentForValidator(ctx context.Context, validatorID s
|
||||
return item, checks, nil
|
||||
}
|
||||
|
||||
// isFIPSettled reports whether an address currently awaiting_self_check
|
||||
// has cleared the configured fip_settle_seconds pause since its floating
|
||||
// IP was associated — withholding the assignment until then is how the
|
||||
// pause is enforced, with zero changes needed to the agent's poll loop or
|
||||
// the assignment endpoint's wire contract (it just keeps seeing 204s).
|
||||
// Settings are read fresh on every call, same as check_types/sites
|
||||
// elsewhere in this file, so an admin change applies immediately even to
|
||||
// an address already mid-wait. A nil FIPAssociatedAt (an in-flight row
|
||||
// from before this feature's migration) is always treated as settled —
|
||||
// upgrading control-api must never newly strand an address that was
|
||||
// already awaiting self-check.
|
||||
func (o *Orchestrator) isFIPSettled(ctx context.Context, item *db.IPQueueItem) (bool, error) {
|
||||
settings, err := o.DB.GetSettings(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if settings.FIPSettleSeconds <= 0 || item.FIPAssociatedAt == nil {
|
||||
return true, nil
|
||||
}
|
||||
deadline := item.FIPAssociatedAt.Add(time.Duration(settings.FIPSettleSeconds) * time.Second)
|
||||
return !db.Now().Before(deadline), nil
|
||||
}
|
||||
|
||||
// SetFIPSettleSeconds validates and persists a new fip_settle_seconds
|
||||
// value. Lives here rather than internal/db because the cross-field rule
|
||||
// below needs o.Cfg, which the db package has no access to: the pause plus
|
||||
// self-check's own timeout must leave room inside the claim lease, or the
|
||||
// lease sweep would reclaim the address before self-check ever gets a
|
||||
// chance to run, producing a perpetual requeue loop.
|
||||
func (o *Orchestrator) SetFIPSettleSeconds(ctx context.Context, seconds int) error {
|
||||
if seconds < 0 {
|
||||
return fmt.Errorf("fip_settle_seconds must be >= 0: %w", db.ErrValidation)
|
||||
}
|
||||
if seconds+o.Cfg.SelfCheckTimeoutSeconds >= o.Cfg.LeaseTTLSeconds {
|
||||
return fmt.Errorf(
|
||||
"fip_settle_seconds (%d) + self_check_timeout_seconds (%d) must be < lease_ttl_seconds (%d): %w",
|
||||
seconds, o.Cfg.SelfCheckTimeoutSeconds, o.Cfg.LeaseTTLSeconds, db.ErrValidation)
|
||||
}
|
||||
return o.DB.SetFIPSettleSeconds(ctx, seconds)
|
||||
}
|
||||
|
||||
// SiteIndexForID resolves a configured site_id to its 1/2/3 index, or
|
||||
// (0, nil) if unconfigured.
|
||||
func (o *Orchestrator) SiteIndexForID(ctx context.Context, siteID string) (int, error) {
|
||||
|
||||
@@ -409,6 +409,124 @@ func TestClearQueueDisassociatesAllFIPs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestFIPSettleDelayWithholdsAssignment proves a nonzero fip_settle_seconds
|
||||
// keeps AssignmentForValidator returning nil (agent keeps polling and
|
||||
// getting nothing) until the configured pause has elapsed since the
|
||||
// floating IP was associated.
|
||||
func TestFIPSettleDelayWithholdsAssignment(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
o, d, mock := newTestOrchestrator(t, 180)
|
||||
if err := o.SetFIPSettleSeconds(ctx, 1); err != nil {
|
||||
t.Fatalf("set fip settle seconds: %v", err)
|
||||
}
|
||||
mock.Seed("fip-1", "1.2.3.4", "svc-project")
|
||||
_ = d.RegisterValidator(ctx, "validator-1", "host-1", "port-1", "v0.1")
|
||||
_ = d.SeedQueue(ctx, []string{"1.2.3.4"})
|
||||
|
||||
o.Tick(ctx) // claim + associate -> awaiting_self_check, fip attached
|
||||
|
||||
item, checks, err := o.AssignmentForValidator(ctx, "validator-1")
|
||||
if err != nil {
|
||||
t.Fatalf("assignment for validator: %v", err)
|
||||
}
|
||||
if item != nil || checks != nil {
|
||||
t.Fatalf("expected no assignment during settle window, got item=%+v checks=%+v", item, checks)
|
||||
}
|
||||
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
|
||||
item, checks, err = o.AssignmentForValidator(ctx, "validator-1")
|
||||
if err != nil {
|
||||
t.Fatalf("assignment for validator after settle: %v", err)
|
||||
}
|
||||
if item == nil {
|
||||
t.Fatalf("expected assignment to be available once settle window elapsed")
|
||||
}
|
||||
if len(checks) == 0 {
|
||||
t.Fatalf("expected check config to be returned alongside the item")
|
||||
}
|
||||
}
|
||||
|
||||
// TestFIPSettleDelayZeroIsNoOp proves the default fip_settle_seconds=0
|
||||
// never withholds an assignment — no behavior change for upgraded-but-
|
||||
// unconfigured installs.
|
||||
func TestFIPSettleDelayZeroIsNoOp(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
o, d, mock := newTestOrchestrator(t, 180)
|
||||
mock.Seed("fip-1", "1.2.3.4", "svc-project")
|
||||
_ = d.RegisterValidator(ctx, "validator-1", "host-1", "port-1", "v0.1")
|
||||
_ = d.SeedQueue(ctx, []string{"1.2.3.4"})
|
||||
|
||||
o.Tick(ctx)
|
||||
|
||||
item, _, err := o.AssignmentForValidator(ctx, "validator-1")
|
||||
if err != nil {
|
||||
t.Fatalf("assignment for validator: %v", err)
|
||||
}
|
||||
if item == nil {
|
||||
t.Fatalf("expected assignment immediately available with fip_settle_seconds=0")
|
||||
}
|
||||
}
|
||||
|
||||
// TestFIPSettleDelayIgnoresNilFIPAssociatedAt proves an in-flight row from
|
||||
// before this feature's migration (fip_associated_at NULL) is never newly
|
||||
// blocked, even with a nonzero configured pause — upgrading control-api
|
||||
// must not strand an address already awaiting self-check.
|
||||
func TestFIPSettleDelayIgnoresNilFIPAssociatedAt(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
o, d, mock := newTestOrchestrator(t, 180)
|
||||
if err := o.SetFIPSettleSeconds(ctx, 60); err != nil {
|
||||
t.Fatalf("set fip settle seconds: %v", err)
|
||||
}
|
||||
mock.Seed("fip-1", "1.2.3.4", "svc-project")
|
||||
_ = d.RegisterValidator(ctx, "validator-1", "host-1", "port-1", "v0.1")
|
||||
_ = d.SeedQueue(ctx, []string{"1.2.3.4"})
|
||||
|
||||
o.Tick(ctx)
|
||||
|
||||
item, err := d.GetIPByAddress(ctx, "1.2.3.4")
|
||||
if err != nil {
|
||||
t.Fatalf("get ip: %v", err)
|
||||
}
|
||||
if _, err := d.ExecContext(ctx, `UPDATE ip_queue SET fip_associated_at=NULL WHERE id=?`, item.ID); err != nil {
|
||||
t.Fatalf("clear fip_associated_at: %v", err)
|
||||
}
|
||||
|
||||
assigned, _, err := o.AssignmentForValidator(ctx, "validator-1")
|
||||
if err != nil {
|
||||
t.Fatalf("assignment for validator: %v", err)
|
||||
}
|
||||
if assigned == nil {
|
||||
t.Fatalf("expected assignment available for a row with nil FIPAssociatedAt despite nonzero settle delay")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetFIPSettleSecondsValidation proves the cross-field rule against
|
||||
// lease_ttl_seconds/self_check_timeout_seconds is enforced.
|
||||
func TestSetFIPSettleSecondsValidation(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
o, _, _ := newTestOrchestrator(t, 180) // SelfCheckTimeoutSeconds: 10 (see newTestOrchestratorWithSites)
|
||||
|
||||
cases := []struct {
|
||||
seconds int
|
||||
wantErr bool
|
||||
}{
|
||||
{-1, true},
|
||||
{170, true}, // 170+10 >= 180
|
||||
{169, false},
|
||||
{0, false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
err := o.SetFIPSettleSeconds(ctx, c.seconds)
|
||||
if c.wantErr && !errors.Is(err, db.ErrValidation) {
|
||||
t.Errorf("seconds=%d: expected ErrValidation, got %v", c.seconds, err)
|
||||
}
|
||||
if !c.wantErr && err != nil {
|
||||
t.Errorf("seconds=%d: expected success, got %v", c.seconds, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestInboundChecksPartialSites confirms a partially-configured sites list
|
||||
// (fewer than 3 slots assigned) only waits on the sites actually
|
||||
// configured — the two unassigned slots are never expected.
|
||||
|
||||
Reference in new issue
Block a user