From 556434f043622e035e630ed08f1286c39ded8526 Mon Sep 17 00:00:00 2001 From: Henry Dollman Date: Sun, 28 Jul 2024 17:33:07 -0400 Subject: [PATCH] fix agent losing track of container after restart --- agent/main.go | 11 +++++++++-- agent/types.go | 28 ++++++++++++++-------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/agent/main.go b/agent/main.go index c18fc6a..15482fe 100644 --- a/agent/main.go +++ b/agent/main.go @@ -178,14 +178,21 @@ func getDockerStats() ([]*ContainerStats, error) { var wg sync.WaitGroup for _, ctr := range containers { - ctr.IdShort = ctr.ID[:12] + ctr.IdShort = ctr.Id[:12] validIds[ctr.IdShort] = struct{}{} + // check if container is less than 1 minute old (possible restart) + // note: can't use Created field because it's not updated on restart + if strings.HasSuffix(ctr.Status, "seconds") { + // if so, remove old container data + delete(containerCpuMap, ctr.IdShort) + } wg.Add(1) go func() { defer wg.Done() cstats, err := getContainerStats(ctr) if err != nil { - // retry once + // delete container from map and retry once + delete(containerCpuMap, ctr.IdShort) cstats, err = getContainerStats(ctr) if err != nil { log.Printf("Error getting container stats: %+v\n", err) diff --git a/agent/types.go b/agent/types.go index ce5bef0..3cfffcf 100644 --- a/agent/types.go +++ b/agent/types.go @@ -42,23 +42,23 @@ type ContainerStats struct { } type Container struct { - ID string `json:"Id"` + Id string IdShort string Names []string - Image string - ImageID string - Command string - Created int64 + Status string + // Image string + // ImageID string + // Command string + // Created int64 // Ports []Port - SizeRw int64 `json:",omitempty"` - SizeRootFs int64 `json:",omitempty"` - Labels map[string]string - State string - Status string - HostConfig struct { - NetworkMode string `json:",omitempty"` - Annotations map[string]string `json:",omitempty"` - } + // SizeRw int64 `json:",omitempty"` + // SizeRootFs int64 `json:",omitempty"` + // Labels map[string]string + // State string + // HostConfig struct { + // NetworkMode string `json:",omitempty"` + // Annotations map[string]string `json:",omitempty"` + // } // NetworkSettings *SummaryNetworkSettings // Mounts []MountPoint }