@@ -21,6 +21,7 @@ import (
21
21
"time"
22
22
23
23
"k8s.io/apimachinery/pkg/api/meta"
24
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24
25
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25
26
"k8s.io/apimachinery/pkg/runtime"
26
27
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -31,10 +32,12 @@ import (
31
32
// InformersMap create and caches Informers for (runtime.Object, schema.GroupVersionKind) pairs.
32
33
// It uses a standard parameter codec constructed based on the given generated Scheme.
33
34
type InformersMap struct {
34
- // we abstract over the details of structured vs unstructured with the specificInformerMaps
35
+ // we abstract over the details of structured/unstructured/metadata with the specificInformerMaps
36
+ // TODO(directxman12): genericize this over different projections now that we have 3 different maps
35
37
36
38
structured * specificInformersMap
37
39
unstructured * specificInformersMap
40
+ metadata * specificInformersMap
38
41
39
42
// Scheme maps runtime.Objects to GroupVersionKinds
40
43
Scheme * runtime.Scheme
@@ -47,58 +50,72 @@ func NewInformersMap(config *rest.Config,
47
50
mapper meta.RESTMapper ,
48
51
resync time.Duration ,
49
52
namespace string ,
50
- fieldSelectorByResource , labelSelectorByResource map [string ]string ) * InformersMap {
53
+ fieldSelectorByResource , labelSelectorByResource map [schema. GroupResource ]string ) * InformersMap {
51
54
52
55
return & InformersMap {
53
56
structured : newStructuredInformersMap (config , scheme , mapper , resync , namespace , fieldSelectorByResource , labelSelectorByResource ),
54
57
unstructured : newUnstructuredInformersMap (config , scheme , mapper , resync , namespace , fieldSelectorByResource , labelSelectorByResource ),
58
+ metadata : newMetadataInformersMap (config , scheme , mapper , resync , namespace , fieldSelectorByResource , labelSelectorByResource ),
55
59
56
60
Scheme : scheme ,
57
61
}
58
62
}
59
63
60
- // Start calls Run on each of the informers and sets started to true. Blocks on the stop channel.
61
- func (m * InformersMap ) Start (stop <- chan struct {}) error {
62
- go m .structured .Start (stop )
63
- go m .unstructured .Start (stop )
64
- <- stop
64
+ // Start calls Run on each of the informers and sets started to true. Blocks on the context.
65
+ func (m * InformersMap ) Start (ctx context.Context ) error {
66
+ go m .structured .Start (ctx )
67
+ go m .unstructured .Start (ctx )
68
+ go m .metadata .Start (ctx )
69
+ <- ctx .Done ()
65
70
return nil
66
71
}
67
72
68
73
// WaitForCacheSync waits until all the caches have been started and synced.
69
- func (m * InformersMap ) WaitForCacheSync (stop <- chan struct {} ) bool {
74
+ func (m * InformersMap ) WaitForCacheSync (ctx context. Context ) bool {
70
75
syncedFuncs := append ([]cache.InformerSynced (nil ), m .structured .HasSyncedFuncs ()... )
71
76
syncedFuncs = append (syncedFuncs , m .unstructured .HasSyncedFuncs ()... )
77
+ syncedFuncs = append (syncedFuncs , m .metadata .HasSyncedFuncs ()... )
72
78
73
- if ! m .structured .waitForStarted (stop ) {
79
+ if ! m .structured .waitForStarted (ctx ) {
74
80
return false
75
81
}
76
- if ! m .unstructured .waitForStarted (stop ) {
82
+ if ! m .unstructured .waitForStarted (ctx ) {
77
83
return false
78
84
}
79
- return cache .WaitForCacheSync (stop , syncedFuncs ... )
85
+ if ! m .metadata .waitForStarted (ctx ) {
86
+ return false
87
+ }
88
+ return cache .WaitForCacheSync (ctx .Done (), syncedFuncs ... )
80
89
}
81
90
82
91
// Get will create a new Informer and add it to the map of InformersMap if none exists. Returns
83
92
// the Informer from the map.
84
93
func (m * InformersMap ) Get (ctx context.Context , gvk schema.GroupVersionKind , obj runtime.Object ) (bool , * MapEntry , error ) {
85
- _ , isUnstructured := obj .(* unstructured.Unstructured )
86
- _ , isUnstructuredList := obj .(* unstructured.UnstructuredList )
87
- isUnstructured = isUnstructured || isUnstructuredList
88
-
89
- if isUnstructured {
94
+ switch obj .(type ) {
95
+ case * unstructured.Unstructured :
96
+ return m .unstructured .Get (ctx , gvk , obj )
97
+ case * unstructured.UnstructuredList :
90
98
return m .unstructured .Get (ctx , gvk , obj )
99
+ case * metav1.PartialObjectMetadata :
100
+ return m .metadata .Get (ctx , gvk , obj )
101
+ case * metav1.PartialObjectMetadataList :
102
+ return m .metadata .Get (ctx , gvk , obj )
103
+ default :
104
+ return m .structured .Get (ctx , gvk , obj )
91
105
}
92
-
93
- return m .structured .Get (ctx , gvk , obj )
94
106
}
95
107
96
108
// newStructuredInformersMap creates a new InformersMap for structured objects.
97
- func newStructuredInformersMap (config * rest.Config , scheme * runtime.Scheme , mapper meta.RESTMapper , resync time.Duration , namespace string , fieldSelectorByResource , labelSelectorByResource map [string ]string ) * specificInformersMap {
98
- return newSpecificInformersMap (config , scheme , mapper , resync , namespace , fieldSelectorByResource , labelSelectorByResource , createStructuredListWatch )
109
+ func newStructuredInformersMap (config * rest.Config , scheme * runtime.Scheme , mapper meta.RESTMapper , resync time.Duration , namespace string , fieldSelectorByResource , labelSelectorByResource map [schema. GroupResource ]string ) * specificInformersMap {
110
+ return newSpecificInformersMap (config , scheme , mapper , resync , namespace , createStructuredListWatch , fieldSelectorByResource , labelSelectorByResource )
99
111
}
100
112
101
113
// newUnstructuredInformersMap creates a new InformersMap for unstructured objects.
102
- func newUnstructuredInformersMap (config * rest.Config , scheme * runtime.Scheme , mapper meta.RESTMapper , resync time.Duration , namespace string , fieldSelectorByResource , labelSelectorByResource map [string ]string ) * specificInformersMap {
103
- return newSpecificInformersMap (config , scheme , mapper , resync , namespace , fieldSelectorByResource , labelSelectorByResource , createUnstructuredListWatch )
114
+ func newUnstructuredInformersMap (config * rest.Config , scheme * runtime.Scheme , mapper meta.RESTMapper , resync time.Duration , namespace string , fieldSelectorByResource , labelSelectorByResource map [schema.GroupResource ]string ) * specificInformersMap {
115
+ return newSpecificInformersMap (config , scheme , mapper , resync , namespace , createUnstructuredListWatch , fieldSelectorByResource , labelSelectorByResource )
116
+ }
117
+
118
+ // newMetadataInformersMap creates a new InformersMap for metadata-only objects.
119
+ func newMetadataInformersMap (config * rest.Config , scheme * runtime.Scheme , mapper meta.RESTMapper , resync time.Duration , namespace string , fieldSelectorByResource , labelSelectorByResource map [schema.GroupResource ]string ) * specificInformersMap {
120
+ return newSpecificInformersMap (config , scheme , mapper , resync , namespace , createMetadataListWatch , fieldSelectorByResource , labelSelectorByResource )
104
121
}
0 commit comments