Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/inventory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func main() {
})
mux.HandleFunc("GET /admin/chaos", c.HandleGetConfig)
mux.HandleFunc("PUT /admin/chaos", c.HandleSetConfig)
mux.HandleFunc("POST /admin/chaos/clear-disk", c.HandleClearDisk)
mux.Handle("/", svc.Mux)

handler := c.Middleware(middleware.Wrap(mux, cfg.ServiceName))
Expand Down
11 changes: 11 additions & 0 deletions internal/chaos/chaos.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,14 @@ func (c *Chaos) HandleSetConfig(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(c.GetConfig())
}

func (c *Chaos) HandleClearDisk(w http.ResponseWriter, r *http.Request) {
if err := c.DiskFiller.Clear(); err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "cleared"})
}
11 changes: 11 additions & 0 deletions internal/chaos/disk_filler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ func (d *DiskFiller) GetConfig() (enabled bool, path string, rateMB int) {
return d.enabled, d.path, d.rateMB
}

func (d *DiskFiller) Clear() error {
d.mu.Lock()
defer d.mu.Unlock()
if d.path == "" {
return nil
}
fillDir := filepath.Join(d.path, "chaos-fill")
slog.Info("chaos: clearing disk fill data", "path", fillDir)
return os.RemoveAll(fillDir)
}

func (d *DiskFiller) startLocked() {
if d.parentCtx == nil || !d.enabled || d.rateMB <= 0 || d.path == "" {
return
Expand Down
26 changes: 26 additions & 0 deletions services/dashboard/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ func (h *Handler) SetServiceChaos(w http.ResponseWriter, r *http.Request) {
io.Copy(w, resp.Body)
}

func (h *Handler) ClearServiceDisk(w http.ResponseWriter, r *http.Request) {
name := r.PathValue("name")
svc := h.findService(name)
if svc == nil {
http.Error(w, `{"error":"service not found"}`, http.StatusNotFound)
return
}

req, err := http.NewRequestWithContext(r.Context(), http.MethodPost, svc.URL+"/admin/chaos/clear-disk", nil)
if err != nil {
http.Error(w, `{"error":"failed to create request"}`, http.StatusInternalServerError)
return
}

resp, err := h.httpClient.Do(req)
if err != nil {
http.Error(w, `{"error":"service unreachable: `+err.Error()+`"}`, http.StatusBadGateway)
return
}
defer resp.Body.Close()

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}

func (h *Handler) findService(name string) *ServiceInfo {
for i := range h.services {
if h.services[i].Name == name {
Expand Down
1 change: 1 addition & 0 deletions services/dashboard/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ func RegisterRoutes(mux *http.ServeMux, h *Handler, staticFS http.Handler) {
mux.HandleFunc("GET /api/services", h.ListServices)
mux.HandleFunc("GET /api/services/{name}/chaos", h.GetServiceChaos)
mux.HandleFunc("PUT /api/services/{name}/chaos", h.SetServiceChaos)
mux.HandleFunc("POST /api/services/{name}/chaos/clear-disk", h.ClearServiceDisk)
mux.Handle("/", staticFS)
}
37 changes: 28 additions & 9 deletions services/dashboard/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ <h1>Chaos Dashboard</h1>
rate_per_sec: parseInt(card.querySelector('#log-rate-' + name).value) || 0,
pattern: card.querySelector('#log-pattern-' + name).value
},
disk_fill: {
disk_fill: name === 'inventory' ? {
enabled: card.querySelector('#disk-enabled-' + name).checked,
path: card.querySelector('#disk-path-' + name).value || '/data',
rate_mb: parseInt(card.querySelector('#disk-rate-' + name).value) || 1
}
} : { enabled: false, path: '/data', rate_mb: 1 }
};
}

Expand Down Expand Up @@ -304,7 +304,7 @@ <h3>Log Volume</h3>
</select>
</div>
</div>
<div class="section">
${name === 'inventory' ? `<div class="section">
<h3>Disk Fill</h3>
<div class="toggle-row">
<label>Enabled</label>
Expand All @@ -321,7 +321,10 @@ <h3>Disk Fill</h3>
<label>MB/sec</label>
<input type="number" id="disk-rate-${name}" min="1" value="${c.disk_fill?.rate_mb || 1}">
</div>
</div>
<div class="field-row" style="margin-top: 12px;">
<button class="btn btn-secondary" onclick="clearDisk('${name}')">Clear Disk</button>
</div>
</div>` : ''}
<div class="actions">
<button class="btn btn-primary" onclick="saveConfig('${name}')">Apply</button>
<button class="btn btn-secondary" onclick="fetchConfig('${name}').then(() => renderService('${name}'))">Refresh</button>
Expand Down Expand Up @@ -355,10 +358,26 @@ <h3>Disk Fill</h3>
list.insertAdjacentHTML('beforeend', latencyRouteRow('', ''));
}

async function clearDisk(name) {
try {
const resp = await fetch(`/api/services/${name}/chaos/clear-disk`, { method: 'POST' });
if (!resp.ok) {
const body = await resp.json();
throw new Error(body.error || `HTTP ${resp.status}`);
}
showToast(`${name}: disk data cleared`, 'success');
} catch (e) {
showToast(`${name}: ${e.message}`, 'error');
}
}

async function applyPreset(presetName) {
const preset = PRESETS[presetName]();
const promises = services.map(svc =>
fetch(`/api/services/${svc.name}/chaos`, {
const promises = services.map(svc => {
const preset = PRESETS[presetName]();
if (svc.name !== 'inventory') {
preset.disk_fill = { enabled: false, path: '/data', rate_mb: 1 };
}
return fetch(`/api/services/${svc.name}/chaos`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(preset)
Expand All @@ -367,8 +386,8 @@ <h3>Disk Fill</h3>
configs[svc.name] = await resp.json();
renderService(svc.name);
}
}).catch(() => {})
);
}).catch(() => {});
});
await Promise.all(promises);
showToast(`Preset "${presetName}" applied to all services`, 'success');
}
Expand Down
Loading