forked from signalfx/signalfx-agent
/
fakek8s.go
377 lines (326 loc) · 11.3 KB
/
fakek8s.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package fakek8s
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"sync"
"github.com/davecgh/go-spew/spew"
log "github.com/sirupsen/logrus"
"github.com/gorilla/mux"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
runtimejson "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/runtime/serializer/streaming"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes/scheme"
restwatch "k8s.io/client-go/rest/watch"
)
type resourceKind string
type resourceName string
// FakeK8s is a mock K8s API server. It can serve both list and watch
// requests.
type FakeK8s struct {
sync.RWMutex
server *httptest.Server
router http.Handler
// Resources that have been inserted on the ResourceInput channel
resources map[resourceKind]map[string]map[resourceName]runtime.Object
eventInput chan watch.Event
// Channels to send new resources to watchers (we only support one watcher
// per resource)
subs map[resourceKind]chan watch.Event
subsMutex sync.Mutex
// Stops the resource accepter goroutine
eventStopper chan struct{}
// Stops all of the watchers
stoppers map[resourceKind]chan struct{}
}
// NewFakeK8s makes a new FakeK8s
func NewFakeK8s() *FakeK8s {
f := &FakeK8s{
resources: make(map[resourceKind]map[string]map[resourceName]runtime.Object),
eventInput: make(chan watch.Event),
subs: make(map[resourceKind]chan watch.Event),
stoppers: make(map[resourceKind]chan struct{}),
}
r := mux.NewRouter()
r.HandleFunc(`/api/v1/namespaces/{namespace}/{resource}/{name}`, f.handleGetResourceByName).Methods("GET")
r.HandleFunc(`/apis/extensions/v1beta1/namespaces/{namespace}/{resource}/{name}`, f.handleGetResourceByName).Methods("GET")
r.HandleFunc("/api/v1/namespaces/{namespace}/{resource}", f.handleListResource).Methods("GET")
r.HandleFunc("/apis/extensions/v1beta1/namespaces/{namespace}/{resource}", f.handleListResource).Methods("GET")
r.HandleFunc("/apis/extensions/v1beta1/{resource}", f.handleListResource).Methods("GET")
r.HandleFunc("/api/v1/{resource}", f.handleListResource).Methods("GET")
r.HandleFunc(`/api/v1/namespaces/{namespace}/{resource}`, f.handleCreateOrReplaceResource).Methods("POST")
r.HandleFunc(`/api/v1/namespaces/{namespace}/{resource}/{name}`, f.handleCreateOrReplaceResource).Methods("PUT")
r.HandleFunc(`/api/v1/namespaces/{namespace}/{resource}/{name}`, f.handleDeleteResource).Methods("DELETE")
r.Use(loggingMiddleware)
f.router = r
return f
}
// Start creates the server and starts it
func (f *FakeK8s) Start() {
f.server = httptest.NewUnstartedServer(f.router)
f.server.StartTLS()
f.eventStopper = make(chan struct{})
go f.acceptEvents(f.eventStopper)
}
// Close stops the server and all watchers
func (f *FakeK8s) Close() {
close(f.eventStopper)
for _, ch := range f.stoppers {
close(ch)
}
f.server.Listener.Close()
//f.server.Close()
}
// URL is the of the mock server to point your objects under test to
func (f *FakeK8s) URL() string {
return f.server.URL
}
// SetInitialList adds resources to the server state that are served when doing
// list requests. l can be a list of any of the supported resource types.
func (f *FakeK8s) SetInitialList(l []runtime.Object) {
for _, r := range l {
resKind := resourceKind(r.GetObjectKind().GroupVersionKind().Kind)
objMeta := r.(metav1.ObjectMetaAccessor).GetObjectMeta()
f.addToResources(resKind, objMeta.GetNamespace(), objMeta.GetName(), r)
}
}
func (f *FakeK8s) acceptEvents(stopper <-chan struct{}) {
log.Debugf("Generating Fake k8s events")
for {
select {
case <-stopper:
return
case e := <-f.eventInput:
resKind := resourceKind(e.Object.GetObjectKind().GroupVersionKind().Kind)
// Send it out to any watchers
// TODO: This only supports watchers across all namespaces
if f.subs[resKind] != nil {
log.Infof("Watch event sent to subscription: %s", spew.Sdump(e))
f.subs[resKind] <- e
} else {
log.Infof("Watch event ignored because nothing was watching for %s", resKind)
}
}
}
}
// Returns whether the object was created (true) or simply replaced (false)
func (f *FakeK8s) addToResources(resKind resourceKind, namespace string, name string, resource runtime.Object) bool {
f.Lock()
defer f.Unlock()
if f.resources[resKind] == nil {
f.resources[resKind] = make(map[string]map[resourceName]runtime.Object, 0)
}
if f.resources[resKind][namespace] == nil {
f.resources[resKind][namespace] = make(map[resourceName]runtime.Object, 0)
}
_, exists := f.resources[resKind][namespace][resourceName(name)]
f.resources[resKind][namespace][resourceName(name)] = resource
return !exists
}
func (f *FakeK8s) handleCreateOrReplaceResource(rw http.ResponseWriter, r *http.Request) {
content, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
panic("Got bad request")
}
obj, _, err := scheme.Codecs.UniversalDeserializer().Decode(content, nil, nil)
ns := mux.Vars(r)["namespace"]
if ns != "" {
obj.(metav1.ObjectMetaAccessor).GetObjectMeta().SetNamespace(ns)
}
f.CreateOrReplaceResource(obj)
}
// CreateOrReplaceResource can be used by unit tests. It will generate a watch
// event.
func (f *FakeK8s) CreateOrReplaceResource(obj runtime.Object) {
resKind := resourceKind(obj.GetObjectKind().GroupVersionKind().Kind)
objMeta := obj.(metav1.ObjectMetaAccessor).GetObjectMeta()
created := f.addToResources(resourceKind(resKind), objMeta.GetNamespace(), objMeta.GetName(), obj)
var eType watch.EventType
if created {
eType = watch.Added
} else {
eType = watch.Modified
}
f.eventInput <- watch.Event{Type: eType, Object: obj}
}
func (f *FakeK8s) handleDeleteResource(rw http.ResponseWriter, r *http.Request) {
f.DeleteResourceByName(string(pluralNameToKind(mux.Vars(r)["resource"])), mux.Vars(r)["namespace"], mux.Vars(r)["name"])
}
// DeleteResource deletes a resource and sends a watch event if actually
// deleted.
func (f *FakeK8s) DeleteResource(obj runtime.Object) bool {
objMeta := obj.(metav1.ObjectMetaAccessor).GetObjectMeta()
return f.DeleteResourceByName(obj.GetObjectKind().GroupVersionKind().Kind, objMeta.GetNamespace(), objMeta.GetName())
}
// DeleteResourceByName removes a resource from the fake api server. It will
// generate a watch event for the deletion if the resource existed.
func (f *FakeK8s) DeleteResourceByName(resKind string, namespace string, name string) bool {
if namespaces := f.resources[resourceKind(resKind)]; namespaces != nil {
if names := namespaces[namespace]; names != nil {
name := resourceName(name)
if obj, ok := names[name]; ok {
log.Infof("Deleting %s %s/%s", resKind, namespace, name)
delete(names, name)
f.eventInput <- watch.Event{Type: watch.Deleted, Object: obj}
return true
}
}
}
return false
}
func (f *FakeK8s) handleGetResourceByName(rw http.ResponseWriter, r *http.Request) {
namespaces := f.resources[pluralNameToKind(mux.Vars(r)["resource"])]
if namespaces == nil {
rw.WriteHeader(http.StatusNotFound)
return
}
names := namespaces[mux.Vars(r)["namespace"]]
if names == nil {
rw.WriteHeader(http.StatusNotFound)
return
}
obj := names[resourceName(mux.Vars(r)["name"])]
if obj == nil {
rw.WriteHeader(http.StatusNotFound)
return
}
s, _ := json.Marshal(obj)
rw.Header().Add("Content-Type", "application/json")
rw.WriteHeader(http.StatusOK)
rw.Write(s)
}
func (f *FakeK8s) handleListResource(rw http.ResponseWriter, r *http.Request) {
rw.Header().Add("Content-Type", "application/json")
isWatch := strings.Contains(r.URL.RawQuery, "watch=true")
namespace := mux.Vars(r)["namespace"]
resKind := pluralNameToKind(mux.Vars(r)["resource"])
if isWatch {
rw.Header().Add("Transfer-Encoding", "chunked")
// This must block in order to continue to be able to write to the
// ResponseWriter
if namespace != "" {
panic("Watches within a single namespace aren't supported")
}
f.startWatcher(resKind, rw)
} else {
f.sendList(resKind, namespace, rw)
}
}
// Start a long running routine that will send everything received on the
// `EventInput` channel as JSON back to the client
func (f *FakeK8s) startWatcher(resKind resourceKind, rw http.ResponseWriter) {
f.subsMutex.Lock()
if f.subs[resKind] != nil {
panic("We don't support more than one watcher at a time!")
}
log.Infof("Adding watcher for %s", resKind)
eventCh := make(chan watch.Event)
f.stoppers[resKind] = make(chan struct{})
// Alias so we only access map inside lock
stopper := f.stoppers[resKind]
f.subs[resKind] = eventCh
f.subsMutex.Unlock()
for {
select {
case r := <-eventCh:
buf := &bytes.Buffer{}
jsonSerializer := runtimejson.NewSerializer(runtimejson.DefaultMetaFactory, scheme.Scheme, scheme.Scheme, false)
directCodecFactory := serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}
innerEncoder := directCodecFactory.EncoderForVersion(jsonSerializer, v1.SchemeGroupVersion)
encoder := restwatch.NewEncoder(streaming.NewEncoder(buf, innerEncoder), innerEncoder)
if err := encoder.Encode(&r); err != nil {
panic("could not encode watch event")
}
rw.Write(buf.Bytes())
rw.Write([]byte("\n"))
rw.(http.Flusher).Flush()
case <-stopper:
return
}
}
}
func (f *FakeK8s) sendList(resKind resourceKind, namespace string, rw http.ResponseWriter) {
items := make([]runtime.RawExtension, 0)
addFromNamespace := func(ns string) {
for _, i := range f.resources[resKind][ns] {
items = append(items, runtime.RawExtension{
Object: i,
})
}
}
if namespace == "" {
for ns := range f.resources[resKind] {
addFromNamespace(ns)
}
} else {
addFromNamespace(namespace)
}
l := v1.List{
TypeMeta: typeMeta(resKind),
ListMeta: metav1.ListMeta{},
Items: items,
}
d, _ := json.Marshal(l)
log.Debugf("list: %s", string(d))
rw.Write(d)
}
func pluralNameToKind(name string) resourceKind {
switch name {
case "pods":
return "Pod"
case "replicationcontrollers":
return "ReplicationController"
case "deployments":
return "Deployment"
case "daemonsets":
return "DaemonSet"
case "replicasets":
return "ReplicaSet"
case "namespaces":
return "Namespace"
case "resourcequotas":
return "ResourceQuota"
case "nodes":
return "Node"
case "secrets":
return "Secret"
case "services":
return "Service"
default:
panic("Unknown resource type: " + name)
}
}
func typeMeta(rt resourceKind) metav1.TypeMeta {
switch string(rt) {
case "Pod":
return metav1.TypeMeta{Kind: "PodList", APIVersion: "v1"}
case "ReplicationController":
return metav1.TypeMeta{Kind: "ReplicationControllerList", APIVersion: "v1"}
case "Deployment":
return metav1.TypeMeta{Kind: "DeploymentList", APIVersion: "extensions/v1beta1"}
case "DaemonSet":
return metav1.TypeMeta{Kind: "DaemonSetList", APIVersion: "extensions/v1beta1"}
case "ReplicaSet":
return metav1.TypeMeta{Kind: "ReplicaSetList", APIVersion: "extensions/v1beta1"}
case "Namespace":
return metav1.TypeMeta{Kind: "NamespaceList", APIVersion: "v1"}
case "ResourceQuota":
return metav1.TypeMeta{Kind: "ResourceQuotaList", APIVersion: "v1"}
case "Node":
return metav1.TypeMeta{Kind: "NodeList", APIVersion: "v1"}
case "Secret":
return metav1.TypeMeta{Kind: "SecretList", APIVersion: "v1"}
case "Service":
return metav1.TypeMeta{Kind: "ServiceList", APIVersion: "v1"}
default:
panic("Unknown resource type: " + string(rt))
}
}