Files
Ronak Nathani f48d5bf9ba Add owner references to EnhancedEvent, consolidate calls to apiserver, make cache size configurable and add metrics for reads (#144)
* Adds ownerReferences to the exported events

This commit uses the same approach as labels and annotations and adds ownerReferences to the EnhancedEvent struct.
The flow is as follows:
* use an LRU cache to store the ownerReferences with object UID as the key
* if the object doesn't exist in cache, look up using dynamic client and store it in cache
* if the object exists in cache, return the value from cache

* Reduce the number of GetObject calls by using a single cache to store all labels, annotations and ownerReferences

Currently, every time there's an event, the events exporter runs GetObject for metadata like labels and annotations
independently. This results in the same object being looked up multiple times for different pieces of the metadata.
These number of calls grow as we want to look up additional information about the object like ownerReferences.
So, in this change, a struct called `ObjectMetadata` is created to capture all the pieces of information that need to be added
to the EnhancedEvent. And every time there's an event, the object is fetched from the kube-apiserver if it's not in the cache already
and all pieces of metadata require only 1 call. The metadata is cached so repeated events about the same object don't
result in more calls.

Additionally, UID + ResourceVersion is used the cacheKey so if the object changes, it's looked up again.

One more change here is introduction of a `deleted` field in the `EnhancedEvent.InvolvedObject` to capture whether the object
is deleted. This helps receivers identify whether a resource is deleted and create rules for it when needed.

Tests are added for these updates and the mock functions are moved to the test files.

* Make the cache size configurable

* Add metrics for number of reads served from cache and kube-apiserver respectively

* Add `deleted` field to the involvedObject in EnhancedEvent to identify whether the resource is being deleted
2023-11-18 01:00:27 +03:00

103 lines
2.7 KiB
Go

package kube
import (
"context"
"strings"
lru "github.com/hashicorp/golang-lru"
"github.com/resmoio/kubernetes-event-exporter/pkg/metrics"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/restmapper"
)
type ObjectMetadataProvider interface {
GetObjectMetadata(reference *v1.ObjectReference, clientset *kubernetes.Clientset, dynClient dynamic.Interface, metricsStore *metrics.Store) (ObjectMetadata, error)
}
type ObjectMetadataCache struct {
cache *lru.ARCCache
}
var _ ObjectMetadataProvider = &ObjectMetadataCache{}
type ObjectMetadata struct {
Annotations map[string]string
Labels map[string]string
OwnerReferences []metav1.OwnerReference
Deleted bool
}
func NewObjectMetadataProvider(size int) ObjectMetadataProvider {
cache, err := lru.NewARC(size)
if err != nil {
panic("cannot init cache: " + err.Error())
}
var o ObjectMetadataProvider = &ObjectMetadataCache{
cache: cache,
}
return o
}
func (o *ObjectMetadataCache) GetObjectMetadata(reference *v1.ObjectReference, clientset *kubernetes.Clientset, dynClient dynamic.Interface, metricsStore *metrics.Store) (ObjectMetadata, error) {
// ResourceVersion changes when the object is updated.
// We use "UID/ResourceVersion" as cache key so that if the object is updated we get the new metadata.
cacheKey := strings.Join([]string{string(reference.UID), reference.ResourceVersion}, "/")
if val, ok := o.cache.Get(cacheKey); ok {
metricsStore.KubeApiReadCacheHits.Inc()
return val.(ObjectMetadata), nil
}
var group, version string
s := strings.Split(reference.APIVersion, "/")
if len(s) == 1 {
group = ""
version = s[0]
} else {
group = s[0]
version = s[1]
}
gk := schema.GroupKind{Group: group, Kind: reference.Kind}
groupResources, err := restmapper.GetAPIGroupResources(clientset.Discovery())
if err != nil {
return ObjectMetadata{}, err
}
rm := restmapper.NewDiscoveryRESTMapper(groupResources)
mapping, err := rm.RESTMapping(gk, version)
if err != nil {
return ObjectMetadata{}, err
}
item, err := dynClient.
Resource(mapping.Resource).
Namespace(reference.Namespace).
Get(context.Background(), reference.Name, metav1.GetOptions{})
metricsStore.KubeApiReadRequests.Inc()
if err != nil {
return ObjectMetadata{}, err
}
objectMetadata := ObjectMetadata{
OwnerReferences: item.GetOwnerReferences(),
Labels: item.GetLabels(),
Annotations: item.GetAnnotations(),
}
if item.GetDeletionTimestamp() != nil {
objectMetadata.Deleted = true
}
o.cache.Add(cacheKey, objectMetadata)
return objectMetadata, nil
}