Skip to content

Commit acc97a2

Browse files
committed
Cache's ByObject is now a map Object -> options
This is a breaking change and revisits some of the previous changes we've made. It brings the options ByObject to a top level map, which now contains all the options the internal informer can be configured with. In addition, it removes the ObjectAll object, which was super confusing and only used to disable the deep copy for every object; instead it's now possible to disable deep copy through a top level option. Signed-off-by: Vince Prignano <[email protected]>
1 parent fc423fc commit acc97a2

File tree

9 files changed

+379
-480
lines changed

9 files changed

+379
-480
lines changed

pkg/cache/cache.go

Lines changed: 84 additions & 194 deletions
Large diffs are not rendered by default.

pkg/cache/cache_test.go

Lines changed: 46 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,6 @@ var _ = Describe("Informer Cache", func() {
119119
var _ = Describe("Multi-Namespace Informer Cache", func() {
120120
CacheTest(cache.MultiNamespacedCacheBuilder([]string{testNamespaceOne, testNamespaceTwo, "default"}), cache.Options{})
121121
})
122-
var _ = Describe("Informer Cache without DeepCopy", func() {
123-
CacheTest(cache.New, cache.Options{
124-
View: cache.ViewOptions{
125-
ByObject: cache.ViewByObject{
126-
UnsafeDisableDeepCopy: cache.DisableDeepCopyByObject{cache.ObjectAll{}: true},
127-
},
128-
},
129-
})
130-
})
131122

132123
var _ = Describe("Cache with transformers", func() {
133124
var (
@@ -190,48 +181,46 @@ var _ = Describe("Cache with transformers", func() {
190181

191182
By("creating the informer cache")
192183
informerCache, err = cache.New(cfg, cache.Options{
193-
View: cache.ViewOptions{
194-
DefaultTransform: func(i interface{}) (interface{}, error) {
195-
obj := i.(runtime.Object)
196-
Expect(obj).NotTo(BeNil())
197-
198-
accessor, err := meta.Accessor(obj)
199-
Expect(err).To(BeNil())
200-
annotations := accessor.GetAnnotations()
184+
DefaultTransform: func(i interface{}) (interface{}, error) {
185+
obj := i.(runtime.Object)
186+
Expect(obj).NotTo(BeNil())
201187

202-
if _, exists := annotations["transformed"]; exists {
203-
// Avoid performing transformation multiple times.
204-
return i, nil
205-
}
188+
accessor, err := meta.Accessor(obj)
189+
Expect(err).To(BeNil())
190+
annotations := accessor.GetAnnotations()
206191

207-
if annotations == nil {
208-
annotations = make(map[string]string)
209-
}
210-
annotations["transformed"] = "default"
211-
accessor.SetAnnotations(annotations)
192+
if _, exists := annotations["transformed"]; exists {
193+
// Avoid performing transformation multiple times.
212194
return i, nil
213-
},
214-
ByObject: cache.ViewByObject{
215-
Transform: cache.TransformByObject{
216-
&corev1.Pod{}: func(i interface{}) (interface{}, error) {
217-
obj := i.(runtime.Object)
218-
Expect(obj).NotTo(BeNil())
219-
accessor, err := meta.Accessor(obj)
220-
Expect(err).To(BeNil())
221-
222-
annotations := accessor.GetAnnotations()
223-
if _, exists := annotations["transformed"]; exists {
224-
// Avoid performing transformation multiple times.
225-
return i, nil
226-
}
227-
228-
if annotations == nil {
229-
annotations = make(map[string]string)
230-
}
231-
annotations["transformed"] = "explicit"
232-
accessor.SetAnnotations(annotations)
195+
}
196+
197+
if annotations == nil {
198+
annotations = make(map[string]string)
199+
}
200+
annotations["transformed"] = "default"
201+
accessor.SetAnnotations(annotations)
202+
return i, nil
203+
},
204+
ByObject: map[client.Object]cache.ByObject{
205+
&corev1.Pod{}: {
206+
Transform: func(i interface{}) (interface{}, error) {
207+
obj := i.(runtime.Object)
208+
Expect(obj).NotTo(BeNil())
209+
accessor, err := meta.Accessor(obj)
210+
Expect(err).To(BeNil())
211+
212+
annotations := accessor.GetAnnotations()
213+
if _, exists := annotations["transformed"]; exists {
214+
// Avoid performing transformation multiple times.
233215
return i, nil
234-
},
216+
}
217+
218+
if annotations == nil {
219+
annotations = make(map[string]string)
220+
}
221+
annotations["transformed"] = "explicit"
222+
accessor.SetAnnotations(annotations)
223+
return i, nil
235224
},
236225
},
237226
},
@@ -370,13 +359,9 @@ var _ = Describe("Cache with selectors", func() {
370359
}
371360

372361
opts := cache.Options{
373-
View: cache.ViewOptions{
374-
DefaultSelector: cache.ObjectSelector{Field: fields.OneTermEqualSelector("metadata.namespace", testNamespaceTwo)},
375-
ByObject: cache.ViewByObject{
376-
Selectors: cache.SelectorsByObject{
377-
&corev1.ServiceAccount{}: {Field: fields.OneTermEqualSelector("metadata.namespace", testNamespaceOne)},
378-
},
379-
},
362+
DefaultSelector: cache.ObjectSelector{Field: fields.OneTermEqualSelector("metadata.namespace", testNamespaceTwo)},
363+
ByObject: map[client.Object]cache.ByObject{
364+
&corev1.ServiceAccount{}: {Field: fields.OneTermEqualSelector("metadata.namespace", testNamespaceOne)},
380365
},
381366
}
382367

@@ -798,7 +783,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
798783

799784
It("should be able to restrict cache to a namespace", func() {
800785
By("creating a namespaced cache")
801-
namespacedCache, err := cache.New(cfg, cache.Options{View: cache.ViewOptions{Namespaces: []string{testNamespaceOne}}})
786+
namespacedCache, err := cache.New(cfg, cache.Options{Namespaces: []string{testNamespaceOne}})
802787
Expect(err).NotTo(HaveOccurred())
803788

804789
By("running the cache and waiting for it to sync")
@@ -1079,7 +1064,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
10791064

10801065
It("should be able to restrict cache to a namespace", func() {
10811066
By("creating a namespaced cache")
1082-
namespacedCache, err := cache.New(cfg, cache.Options{View: cache.ViewOptions{Namespaces: []string{testNamespaceOne}}})
1067+
namespacedCache, err := cache.New(cfg, cache.Options{Namespaces: []string{testNamespaceOne}})
10831068
Expect(err).NotTo(HaveOccurred())
10841069

10851070
By("running the cache and waiting for it to sync")
@@ -1233,14 +1218,10 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
12331218
By("creating the cache")
12341219
builder := cache.BuilderWithOptions(
12351220
cache.Options{
1236-
View: cache.ViewOptions{
1237-
ByObject: cache.ViewByObject{
1238-
Selectors: cache.SelectorsByObject{
1239-
&corev1.Pod{}: {
1240-
Label: labels.Set(tc.labelSelectors).AsSelector(),
1241-
Field: fields.Set(tc.fieldSelectors).AsSelector(),
1242-
},
1243-
},
1221+
ByObject: map[client.Object]cache.ByObject{
1222+
&corev1.Pod{}: {
1223+
Label: labels.Set(tc.labelSelectors).AsSelector(),
1224+
Field: fields.Set(tc.fieldSelectors).AsSelector(),
12441225
},
12451226
},
12461227
},
@@ -1822,12 +1803,5 @@ func isKubeService(svc metav1.Object) bool {
18221803
}
18231804

18241805
func isPodDisableDeepCopy(opts cache.Options) bool {
1825-
if d, ok := opts.View.ByObject.UnsafeDisableDeepCopy[&corev1.Pod{}]; ok {
1826-
return d
1827-
} else if d, ok = opts.View.ByObject.UnsafeDisableDeepCopy[cache.ObjectAll{}]; ok {
1828-
return d
1829-
} else if d, ok = opts.View.ByObject.UnsafeDisableDeepCopy[&cache.ObjectAll{}]; ok {
1830-
return d
1831-
}
1832-
return false
1806+
return opts.ByObject[&corev1.Pod{}].UnsafeDisableDeepCopy
18331807
}

0 commit comments

Comments
 (0)