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