Skip to content

Commit 4492d5e

Browse files
b84330808claude
andcommitted
fix(go.d/docker): remove only the charts of the removed container
removeContainerCharts matched chart IDs by the "container_<name>" prefix, so removing container "foo" also marked the charts of "foo-bar" (and of "foo_state", via "container_foo_state_state") as obsolete. Those containers stay in c.containers, so their charts were never re-created until the plugin restarted. Match the exact chart IDs built from the per-container templates instead. Fixes #24230 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 30375cf commit 4492d5e

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

‎src/go/plugin/go.d/collector/docker/charts.go‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package docker
44

55
import (
66
"fmt"
7-
"strings"
87

98
"github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
109
)
@@ -164,10 +163,10 @@ func (c *Collector) addContainerCharts(name, image string) {
164163
}
165164

166165
func (c *Collector) removeContainerCharts(name string) {
167-
px := fmt.Sprintf("container_%s", name)
168-
169-
for _, chart := range *c.Charts() {
170-
if strings.HasPrefix(chart.ID, px) {
166+
// Match exact chart IDs: a prefix match would also remove the charts of
167+
// other containers whose names start with this one (e.g. "foo" and "foo-bar").
168+
for _, tmpl := range containerChartsTmpl {
169+
if chart := c.Charts().Get(fmt.Sprintf(tmpl.ID, name)); chart != nil {
171170
chart.MarkRemove()
172171
chart.MarkNotCreated()
173172
}

‎src/go/plugin/go.d/collector/docker/collector_test.go‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"errors"
88
"os"
9+
"slices"
910
"strings"
1011
"testing"
1112
"time"
@@ -781,6 +782,39 @@ func TestCollector_Collect_FailedCollectionLeavesChartsUnchanged(t *testing.T) {
781782
assert.Equal(t, chartIDs(summaryCharts.Copy()), chartIDs(collr.Charts()))
782783
}
783784

785+
func TestCollector_Collect_RemovesOnlyChartsOfRemovedContainer(t *testing.T) {
786+
cntr := func(name string) typesContainer.Summary {
787+
return typesContainer.Summary{Names: []string{"/" + name}, State: "running", Image: "example/example:v1", ImageID: "sha256:v1"}
788+
}
789+
790+
m := &mockClient{}
791+
collr := New()
792+
collr.newClient = prepareNewClientFunc(m)
793+
require.NoError(t, collr.Init(context.Background()))
794+
795+
m.containers = []typesContainer.Summary{cntr("foo"), cntr("foo-bar"), cntr("foo_state")}
796+
require.NotNil(t, collr.Collect(context.Background()))
797+
798+
m.containers = []typesContainer.Summary{cntr("foo-bar"), cntr("foo_state")}
799+
require.NotNil(t, collr.Collect(context.Background()))
800+
801+
var n int
802+
for _, chart := range *collr.Charts() {
803+
if !strings.HasPrefix(chart.ID, "container_") {
804+
continue
805+
}
806+
n++
807+
var name string
808+
for _, l := range chart.Labels {
809+
if l.Key == "container_name" {
810+
name = l.Value
811+
}
812+
}
813+
assert.Equalf(t, name == "foo", chart.Obsolete, "chart '%s' (container '%s') obsolete", chart.ID, name)
814+
}
815+
assert.Equal(t, 6, n)
816+
}
817+
784818
func chartIDs(charts *collectorapi.Charts) []string {
785819
var ids []string
786820
for _, chart := range *charts {
@@ -855,6 +889,7 @@ type mockClient struct {
855889
images []typesImage.Summary
856890
imageListCalls int
857891
containerImageID string
892+
containers []typesContainer.Summary
858893
}
859894

860895
func (m *mockClient) Info(_ context.Context, _ docker.InfoOptions) (docker.SystemInfoResult, error) {
@@ -922,6 +957,13 @@ func (m *mockClient) ContainerList(_ context.Context, opts docker.ContainerListO
922957
}
923958
}
924959

960+
if m.containers != nil {
961+
containers = nil
962+
if typesContainer.HealthStatus(status) == typesContainer.NoHealthcheck {
963+
containers = slices.Clone(m.containers)
964+
}
965+
}
966+
925967
if m.containerImageID != "" {
926968
for i := range containers {
927969
containers[i].ImageID = m.containerImageID

0 commit comments

Comments
 (0)