Skip to content

Commit 4462fd4

Browse files
authored
Merge pull request #1195 from vincepri/reconcile-object
⚠ Introduce and use client.Object and client.ObjectList
2 parents baa2707 + 00af7b6 commit 4462fd4

29 files changed

+181
-141
lines changed

pkg/builder/controller.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
"strings"
2222

2323
"github.com/go-logr/logr"
24-
"k8s.io/apimachinery/pkg/runtime"
2524
"k8s.io/apimachinery/pkg/runtime/schema"
2625
"k8s.io/client-go/rest"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
2727
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
2828
"sigs.k8s.io/controller-runtime/pkg/controller"
2929
"sigs.k8s.io/controller-runtime/pkg/handler"
@@ -57,7 +57,7 @@ func ControllerManagedBy(m manager.Manager) *Builder {
5757

5858
// ForInput represents the information set by For method.
5959
type ForInput struct {
60-
object runtime.Object
60+
object client.Object
6161
predicates []predicate.Predicate
6262
err error
6363
}
@@ -66,7 +66,7 @@ type ForInput struct {
6666
// update events by *reconciling the object*.
6767
// This is the equivalent of calling
6868
// Watches(&source.Kind{Type: apiType}, &handler.EnqueueRequestForObject{})
69-
func (blder *Builder) For(object runtime.Object, opts ...ForOption) *Builder {
69+
func (blder *Builder) For(object client.Object, opts ...ForOption) *Builder {
7070
if blder.forInput.object != nil {
7171
blder.forInput.err = fmt.Errorf("For(...) should only be called once, could not assign multiple objects for reconciliation")
7272
return blder
@@ -82,14 +82,14 @@ func (blder *Builder) For(object runtime.Object, opts ...ForOption) *Builder {
8282

8383
// OwnsInput represents the information set by Owns method.
8484
type OwnsInput struct {
85-
object runtime.Object
85+
object client.Object
8686
predicates []predicate.Predicate
8787
}
8888

8989
// Owns defines types of Objects being *generated* by the ControllerManagedBy, and configures the ControllerManagedBy to respond to
9090
// create / delete / update events by *reconciling the owner object*. This is the equivalent of calling
9191
// Watches(&source.Kind{Type: <ForType-forInput>}, &handler.EnqueueRequestForOwner{OwnerType: apiType, IsController: true})
92-
func (blder *Builder) Owns(object runtime.Object, opts ...OwnsOption) *Builder {
92+
func (blder *Builder) Owns(object client.Object, opts ...OwnsOption) *Builder {
9393
input := OwnsInput{object: object}
9494
for _, opt := range opts {
9595
opt.ApplyToOwns(&input)

pkg/builder/controller_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,10 @@ func doReconcileTest(nameSuffix string, stop chan struct{}, blder *Builder, mgr
458458

459459
var _ runtime.Object = &fakeType{}
460460

461-
type fakeType struct{}
461+
type fakeType struct {
462+
metav1.TypeMeta
463+
metav1.ObjectMeta
464+
}
462465

463466
func (*fakeType) GetObjectKind() schema.ObjectKind { return nil }
464467
func (*fakeType) DeepCopyObject() runtime.Object { return nil }

pkg/builder/webhook_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
. "github.com/onsi/ginkgo"
2929
. "github.com/onsi/gomega"
30+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3031
"k8s.io/apimachinery/pkg/runtime"
3132
"k8s.io/apimachinery/pkg/runtime/schema"
3233
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -465,6 +466,9 @@ func (v *TestValidator) ValidateDelete() error {
465466
var _ runtime.Object = &TestDefaultValidator{}
466467

467468
type TestDefaultValidator struct {
469+
metav1.TypeMeta
470+
metav1.ObjectMeta
471+
468472
Replica int `json:"replica,omitempty"`
469473
}
470474

pkg/cache/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type Cache interface {
5252
type Informers interface {
5353
// GetInformer fetches or constructs an informer for the given object that corresponds to a single
5454
// API kind and resource.
55-
GetInformer(ctx context.Context, obj runtime.Object) (Informer, error)
55+
GetInformer(ctx context.Context, obj client.Object) (Informer, error)
5656

5757
// GetInformerForKind is similar to GetInformer, except that it takes a group-version-kind, instead
5858
// of the underlying object.

pkg/cache/cache_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"k8s.io/apimachinery/pkg/api/errors"
2727
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
29-
"k8s.io/apimachinery/pkg/runtime"
3029
"k8s.io/apimachinery/pkg/runtime/schema"
3130
kscheme "k8s.io/client-go/kubernetes/scheme"
3231
"k8s.io/client-go/rest"
@@ -42,7 +41,7 @@ const testNamespaceThree = "test-namespace-3"
4241

4342
// TODO(community): Pull these helper functions into testenv.
4443
// Restart policy is included to allow indexing on that field.
45-
func createPod(name, namespace string, restartPolicy kcorev1.RestartPolicy) runtime.Object {
44+
func createPod(name, namespace string, restartPolicy kcorev1.RestartPolicy) client.Object {
4645
three := int64(3)
4746
pod := &kcorev1.Pod{
4847
ObjectMeta: kmetav1.ObjectMeta{
@@ -65,7 +64,7 @@ func createPod(name, namespace string, restartPolicy kcorev1.RestartPolicy) runt
6564
return pod
6665
}
6766

68-
func deletePod(pod runtime.Object) {
67+
func deletePod(pod client.Object) {
6968
cl, err := client.New(cfg, client.Options{})
7069
Expect(err).NotTo(HaveOccurred())
7170
err = cl.Delete(context.Background(), pod)
@@ -86,10 +85,10 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
8685
informerCache cache.Cache
8786
informerCacheCtx context.Context
8887
informerCacheCancel context.CancelFunc
89-
knownPod1 runtime.Object
90-
knownPod2 runtime.Object
91-
knownPod3 runtime.Object
92-
knownPod4 runtime.Object
88+
knownPod1 client.Object
89+
knownPod2 client.Object
90+
knownPod3 client.Object
91+
knownPod4 client.Object
9392
)
9493

9594
BeforeEach(func() {
@@ -566,7 +565,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
566565

567566
By("indexing the restartPolicy field of the Pod object before starting")
568567
pod := &kcorev1.Pod{}
569-
indexFunc := func(obj runtime.Object) []string {
568+
indexFunc := func(obj client.Object) []string {
570569
return []string{string(obj.(*kcorev1.Pod).Spec.RestartPolicy)}
571570
}
572571
Expect(informer.IndexField(context.TODO(), pod, "spec.restartPolicy", indexFunc)).To(Succeed())
@@ -684,7 +683,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
684683
Version: "v1",
685684
Kind: "Pod",
686685
})
687-
indexFunc := func(obj runtime.Object) []string {
686+
indexFunc := func(obj client.Object) []string {
688687
s, ok := obj.(*unstructured.Unstructured).Object["spec"]
689688
if !ok {
690689
return []string{}

pkg/cache/informer_cache.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type informerCache struct {
5151
}
5252

5353
// Get implements Reader
54-
func (ip *informerCache) Get(ctx context.Context, key client.ObjectKey, out runtime.Object) error {
54+
func (ip *informerCache) Get(ctx context.Context, key client.ObjectKey, out client.Object) error {
5555
gvk, err := apiutil.GVKForObject(out, ip.Scheme)
5656
if err != nil {
5757
return err
@@ -69,7 +69,7 @@ func (ip *informerCache) Get(ctx context.Context, key client.ObjectKey, out runt
6969
}
7070

7171
// List implements Reader
72-
func (ip *informerCache) List(ctx context.Context, out runtime.Object, opts ...client.ListOption) error {
72+
func (ip *informerCache) List(ctx context.Context, out client.ObjectList, opts ...client.ListOption) error {
7373

7474
gvk, cacheTypeObj, err := ip.objectTypeForListObject(out)
7575
if err != nil {
@@ -91,7 +91,7 @@ func (ip *informerCache) List(ctx context.Context, out runtime.Object, opts ...c
9191
// objectTypeForListObject tries to find the runtime.Object and associated GVK
9292
// for a single object corresponding to the passed-in list type. We need them
9393
// because they are used as cache map key.
94-
func (ip *informerCache) objectTypeForListObject(list runtime.Object) (*schema.GroupVersionKind, runtime.Object, error) {
94+
func (ip *informerCache) objectTypeForListObject(list client.ObjectList) (*schema.GroupVersionKind, runtime.Object, error) {
9595
gvk, err := apiutil.GVKForObject(list, ip.Scheme)
9696
if err != nil {
9797
return nil, nil, err
@@ -146,7 +146,7 @@ func (ip *informerCache) GetInformerForKind(ctx context.Context, gvk schema.Grou
146146
}
147147

148148
// GetInformer returns the informer for the obj
149-
func (ip *informerCache) GetInformer(ctx context.Context, obj runtime.Object) (Informer, error) {
149+
func (ip *informerCache) GetInformer(ctx context.Context, obj client.Object) (Informer, error) {
150150
gvk, err := apiutil.GVKForObject(obj, ip.Scheme)
151151
if err != nil {
152152
return nil, err
@@ -170,7 +170,7 @@ func (ip *informerCache) NeedLeaderElection() bool {
170170
// to List. For one-to-one compatibility with "normal" field selectors, only return one value.
171171
// The values may be anything. They will automatically be prefixed with the namespace of the
172172
// given object, if present. The objects passed are guaranteed to be objects of the correct type.
173-
func (ip *informerCache) IndexField(ctx context.Context, obj runtime.Object, field string, extractValue client.IndexerFunc) error {
173+
func (ip *informerCache) IndexField(ctx context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error {
174174
informer, err := ip.GetInformer(ctx, obj)
175175
if err != nil {
176176
return err
@@ -181,7 +181,7 @@ func (ip *informerCache) IndexField(ctx context.Context, obj runtime.Object, fie
181181
func indexByField(indexer Informer, field string, extractor client.IndexerFunc) error {
182182
indexFunc := func(objRaw interface{}) ([]string, error) {
183183
// TODO(directxman12): check if this is the correct type?
184-
obj, isObj := objRaw.(runtime.Object)
184+
obj, isObj := objRaw.(client.Object)
185185
if !isObj {
186186
return nil, fmt.Errorf("object of type %T is not an Object", objRaw)
187187
}

pkg/cache/informer_cache_unit_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ var _ = Describe("ip.objectTypeForListObject", func() {
2525
InformersMap: &internal.InformersMap{Scheme: scheme.Scheme},
2626
}
2727

28-
It("should error on non-list types", func() {
29-
_, _, err := ip.objectTypeForListObject(&corev1.Pod{})
30-
Expect(err).To(HaveOccurred())
31-
Expect(err.Error()).To(Equal(`non-list type *v1.Pod (kind "/v1, Kind=Pod") passed as output`))
32-
})
33-
3428
It("should find the object type for unstructured lists", func() {
3529
unstructuredList := &unstructured.UnstructuredList{}
3630
unstructuredList.SetAPIVersion("v1")

pkg/cache/informertest/fake_cache.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (c *FakeInformers) FakeInformerForKind(ctx context.Context, gvk schema.Grou
6767
}
6868

6969
// GetInformer implements Informers
70-
func (c *FakeInformers) GetInformer(ctx context.Context, obj runtime.Object) (cache.Informer, error) {
70+
func (c *FakeInformers) GetInformer(ctx context.Context, obj client.Object) (cache.Informer, error) {
7171
if c.Scheme == nil {
7272
c.Scheme = scheme.Scheme
7373
}
@@ -126,16 +126,16 @@ func (c *FakeInformers) Start(ctx context.Context) error {
126126
}
127127

128128
// IndexField implements Cache
129-
func (c *FakeInformers) IndexField(ctx context.Context, obj runtime.Object, field string, extractValue client.IndexerFunc) error {
129+
func (c *FakeInformers) IndexField(ctx context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error {
130130
return nil
131131
}
132132

133133
// Get implements Cache
134-
func (c *FakeInformers) Get(ctx context.Context, key client.ObjectKey, obj runtime.Object) error {
134+
func (c *FakeInformers) Get(ctx context.Context, key client.ObjectKey, obj client.Object) error {
135135
return nil
136136
}
137137

138138
// List implements Cache
139-
func (c *FakeInformers) List(ctx context.Context, list runtime.Object, opts ...client.ListOption) error {
139+
func (c *FakeInformers) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
140140
return nil
141141
}

pkg/cache/internal/cache_reader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type CacheReader struct {
4545
}
4646

4747
// Get checks the indexer for the object and writes a copy of it if found
48-
func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out runtime.Object) error {
48+
func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out client.Object) error {
4949
storeKey := objectKeyToStoreKey(key)
5050

5151
// Lookup the object from the indexer cache
@@ -87,7 +87,7 @@ func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out runtime.O
8787
}
8888

8989
// List lists items out of the indexer and writes them to out
90-
func (c *CacheReader) List(_ context.Context, out runtime.Object, opts ...client.ListOption) error {
90+
func (c *CacheReader) List(_ context.Context, out client.ObjectList, opts ...client.ListOption) error {
9191
var objs []interface{}
9292
var err error
9393

pkg/cache/multi_namespace_cache.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type multiNamespaceCache struct {
7070
var _ Cache = &multiNamespaceCache{}
7171

7272
// Methods for multiNamespaceCache to conform to the Informers interface
73-
func (c *multiNamespaceCache) GetInformer(ctx context.Context, obj runtime.Object) (Informer, error) {
73+
func (c *multiNamespaceCache) GetInformer(ctx context.Context, obj client.Object) (Informer, error) {
7474
informers := map[string]Informer{}
7575
for ns, cache := range c.namespaceToCache {
7676
informer, err := cache.GetInformer(ctx, obj)
@@ -117,7 +117,7 @@ func (c *multiNamespaceCache) WaitForCacheSync(ctx context.Context) bool {
117117
return synced
118118
}
119119

120-
func (c *multiNamespaceCache) IndexField(ctx context.Context, obj runtime.Object, field string, extractValue client.IndexerFunc) error {
120+
func (c *multiNamespaceCache) IndexField(ctx context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error {
121121
for _, cache := range c.namespaceToCache {
122122
if err := cache.IndexField(ctx, obj, field, extractValue); err != nil {
123123
return err
@@ -126,7 +126,7 @@ func (c *multiNamespaceCache) IndexField(ctx context.Context, obj runtime.Object
126126
return nil
127127
}
128128

129-
func (c *multiNamespaceCache) Get(ctx context.Context, key client.ObjectKey, obj runtime.Object) error {
129+
func (c *multiNamespaceCache) Get(ctx context.Context, key client.ObjectKey, obj client.Object) error {
130130
cache, ok := c.namespaceToCache[key.Namespace]
131131
if !ok {
132132
return fmt.Errorf("unable to get: %v because of unknown namespace for the cache", key)
@@ -135,7 +135,7 @@ func (c *multiNamespaceCache) Get(ctx context.Context, key client.ObjectKey, obj
135135
}
136136

137137
// List multi namespace cache will get all the objects in the namespaces that the cache is watching if asked for all namespaces.
138-
func (c *multiNamespaceCache) List(ctx context.Context, list runtime.Object, opts ...client.ListOption) error {
138+
func (c *multiNamespaceCache) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
139139
listOpts := client.ListOptions{}
140140
listOpts.ApplyOptions(opts)
141141
if listOpts.Namespace != corev1.NamespaceAll {
@@ -157,7 +157,7 @@ func (c *multiNamespaceCache) List(ctx context.Context, list runtime.Object, opt
157157
}
158158
var resourceVersion string
159159
for _, cache := range c.namespaceToCache {
160-
listObj := list.DeepCopyObject()
160+
listObj := list.DeepCopyObject().(client.ObjectList)
161161
err = cache.List(ctx, listObj, opts...)
162162
if err != nil {
163163
return err

pkg/client/client.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (c *client) RESTMapper() meta.RESTMapper {
124124
}
125125

126126
// Create implements client.Client
127-
func (c *client) Create(ctx context.Context, obj runtime.Object, opts ...CreateOption) error {
127+
func (c *client) Create(ctx context.Context, obj Object, opts ...CreateOption) error {
128128
_, ok := obj.(*unstructured.Unstructured)
129129
if ok {
130130
return c.unstructuredClient.Create(ctx, obj, opts...)
@@ -133,7 +133,7 @@ func (c *client) Create(ctx context.Context, obj runtime.Object, opts ...CreateO
133133
}
134134

135135
// Update implements client.Client
136-
func (c *client) Update(ctx context.Context, obj runtime.Object, opts ...UpdateOption) error {
136+
func (c *client) Update(ctx context.Context, obj Object, opts ...UpdateOption) error {
137137
defer c.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
138138
_, ok := obj.(*unstructured.Unstructured)
139139
if ok {
@@ -143,7 +143,7 @@ func (c *client) Update(ctx context.Context, obj runtime.Object, opts ...UpdateO
143143
}
144144

145145
// Delete implements client.Client
146-
func (c *client) Delete(ctx context.Context, obj runtime.Object, opts ...DeleteOption) error {
146+
func (c *client) Delete(ctx context.Context, obj Object, opts ...DeleteOption) error {
147147
_, ok := obj.(*unstructured.Unstructured)
148148
if ok {
149149
return c.unstructuredClient.Delete(ctx, obj, opts...)
@@ -152,7 +152,7 @@ func (c *client) Delete(ctx context.Context, obj runtime.Object, opts ...DeleteO
152152
}
153153

154154
// DeleteAllOf implements client.Client
155-
func (c *client) DeleteAllOf(ctx context.Context, obj runtime.Object, opts ...DeleteAllOfOption) error {
155+
func (c *client) DeleteAllOf(ctx context.Context, obj Object, opts ...DeleteAllOfOption) error {
156156
_, ok := obj.(*unstructured.Unstructured)
157157
if ok {
158158
return c.unstructuredClient.DeleteAllOf(ctx, obj, opts...)
@@ -161,7 +161,7 @@ func (c *client) DeleteAllOf(ctx context.Context, obj runtime.Object, opts ...De
161161
}
162162

163163
// Patch implements client.Client
164-
func (c *client) Patch(ctx context.Context, obj runtime.Object, patch Patch, opts ...PatchOption) error {
164+
func (c *client) Patch(ctx context.Context, obj Object, patch Patch, opts ...PatchOption) error {
165165
defer c.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
166166
_, ok := obj.(*unstructured.Unstructured)
167167
if ok {
@@ -171,7 +171,7 @@ func (c *client) Patch(ctx context.Context, obj runtime.Object, patch Patch, opt
171171
}
172172

173173
// Get implements client.Client
174-
func (c *client) Get(ctx context.Context, key ObjectKey, obj runtime.Object) error {
174+
func (c *client) Get(ctx context.Context, key ObjectKey, obj Object) error {
175175
_, ok := obj.(*unstructured.Unstructured)
176176
if ok {
177177
return c.unstructuredClient.Get(ctx, key, obj)
@@ -180,7 +180,7 @@ func (c *client) Get(ctx context.Context, key ObjectKey, obj runtime.Object) err
180180
}
181181

182182
// List implements client.Client
183-
func (c *client) List(ctx context.Context, obj runtime.Object, opts ...ListOption) error {
183+
func (c *client) List(ctx context.Context, obj ObjectList, opts ...ListOption) error {
184184
_, ok := obj.(*unstructured.UnstructuredList)
185185
if ok {
186186
return c.unstructuredClient.List(ctx, obj, opts...)
@@ -202,7 +202,7 @@ type statusWriter struct {
202202
var _ StatusWriter = &statusWriter{}
203203

204204
// Update implements client.StatusWriter
205-
func (sw *statusWriter) Update(ctx context.Context, obj runtime.Object, opts ...UpdateOption) error {
205+
func (sw *statusWriter) Update(ctx context.Context, obj Object, opts ...UpdateOption) error {
206206
defer sw.client.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
207207
_, ok := obj.(*unstructured.Unstructured)
208208
if ok {
@@ -212,7 +212,7 @@ func (sw *statusWriter) Update(ctx context.Context, obj runtime.Object, opts ...
212212
}
213213

214214
// Patch implements client.Client
215-
func (sw *statusWriter) Patch(ctx context.Context, obj runtime.Object, patch Patch, opts ...PatchOption) error {
215+
func (sw *statusWriter) Patch(ctx context.Context, obj Object, patch Patch, opts ...PatchOption) error {
216216
defer sw.client.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
217217
_, ok := obj.(*unstructured.Unstructured)
218218
if ok {

pkg/client/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2815,12 +2815,12 @@ type fakeReader struct {
28152815
Called int
28162816
}
28172817

2818-
func (f *fakeReader) Get(ctx context.Context, key client.ObjectKey, obj runtime.Object) error {
2818+
func (f *fakeReader) Get(ctx context.Context, key client.ObjectKey, obj client.Object) error {
28192819
f.Called = f.Called + 1
28202820
return nil
28212821
}
28222822

2823-
func (f *fakeReader) List(ctx context.Context, list runtime.Object, opts ...client.ListOption) error {
2823+
func (f *fakeReader) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
28242824
f.Called = f.Called + 1
28252825
return nil
28262826
}

0 commit comments

Comments
 (0)