11package k8s
22
33import (
4+ "fmt"
45 "time"
56
7+ "github.com/go-logr/logr"
68 "github.com/pmylund/go-cache"
79 "k8s.io/apimachinery/pkg/types"
810
911 "github.com/jetstack/preflight/api"
10- "github.com/jetstack/preflight/pkg/logs"
1112)
1213
1314// time interface, this is used to fetch the current time
@@ -30,9 +31,17 @@ type cacheResource interface {
3031 GetNamespace () string
3132}
3233
34+ func logCacheUpdateFailure (log logr.Logger , obj interface {}, operation string ) {
35+ // We use WithCallStackHelper to ensure the correct caller line numbers in the log messages
36+ helper , log := log .WithCallStackHelper ()
37+ helper ()
38+ err := fmt .Errorf ("not a cacheResource type: %T missing metadata/uid field" , obj )
39+ log .Error (err , "Cache update failure" , "operation" , operation )
40+ }
41+
3342// onAdd handles the informer creation events, adding the created runtime.Object
3443// to the data gatherer's cache. The cache key is the uid of the object
35- func onAdd (obj interface {}, dgCache * cache.Cache ) {
44+ func onAdd (log logr. Logger , obj interface {}, dgCache * cache.Cache ) {
3645 item , ok := obj .(cacheResource )
3746 if ok {
3847 cacheObject := & api.GatheredResource {
@@ -41,36 +50,34 @@ func onAdd(obj interface{}, dgCache *cache.Cache) {
4150 dgCache .Set (string (item .GetUID ()), cacheObject , cache .DefaultExpiration )
4251 return
4352 }
44- logs .Log .Printf ("could not %q resource to the cache, missing metadata/uid field" , "add" )
45-
53+ logCacheUpdateFailure (log , obj , "add" )
4654}
4755
4856// onUpdate handles the informer update events, replacing the old object with the new one
4957// if it's present in the data gatherer's cache, (if the object isn't present, it gets added).
5058// The cache key is the uid of the object
51- func onUpdate (old , new interface {}, dgCache * cache.Cache ) {
59+ func onUpdate (log logr. Logger , old , new interface {}, dgCache * cache.Cache ) {
5260 item , ok := old .(cacheResource )
5361 if ok {
5462 cacheObject := updateCacheGatheredResource (string (item .GetUID ()), new , dgCache )
5563 dgCache .Set (string (item .GetUID ()), cacheObject , cache .DefaultExpiration )
5664 return
5765 }
58-
59- logs .Log .Printf ("could not %q resource to the cache, missing metadata/uid field" , "update" )
66+ logCacheUpdateFailure (log , old , "update" )
6067}
6168
6269// onDelete handles the informer deletion events, updating the object's properties with the deletion
6370// time of the object (but not removing the object from the cache).
6471// The cache key is the uid of the object
65- func onDelete (obj interface {}, dgCache * cache.Cache ) {
72+ func onDelete (log logr. Logger , obj interface {}, dgCache * cache.Cache ) {
6673 item , ok := obj .(cacheResource )
6774 if ok {
6875 cacheObject := updateCacheGatheredResource (string (item .GetUID ()), obj , dgCache )
6976 cacheObject .DeletedAt = api.Time {Time : clock .now ()}
7077 dgCache .Set (string (item .GetUID ()), cacheObject , cache .DefaultExpiration )
7178 return
7279 }
73- logs . Log . Printf ( "could not %q resource to the cache, missing metadata/uid field" , "delete" )
80+ logCacheUpdateFailure ( log , obj , "delete" )
7481}
7582
7683// creates a new updated instance of a cache object, with the resource
0 commit comments