Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kube meta store: Make sure all the object metadata is deleted #1359

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Make sure all the object metadata is deleted
  • Loading branch information
mariomac committed Nov 14, 2024
commit 5e4f4257448fa5eb59a4180127bf4c76fb51fc04
35 changes: 23 additions & 12 deletions pkg/internal/kube/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ type qualifiedName struct {
kind string
}

func qName(om *informer.ObjectMeta) qualifiedName {
return qualifiedName{name: om.Name, namespace: om.Namespace, kind: om.Kind}
}

// Store aggregates Kubernetes information from multiple sources:
// - the informer that keep an indexed copy of the existing pods and replicasets.
// - the inspected container.Info objects, indexed either by container ID and PID namespace
Expand Down Expand Up @@ -60,7 +64,7 @@ type Store struct {
objectMetaByIP map[string]*informer.ObjectMeta
// used to track the changed/removed IPs of a given object
// and remove them from objectMetaByIP on update or deletion
objectIPsByName map[qualifiedName][]string
objectMetaByQName map[qualifiedName]*informer.ObjectMeta
otelServiceInfoByIP map[string]OTelServiceNamePair

// Instead of subscribing to the informer directly, the rest of components
Expand All @@ -78,7 +82,7 @@ func NewStore(kubeMetadata meta.Notifier) *Store {
podsByContainer: map[string]*informer.ObjectMeta{},
containerByPID: map[uint32]*container.Info{},
objectMetaByIP: map[string]*informer.ObjectMeta{},
objectIPsByName: map[qualifiedName][]string{},
objectMetaByQName: map[qualifiedName]*informer.ObjectMeta{},
containersByOwner: map[string][]*informer.ContainerInfo{},
otelServiceInfoByIP: map[string]OTelServiceNamePair{},
metadataNotifier: kubeMetadata,
Expand Down Expand Up @@ -140,9 +144,7 @@ func (s *Store) addObjectMeta(meta *informer.ObjectMeta) {
s.access.Lock()
defer s.access.Unlock()

s.unlockedAddObjectMeta(qualifiedName{
name: meta.Name, namespace: meta.Namespace, kind: meta.Kind,
}, meta)
s.unlockedAddObjectMeta(qName(meta), meta)
}

func (s *Store) updateObjectMeta(meta *informer.ObjectMeta) {
Expand All @@ -151,9 +153,9 @@ func (s *Store) updateObjectMeta(meta *informer.ObjectMeta) {

// if the update removes IPs from the original object meta,
// we remove them from the indexes
qn := qualifiedName{name: meta.Name, namespace: meta.Namespace, kind: meta.Kind}
if ips, ok := s.objectIPsByName[qn]; ok {
for _, ip := range ips {
qn := qName(meta)
if om, ok := s.objectMetaByQName[qn]; ok {
for _, ip := range om.Ips {
// theoretically, linear search into a list is not efficient and we should first build a map
// with all the IPs
// however, the IPs slice is expected to have a small size (few entries), so
Expand All @@ -169,7 +171,7 @@ func (s *Store) updateObjectMeta(meta *informer.ObjectMeta) {
}

func (s *Store) unlockedAddObjectMeta(qn qualifiedName, meta *informer.ObjectMeta) {
s.objectIPsByName[qn] = meta.Ips
s.objectMetaByQName[qn] = meta

for _, ip := range meta.Ips {
s.objectMetaByIP[ip] = meta
Expand Down Expand Up @@ -207,9 +209,18 @@ func (s *Store) deleteObjectMeta(meta *informer.ObjectMeta) {
// Otel variables on specific pods can change the outcome.
s.otelServiceInfoByIP = map[string]OTelServiceNamePair{}

delete(s.objectIPsByName, qualifiedName{
name: meta.Name, namespace: meta.Namespace, kind: meta.Kind,
})
// cleanup both the objectMeta information from the received event
// as well as from any previous snapshot in the system whose IPs and/or
// containers could have been removed in the last snapshot

if previousObject, ok := s.objectMetaByQName[qName(meta)]; ok {
s.unlockedDeleteObjectMeta(previousObject)
}
s.unlockedDeleteObjectMeta(meta)
}

func (s *Store) unlockedDeleteObjectMeta(meta *informer.ObjectMeta) {
delete(s.objectMetaByQName, qName(meta))
for _, ip := range meta.Ips {
delete(s.objectMetaByIP, ip)
}
Expand Down
Loading