Skip to content

Commit 683ea69

Browse files
yhabteabjulianbrost
authored andcommitted
Remove objects from cache when processing Icinga 2 delete event
1 parent f8c3125 commit 683ea69

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

internal/icinga2/client.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
"github.com/google/uuid"
88
lru "github.com/hashicorp/golang-lru/v2"
99
"github.com/icinga/icinga-notifications/internal/event"
10+
"github.com/icinga/icinga-notifications/internal/object"
1011
"go.uber.org/zap"
1112
"golang.org/x/sync/errgroup"
1213
"net/http"
1314
"net/url"
15+
"strings"
1416
"time"
1517
)
1618

@@ -190,6 +192,17 @@ func (client *Client) deleteExtraTagsCacheFor(result *ObjectCreatedDeleted) erro
190192
return fmt.Errorf("cannot delete object extra tags for unknown object_type %q", result.ObjectType)
191193
}
192194

195+
if result.EventType == typeObjectDeleted {
196+
names := strings.Split(result.ObjectName, "!")
197+
tags := map[string]string{"host": names[0]}
198+
if len(names) == 2 {
199+
tags["service"] = names[1]
200+
}
201+
202+
// Delete the object from our global cache to avoid having huge dangling objects that don't exist in Icinga 2.
203+
object.DeleteFromCache(object.ID(client.EventSourceId, tags))
204+
}
205+
193206
// The checkable has just been either deleted or created, so delete all existing extra tags from our cache
194207
// store as well and will be refreshed on the next access when Icinga 2 emits any other event for that object.
195208
client.eventExtraTagsCache.Remove(result.ObjectName)

internal/object/objects.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ var (
1717
cacheMu sync.Mutex
1818
)
1919

20+
// DeleteFromCache deletes the Object from the global cache store matching the given ID (if any).
21+
func DeleteFromCache(id types.Binary) {
22+
cacheMu.Lock()
23+
defer cacheMu.Unlock()
24+
25+
delete(cache, id.String())
26+
}
27+
2028
// RestoreMutedObjects restores all muted objects and their extra / ID tags from the database.
2129
// Note, this function only retrieves muted objects without non-recovered incident.
2230
//

0 commit comments

Comments
 (0)