added FIP Cooldown before start

This commit is contained in:
ayurishchev committed 2026-08-24 10:29:08 +03:00
1 parent 291eea3eb8
commit f22ad569b6
36 files changed
+1182 -25

No files matched your search

+7
View File
@@ -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"`
}
+28
View File
@@ -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})
}
+90
View File
@@ -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)
}
}
+3
View File
@@ -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)
}