Files
Pulse/internal/alerts/docker_override_migration.go
rcourtman 38954b0b71 Give Docker containers and services without IDs their own alert refs
A container reported without an ID alerted under its Docker host's own
reference (docker:<host>). Its alerts showed as host alerts, joined the
host's resource history, were never cleared when the container left the
report, and made the host's Patrol scope alias ambiguous. A Swarm service
without an ID put its normalized name in the service-ID slot, so a name
equal to another service's ID shared that service's alerts and history,
and the history resolver had to treat the whole cluster as a conflict.

Both now alert under their name, marked in the ID position
(docker:<host>/name:<name>, docker:<host>/service/name:<name>). Docker
and Swarm IDs never contain a colon, so these cannot equal a host or ID
reference, and one with neither an ID nor a name raises no alert. The
policy switches, cleanup, Patrol aliases and the history resolver use the
same builders and parse the same shapes, so the resolver no longer needs
the cluster-wide conflict. A pre-upgrade container alert left on the
host's reference is cleared on the host's next report, and a pending
image update recorded there keeps its age.

Canonical state IDs were split at the first "::", but Docker hosts that
collide are disambiguated as "<base>::<suffix>". Every container alert
of such a host was rewritten to the reference of the host named <base>,
so it joined that host's history and skipped its own host's cleanup.
State IDs now split before the alert's recorded spec ID, or after its
own resource ID, so alerts saved that way also recover on restore.
2026-10-06 23:27:50 +01:00

160 lines
4.8 KiB
Go

package alerts
import (
"strings"
"github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
)
// MigrateDockerContainerOverrideKeys re-homes container overrides onto the
// stable host+name key and prunes entries whose container no longer exists.
// Container IDs change on every recreate, so an image update orphaned the
// user's per-container alert toggle and left a dead entry in alerts.json
// forever (#1601). Three key shapes exist in the wild:
//
// docker:{host}/{name} stable (target, written by the UI now)
// docker:{host}/{container ID} legacy backend key (v5-era writes)
// docker:{host}/app-container-{16 hex} unified hash IDs the v6 UI wrote
//
// Live legacy and hash keys are moved onto the stable key; orphaned ID-shaped
// keys are pruned. Only hosts with at least one docker-sourced app-container
// in the snapshot are touched (an empty set can be a transient collection
// failure), and name-keyed overrides for absent containers are kept so a
// later recreate under the same name still honours them. Like
// MigrateCanonicalOverrideKeys, the config is mutated copy-on-write: callers
// hand in a GetConfig() snapshot whose Overrides map is shared with the live
// manager config.
func MigrateDockerContainerOverrideKeys(config *AlertConfig, resources []unifiedresources.Resource) bool {
if config == nil || len(config.Overrides) == 0 || len(resources) == 0 {
return false
}
changed := false
overrides := make(map[string]ThresholdConfig, len(config.Overrides))
for resourceID, override := range config.Overrides {
overrides[resourceID] = override
}
rehome := func(fromKey, toKey string) {
override, exists := overrides[fromKey]
if !exists {
return
}
if toKey != "" && toKey != fromKey {
if _, taken := overrides[toKey]; !taken {
overrides[toKey] = override
}
}
delete(overrides, fromKey)
changed = true
}
liveKeysByHost := make(map[string]map[string]struct{})
for _, resource := range resources {
if unifiedresources.CanonicalResourceType(resource.Type) != unifiedresources.ResourceTypeAppContainer {
continue
}
if resource.Docker == nil {
continue
}
hostID := strings.TrimSpace(resource.Docker.HostSourceID)
if hostID == "" {
continue
}
live := liveKeysByHost[hostID]
if live == nil {
live = make(map[string]struct{})
liveKeysByHost[hostID] = live
}
stableKey := dockerContainerOverrideKey(hostID, resource.Name)
if stableKey != "" {
live[stableKey] = struct{}{}
}
legacyKey := ""
if containerID := strings.TrimSpace(resource.Docker.ContainerID); containerID != "" {
legacyKey = DockerContainerResourceID(hostID, containerID, "")
live[legacyKey] = struct{}{}
}
if legacyKey != "" && legacyKey != stableKey {
rehome(legacyKey, stableKey)
}
// The v6 thresholds UI keyed container overrides by the unified hash
// ID, which the evaluator never read. Re-home them while the hash is
// still resolvable to a live container.
if resourceID := strings.TrimSpace(resource.ID); resourceID != "" {
if hashKey := "docker:" + hostID + "/" + resourceID; hashKey != stableKey {
rehome(hashKey, stableKey)
}
}
}
for hostID, live := range liveKeysByHost {
if len(live) == 0 {
continue
}
prefix := "docker:" + hostID + "/"
for key := range overrides {
if !strings.HasPrefix(key, prefix) {
continue
}
rest := key[len(prefix):]
// "service/<name>" keys belong to Swarm services.
if strings.Contains(rest, "/") {
continue
}
if _, isLive := live[key]; isLive {
continue
}
// A suffix that is neither a container ID nor a unified hash ID
// is a name-keyed override for a currently absent container; keep
// it so a recreate under the same name still honours it.
if !looksLikeDockerContainerID(rest) && !looksLikeUnifiedAppContainerID(rest) {
continue
}
delete(overrides, key)
changed = true
}
}
if changed {
config.Overrides = overrides
}
return changed
}
// looksLikeDockerContainerID reports whether the value has the shape of a
// Docker container ID (12-char short or 64-char full hex).
func looksLikeDockerContainerID(value string) bool {
if len(value) != 12 && len(value) != 64 {
return false
}
return isHexString(value)
}
// looksLikeUnifiedAppContainerID reports whether the value has the shape of a
// unified app-container resource ID ("app-container-" + 16 hex chars).
func looksLikeUnifiedAppContainerID(value string) bool {
const prefix = "app-container-"
if !strings.HasPrefix(value, prefix) {
return false
}
suffix := value[len(prefix):]
return len(suffix) == 16 && isHexString(suffix)
}
func isHexString(value string) bool {
if value == "" {
return false
}
for _, r := range value {
if (r < '0' || r > '9') && (r < 'a' || r > 'f') && (r < 'A' || r > 'F') {
return false
}
}
return true
}