/
fake_client.go
186 lines (154 loc) · 5.34 KB
/
fake_client.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package fake
import (
"context"
"strings"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
fakedynamic "k8s.io/client-go/dynamic/fake"
"k8s.io/client-go/kubernetes"
faketyped "k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
configv1 "github.com/openshift/api/config/v1"
cnoclient "github.com/openshift/cluster-network-operator/pkg/client"
"github.com/openshift/cluster-network-operator/pkg/names"
osoperclient "github.com/openshift/client-go/operator/clientset/versioned"
operatorv1helpers "github.com/openshift/library-go/pkg/operator/v1helpers"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
crfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
)
type FakeClient struct {
clusterClients map[string]*FakeClusterClient
}
type FakeClusterClient struct {
// dynclient is an untyped, uncached client for making direct requests
// against the apiserver.
dynclient dynamic.Interface
// kClient is an fake kubernetes client for kubernetes objects
kClient kubernetes.Interface
// crclient is the controller-runtime ClusterClient, for controllers that have
// not yet been migrated.
crclient crclient.Client
}
func (fc *FakeClient) ClientFor(name string) cnoclient.ClusterClient {
if len(name) == 0 {
return fc.Default()
}
return fc.clusterClients[name]
}
func (fc *FakeClient) Default() cnoclient.ClusterClient {
return fc.ClientFor(names.DefaultClusterName)
}
func (fc *FakeClient) Start(context.Context) error {
return nil
}
func (fc *FakeClient) Clients() map[string]cnoclient.ClusterClient {
out := make(map[string]cnoclient.ClusterClient)
for k, v := range fc.clusterClients {
out[k] = v
}
return out
}
func isOpenShiftObject(obj crclient.Object) bool {
kKind, _, _ := scheme.Scheme.ObjectKinds(obj)
for _, v := range kKind {
if strings.HasSuffix(v.Group, "openshift.io") {
return true
}
}
return false
}
// NewFakeClient creates a fake client with a backing store that contains the given objexts.
//
// Note that, due to limitations in the test infrastructure, each client has an independent store.
// This means that changes made in, say, the crclient, won't show up in the Dynamic client or the typed
// Kubernetes client
// TODO: stop using the crclient entirely
// TODO: Somehow convince upstream client-go to allow sharing the store between the dynamic and typed clients.
//
// (this is't that big a deal since we don't actually use the typed client that much).
func NewFakeClient(objs ...crclient.Object) cnoclient.Client {
// silly go type conversion
oo := make([]runtime.Object, 0, len(objs))
ooTyped := make([]runtime.Object, 0, len(objs))
for _, o := range objs {
oo = append(oo, o)
if !isOpenShiftObject(o) {
ooTyped = append(ooTyped, o)
}
}
co := &configv1.ClusterOperator{ObjectMeta: metav1.ObjectMeta{Name: ""}}
fc := FakeClusterClient{
kClient: faketyped.NewSimpleClientset(ooTyped...),
dynclient: fakedynamic.NewSimpleDynamicClient(scheme.Scheme, oo...),
crclient: crfake.NewClientBuilder().WithStatusSubresource(co).WithObjects(objs...).Build(),
}
return &FakeClient{
clusterClients: map[string]*FakeClusterClient{
names.DefaultClusterName: &fc,
},
}
}
type fakeRESTMapper struct {
kindForInput schema.GroupVersionResource
}
func (f *fakeRESTMapper) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
f.kindForInput = resource
return schema.GroupVersionKind{
Group: "test",
Version: "test",
Kind: "test"}, nil
}
func (f *fakeRESTMapper) KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
return nil, nil
}
func (f *fakeRESTMapper) ResourceFor(input schema.GroupVersionResource) (schema.GroupVersionResource, error) {
return schema.GroupVersionResource{}, nil
}
func (f *fakeRESTMapper) ResourcesFor(input schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
return nil, nil
}
func (f *fakeRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*meta.RESTMapping, error) {
return nil, nil
}
func (f *fakeRESTMapper) RESTMappings(gk schema.GroupKind, versions ...string) ([]*meta.RESTMapping, error) {
return nil, nil
}
func (f *fakeRESTMapper) ResourceSingularizer(resource string) (singular string, err error) {
return "", nil
}
func (fc *FakeClusterClient) Kubernetes() kubernetes.Interface {
return fc.kClient
}
func (fc *FakeClusterClient) OpenshiftOperatorClient() *osoperclient.Clientset {
panic("not implemented!")
}
func (fc *FakeClusterClient) Config() *rest.Config {
return nil
}
func (fc *FakeClusterClient) Dynamic() dynamic.Interface {
return fc.dynclient
}
func (fc *FakeClusterClient) CRClient() crclient.Client {
return fc.crclient
}
func (fc *FakeClusterClient) RESTMapper() meta.RESTMapper {
return &fakeRESTMapper{}
}
func (fc *FakeClusterClient) Scheme() *runtime.Scheme {
panic("not implemented!")
}
func (fc *FakeClusterClient) OperatorHelperClient() operatorv1helpers.OperatorClient {
panic("not implemented!")
}
func (fc *FakeClusterClient) HostPort() (string, string) {
return "testing", "9999"
}
func (fc *FakeClusterClient) AddCustomInformer(inf cache.SharedInformer) {
klog.Warningf("the fake Kubernetes client doesn't support informers!")
}