Skip to content

Commit c535be4

Browse files
authored
Merge pull request #5 from tinybirdco/4-fix-validate-key
Validate if object cache key exists
2 parents bd8f0c5 + 876ff05 commit c535be4

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

pkg/kube/objects.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ func NewObjectMetadataProvider(size int) ObjectMetadataProvider {
4747
func (o *ObjectMetadataCache) GetObjectMetadata(reference *v1.ObjectReference, clientset *kubernetes.Clientset, dynClient dynamic.Interface, metricsStore *metrics.Store) (ObjectMetadata, error) {
4848
// ResourceVersion changes when the object is updated.
4949
// We use "UID/ResourceVersion" as cache key so that if the object is updated we get the new metadata.
50-
cacheKey := strings.Join([]string{string(reference.UID), reference.ResourceVersion}, "/")
51-
if val, ok := o.cache.Get(cacheKey); ok {
52-
metricsStore.KubeApiReadCacheHits.Inc()
53-
return val.(ObjectMetadata), nil
50+
// UID and ResourceVersion are not always present, so we need to check if they exist before using them as cache key.
51+
var cacheKey string
52+
if reference.UID != "" && reference.ResourceVersion != "" {
53+
cacheKey = strings.Join([]string{string(reference.UID), reference.ResourceVersion}, "/")
54+
if val, ok := o.cache.Get(cacheKey); ok {
55+
metricsStore.KubeApiReadCacheHits.Inc()
56+
return val.(ObjectMetadata), nil
57+
}
5458
}
5559

5660
var group, version string
@@ -97,6 +101,8 @@ func (o *ObjectMetadataCache) GetObjectMetadata(reference *v1.ObjectReference, c
97101
objectMetadata.Deleted = true
98102
}
99103

100-
o.cache.Add(cacheKey, objectMetadata)
104+
if cacheKey != "" {
105+
o.cache.Add(cacheKey, objectMetadata)
106+
}
101107
return objectMetadata, nil
102108
}

0 commit comments

Comments
 (0)