feat: configurable unused images threshold with dashboard warning
- Add image_prune_threshold_mb setting (default 1024 MB) - Add GET /api/docker/unused-images endpoint returning unused image count, size, and threshold status - Dashboard shows amber warning banner when unused project images exceed threshold - Banner links to settings page for pruning, shows count and human-readable size (MB/GB) - Threshold configurable in Docker Image Cleanup section of settings - DB migration + schema for image_prune_threshold_mb
This commit is contained in:
@@ -150,6 +150,75 @@ func sanitizeDockerLogLine(line string) string {
|
||||
return line
|
||||
}
|
||||
|
||||
// unusedImageStats handles GET /api/docker/unused-images.
|
||||
// Returns the total size of unused project images and whether the threshold is exceeded.
|
||||
func (s *Server) unusedImageStats(w http.ResponseWriter, r *http.Request) {
|
||||
if s.docker == nil {
|
||||
respondJSON(w, http.StatusOK, map[string]any{
|
||||
"total_size_mb": 0, "count": 0, "threshold_mb": 0, "exceeded": false,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
settings, err := s.store.GetSettings()
|
||||
if err != nil {
|
||||
slog.Error("unused images: get settings", "error", err)
|
||||
respondError(w, http.StatusInternalServerError, "internal server error")
|
||||
return
|
||||
}
|
||||
|
||||
projects, err := s.store.GetAllProjects()
|
||||
if err != nil {
|
||||
slog.Error("unused images: list projects", "error", err)
|
||||
respondError(w, http.StatusInternalServerError, "internal server error")
|
||||
return
|
||||
}
|
||||
|
||||
// Build set of active image refs.
|
||||
activeImages := make(map[string]bool)
|
||||
for _, p := range projects {
|
||||
stages, _ := s.store.GetStagesByProjectID(p.ID)
|
||||
for _, st := range stages {
|
||||
instances, _ := s.store.GetInstancesByStageID(st.ID)
|
||||
for _, inst := range instances {
|
||||
if inst.ImageTag != "" {
|
||||
activeImages[p.Image+":"+inst.ImageTag] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sum unused image sizes.
|
||||
ctx := r.Context()
|
||||
var totalSize int64
|
||||
var count int
|
||||
for _, p := range projects {
|
||||
if p.Image == "" {
|
||||
continue
|
||||
}
|
||||
images, err := s.docker.ListImagesByRef(ctx, p.Image)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, img := range images {
|
||||
if !activeImages[img.Ref] {
|
||||
totalSize += img.Size
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
totalMB := totalSize / (1024 * 1024)
|
||||
exceeded := settings.ImagePruneThresholdMB > 0 && int(totalMB) >= settings.ImagePruneThresholdMB
|
||||
|
||||
respondJSON(w, http.StatusOK, map[string]any{
|
||||
"total_size_mb": totalMB,
|
||||
"count": count,
|
||||
"threshold_mb": settings.ImagePruneThresholdMB,
|
||||
"exceeded": exceeded,
|
||||
})
|
||||
}
|
||||
|
||||
// pruneImages handles POST /api/docker/prune-images.
|
||||
// Only removes images that belong to Docker Watcher projects (not all system images).
|
||||
func (s *Server) pruneImages(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -198,6 +198,7 @@ func (s *Server) Router() chi.Router {
|
||||
r.Get("/auth/me", s.currentUser)
|
||||
r.Post("/auth/logout", s.logout)
|
||||
r.Get("/proxies", s.listProxyRoutes)
|
||||
r.Get("/docker/unused-images", s.unusedImageStats)
|
||||
r.Get("/projects", s.listProjects)
|
||||
r.Route("/projects/{id}", func(r chi.Router) {
|
||||
r.Get("/", s.getProject)
|
||||
|
||||
@@ -37,6 +37,7 @@ type settingsRequest struct {
|
||||
CloudflareAPIToken string `json:"cloudflare_api_token"`
|
||||
CloudflareZoneID *string `json:"cloudflare_zone_id,omitempty"`
|
||||
NpmAccessListID *int `json:"npm_access_list_id,omitempty"`
|
||||
ImagePruneThresholdMB *int `json:"image_prune_threshold_mb,omitempty"`
|
||||
NpmRemote *bool `json:"npm_remote,omitempty"`
|
||||
ProxyProvider *string `json:"proxy_provider,omitempty"`
|
||||
TraefikEntrypoint *string `json:"traefik_entrypoint,omitempty"`
|
||||
@@ -68,6 +69,7 @@ func (s *Server) getSettings(w http.ResponseWriter, r *http.Request) {
|
||||
"npm_email": settings.NpmEmail,
|
||||
"has_npm_password": settings.NpmPassword != "",
|
||||
"npm_remote": settings.NpmRemote,
|
||||
"image_prune_threshold_mb": settings.ImagePruneThresholdMB,
|
||||
"npm_access_list_id": settings.NpmAccessListID,
|
||||
"polling_interval": settings.PollingInterval,
|
||||
"ssl_certificate_id": settings.SSLCertificateID,
|
||||
@@ -195,6 +197,9 @@ func (s *Server) updateSettings(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
updated.ProxyProvider = prov
|
||||
}
|
||||
if req.ImagePruneThresholdMB != nil {
|
||||
updated.ImagePruneThresholdMB = *req.ImagePruneThresholdMB
|
||||
}
|
||||
if req.NpmRemote != nil {
|
||||
updated.NpmRemote = *req.NpmRemote
|
||||
}
|
||||
|
||||
@@ -74,6 +74,7 @@ type Settings struct {
|
||||
TraefikCertResolver string `json:"traefik_cert_resolver"`
|
||||
TraefikNetwork string `json:"traefik_network"`
|
||||
TraefikAPIURL string `json:"traefik_api_url"`
|
||||
ImagePruneThresholdMB int `json:"image_prune_threshold_mb"`
|
||||
BackupEnabled bool `json:"backup_enabled"`
|
||||
BackupIntervalHours int `json:"backup_interval_hours"`
|
||||
BackupRetentionCount int `json:"backup_retention_count"`
|
||||
|
||||
@@ -16,6 +16,7 @@ func (s *Store) GetSettings() (Settings, error) {
|
||||
cloudflare_api_token, cloudflare_zone_id,
|
||||
npm_remote, npm_access_list_id, proxy_provider,
|
||||
traefik_entrypoint, traefik_cert_resolver, traefik_network, traefik_api_url,
|
||||
image_prune_threshold_mb,
|
||||
backup_enabled, backup_interval_hours, backup_retention_count,
|
||||
updated_at
|
||||
FROM settings WHERE id = 1`,
|
||||
@@ -26,6 +27,7 @@ func (s *Store) GetSettings() (Settings, error) {
|
||||
&st.CloudflareAPIToken, &st.CloudflareZoneID,
|
||||
&npmRemote, &st.NpmAccessListID, &st.ProxyProvider,
|
||||
&st.TraefikEntrypoint, &st.TraefikCertResolver, &st.TraefikNetwork, &st.TraefikAPIURL,
|
||||
&st.ImagePruneThresholdMB,
|
||||
&backupEnabled, &st.BackupIntervalHours, &st.BackupRetentionCount,
|
||||
&st.UpdatedAt)
|
||||
if err != nil {
|
||||
@@ -61,6 +63,7 @@ func (s *Store) UpdateSettings(st Settings) error {
|
||||
cloudflare_api_token=?, cloudflare_zone_id=?,
|
||||
npm_remote=?, npm_access_list_id=?, proxy_provider=?,
|
||||
traefik_entrypoint=?, traefik_cert_resolver=?, traefik_network=?, traefik_api_url=?,
|
||||
image_prune_threshold_mb=?,
|
||||
backup_enabled=?, backup_interval_hours=?, backup_retention_count=?,
|
||||
updated_at=?
|
||||
WHERE id = 1`,
|
||||
@@ -71,6 +74,7 @@ func (s *Store) UpdateSettings(st Settings) error {
|
||||
st.CloudflareAPIToken, st.CloudflareZoneID,
|
||||
npmRemote, st.NpmAccessListID, st.ProxyProvider,
|
||||
st.TraefikEntrypoint, st.TraefikCertResolver, st.TraefikNetwork, st.TraefikAPIURL,
|
||||
st.ImagePruneThresholdMB,
|
||||
backupEnabled, st.BackupIntervalHours, st.BackupRetentionCount,
|
||||
st.UpdatedAt,
|
||||
)
|
||||
|
||||
@@ -121,6 +121,8 @@ func (s *Store) runMigrations() error {
|
||||
`ALTER TABLE projects ADD COLUMN npm_access_list_id INTEGER NOT NULL DEFAULT 0`,
|
||||
// Separate public IP for DNS A records.
|
||||
`ALTER TABLE settings ADD COLUMN public_ip TEXT NOT NULL DEFAULT ''`,
|
||||
// Image prune threshold (MB). Warn on dashboard when exceeded. 0 = disabled.
|
||||
`ALTER TABLE settings ADD COLUMN image_prune_threshold_mb INTEGER NOT NULL DEFAULT 1024`,
|
||||
}
|
||||
|
||||
for _, m := range migrations {
|
||||
@@ -225,6 +227,7 @@ CREATE TABLE IF NOT EXISTS settings (
|
||||
base_volume_path TEXT NOT NULL DEFAULT '',
|
||||
ssl_certificate_id INTEGER NOT NULL DEFAULT 0,
|
||||
npm_remote INTEGER NOT NULL DEFAULT 0,
|
||||
image_prune_threshold_mb INTEGER NOT NULL DEFAULT 1024,
|
||||
npm_access_list_id INTEGER NOT NULL DEFAULT 0,
|
||||
traefik_entrypoint TEXT NOT NULL DEFAULT 'websecure',
|
||||
traefik_cert_resolver TEXT NOT NULL DEFAULT 'letsencrypt',
|
||||
|
||||
Reference in New Issue
Block a user