Debian Codegraph @

pkg/endpoints/handlers/response.go

raw
1 /*
2 Copyright 2017 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 handlers
18
19 import (
20 "context"
21 "encoding/base64"
22 "encoding/json"
23 "fmt"
24 "io"
25 "net/http"
26 "reflect"
27
28 "k8s.io/apimachinery/pkg/api/errors"
29 "k8s.io/apimachinery/pkg/api/meta"
30 metainternalversionscheme "k8s.io/apimachinery/pkg/apis/meta/internalversion/scheme"
31 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32 metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
33 "k8s.io/apimachinery/pkg/apis/meta/v1beta1/validation"
34 "k8s.io/apimachinery/pkg/runtime"
35 "k8s.io/apimachinery/pkg/runtime/schema"
36 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
37 "k8s.io/apimachinery/pkg/watch"
38 "k8s.io/apiserver/pkg/endpoints/handlers/negotiation"
39 "k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
40 "k8s.io/apiserver/pkg/endpoints/metrics"
41 endpointsrequest "k8s.io/apiserver/pkg/endpoints/request"
42 "k8s.io/apiserver/pkg/storage"
43 "k8s.io/apiserver/pkg/util/apihelpers"
44 "k8s.io/klog/v2"
45 )
46
47 // watchEmbeddedEncoder performs encoding of the embedded object.
48 //
49 // NOTE: watchEmbeddedEncoder is NOT thread-safe.
50 type watchEmbeddedEncoder struct {
51 encoder runtime.Encoder
52
53 ctx context.Context
54
55 // target, if non-nil, configures transformation type.
56 // The other options are ignored if target is nil.
57 target *schema.GroupVersionKind
58 tableOptions *metav1.TableOptions
59 scope *RequestScope
60
61 // identifier of the encoder, computed lazily
62 identifier runtime.Identifier
63 }
64
65 func newWatchEmbeddedEncoder(ctx context.Context, encoder runtime.Encoder, target *schema.GroupVersionKind, tableOptions *metav1.TableOptions, scope *RequestScope) *watchEmbeddedEncoder {
66 return &watchEmbeddedEncoder{
67 encoder: encoder,
68 ctx: ctx,
69 target: target,
70 tableOptions: tableOptions,
71 scope: scope,
72 }
73 }
74
75 // Encode implements runtime.Encoder interface.
76 func (e *watchEmbeddedEncoder) Encode(obj runtime.Object, w io.Writer) error {
77 if co, ok := obj.(runtime.CacheableObject); ok {
78 return co.CacheEncode(e.Identifier(), e.doEncode, w)
79 }
80 return e.doEncode(obj, w)
81 }
82
83 func (e *watchEmbeddedEncoder) doEncode(obj runtime.Object, w io.Writer) error {
84 result, err := doTransformObject(e.ctx, obj, e.tableOptions, e.target, e.scope)
85 if err != nil {
86 utilruntime.HandleError(fmt.Errorf("failed to transform object %v: %v", reflect.TypeOf(obj), err))
87 result = obj
88 }
89
90 // When we are tranforming to a table, use the original table options when
91 // we should print headers only on the first object - headers should be
92 // omitted on subsequent events.
93 if e.tableOptions != nil && !e.tableOptions.NoHeaders {
94 e.tableOptions.NoHeaders = true
95 // With options change, we should recompute the identifier.
96 // Clearing this will trigger lazy recompute when needed.
97 e.identifier = ""
98 }
99
100 return e.encoder.Encode(result, w)
101 }
102
103 // Identifier implements runtime.Encoder interface.
104 func (e *watchEmbeddedEncoder) Identifier() runtime.Identifier {
105 if e.identifier == "" {
106 e.identifier = e.embeddedIdentifier()
107 }
108 return e.identifier
109 }
110
111 type watchEmbeddedEncoderIdentifier struct {
112 Name string `json:"name,omitempty"`
113 Encoder string `json:"encoder,omitempty"`
114 Target string `json:"target,omitempty"`
115 Options metav1.TableOptions `json:"options,omitempty"`
116 NoHeaders bool `json:"noHeaders,omitempty"`
117 }
118
119 func (e *watchEmbeddedEncoder) embeddedIdentifier() runtime.Identifier {
120 if e.target == nil {
121 // If no conversion is performed, we effective only use
122 // the embedded identifier.
123 return e.encoder.Identifier()
124 }
125 identifier := watchEmbeddedEncoderIdentifier{
126 Name: "watch-embedded",
127 Encoder: string(e.encoder.Identifier()),
128 Target: e.target.String(),
129 }
130 if e.target.Kind == "Table" && e.tableOptions != nil {
131 identifier.Options = *e.tableOptions
132 identifier.NoHeaders = e.tableOptions.NoHeaders
133 }
134
135 result, err := json.Marshal(identifier)
136 if err != nil {
137 klog.Fatalf("Failed marshaling identifier for watchEmbeddedEncoder: %v", err)
138 }
139 return runtime.Identifier(result)
140 }
141
142 // watchEncoder performs encoding of the watch events.
143 //
144 // NOTE: watchEncoder is NOT thread-safe.
145 type watchEncoder struct {
146 ctx context.Context
147 kind schema.GroupVersionKind
148 embeddedEncoder runtime.Encoder
149 encoder runtime.Encoder
150 framer io.Writer
151
152 watchListTransformerFn watchListTransformerFunction
153
154 buffer runtime.Splice
155 eventBuffer runtime.Splice
156
157 currentEmbeddedIdentifier runtime.Identifier
158 identifiers map[watch.EventType]runtime.Identifier
159 }
160
161 func newWatchEncoder(ctx context.Context, kind schema.GroupVersionKind, embeddedEncoder runtime.Encoder, encoder runtime.Encoder, framer io.Writer, watchListTransformerFn watchListTransformerFunction) *watchEncoder {
162 return &watchEncoder{
163 ctx: ctx,
164 kind: kind,
165 embeddedEncoder: embeddedEncoder,
166 encoder: encoder,
167 framer: framer,
168 watchListTransformerFn: watchListTransformerFn,
169 buffer: runtime.NewSpliceBuffer(),
170 eventBuffer: runtime.NewSpliceBuffer(),
171 }
172 }
173
174 // Encode encodes a given watch event.
175 // NOTE: if events object is implementing the CacheableObject interface,
176 //
177 // the serialized version is cached in that object [not the event itself].
178 func (e *watchEncoder) Encode(event watch.Event) error {
179 encodeFunc := func(obj runtime.Object, w io.Writer) error {
180 return e.doEncode(obj, event, w)
181 }
182 if event.Type == watch.Bookmark {
183 // Bookmark objects are small, and we don't yet support serialization for them.
184 // Additionally, we need to additionally transform them to support watch-list feature
185 event = e.watchListTransformerFn(event)
186 return encodeFunc(event.Object, e.framer)
187 }
188 if co, ok := event.Object.(runtime.CacheableObject); ok {
189 return co.CacheEncode(e.identifier(event.Type), encodeFunc, e.framer)
190 }
191 return encodeFunc(event.Object, e.framer)
192 }
193
194 func (e *watchEncoder) doEncode(obj runtime.Object, event watch.Event, w io.Writer) error {
195 defer e.buffer.Reset()
196
197 if err := e.embeddedEncoder.Encode(obj, e.buffer); err != nil {
198 return fmt.Errorf("unable to encode watch object %T: %v", obj, err)
199 }
200
201 // ContentType is not required here because we are defaulting to the serializer type.
202 outEvent := &metav1.WatchEvent{
203 Type: string(event.Type),
204 Object: runtime.RawExtension{Raw: e.buffer.Bytes()},
205 }
206 metrics.WatchEventsSizes.WithContext(e.ctx).WithLabelValues(e.kind.Group, e.kind.Version, e.kind.Kind).Observe(float64(len(outEvent.Object.Raw)))
207
208 defer e.eventBuffer.Reset()
209 if err := e.encoder.Encode(outEvent, e.eventBuffer); err != nil {
210 return fmt.Errorf("unable to encode watch object %T: %v (%#v)", outEvent, err, e)
211 }
212
213 _, err := w.Write(e.eventBuffer.Bytes())
214 return err
215 }
216
217 type watchEncoderIdentifier struct {
218 Name string `json:"name,omitempty"`
219 EmbeddedEncoder string `json:"embeddedEncoder,omitempty"`
220 Encoder string `json:"encoder,omitempty"`
221 EventType string `json:"eventType,omitempty"`
222 }
223
224 func (e *watchEncoder) identifier(eventType watch.EventType) runtime.Identifier {
225 // We need to take into account that in embeddedEncoder includes table
226 // transformer, then its identifier is dynamic. As a result, whenever
227 // the identifier of embeddedEncoder changes, we need to invalidate the
228 // whole identifiers cache.
229 // TODO(wojtek-t): Can we optimize it somehow?
230 if e.currentEmbeddedIdentifier != e.embeddedEncoder.Identifier() {
231 e.currentEmbeddedIdentifier = e.embeddedEncoder.Identifier()
232 e.identifiers = map[watch.EventType]runtime.Identifier{}
233 }
234 if _, ok := e.identifiers[eventType]; !ok {
235 e.identifiers[eventType] = e.typeIdentifier(eventType)
236 }
237 return e.identifiers[eventType]
238 }
239
240 func (e *watchEncoder) typeIdentifier(eventType watch.EventType) runtime.Identifier {
241 // The eventType is a non-standard pattern. This is coming from the fact
242 // that we're effectively serializing the whole watch event, but storing
243 // it in serializations of the Object within the watch event.
244 identifier := watchEncoderIdentifier{
245 Name: "watch",
246 EmbeddedEncoder: string(e.embeddedEncoder.Identifier()),
247 Encoder: string(e.encoder.Identifier()),
248 EventType: string(eventType),
249 }
250
251 result, err := json.Marshal(identifier)
252 if err != nil {
253 klog.Fatalf("Failed marshaling identifier for watchEncoder: %v", err)
254 }
255 return runtime.Identifier(result)
256 }
257
258 // doTransformResponseObject is used for handling all requests, including watch.
259 func doTransformObject(ctx context.Context, obj runtime.Object, opts interface{}, target *schema.GroupVersionKind, scope *RequestScope) (runtime.Object, error) {
260 if _, ok := obj.(*metav1.Status); ok {
261 return obj, nil
262 }
263
264 switch {
265 case target == nil:
266 // If we ever change that from a no-op, the identifier of
267 // the watchEmbeddedEncoder has to be adjusted accordingly.
268 return obj, nil
269
270 case target.Kind == "PartialObjectMetadata":
271 return asPartialObjectMetadata(obj, target.GroupVersion())
272
273 case target.Kind == "PartialObjectMetadataList":
274 return asPartialObjectMetadataList(obj, target.GroupVersion())
275
276 case target.Kind == "Table":
277 options, ok := opts.(*metav1.TableOptions)
278 if !ok {
279 return nil, fmt.Errorf("unexpected TableOptions, got %T", opts)
280 }
281 return asTable(ctx, obj, options, scope, target.GroupVersion())
282
283 default:
284 accepted, _ := negotiation.MediaTypesForSerializer(apihelpers.GetMetaInternalVersionCodecs())
285 err := negotiation.NewNotAcceptableError(accepted)
286 return nil, err
287 }
288 }
289
290 // optionsForTransform will load and validate any additional query parameter options for
291 // a conversion or return an error.
292 func optionsForTransform(mediaType negotiation.MediaTypeOptions, req *http.Request) (interface{}, error) {
293 switch target := mediaType.Convert; {
294 case target == nil:
295 case target.Kind == "Table" && (target.GroupVersion() == metav1beta1.SchemeGroupVersion || target.GroupVersion() == metav1.SchemeGroupVersion):
296 opts := &metav1.TableOptions{}
297 if err := metainternalversionscheme.ParameterCodec.DecodeParameters(req.URL.Query(), metav1.SchemeGroupVersion, opts); err != nil {
298 return nil, err
299 }
300 switch errs := validation.ValidateTableOptions(opts); len(errs) {
301 case 0:
302 return opts, nil
303 case 1:
304 return nil, errors.NewBadRequest(fmt.Sprintf("Unable to convert to Table as requested: %v", errs[0].Error()))
305 default:
306 return nil, errors.NewBadRequest(fmt.Sprintf("Unable to convert to Table as requested: %v", errs))
307 }
308 }
309 return nil, nil
310 }
311
312 // targetEncodingForTransform returns the appropriate serializer for the input media type
313 func targetEncodingForTransform(scope *RequestScope, mediaType negotiation.MediaTypeOptions, req *http.Request) (schema.GroupVersionKind, runtime.NegotiatedSerializer, bool) {
314 switch target := mediaType.Convert; {
315 case target == nil:
316 case (target.Kind == "PartialObjectMetadata" || target.Kind == "PartialObjectMetadataList" || target.Kind == "Table") &&
317 (target.GroupVersion() == metav1beta1.SchemeGroupVersion || target.GroupVersion() == metav1.SchemeGroupVersion):
318 return *target, apihelpers.GetMetaInternalVersionCodecs(), true
319 }
320 return scope.Kind, scope.Serializer, false
321 }
322
323 // transformResponseObject takes an object loaded from storage and performs any necessary transformations.
324 // Will write the complete response object.
325 // transformResponseObject is used only for handling non-streaming requests.
326 func transformResponseObject(ctx context.Context, scope *RequestScope, req *http.Request, w http.ResponseWriter, statusCode int, mediaType negotiation.MediaTypeOptions, result runtime.Object) {
327 options, err := optionsForTransform(mediaType, req)
328 if err != nil {
329 scope.err(err, w, req)
330 return
331 }
332
333 // ensure that for empty lists we don't return <nil> items.
334 // This is safe to modify without deep-copying the object, as
335 // List objects themselves are never cached.
336 if meta.IsListType(result) && meta.LenList(result) == 0 {
337 if err := meta.SetList(result, []runtime.Object{}); err != nil {
338 scope.err(err, w, req)
339 return
340 }
341 }
342
343 var obj runtime.Object
344 do := func() {
345 obj, err = doTransformObject(ctx, result, options, mediaType.Convert, scope)
346 }
347 endpointsrequest.TrackTransformResponseObjectLatency(ctx, do)
348
349 if err != nil {
350 scope.err(err, w, req)
351 return
352 }
353 kind, serializer, _ := targetEncodingForTransform(scope, mediaType, req)
354 responsewriters.WriteObjectNegotiated(serializer, scope, kind.GroupVersion(), w, req, statusCode, obj, false)
355 }
356
357 // errNotAcceptable indicates Accept negotiation has failed
358 type errNotAcceptable struct {
359 message string
360 }
361
362 func newNotAcceptableError(message string) error {
363 return errNotAcceptable{message}
364 }
365
366 func (e errNotAcceptable) Error() string {
367 return e.message
368 }
369
370 func (e errNotAcceptable) Status() metav1.Status {
371 return metav1.Status{
372 Status: metav1.StatusFailure,
373 Code: http.StatusNotAcceptable,
374 Reason: metav1.StatusReason("NotAcceptable"),
375 Message: e.Error(),
376 }
377 }
378
379 func asTable(ctx context.Context, result runtime.Object, opts *metav1.TableOptions, scope *RequestScope, groupVersion schema.GroupVersion) (runtime.Object, error) {
380 switch groupVersion {
381 case metav1beta1.SchemeGroupVersion, metav1.SchemeGroupVersion:
382 default:
383 return nil, newNotAcceptableError(fmt.Sprintf("no Table exists in group version %s", groupVersion))
384 }
385
386 obj, err := scope.TableConvertor.ConvertToTable(ctx, result, opts)
387 if err != nil {
388 return nil, err
389 }
390
391 table := (*metav1.Table)(obj)
392
393 for i := range table.Rows {
394 item := &table.Rows[i]
395 switch opts.IncludeObject {
396 case metav1.IncludeObject:
397 item.Object.Object, err = scope.Convertor.ConvertToVersion(item.Object.Object, scope.Kind.GroupVersion())
398 if err != nil {
399 return nil, err
400 }
401 // TODO: rely on defaulting for the value here?
402 case metav1.IncludeMetadata, "":
403 m, err := meta.Accessor(item.Object.Object)
404 if err != nil {
405 return nil, err
406 }
407 // TODO: turn this into an internal type and do conversion in order to get object kind automatically set?
408 partial := meta.AsPartialObjectMetadata(m)
409 partial.GetObjectKind().SetGroupVersionKind(groupVersion.WithKind("PartialObjectMetadata"))
410 item.Object.Object = partial
411 case metav1.IncludeNone:
412 item.Object.Object = nil
413 default:
414 err = errors.NewBadRequest(fmt.Sprintf("unrecognized includeObject value: %q", opts.IncludeObject))
415 return nil, err
416 }
417 }
418
419 return table, nil
420 }
421
422 func asPartialObjectMetadata(result runtime.Object, groupVersion schema.GroupVersion) (runtime.Object, error) {
423 if meta.IsListType(result) {
424 err := newNotAcceptableError(fmt.Sprintf("you requested PartialObjectMetadata, but the requested object is a list (%T)", result))
425 return nil, err
426 }
427 switch groupVersion {
428 case metav1beta1.SchemeGroupVersion, metav1.SchemeGroupVersion:
429 default:
430 return nil, newNotAcceptableError(fmt.Sprintf("no PartialObjectMetadataList exists in group version %s", groupVersion))
431 }
432 m, err := meta.Accessor(result)
433 if err != nil {
434 return nil, err
435 }
436 partial := meta.AsPartialObjectMetadata(m)
437 partial.GetObjectKind().SetGroupVersionKind(groupVersion.WithKind("PartialObjectMetadata"))
438 return partial, nil
439 }
440
441 func asPartialObjectMetadataList(result runtime.Object, groupVersion schema.GroupVersion) (runtime.Object, error) {
442 li, ok := result.(metav1.ListInterface)
443 if !ok {
444 return nil, newNotAcceptableError(fmt.Sprintf("you requested PartialObjectMetadataList, but the requested object is not a list (%T)", result))
445 }
446
447 gvk := groupVersion.WithKind("PartialObjectMetadata")
448 switch {
449 case groupVersion == metav1beta1.SchemeGroupVersion:
450 list := &metav1beta1.PartialObjectMetadataList{}
451 err := meta.EachListItem(result, func(obj runtime.Object) error {
452 m, err := meta.Accessor(obj)
453 if err != nil {
454 return err
455 }
456 partial := meta.AsPartialObjectMetadata(m)
457 partial.GetObjectKind().SetGroupVersionKind(gvk)
458 list.Items = append(list.Items, *partial)
459 return nil
460 })
461 if err != nil {
462 return nil, err
463 }
464 list.ResourceVersion = li.GetResourceVersion()
465 list.Continue = li.GetContinue()
466 list.RemainingItemCount = li.GetRemainingItemCount()
467 return list, nil
468
469 case groupVersion == metav1.SchemeGroupVersion:
470 list := &metav1.PartialObjectMetadataList{}
471 err := meta.EachListItem(result, func(obj runtime.Object) error {
472 m, err := meta.Accessor(obj)
473 if err != nil {
474 return err
475 }
476 partial := meta.AsPartialObjectMetadata(m)
477 partial.GetObjectKind().SetGroupVersionKind(gvk)
478 list.Items = append(list.Items, *partial)
479 return nil
480 })
481 if err != nil {
482 return nil, err
483 }
484 list.ResourceVersion = li.GetResourceVersion()
485 list.Continue = li.GetContinue()
486 list.RemainingItemCount = li.GetRemainingItemCount()
487 return list, nil
488
489 default:
490 return nil, newNotAcceptableError(fmt.Sprintf("no PartialObjectMetadataList exists in group version %s", groupVersion))
491 }
492 }
493
494 // watchListTransformerFunction an optional function
495 // applied to watchlist bookmark events that transforms
496 // the embedded object before sending it to a client.
497 type watchListTransformerFunction func(watch.Event) watch.Event
498
499 // watchListTransformer performs transformation of
500 // a special watchList bookmark event.
501 //
502 // The bookmark is annotated with InitialEventsListBlueprintAnnotationKey
503 // and contains an empty, versioned list that we must encode in the requested format
504 // (e.g., protobuf, JSON, CBOR) and then store as a base64-encoded string.
505 type watchListTransformer struct {
506 initialEventsListBlueprint runtime.Object
507 targetGVK *schema.GroupVersionKind
508 negotiatedEncoder runtime.Encoder
509 buffer runtime.Splice
510 }
511
512 // createWatchListTransformerIfRequested returns a transformer function for watchlist bookmark event.
513 func newWatchListTransformer(initialEventsListBlueprint runtime.Object, targetGVK *schema.GroupVersionKind, negotiatedEncoder runtime.Encoder) *watchListTransformer {
514 return &watchListTransformer{
515 initialEventsListBlueprint: initialEventsListBlueprint,
516 targetGVK: targetGVK,
517 negotiatedEncoder: negotiatedEncoder,
518 buffer: runtime.NewSpliceBuffer(),
519 }
520 }
521
522 func (e *watchListTransformer) transform(event watch.Event) watch.Event {
523 if e.initialEventsListBlueprint == nil {
524 return event
525 }
526 hasAnnotation, err := storage.HasInitialEventsEndBookmarkAnnotation(event.Object)
527 if err != nil {
528 return newWatchEventErrorFor(err)
529 }
530 if !hasAnnotation {
531 return event
532 }
533
534 if err = e.encodeInitialEventsListBlueprint(event.Object); err != nil {
535 return newWatchEventErrorFor(err)
536 }
537
538 return event
539 }
540
541 func (e *watchListTransformer) encodeInitialEventsListBlueprint(object runtime.Object) error {
542 initialEventsListBlueprint, err := e.transformInitialEventsListBlueprint()
543 if err != nil {
544 return err
545 }
546
547 defer e.buffer.Reset()
548 if err = e.negotiatedEncoder.Encode(initialEventsListBlueprint, e.buffer); err != nil {
549 return err
550 }
551 encodedInitialEventsListBlueprint := e.buffer.Bytes()
552
553 // the storage layer creates a deep copy of the obj before modifying it.
554 // since the object has the annotation, we can modify it directly.
555 objectMeta, err := meta.Accessor(object)
556 if err != nil {
557 return err
558 }
559 annotations := objectMeta.GetAnnotations()
560 annotations[metav1.InitialEventsListBlueprintAnnotationKey] = base64.StdEncoding.EncodeToString(encodedInitialEventsListBlueprint)
561 objectMeta.SetAnnotations(annotations)
562
563 return nil
564 }
565
566 func (e *watchListTransformer) transformInitialEventsListBlueprint() (runtime.Object, error) {
567 if e.targetGVK != nil && e.targetGVK.Kind == "PartialObjectMetadata" {
568 return asPartialObjectMetadataList(e.initialEventsListBlueprint, e.targetGVK.GroupVersion())
569 }
570 return e.initialEventsListBlueprint, nil
571 }
572
573 func newWatchEventErrorFor(err error) watch.Event {
574 return watch.Event{
575 Type: watch.Error,
576 Object: &metav1.Status{
577 Status: metav1.StatusFailure,
578 Message: err.Error(),
579 Reason: metav1.StatusReasonInternalError,
580 Code: http.StatusInternalServerError,
581 },
582 }
583 }