Skip to content

Commit 0471c56

Browse files
b84330808claude
andcommitted
fix(go.d/wireguard): remove only the removed device's charts
removeDeviceCharts matched chart IDs by an unterminated prefix, "device_<name>", so removing wg1 also marked every chart of wg10-wg19 as obsolete. The surviving device stays in c.devices, so its charts were never re-created until the plugin restarted. Match the exact chart IDs built from deviceChartsTmpl instead, as in ntpd and nvme. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2174874 commit 0471c56

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,15 @@ func (c *Collector) addNewDeviceCharts(device string) {
105105
}
106106

107107
func (c *Collector) removeDeviceCharts(device string) {
108-
prefix := fmt.Sprintf("device_%s", device)
108+
// Match exact chart IDs: a prefix match would also remove the charts of
109+
// other devices whose name starts with this one (e.g. wg1 and wg10).
110+
ids := make(map[string]bool, len(deviceChartsTmpl))
111+
for _, tmpl := range deviceChartsTmpl {
112+
ids[fmt.Sprintf(tmpl.ID, device)] = true
113+
}
109114

110115
for _, c := range *c.Charts() {
111-
if strings.HasPrefix(c.ID, prefix) {
116+
if ids[c.ID] {
112117
c.MarkRemove()
113118
c.MarkNotCreated()
114119
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,32 @@ func TestCollector_Collect(t *testing.T) {
327327
},
328328
},
329329
},
330+
"device removed at run time, device with a longer name is kept": {
331+
{
332+
prepareMock: func(m *mockClient) {
333+
m.devices = append(m.devices, prepareDevice(1))
334+
m.devices = append(m.devices, prepareDevice(10))
335+
},
336+
check: func(t *testing.T, collr *Collector) {
337+
_ = collr.Collect(context.Background())
338+
},
339+
},
340+
{
341+
prepareMock: func(m *mockClient) {
342+
m.devices = m.devices[1:]
343+
},
344+
check: func(t *testing.T, collr *Collector) {
345+
collr.cleanupEvery = time.Second
346+
time.Sleep(time.Second)
347+
_ = collr.Collect(context.Background())
348+
assert.Equal(t, len(deviceChartsTmpl)*2, len(*collr.Charts()))
349+
assert.Equal(t, len(deviceChartsTmpl)*1, calcObsoleteCharts(collr.Charts()))
350+
for _, chart := range *collr.Charts() {
351+
assert.Equalf(t, strings.HasPrefix(chart.ID, "device_wg1_"), chart.Obsolete, "chart %s", chart.ID)
352+
}
353+
},
354+
},
355+
},
330356
"peer added at runtime": {
331357
{
332358
prepareMock: func(m *mockClient) {

0 commit comments

Comments
 (0)