| 1 |
/* |
| 2 |
Copyright 2014 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 endpoints |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"context" |
| 22 |
"encoding/json" |
| 23 |
"fmt" |
| 24 |
"io" |
| 25 |
"io/ioutil" |
| 26 |
"net/http" |
| 27 |
"net/http/httptest" |
| 28 |
"net/url" |
| 29 |
"reflect" |
| 30 |
"strings" |
| 31 |
"sync" |
| 32 |
"testing" |
| 33 |
"time" |
| 34 |
|
| 35 |
"github.com/google/go-cmp/cmp" |
| 36 |
"golang.org/x/net/websocket" |
| 37 |
apiequality "k8s.io/apimachinery/pkg/api/equality" |
| 38 |
"k8s.io/apimachinery/pkg/api/errors" |
| 39 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 40 |
"k8s.io/apimachinery/pkg/fields" |
| 41 |
"k8s.io/apimachinery/pkg/labels" |
| 42 |
"k8s.io/apimachinery/pkg/runtime" |
| 43 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 44 |
"k8s.io/apimachinery/pkg/runtime/serializer/streaming" |
| 45 |
"k8s.io/apimachinery/pkg/util/wait" |
| 46 |
"k8s.io/apimachinery/pkg/watch" |
| 47 |
example "k8s.io/apiserver/pkg/apis/example" |
| 48 |
"k8s.io/apiserver/pkg/endpoints/handlers" |
| 49 |
"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters" |
| 50 |
apitesting "k8s.io/apiserver/pkg/endpoints/testing" |
| 51 |
"k8s.io/apiserver/pkg/registry/rest" |
| 52 |
"k8s.io/client-go/dynamic" |
| 53 |
restclient "k8s.io/client-go/rest" |
| 54 |
) |
| 55 |
|
| 56 |
// watchJSON defines the expected JSON wire equivalent of watch.Event |
| 57 |
type watchJSON struct { |
| 58 |
Type watch.EventType `json:"type,omitempty"` |
| 59 |
Object json.RawMessage `json:"object,omitempty"` |
| 60 |
} |
| 61 |
|
| 62 |
// roundTripOrDie round trips an object to get defaults set. |
| 63 |
func roundTripOrDie(codec runtime.Codec, object runtime.Object) runtime.Object { |
| 64 |
data, err := runtime.Encode(codec, object) |
| 65 |
if err != nil { |
| 66 |
panic(err) |
| 67 |
} |
| 68 |
obj, err := runtime.Decode(codec, data) |
| 69 |
if err != nil { |
| 70 |
panic(err) |
| 71 |
} |
| 72 |
return obj |
| 73 |
} |
| 74 |
|
| 75 |
var watchTestTable = []struct { |
| 76 |
t watch.EventType |
| 77 |
obj runtime.Object |
| 78 |
}{ |
| 79 |
{watch.Added, &apitesting.Simple{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}}, |
| 80 |
{watch.Modified, &apitesting.Simple{ObjectMeta: metav1.ObjectMeta{Name: "bar"}}}, |
| 81 |
{watch.Deleted, &apitesting.Simple{ObjectMeta: metav1.ObjectMeta{Name: "bar"}}}, |
| 82 |
} |
| 83 |
|
| 84 |
func podWatchTestTable() []struct { |
| 85 |
t watch.EventType |
| 86 |
obj runtime.Object |
| 87 |
} { |
| 88 |
// creaze lazily here in a func because podWatchTestTable can only be used after all types are registered. |
| 89 |
return []struct { |
| 90 |
t watch.EventType |
| 91 |
obj runtime.Object |
| 92 |
}{ |
| 93 |
{watch.Added, roundTripOrDie(codec, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})}, |
| 94 |
{watch.Modified, roundTripOrDie(codec, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar"}})}, |
| 95 |
{watch.Deleted, roundTripOrDie(codec, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar"}})}, |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
func TestWatchWebsocket(t *testing.T) { |
| 100 |
simpleStorage := &SimpleRESTStorage{} |
| 101 |
_ = rest.Watcher(simpleStorage) // Give compile error if this doesn't work. |
| 102 |
handler := handle(map[string]rest.Storage{"simples": simpleStorage}) |
| 103 |
server := httptest.NewServer(handler) |
| 104 |
defer server.Close() |
| 105 |
|
| 106 |
dest, _ := url.Parse(server.URL) |
| 107 |
dest.Scheme = "ws" // Required by websocket, though the server never sees it. |
| 108 |
dest.Path = "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/watch/simples" |
| 109 |
dest.RawQuery = "" |
| 110 |
|
| 111 |
ws, err := websocket.Dial(dest.String(), "", "http://localhost") |
| 112 |
if err != nil { |
| 113 |
t.Fatalf("unexpected error: %v", err) |
| 114 |
} |
| 115 |
|
| 116 |
try := func(action watch.EventType, object runtime.Object) { |
| 117 |
// Send |
| 118 |
simpleStorage.fakeWatch.Action(action, object) |
| 119 |
// Test receive |
| 120 |
var got watchJSON |
| 121 |
err := websocket.JSON.Receive(ws, &got) |
| 122 |
if err != nil { |
| 123 |
t.Fatalf("Unexpected error: %v", err) |
| 124 |
} |
| 125 |
if got.Type != action { |
| 126 |
t.Errorf("Unexpected type: %v", got.Type) |
| 127 |
} |
| 128 |
gotObj, err := runtime.Decode(codec, got.Object) |
| 129 |
if err != nil { |
| 130 |
t.Fatalf("Decode error: %v\n%v", err, got) |
| 131 |
} |
| 132 |
if e, a := object, gotObj; !reflect.DeepEqual(e, a) { |
| 133 |
t.Errorf("Expected %#v, got %#v", e, a) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
for _, item := range watchTestTable { |
| 138 |
try(item.t, item.obj) |
| 139 |
} |
| 140 |
simpleStorage.fakeWatch.Stop() |
| 141 |
|
| 142 |
var got watchJSON |
| 143 |
err = websocket.JSON.Receive(ws, &got) |
| 144 |
if err == nil { |
| 145 |
t.Errorf("Unexpected non-error") |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func TestWatchWebsocketClientClose(t *testing.T) { |
| 150 |
simpleStorage := &SimpleRESTStorage{} |
| 151 |
_ = rest.Watcher(simpleStorage) // Give compile error if this doesn't work. |
| 152 |
handler := handle(map[string]rest.Storage{"simples": simpleStorage}) |
| 153 |
server := httptest.NewServer(handler) |
| 154 |
defer server.Close() |
| 155 |
|
| 156 |
dest, _ := url.Parse(server.URL) |
| 157 |
dest.Scheme = "ws" // Required by websocket, though the server never sees it. |
| 158 |
dest.Path = "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/watch/simples" |
| 159 |
dest.RawQuery = "" |
| 160 |
|
| 161 |
ws, err := websocket.Dial(dest.String(), "", "http://localhost") |
| 162 |
if err != nil { |
| 163 |
t.Fatalf("unexpected error: %v", err) |
| 164 |
} |
| 165 |
|
| 166 |
try := func(action watch.EventType, object runtime.Object) { |
| 167 |
// Send |
| 168 |
simpleStorage.fakeWatch.Action(action, object) |
| 169 |
// Test receive |
| 170 |
var got watchJSON |
| 171 |
err := websocket.JSON.Receive(ws, &got) |
| 172 |
if err != nil { |
| 173 |
t.Fatalf("Unexpected error: %v", err) |
| 174 |
} |
| 175 |
if got.Type != action { |
| 176 |
t.Errorf("Unexpected type: %v", got.Type) |
| 177 |
} |
| 178 |
gotObj, err := runtime.Decode(codec, got.Object) |
| 179 |
if err != nil { |
| 180 |
t.Fatalf("Decode error: %v\n%v", err, got) |
| 181 |
} |
| 182 |
if e, a := object, gotObj; !reflect.DeepEqual(e, a) { |
| 183 |
t.Errorf("Expected %#v, got %#v", e, a) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
// Send/receive should work |
| 188 |
for _, item := range watchTestTable { |
| 189 |
try(item.t, item.obj) |
| 190 |
} |
| 191 |
|
| 192 |
// Sending normal data should be ignored |
| 193 |
websocket.JSON.Send(ws, map[string]interface{}{"test": "data"}) |
| 194 |
|
| 195 |
// Send/receive should still work |
| 196 |
for _, item := range watchTestTable { |
| 197 |
try(item.t, item.obj) |
| 198 |
} |
| 199 |
|
| 200 |
// Client requests a close |
| 201 |
ws.Close() |
| 202 |
|
| 203 |
select { |
| 204 |
case data, ok := <-simpleStorage.fakeWatch.ResultChan(): |
| 205 |
if ok { |
| 206 |
t.Errorf("expected a closed result channel, but got watch result %#v", data) |
| 207 |
} |
| 208 |
case <-time.After(5 * time.Second): |
| 209 |
t.Errorf("watcher did not close when client closed") |
| 210 |
} |
| 211 |
|
| 212 |
var got watchJSON |
| 213 |
err = websocket.JSON.Receive(ws, &got) |
| 214 |
if err == nil { |
| 215 |
t.Errorf("Unexpected non-error") |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
func TestWatchClientClose(t *testing.T) { |
| 220 |
simpleStorage := &SimpleRESTStorage{} |
| 221 |
_ = rest.Watcher(simpleStorage) // Give compile error if this doesn't work. |
| 222 |
handler := handle(map[string]rest.Storage{"simples": simpleStorage}) |
| 223 |
server := httptest.NewServer(handler) |
| 224 |
defer server.Close() |
| 225 |
|
| 226 |
dest, _ := url.Parse(server.URL) |
| 227 |
dest.Path = "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/simples" |
| 228 |
dest.RawQuery = "watch=1" |
| 229 |
|
| 230 |
request, err := http.NewRequest("GET", dest.String(), nil) |
| 231 |
if err != nil { |
| 232 |
t.Fatalf("unexpected error: %v", err) |
| 233 |
} |
| 234 |
request.Header.Add("Accept", "application/json") |
| 235 |
|
| 236 |
response, err := http.DefaultClient.Do(request) |
| 237 |
if err != nil { |
| 238 |
t.Fatalf("unexpected error: %v", err) |
| 239 |
} |
| 240 |
|
| 241 |
if response.StatusCode != http.StatusOK { |
| 242 |
b, _ := ioutil.ReadAll(response.Body) |
| 243 |
t.Fatalf("Unexpected response: %#v\n%s", response, string(b)) |
| 244 |
} |
| 245 |
|
| 246 |
// Close response to cause a cancel on the server |
| 247 |
if err := response.Body.Close(); err != nil { |
| 248 |
t.Fatalf("Unexpected close client err: %v", err) |
| 249 |
} |
| 250 |
|
| 251 |
select { |
| 252 |
case data, ok := <-simpleStorage.fakeWatch.ResultChan(): |
| 253 |
if ok { |
| 254 |
t.Errorf("expected a closed result channel, but got watch result %#v", data) |
| 255 |
} |
| 256 |
case <-time.After(5 * time.Second): |
| 257 |
t.Errorf("watcher did not close when client closed") |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
func TestWatchRead(t *testing.T) { |
| 262 |
simpleStorage := &SimpleRESTStorage{} |
| 263 |
_ = rest.Watcher(simpleStorage) // Give compile error if this doesn't work. |
| 264 |
handler := handle(map[string]rest.Storage{"simples": simpleStorage}) |
| 265 |
server := httptest.NewServer(handler) |
| 266 |
defer server.Close() |
| 267 |
|
| 268 |
dest, _ := url.Parse(server.URL) |
| 269 |
dest.Path = "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/simples" |
| 270 |
dest.RawQuery = "watch=1" |
| 271 |
|
| 272 |
connectHTTP := func(accept string) (io.ReadCloser, string) { |
| 273 |
client := http.Client{} |
| 274 |
request, err := http.NewRequest("GET", dest.String(), nil) |
| 275 |
if err != nil { |
| 276 |
t.Fatalf("unexpected error: %v", err) |
| 277 |
} |
| 278 |
request.Header.Add("Accept", accept) |
| 279 |
|
| 280 |
response, err := client.Do(request) |
| 281 |
if err != nil { |
| 282 |
t.Fatalf("unexpected error: %v", err) |
| 283 |
} |
| 284 |
|
| 285 |
if response.StatusCode != http.StatusOK { |
| 286 |
b, _ := ioutil.ReadAll(response.Body) |
| 287 |
t.Fatalf("Unexpected response for accept: %q: %#v\n%s", accept, response, string(b)) |
| 288 |
} |
| 289 |
return response.Body, response.Header.Get("Content-Type") |
| 290 |
} |
| 291 |
|
| 292 |
connectWebSocket := func(accept string) (io.ReadCloser, string) { |
| 293 |
dest := *dest |
| 294 |
dest.Scheme = "ws" // Required by websocket, though the server never sees it. |
| 295 |
config, err := websocket.NewConfig(dest.String(), "http://localhost") |
| 296 |
if err != nil { |
| 297 |
t.Fatalf("unexpected error: %v", err) |
| 298 |
} |
| 299 |
config.Header.Add("Accept", accept) |
| 300 |
ws, err := websocket.DialConfig(config) |
| 301 |
if err != nil { |
| 302 |
t.Fatalf("unexpected error: %v", err) |
| 303 |
} |
| 304 |
return ws, "__default__" |
| 305 |
} |
| 306 |
|
| 307 |
testCases := []struct { |
| 308 |
Accept string |
| 309 |
ExpectedContentType string |
| 310 |
MediaType string |
| 311 |
}{ |
| 312 |
{ |
| 313 |
Accept: "application/json", |
| 314 |
ExpectedContentType: "application/json", |
| 315 |
MediaType: "application/json", |
| 316 |
}, |
| 317 |
{ |
| 318 |
Accept: "application/json;stream=watch", |
| 319 |
ExpectedContentType: "application/json", // legacy behavior |
| 320 |
MediaType: "application/json", |
| 321 |
}, |
| 322 |
// TODO: yaml stream serialization requires that RawExtension.MarshalJSON |
| 323 |
// be able to understand nested encoding (since yaml calls json.Marshal |
| 324 |
// rather than yaml.Marshal, which results in the raw bytes being in yaml). |
| 325 |
/*{ |
| 326 |
Accept: "application/yaml", |
| 327 |
ExpectedContentType: "application/yaml;stream=watch", |
| 328 |
MediaType: "application/yaml", |
| 329 |
},*/ |
| 330 |
{ |
| 331 |
Accept: "application/vnd.kubernetes.protobuf", |
| 332 |
ExpectedContentType: "application/vnd.kubernetes.protobuf;stream=watch", |
| 333 |
MediaType: "application/vnd.kubernetes.protobuf", |
| 334 |
}, |
| 335 |
{ |
| 336 |
Accept: "application/vnd.kubernetes.protobuf;stream=watch", |
| 337 |
ExpectedContentType: "application/vnd.kubernetes.protobuf;stream=watch", |
| 338 |
MediaType: "application/vnd.kubernetes.protobuf", |
| 339 |
}, |
| 340 |
} |
| 341 |
protocols := []struct { |
| 342 |
name string |
| 343 |
selfFraming bool |
| 344 |
fn func(string) (io.ReadCloser, string) |
| 345 |
}{ |
| 346 |
{name: "http", fn: connectHTTP}, |
| 347 |
{name: "websocket", selfFraming: true, fn: connectWebSocket}, |
| 348 |
} |
| 349 |
|
| 350 |
for _, protocol := range protocols { |
| 351 |
for _, test := range testCases { |
| 352 |
func() { |
| 353 |
info, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), test.MediaType) |
| 354 |
if !ok || info.StreamSerializer == nil { |
| 355 |
t.Fatal(info) |
| 356 |
} |
| 357 |
streamSerializer := info.StreamSerializer |
| 358 |
|
| 359 |
r, contentType := protocol.fn(test.Accept) |
| 360 |
defer r.Close() |
| 361 |
|
| 362 |
if contentType != "__default__" && contentType != test.ExpectedContentType { |
| 363 |
t.Errorf("Unexpected content type: %#v", contentType) |
| 364 |
} |
| 365 |
objectCodec := codecs.DecoderToVersion(info.Serializer, testInternalGroupVersion) |
| 366 |
|
| 367 |
var fr io.ReadCloser = r |
| 368 |
if !protocol.selfFraming { |
| 369 |
fr = streamSerializer.Framer.NewFrameReader(r) |
| 370 |
} |
| 371 |
d := streaming.NewDecoder(fr, streamSerializer.Serializer) |
| 372 |
|
| 373 |
var w *watch.FakeWatcher |
| 374 |
for w == nil { |
| 375 |
w = simpleStorage.Watcher() |
| 376 |
time.Sleep(time.Millisecond) |
| 377 |
} |
| 378 |
|
| 379 |
for i, item := range podWatchTestTable() { |
| 380 |
action, object := item.t, item.obj |
| 381 |
name := fmt.Sprintf("%s-%s-%d", protocol.name, test.MediaType, i) |
| 382 |
|
| 383 |
// Send |
| 384 |
w.Action(action, object) |
| 385 |
// Test receive |
| 386 |
var got metav1.WatchEvent |
| 387 |
_, _, err := d.Decode(nil, &got) |
| 388 |
if err != nil { |
| 389 |
t.Fatalf("%s: Unexpected error: %v", name, err) |
| 390 |
} |
| 391 |
if got.Type != string(action) { |
| 392 |
t.Errorf("%s: Unexpected type: %v", name, got.Type) |
| 393 |
} |
| 394 |
|
| 395 |
gotObj, err := runtime.Decode(objectCodec, got.Object.Raw) |
| 396 |
if err != nil { |
| 397 |
t.Fatalf("%s: Decode error: %v", name, err) |
| 398 |
} |
| 399 |
if e, a := object, gotObj; !apiequality.Semantic.DeepEqual(e, a) { |
| 400 |
t.Errorf("%s: different: %s", name, cmp.Diff(e, a)) |
| 401 |
} |
| 402 |
} |
| 403 |
w.Stop() |
| 404 |
|
| 405 |
var got metav1.WatchEvent |
| 406 |
_, _, err := d.Decode(nil, &got) |
| 407 |
if err == nil { |
| 408 |
t.Errorf("Unexpected non-error") |
| 409 |
} |
| 410 |
}() |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
func TestWatchHTTPAccept(t *testing.T) { |
| 416 |
simpleStorage := &SimpleRESTStorage{} |
| 417 |
handler := handle(map[string]rest.Storage{"simples": simpleStorage}) |
| 418 |
server := httptest.NewServer(handler) |
| 419 |
defer server.Close() |
| 420 |
client := http.Client{} |
| 421 |
|
| 422 |
dest, _ := url.Parse(server.URL) |
| 423 |
dest.Path = "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/watch/simples" |
| 424 |
dest.RawQuery = "" |
| 425 |
|
| 426 |
request, err := http.NewRequest("GET", dest.String(), nil) |
| 427 |
if err != nil { |
| 428 |
t.Errorf("unexpected error: %v", err) |
| 429 |
} |
| 430 |
|
| 431 |
request.Header.Set("Accept", "application/XYZ") |
| 432 |
response, err := client.Do(request) |
| 433 |
if err != nil { |
| 434 |
t.Errorf("unexpected error: %v", err) |
| 435 |
} |
| 436 |
|
| 437 |
// TODO: once this is fixed, this test will change |
| 438 |
if response.StatusCode != http.StatusNotAcceptable { |
| 439 |
t.Errorf("Unexpected response %#v", response) |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
func TestWatchParamParsing(t *testing.T) { |
| 444 |
simpleStorage := &SimpleRESTStorage{} |
| 445 |
handler := handle(map[string]rest.Storage{ |
| 446 |
"simples": simpleStorage, |
| 447 |
"simpleroots": simpleStorage, |
| 448 |
}) |
| 449 |
server := httptest.NewServer(handler) |
| 450 |
defer server.Close() |
| 451 |
|
| 452 |
dest, _ := url.Parse(server.URL) |
| 453 |
|
| 454 |
rootPath := "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/watch/simples" |
| 455 |
namespacedPath := "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/watch/namespaces/other/simpleroots" |
| 456 |
|
| 457 |
table := []struct { |
| 458 |
path string |
| 459 |
rawQuery string |
| 460 |
resourceVersion string |
| 461 |
labelSelector string |
| 462 |
fieldSelector string |
| 463 |
namespace string |
| 464 |
}{ |
| 465 |
{ |
| 466 |
path: rootPath, |
| 467 |
rawQuery: "resourceVersion=1234", |
| 468 |
resourceVersion: "1234", |
| 469 |
labelSelector: "", |
| 470 |
fieldSelector: "", |
| 471 |
namespace: metav1.NamespaceAll, |
| 472 |
}, { |
| 473 |
path: rootPath, |
| 474 |
rawQuery: "resourceVersion=314159&fieldSelector=Host%3D&labelSelector=name%3Dfoo", |
| 475 |
resourceVersion: "314159", |
| 476 |
labelSelector: "name=foo", |
| 477 |
fieldSelector: "Host=", |
| 478 |
namespace: metav1.NamespaceAll, |
| 479 |
}, { |
| 480 |
path: rootPath, |
| 481 |
rawQuery: "fieldSelector=id%3dfoo&resourceVersion=1492", |
| 482 |
resourceVersion: "1492", |
| 483 |
labelSelector: "", |
| 484 |
fieldSelector: "id=foo", |
| 485 |
namespace: metav1.NamespaceAll, |
| 486 |
}, { |
| 487 |
path: rootPath, |
| 488 |
rawQuery: "", |
| 489 |
resourceVersion: "", |
| 490 |
labelSelector: "", |
| 491 |
fieldSelector: "", |
| 492 |
namespace: metav1.NamespaceAll, |
| 493 |
}, |
| 494 |
{ |
| 495 |
path: namespacedPath, |
| 496 |
rawQuery: "resourceVersion=1234", |
| 497 |
resourceVersion: "1234", |
| 498 |
labelSelector: "", |
| 499 |
fieldSelector: "", |
| 500 |
namespace: "other", |
| 501 |
}, { |
| 502 |
path: namespacedPath, |
| 503 |
rawQuery: "resourceVersion=314159&fieldSelector=Host%3D&labelSelector=name%3Dfoo", |
| 504 |
resourceVersion: "314159", |
| 505 |
labelSelector: "name=foo", |
| 506 |
fieldSelector: "Host=", |
| 507 |
namespace: "other", |
| 508 |
}, { |
| 509 |
path: namespacedPath, |
| 510 |
rawQuery: "fieldSelector=id%3dfoo&resourceVersion=1492", |
| 511 |
resourceVersion: "1492", |
| 512 |
labelSelector: "", |
| 513 |
fieldSelector: "id=foo", |
| 514 |
namespace: "other", |
| 515 |
}, { |
| 516 |
path: namespacedPath, |
| 517 |
rawQuery: "", |
| 518 |
resourceVersion: "", |
| 519 |
labelSelector: "", |
| 520 |
fieldSelector: "", |
| 521 |
namespace: "other", |
| 522 |
}, |
| 523 |
} |
| 524 |
|
| 525 |
for _, item := range table { |
| 526 |
simpleStorage.requestedLabelSelector = labels.Everything() |
| 527 |
simpleStorage.requestedFieldSelector = fields.Everything() |
| 528 |
simpleStorage.requestedResourceVersion = "5" // Prove this is set in all cases |
| 529 |
simpleStorage.requestedResourceNamespace = "" |
| 530 |
dest.Path = item.path |
| 531 |
dest.RawQuery = item.rawQuery |
| 532 |
resp, err := http.Get(dest.String()) |
| 533 |
if err != nil { |
| 534 |
t.Errorf("%v: unexpected error: %v", item.rawQuery, err) |
| 535 |
continue |
| 536 |
} |
| 537 |
resp.Body.Close() |
| 538 |
if e, a := item.namespace, simpleStorage.requestedResourceNamespace; e != a { |
| 539 |
t.Errorf("%v: expected %v, got %v", item.rawQuery, e, a) |
| 540 |
} |
| 541 |
if e, a := item.resourceVersion, simpleStorage.requestedResourceVersion; e != a { |
| 542 |
t.Errorf("%v: expected %v, got %v", item.rawQuery, e, a) |
| 543 |
} |
| 544 |
if e, a := item.labelSelector, simpleStorage.requestedLabelSelector.String(); e != a { |
| 545 |
t.Errorf("%v: expected %v, got %v", item.rawQuery, e, a) |
| 546 |
} |
| 547 |
if e, a := item.fieldSelector, simpleStorage.requestedFieldSelector.String(); e != a { |
| 548 |
t.Errorf("%v: expected %v, got %v", item.rawQuery, e, a) |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
func TestWatchProtocolSelection(t *testing.T) { |
| 554 |
simpleStorage := &SimpleRESTStorage{} |
| 555 |
handler := handle(map[string]rest.Storage{"simples": simpleStorage}) |
| 556 |
server := httptest.NewServer(handler) |
| 557 |
defer server.Close() |
| 558 |
defer server.CloseClientConnections() |
| 559 |
client := http.Client{} |
| 560 |
|
| 561 |
dest, _ := url.Parse(server.URL) |
| 562 |
dest.Path = "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/watch/simples" |
| 563 |
dest.RawQuery = "" |
| 564 |
|
| 565 |
table := []struct { |
| 566 |
isWebsocket bool |
| 567 |
connHeader string |
| 568 |
}{ |
| 569 |
{true, "Upgrade"}, |
| 570 |
{true, "keep-alive, Upgrade"}, |
| 571 |
{true, "upgrade"}, |
| 572 |
{false, "keep-alive"}, |
| 573 |
} |
| 574 |
|
| 575 |
for _, item := range table { |
| 576 |
request, err := http.NewRequest("GET", dest.String(), nil) |
| 577 |
if err != nil { |
| 578 |
t.Errorf("unexpected error: %v", err) |
| 579 |
} |
| 580 |
request.Header.Set("Connection", item.connHeader) |
| 581 |
request.Header.Set("Upgrade", "websocket") |
| 582 |
|
| 583 |
response, err := client.Do(request) |
| 584 |
if err != nil { |
| 585 |
t.Errorf("unexpected error: %v", err) |
| 586 |
} |
| 587 |
|
| 588 |
// The requests recognized as websocket requests based on connection |
| 589 |
// and upgrade headers will not also have the necessary Sec-Websocket-* |
| 590 |
// headers so it is expected to throw a 400 |
| 591 |
if item.isWebsocket && response.StatusCode != http.StatusBadRequest { |
| 592 |
t.Errorf("Unexpected response %#v", response) |
| 593 |
} |
| 594 |
|
| 595 |
if !item.isWebsocket && response.StatusCode != http.StatusOK { |
| 596 |
t.Errorf("Unexpected response %#v", response) |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
} |
| 601 |
|
| 602 |
type fakeTimeoutFactory struct { |
| 603 |
timeoutCh chan time.Time |
| 604 |
done chan struct{} |
| 605 |
} |
| 606 |
|
| 607 |
func (t *fakeTimeoutFactory) TimeoutCh() (<-chan time.Time, func() bool) { |
| 608 |
return t.timeoutCh, func() bool { |
| 609 |
defer close(t.done) |
| 610 |
return true |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
// serveWatch will serve a watch response according to the watcher and watchServer. |
| 615 |
// Before watchServer.HandleHTTP, an error may occur like k8s.io/apiserver/pkg/endpoints/handlers/watch.go#serveWatch does. |
| 616 |
func serveWatch(watcher watch.Interface, watchServer *handlers.WatchServer, preServeErr error) http.HandlerFunc { |
| 617 |
return func(w http.ResponseWriter, req *http.Request) { |
| 618 |
defer watcher.Stop() |
| 619 |
|
| 620 |
if preServeErr != nil { |
| 621 |
responsewriters.ErrorNegotiated(preServeErr, watchServer.Scope.Serializer, watchServer.Scope.Kind.GroupVersion(), w, req) |
| 622 |
return |
| 623 |
} |
| 624 |
|
| 625 |
watchServer.HandleHTTP(w, req) |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
func TestWatchHTTPErrors(t *testing.T) { |
| 630 |
watcher := watch.NewFake() |
| 631 |
timeoutCh := make(chan time.Time) |
| 632 |
done := make(chan struct{}) |
| 633 |
|
| 634 |
info, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), runtime.ContentTypeJSON) |
| 635 |
if !ok || info.StreamSerializer == nil { |
| 636 |
t.Fatal(info) |
| 637 |
} |
| 638 |
serializer := info.StreamSerializer |
| 639 |
|
| 640 |
// Setup a new watchserver |
| 641 |
watchServer := &handlers.WatchServer{ |
| 642 |
Scope: &handlers.RequestScope{}, |
| 643 |
Watching: watcher, |
| 644 |
|
| 645 |
MediaType: "testcase/json", |
| 646 |
Framer: serializer.Framer, |
| 647 |
Encoder: newCodec, |
| 648 |
EmbeddedEncoder: newCodec, |
| 649 |
|
| 650 |
TimeoutFactory: &fakeTimeoutFactory{timeoutCh, done}, |
| 651 |
} |
| 652 |
|
| 653 |
s := httptest.NewServer(serveWatch(watcher, watchServer, nil)) |
| 654 |
defer s.Close() |
| 655 |
|
| 656 |
// Setup a client |
| 657 |
dest, _ := url.Parse(s.URL) |
| 658 |
dest.Path = "/" + prefix + "/" + newGroupVersion.Group + "/" + newGroupVersion.Version + "/simple" |
| 659 |
dest.RawQuery = "watch=true" |
| 660 |
|
| 661 |
req, _ := http.NewRequest("GET", dest.String(), nil) |
| 662 |
client := http.Client{} |
| 663 |
resp, err := client.Do(req) |
| 664 |
if err != nil { |
| 665 |
t.Fatalf("Unexpected error: %v", err) |
| 666 |
} |
| 667 |
errStatus := errors.NewInternalError(fmt.Errorf("we got an error")).Status() |
| 668 |
watcher.Error(&errStatus) |
| 669 |
watcher.Stop() |
| 670 |
|
| 671 |
// Make sure we can actually watch an endpoint |
| 672 |
decoder := json.NewDecoder(resp.Body) |
| 673 |
var got watchJSON |
| 674 |
err = decoder.Decode(&got) |
| 675 |
if err != nil { |
| 676 |
t.Fatalf("Unexpected error: %v", err) |
| 677 |
} |
| 678 |
if got.Type != watch.Error { |
| 679 |
t.Fatalf("unexpected watch type: %#v", got) |
| 680 |
} |
| 681 |
status := &metav1.Status{} |
| 682 |
if err := json.Unmarshal(got.Object, status); err != nil { |
| 683 |
t.Fatal(err) |
| 684 |
} |
| 685 |
if status.Kind != "Status" || status.APIVersion != "v1" || status.Code != 500 || status.Status != "Failure" || !strings.Contains(status.Message, "we got an error") { |
| 686 |
t.Fatalf("error: %#v", status) |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
func TestWatchHTTPErrorsBeforeServe(t *testing.T) { |
| 691 |
watcher := watch.NewFake() |
| 692 |
timeoutCh := make(chan time.Time) |
| 693 |
done := make(chan struct{}) |
| 694 |
|
| 695 |
info, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), runtime.ContentTypeJSON) |
| 696 |
if !ok || info.StreamSerializer == nil { |
| 697 |
t.Fatal(info) |
| 698 |
} |
| 699 |
serializer := info.StreamSerializer |
| 700 |
|
| 701 |
// Setup a new watchserver |
| 702 |
watchServer := &handlers.WatchServer{ |
| 703 |
Scope: &handlers.RequestScope{ |
| 704 |
Serializer: runtime.NewSimpleNegotiatedSerializer(info), |
| 705 |
Kind: testGroupVersion.WithKind("test"), |
| 706 |
}, |
| 707 |
Watching: watcher, |
| 708 |
|
| 709 |
MediaType: "testcase/json", |
| 710 |
Framer: serializer.Framer, |
| 711 |
Encoder: newCodec, |
| 712 |
EmbeddedEncoder: newCodec, |
| 713 |
|
| 714 |
TimeoutFactory: &fakeTimeoutFactory{timeoutCh, done}, |
| 715 |
} |
| 716 |
|
| 717 |
errStatus := errors.NewInternalError(fmt.Errorf("we got an error")) |
| 718 |
|
| 719 |
s := httptest.NewServer(serveWatch(watcher, watchServer, errStatus)) |
| 720 |
defer s.Close() |
| 721 |
|
| 722 |
// Setup a client |
| 723 |
dest, _ := url.Parse(s.URL) |
| 724 |
dest.Path = "/" + prefix + "/" + newGroupVersion.Group + "/" + newGroupVersion.Version + "/simple" |
| 725 |
dest.RawQuery = "watch=true" |
| 726 |
|
| 727 |
req, _ := http.NewRequest("GET", dest.String(), nil) |
| 728 |
client := http.Client{} |
| 729 |
resp, err := client.Do(req) |
| 730 |
if err != nil { |
| 731 |
t.Fatalf("Unexpected error: %v", err) |
| 732 |
} |
| 733 |
|
| 734 |
// We had already got an error before watch serve started |
| 735 |
decoder := json.NewDecoder(resp.Body) |
| 736 |
var status *metav1.Status |
| 737 |
err = decoder.Decode(&status) |
| 738 |
if err != nil { |
| 739 |
t.Fatalf("Unexpected error: %v", err) |
| 740 |
} |
| 741 |
if status.Kind != "Status" || status.APIVersion != "v1" || status.Code != 500 || status.Status != "Failure" || !strings.Contains(status.Message, "we got an error") { |
| 742 |
t.Fatalf("error: %#v", status) |
| 743 |
} |
| 744 |
|
| 745 |
// check for leaks |
| 746 |
if !watcher.IsStopped() { |
| 747 |
t.Errorf("Leaked watcher goruntine after request done") |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
func TestWatchHTTPDynamicClientErrors(t *testing.T) { |
| 752 |
watcher := watch.NewFake() |
| 753 |
timeoutCh := make(chan time.Time) |
| 754 |
done := make(chan struct{}) |
| 755 |
|
| 756 |
info, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), runtime.ContentTypeJSON) |
| 757 |
if !ok || info.StreamSerializer == nil { |
| 758 |
t.Fatal(info) |
| 759 |
} |
| 760 |
serializer := info.StreamSerializer |
| 761 |
|
| 762 |
// Setup a new watchserver |
| 763 |
watchServer := &handlers.WatchServer{ |
| 764 |
Scope: &handlers.RequestScope{}, |
| 765 |
Watching: watcher, |
| 766 |
|
| 767 |
MediaType: "testcase/json", |
| 768 |
Framer: serializer.Framer, |
| 769 |
Encoder: newCodec, |
| 770 |
EmbeddedEncoder: newCodec, |
| 771 |
|
| 772 |
TimeoutFactory: &fakeTimeoutFactory{timeoutCh, done}, |
| 773 |
} |
| 774 |
|
| 775 |
s := httptest.NewServer(serveWatch(watcher, watchServer, nil)) |
| 776 |
defer s.Close() |
| 777 |
defer s.CloseClientConnections() |
| 778 |
|
| 779 |
client := dynamic.NewForConfigOrDie(&restclient.Config{ |
| 780 |
Host: s.URL, |
| 781 |
APIPath: "/" + prefix, |
| 782 |
}).Resource(newGroupVersion.WithResource("simple")) |
| 783 |
|
| 784 |
_, err := client.Watch(context.TODO(), metav1.ListOptions{}) |
| 785 |
if err == nil { |
| 786 |
t.Fatal(err) |
| 787 |
} |
| 788 |
if err.Error() != "no stream serializers registered for testcase/json" { |
| 789 |
t.Fatalf("unexpected error: %v", err) |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
func TestWatchHTTPTimeout(t *testing.T) { |
| 794 |
watcher := watch.NewFake() |
| 795 |
timeoutCh := make(chan time.Time) |
| 796 |
done := make(chan struct{}) |
| 797 |
|
| 798 |
info, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), runtime.ContentTypeJSON) |
| 799 |
if !ok || info.StreamSerializer == nil { |
| 800 |
t.Fatal(info) |
| 801 |
} |
| 802 |
serializer := info.StreamSerializer |
| 803 |
|
| 804 |
// Setup a new watchserver |
| 805 |
watchServer := &handlers.WatchServer{ |
| 806 |
Scope: &handlers.RequestScope{}, |
| 807 |
Watching: watcher, |
| 808 |
|
| 809 |
MediaType: "testcase/json", |
| 810 |
Framer: serializer.Framer, |
| 811 |
Encoder: newCodec, |
| 812 |
EmbeddedEncoder: newCodec, |
| 813 |
|
| 814 |
TimeoutFactory: &fakeTimeoutFactory{timeoutCh, done}, |
| 815 |
} |
| 816 |
|
| 817 |
s := httptest.NewServer(serveWatch(watcher, watchServer, nil)) |
| 818 |
defer s.Close() |
| 819 |
|
| 820 |
// Setup a client |
| 821 |
dest, _ := url.Parse(s.URL) |
| 822 |
dest.Path = "/" + prefix + "/" + newGroupVersion.Group + "/" + newGroupVersion.Version + "/simple" |
| 823 |
dest.RawQuery = "watch=true" |
| 824 |
|
| 825 |
req, _ := http.NewRequest("GET", dest.String(), nil) |
| 826 |
client := http.Client{} |
| 827 |
resp, err := client.Do(req) |
| 828 |
if err != nil { |
| 829 |
t.Fatalf("Unexpected error: %v", err) |
| 830 |
} |
| 831 |
watcher.Add(&apitesting.Simple{TypeMeta: metav1.TypeMeta{APIVersion: newGroupVersion.String()}}) |
| 832 |
|
| 833 |
// Make sure we can actually watch an endpoint |
| 834 |
decoder := json.NewDecoder(resp.Body) |
| 835 |
var got watchJSON |
| 836 |
err = decoder.Decode(&got) |
| 837 |
if err != nil { |
| 838 |
t.Fatalf("Unexpected error: %v", err) |
| 839 |
} |
| 840 |
|
| 841 |
// Timeout and check for leaks |
| 842 |
close(timeoutCh) |
| 843 |
select { |
| 844 |
case <-done: |
| 845 |
eventCh := watcher.ResultChan() |
| 846 |
select { |
| 847 |
case _, opened := <-eventCh: |
| 848 |
if opened { |
| 849 |
t.Errorf("Watcher received unexpected event") |
| 850 |
} |
| 851 |
if !watcher.IsStopped() { |
| 852 |
t.Errorf("Watcher is not stopped") |
| 853 |
} |
| 854 |
case <-time.After(wait.ForeverTestTimeout): |
| 855 |
t.Errorf("Leaked watch on timeout") |
| 856 |
} |
| 857 |
case <-time.After(wait.ForeverTestTimeout): |
| 858 |
t.Errorf("Failed to stop watcher after %s of timeout signal", wait.ForeverTestTimeout.String()) |
| 859 |
} |
| 860 |
|
| 861 |
// Make sure we can't receive any more events through the timeout watch |
| 862 |
err = decoder.Decode(&got) |
| 863 |
if err != io.EOF { |
| 864 |
t.Errorf("Unexpected non-error") |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
// BenchmarkWatchHTTP measures the cost of serving a watch. |
| 869 |
func BenchmarkWatchHTTP(b *testing.B) { |
| 870 |
items := benchmarkItems(b) |
| 871 |
|
| 872 |
// use ASCII names to capture the cost of handling ASCII only self-links |
| 873 |
for i := range items { |
| 874 |
item := &items[i] |
| 875 |
item.Namespace = fmt.Sprintf("namespace-%d", i) |
| 876 |
item.Name = fmt.Sprintf("reasonable-name-%d", i) |
| 877 |
} |
| 878 |
|
| 879 |
runWatchHTTPBenchmark(b, toObjectSlice(items), "") |
| 880 |
} |
| 881 |
|
| 882 |
func BenchmarkWatchHTTP_UTF8(b *testing.B) { |
| 883 |
items := benchmarkItems(b) |
| 884 |
|
| 885 |
// use UTF names to capture the cost of handling UTF-8 escaping in self-links |
| 886 |
for i := range items { |
| 887 |
item := &items[i] |
| 888 |
item.Namespace = fmt.Sprintf("躀痢疈蜧í柢-%d", i) |
| 889 |
item.Name = fmt.Sprintf("翏Ŏ熡韐-%d", i) |
| 890 |
} |
| 891 |
|
| 892 |
runWatchHTTPBenchmark(b, toObjectSlice(items), "") |
| 893 |
} |
| 894 |
|
| 895 |
func toObjectSlice(in []example.Pod) []runtime.Object { |
| 896 |
var res []runtime.Object |
| 897 |
for _, pod := range in { |
| 898 |
res = append(res, &pod) |
| 899 |
} |
| 900 |
return res |
| 901 |
} |
| 902 |
|
| 903 |
func runWatchHTTPBenchmark(b *testing.B, items []runtime.Object, contentType string) { |
| 904 |
simpleStorage := &SimpleRESTStorage{} |
| 905 |
handler := handle(map[string]rest.Storage{"simples": simpleStorage}) |
| 906 |
server := httptest.NewServer(handler) |
| 907 |
defer server.Close() |
| 908 |
client := http.Client{} |
| 909 |
|
| 910 |
dest, _ := url.Parse(server.URL) |
| 911 |
dest.Path = "/" + prefix + "/" + newGroupVersion.Group + "/" + newGroupVersion.Version + "/watch/simples" |
| 912 |
dest.RawQuery = "" |
| 913 |
|
| 914 |
request, err := http.NewRequest("GET", dest.String(), nil) |
| 915 |
if err != nil { |
| 916 |
b.Fatalf("unexpected error: %v", err) |
| 917 |
} |
| 918 |
request.Header.Add("Accept", contentType) |
| 919 |
|
| 920 |
response, err := client.Do(request) |
| 921 |
if err != nil { |
| 922 |
b.Fatalf("unexpected error: %v", err) |
| 923 |
} |
| 924 |
if response.StatusCode != http.StatusOK { |
| 925 |
b.Fatalf("Unexpected response %#v", response) |
| 926 |
} |
| 927 |
|
| 928 |
wg := sync.WaitGroup{} |
| 929 |
wg.Add(1) |
| 930 |
go func() { |
| 931 |
defer response.Body.Close() |
| 932 |
if _, err := io.Copy(ioutil.Discard, response.Body); err != nil { |
| 933 |
b.Error(err) |
| 934 |
} |
| 935 |
wg.Done() |
| 936 |
}() |
| 937 |
|
| 938 |
actions := []watch.EventType{watch.Added, watch.Modified, watch.Deleted} |
| 939 |
|
| 940 |
b.ResetTimer() |
| 941 |
for i := 0; i < b.N; i++ { |
| 942 |
simpleStorage.fakeWatch.Action(actions[i%len(actions)], items[i%len(items)]) |
| 943 |
} |
| 944 |
simpleStorage.fakeWatch.Stop() |
| 945 |
wg.Wait() |
| 946 |
b.StopTimer() |
| 947 |
} |
| 948 |
|
| 949 |
// BenchmarkWatchWebsocket measures the cost of serving a watch. |
| 950 |
func BenchmarkWatchWebsocket(b *testing.B) { |
| 951 |
items := benchmarkItems(b) |
| 952 |
|
| 953 |
simpleStorage := &SimpleRESTStorage{} |
| 954 |
handler := handle(map[string]rest.Storage{"simples": simpleStorage}) |
| 955 |
server := httptest.NewServer(handler) |
| 956 |
defer server.Close() |
| 957 |
|
| 958 |
dest, _ := url.Parse(server.URL) |
| 959 |
dest.Scheme = "ws" // Required by websocket, though the server never sees it. |
| 960 |
dest.Path = "/" + prefix + "/" + newGroupVersion.Group + "/" + newGroupVersion.Version + "/watch/simples" |
| 961 |
dest.RawQuery = "" |
| 962 |
|
| 963 |
ws, err := websocket.Dial(dest.String(), "", "http://localhost") |
| 964 |
if err != nil { |
| 965 |
b.Fatalf("unexpected error: %v", err) |
| 966 |
} |
| 967 |
|
| 968 |
wg := sync.WaitGroup{} |
| 969 |
wg.Add(1) |
| 970 |
go func() { |
| 971 |
defer ws.Close() |
| 972 |
if _, err := io.Copy(ioutil.Discard, ws); err != nil { |
| 973 |
b.Error(err) |
| 974 |
} |
| 975 |
wg.Done() |
| 976 |
}() |
| 977 |
|
| 978 |
actions := []watch.EventType{watch.Added, watch.Modified, watch.Deleted} |
| 979 |
|
| 980 |
b.ResetTimer() |
| 981 |
for i := 0; i < b.N; i++ { |
| 982 |
simpleStorage.fakeWatch.Action(actions[i%len(actions)], &items[i%len(items)]) |
| 983 |
} |
| 984 |
simpleStorage.fakeWatch.Stop() |
| 985 |
wg.Wait() |
| 986 |
b.StopTimer() |
| 987 |
} |
| 988 |
|
| 989 |
// BenchmarkWatchProtobuf measures the cost of serving a watch. |
| 990 |
func BenchmarkWatchProtobuf(b *testing.B) { |
| 991 |
items := benchmarkItems(b) |
| 992 |
|
| 993 |
runWatchHTTPBenchmark(b, toObjectSlice(items), "application/vnd.kubernetes.protobuf") |
| 994 |
} |
| 995 |
|
| 996 |
type fakeCachingObject struct { |
| 997 |
obj runtime.Object |
| 998 |
|
| 999 |
once sync.Once |
| 1000 |
raw []byte |
| 1001 |
err error |
| 1002 |
} |
| 1003 |
|
| 1004 |
func (f *fakeCachingObject) CacheEncode(_ runtime.Identifier, encode func(runtime.Object, io.Writer) error, w io.Writer) error { |
| 1005 |
f.once.Do(func() { |
| 1006 |
buffer := bytes.NewBuffer(nil) |
| 1007 |
f.err = encode(f.obj, buffer) |
| 1008 |
f.raw = buffer.Bytes() |
| 1009 |
}) |
| 1010 |
|
| 1011 |
if f.err != nil { |
| 1012 |
return f.err |
| 1013 |
} |
| 1014 |
|
| 1015 |
_, err := w.Write(f.raw) |
| 1016 |
return err |
| 1017 |
} |
| 1018 |
|
| 1019 |
func (f *fakeCachingObject) GetObject() runtime.Object { |
| 1020 |
return f.obj |
| 1021 |
} |
| 1022 |
|
| 1023 |
func (f *fakeCachingObject) GetObjectKind() schema.ObjectKind { |
| 1024 |
return f.obj.GetObjectKind() |
| 1025 |
} |
| 1026 |
|
| 1027 |
func (f *fakeCachingObject) DeepCopyObject() runtime.Object { |
| 1028 |
return &fakeCachingObject{obj: f.obj.DeepCopyObject()} |
| 1029 |
} |
| 1030 |
|
| 1031 |
var _ runtime.CacheableObject = &fakeCachingObject{} |
| 1032 |
var _ runtime.Object = &fakeCachingObject{} |
| 1033 |
|
| 1034 |
func wrapCachingObject(in []example.Pod) []runtime.Object { |
| 1035 |
var res []runtime.Object |
| 1036 |
for _, pod := range in { |
| 1037 |
res = append(res, &fakeCachingObject{obj: &pod}) |
| 1038 |
} |
| 1039 |
return res |
| 1040 |
} |
| 1041 |
|
| 1042 |
func BenchmarkWatchCachingObjectJSON(b *testing.B) { |
| 1043 |
items := benchmarkItems(b) |
| 1044 |
|
| 1045 |
runWatchHTTPBenchmark(b, wrapCachingObject(items), "") |
| 1046 |
} |
| 1047 |
|
| 1048 |
func BenchmarkWatchCachingObjectProtobuf(b *testing.B) { |
| 1049 |
items := benchmarkItems(b) |
| 1050 |
|
| 1051 |
runWatchHTTPBenchmark(b, wrapCachingObject(items), "application/vnd.kubernetes.protobuf") |
| 1052 |
} |