@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- package internal
17+ package cache
1818
1919import (
2020 "context"
@@ -56,7 +56,7 @@ type multiScopedCache struct {
5656// resources in the given namespace. namespacedInformers is the set of resource
5757// types which should only be watched in the given namespace.
5858// namespacedInformers expects Namespaced resource types.
59- func NewMultiScopedCache (scheme * runtime. Scheme , namespace string , namespacedInformers []schema.GroupKind ) cache.NewCacheFunc {
59+ func NewMultiScopedCache (namespace string , namespacedInformers []schema.GroupKind ) cache.NewCacheFunc {
6060 return func (config * rest.Config , opts cache.Options ) (cache.Cache , error ) {
6161 namespacedOpts := opts
6262 namespacedOpts .Namespace = namespace
@@ -71,8 +71,9 @@ func NewMultiScopedCache(scheme *runtime.Scheme, namespace string, namespacedInf
7171 if err != nil {
7272 return nil , err
7373 }
74+
7475 return & multiScopedCache {
75- scheme : scheme ,
76+ scheme : opts . Scheme ,
7677 namespacedCache : namespacedCache ,
7778 clusterCache : clusterCache ,
7879 namespacedInformers : namespacedInformers ,
@@ -85,13 +86,23 @@ func (b *multiScopedCache) GetInformer(ctx context.Context, obj client.Object) (
8586 if err := setGroupVersionKind (b .scheme , obj ); err != nil {
8687 return nil , err
8788 }
88- return b .cacheFromGVK (obj .GetObjectKind ().GroupVersionKind ()).GetInformer (ctx , obj )
89+
90+ cache , err := b .cacheFromGVK (obj .GetObjectKind ().GroupVersionKind ())
91+ if err != nil {
92+ return nil , err
93+ }
94+ return cache .GetInformer (ctx , obj )
8995}
9096
9197// GetInformerForKind returns the underlying cache's GetInformerForKind based
9298// on resource type.
9399func (b * multiScopedCache ) GetInformerForKind (ctx context.Context , gvk schema.GroupVersionKind ) (cache.Informer , error ) {
94- return b .cacheFromGVK (gvk ).GetInformerForKind (ctx , gvk )
100+
101+ cache , err := b .cacheFromGVK (gvk )
102+ if err != nil {
103+ return nil , err
104+ }
105+ return cache .GetInformerForKind (ctx , gvk )
95106}
96107
97108// Start starts both the cluster and namespaced caches. Returned is an
@@ -137,46 +148,61 @@ func (b *multiScopedCache) IndexField(ctx context.Context, obj client.Object, fi
137148 if err := setGroupVersionKind (b .scheme , obj ); err != nil {
138149 return err
139150 }
140- return b .cacheFromGVK (obj .GetObjectKind ().GroupVersionKind ()).IndexField (ctx , obj , field , extractValue )
151+
152+ cache , err := b .cacheFromGVK (obj .GetObjectKind ().GroupVersionKind ())
153+ if err != nil {
154+ return err
155+ }
156+ return cache .IndexField (ctx , obj , field , extractValue )
141157}
142158
143159// Get returns the underlying cache's Get based on resource type.
144160func (b * multiScopedCache ) Get (ctx context.Context , key client.ObjectKey , obj client.Object ) error {
145161 if err := setGroupVersionKind (b .scheme , obj ); err != nil {
146162 return err
147163 }
148- return b .cacheFromGVK (obj .GetObjectKind ().GroupVersionKind ()).Get (ctx , key , obj )
164+
165+ cache , err := b .cacheFromGVK (obj .GetObjectKind ().GroupVersionKind ())
166+ if err != nil {
167+ return err
168+ }
169+ return cache .Get (ctx , key , obj )
149170}
150171
151172// List returns the underlying cache's List based on resource type.
152173func (b * multiScopedCache ) List (ctx context.Context , list client.ObjectList , opts ... client.ListOption ) error {
153174 if err := setGroupVersionKind (b .scheme , list ); err != nil {
154175 return err
155176 }
156- return b .cacheFromGVK (list .GetObjectKind ().GroupVersionKind ()).List (ctx , list , opts ... )
177+
178+ cache , err := b .cacheFromGVK (list .GetObjectKind ().GroupVersionKind ())
179+ if err != nil {
180+ return err
181+ }
182+ return cache .List (ctx , list , opts ... )
157183}
158184
159185// cacheFromGVK returns either the cluster or namespaced cache, based on the
160186// resource type given.
161- func (b * multiScopedCache ) cacheFromGVK (gvk schema.GroupVersionKind ) cache.Cache {
187+ func (b * multiScopedCache ) cacheFromGVK (gvk schema.GroupVersionKind ) ( cache.Cache , error ) {
162188 if gvk .Group == "" && gvk .Kind == "" {
163- panic ( "The Group and/or Kind must be set" )
189+ return nil , fmt . Errorf ( "the Group and/or Kind must be set" )
164190 }
165191
166192 for _ , namespacedInformer := range b .namespacedInformers {
167193 if namespacedInformer .Group == gvk .Group && namespacedInformer .Kind == gvk .Kind {
168- return b .namespacedCache
194+ return b .namespacedCache , nil
169195 }
170196 }
171- return b .clusterCache
197+ return b .clusterCache , nil
172198}
173199
174200// setGroupVersionKind populates the Group and Kind fields of obj using the
175201// scheme type registry.
176202// Inspired by https://github.com/kubernetes-sigs/controller-runtime/issues/1735#issuecomment-984763173
177203func setGroupVersionKind (scheme * runtime.Scheme , obj runtime.Object ) error {
178204 gvk := obj .GetObjectKind ().GroupVersionKind ()
179- if gvk .Group != "" || gvk .Kind != "" {
205+ if gvk .Group != "" || gvk .Kind != "" || scheme == nil {
180206 return nil // eg. in case of PartialMetadata, we don't want to overwrite the Group/ Kind
181207 }
182208
0 commit comments