sigs.k8s.io/controller-runtime@v0.18.2/pkg/cache/informertest/fake_cache.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package informertest 18 19 import ( 20 "context" 21 22 "k8s.io/apimachinery/pkg/runtime" 23 "k8s.io/apimachinery/pkg/runtime/schema" 24 "k8s.io/client-go/kubernetes/scheme" 25 toolscache "k8s.io/client-go/tools/cache" 26 27 "sigs.k8s.io/controller-runtime/pkg/cache" 28 "sigs.k8s.io/controller-runtime/pkg/client" 29 "sigs.k8s.io/controller-runtime/pkg/controller/controllertest" 30 ) 31 32 var _ cache.Cache = &FakeInformers{} 33 34 // FakeInformers is a fake implementation of Informers. 35 type FakeInformers struct { 36 InformersByGVK map[schema.GroupVersionKind]toolscache.SharedIndexInformer 37 Scheme *runtime.Scheme 38 Error error 39 Synced *bool 40 } 41 42 // GetInformerForKind implements Informers. 43 func (c *FakeInformers) GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind, opts ...cache.InformerGetOption) (cache.Informer, error) { 44 if c.Scheme == nil { 45 c.Scheme = scheme.Scheme 46 } 47 obj, err := c.Scheme.New(gvk) 48 if err != nil { 49 return nil, err 50 } 51 return c.informerFor(gvk, obj) 52 } 53 54 // FakeInformerForKind implements Informers. 55 func (c *FakeInformers) FakeInformerForKind(ctx context.Context, gvk schema.GroupVersionKind) (*controllertest.FakeInformer, error) { 56 i, err := c.GetInformerForKind(ctx, gvk) 57 if err != nil { 58 return nil, err 59 } 60 return i.(*controllertest.FakeInformer), nil 61 } 62 63 // GetInformer implements Informers. 64 func (c *FakeInformers) GetInformer(ctx context.Context, obj client.Object, opts ...cache.InformerGetOption) (cache.Informer, error) { 65 if c.Scheme == nil { 66 c.Scheme = scheme.Scheme 67 } 68 gvks, _, err := c.Scheme.ObjectKinds(obj) 69 if err != nil { 70 return nil, err 71 } 72 gvk := gvks[0] 73 return c.informerFor(gvk, obj) 74 } 75 76 // RemoveInformer implements Informers. 77 func (c *FakeInformers) RemoveInformer(ctx context.Context, obj client.Object) error { 78 if c.Scheme == nil { 79 c.Scheme = scheme.Scheme 80 } 81 gvks, _, err := c.Scheme.ObjectKinds(obj) 82 if err != nil { 83 return err 84 } 85 gvk := gvks[0] 86 delete(c.InformersByGVK, gvk) 87 return nil 88 } 89 90 // WaitForCacheSync implements Informers. 91 func (c *FakeInformers) WaitForCacheSync(ctx context.Context) bool { 92 if c.Synced == nil { 93 return true 94 } 95 return *c.Synced 96 } 97 98 // FakeInformerFor implements Informers. 99 func (c *FakeInformers) FakeInformerFor(ctx context.Context, obj client.Object) (*controllertest.FakeInformer, error) { 100 i, err := c.GetInformer(ctx, obj) 101 if err != nil { 102 return nil, err 103 } 104 return i.(*controllertest.FakeInformer), nil 105 } 106 107 func (c *FakeInformers) informerFor(gvk schema.GroupVersionKind, _ runtime.Object) (toolscache.SharedIndexInformer, error) { 108 if c.Error != nil { 109 return nil, c.Error 110 } 111 if c.InformersByGVK == nil { 112 c.InformersByGVK = map[schema.GroupVersionKind]toolscache.SharedIndexInformer{} 113 } 114 informer, ok := c.InformersByGVK[gvk] 115 if ok { 116 return informer, nil 117 } 118 119 c.InformersByGVK[gvk] = &controllertest.FakeInformer{} 120 return c.InformersByGVK[gvk], nil 121 } 122 123 // Start implements Informers. 124 func (c *FakeInformers) Start(ctx context.Context) error { 125 return c.Error 126 } 127 128 // IndexField implements Cache. 129 func (c *FakeInformers) IndexField(ctx context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error { 130 return nil 131 } 132 133 // Get implements Cache. 134 func (c *FakeInformers) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { 135 return nil 136 } 137 138 // List implements Cache. 139 func (c *FakeInformers) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { 140 return nil 141 }