6
6
7
7
"github.com/jetstack/preflight/api"
8
8
"github.com/pmylund/go-cache"
9
- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured "
9
+ "k8s.io/apimachinery/pkg/types "
10
10
)
11
11
12
12
// time interface, this is used to fetch the current time
@@ -24,60 +24,52 @@ func (*realTime) now() time.Time {
24
24
return time .Now ()
25
25
}
26
26
27
+ type cacheResource interface {
28
+ GetUID () types.UID
29
+ GetNamespace () string
30
+ }
31
+
27
32
// onAdd handles the informer creation events, adding the created runtime.Object
28
33
// to the data gatherer's cache. The cache key is the uid of the object
29
34
func onAdd (obj interface {}, dgCache * cache.Cache ) {
30
- item := obj .(* unstructured.Unstructured )
31
- if metadata , ok := item .Object ["metadata" ]; ok {
32
- data := metadata .(map [string ]interface {})
33
- if uid , ok := data ["uid" ]; ok {
34
- cacheObject := & api.GatheredResource {
35
- Resource : obj ,
36
- }
37
- dgCache .Set (uid .(string ), cacheObject , cache .DefaultExpiration )
38
- } else {
39
- log .Printf ("could not %q resource %q to the cache, missing uid field" , "add" , data ["name" ].(string ))
35
+ item , ok := obj .(cacheResource )
36
+ if ok {
37
+ cacheObject := & api.GatheredResource {
38
+ Resource : obj ,
40
39
}
41
- } else {
42
- log . Printf ( "could not %q resource to the cache, missing metadata" , "add" )
40
+ dgCache . Set ( string ( item . GetUID ()), cacheObject , cache . DefaultExpiration )
41
+ return
43
42
}
43
+ log .Printf ("could not %q resource to the cache, missing metadata/uid field" , "add" )
44
+
44
45
}
45
46
46
47
// onUpdate handles the informer update events, replacing the old object with the new one
47
48
// if it's present in the data gatherer's cache, (if the object isn't present, it gets added).
48
49
// The cache key is the uid of the object
49
50
func onUpdate (old , new interface {}, dgCache * cache.Cache ) {
50
- item := old .(* unstructured.Unstructured )
51
- if metadata , ok := item .Object ["metadata" ]; ok {
52
- data := metadata .(map [string ]interface {})
53
- if uid , ok := data ["uid" ]; ok {
54
- cacheObject := updateCacheGatheredResource (uid .(string ), new , dgCache )
55
- dgCache .Set (uid .(string ), cacheObject , cache .DefaultExpiration )
56
- } else {
57
- log .Printf ("could not %q resource %q to the cache, missing uid field" , "update" , data ["name" ].(string ))
58
- }
59
- } else {
60
- log .Printf ("could not %q resource to the cache, missing metadata" , "update" )
51
+ item , ok := old .(cacheResource )
52
+ if ok {
53
+ cacheObject := updateCacheGatheredResource (string (item .GetUID ()), new , dgCache )
54
+ dgCache .Set (string (item .GetUID ()), cacheObject , cache .DefaultExpiration )
55
+ return
61
56
}
57
+
58
+ log .Printf ("could not %q resource to the cache, missing metadata/uid field" , "update" )
62
59
}
63
60
64
61
// onDelete handles the informer deletion events, updating the object's properties with the deletion
65
62
// time of the object (but not removing the object from the cache).
66
63
// The cache key is the uid of the object
67
64
func onDelete (obj interface {}, dgCache * cache.Cache ) {
68
- item := obj .(* unstructured.Unstructured )
69
- if metadata , ok := item .Object ["metadata" ]; ok {
70
- data := metadata .(map [string ]interface {})
71
- if uid , ok := data ["uid" ]; ok {
72
- cacheObject := updateCacheGatheredResource (uid .(string ), obj , dgCache )
73
- cacheObject .DeletedAt = api.Time {Time : clock .now ()}
74
- dgCache .Set (uid .(string ), cacheObject , cache .DefaultExpiration )
75
- } else {
76
- log .Printf ("could not %q resource %q to the cache, missing uid field" , "delete" , data ["name" ].(string ))
77
- }
78
- } else {
79
- log .Printf ("could not %q resource to the cache, missing metadata" , "delete" )
65
+ item , ok := obj .(cacheResource )
66
+ if ok {
67
+ cacheObject := updateCacheGatheredResource (string (item .GetUID ()), obj , dgCache )
68
+ cacheObject .DeletedAt = api.Time {Time : clock .now ()}
69
+ dgCache .Set (string (item .GetUID ()), cacheObject , cache .DefaultExpiration )
70
+ return
80
71
}
72
+ log .Printf ("could not %q resource to the cache, missing metadata/uid field" , "delete" )
81
73
}
82
74
83
75
// creates a new updated instance of a cache object, with the resource
0 commit comments