2 Copyright 2014 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23 "k8s.io/apimachinery/pkg/runtime"
24 "k8s.io/apimachinery/pkg/runtime/serializer/streaming"
25 "k8s.io/apimachinery/pkg/watch"
28 // Decoder implements the watch.Decoder interface for io.ReadClosers that
29 // have contents which consist of a series of watchEvent objects encoded
30 // with the given streaming decoder. The internal objects will be then
31 // decoded by the embedded decoder.
33 decoder streaming.Decoder
34 embeddedDecoder runtime.Decoder
37 // NewDecoder creates an Decoder for the given writer and codec.
38 func NewDecoder(decoder streaming.Decoder, embeddedDecoder runtime.Decoder) *Decoder {
41 embeddedDecoder: embeddedDecoder,
45 // Decode blocks until it can return the next object in the reader. Returns an error
46 // if the reader is closed or an object can't be decoded.
47 func (d *Decoder) Decode() (watch.EventType, runtime.Object, error) {
48 var got metav1.WatchEvent
49 res, _, err := d.decoder.Decode(nil, &got)
54 return "", nil, fmt.Errorf("unable to decode to metav1.Event")
57 case string(watch.Added), string(watch.Modified), string(watch.Deleted), string(watch.Error):
59 return "", nil, fmt.Errorf("got invalid watch event type: %v", got.Type)
62 obj, err := runtime.Decode(d.embeddedDecoder, got.Object.Raw)
64 return "", nil, fmt.Errorf("unable to decode watch event: %v", err)
66 return watch.EventType(got.Type), obj, nil
69 // Close closes the underlying r.
70 func (d *Decoder) Close() {