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 // Encoder serializes watch.Events into io.Writer. The internal objects
29 // are encoded using embedded encoder, and the outer Event is serialized
31 // TODO: this type is only used by tests
33 encoder streaming.Encoder
34 embeddedEncoder runtime.Encoder
37 func NewEncoder(encoder streaming.Encoder, embeddedEncoder runtime.Encoder) *Encoder {
40 embeddedEncoder: embeddedEncoder,
44 // Encode writes an event to the writer. Returns an error
45 // if the writer is closed or an object can't be encoded.
46 func (e *Encoder) Encode(event *watch.Event) error {
47 data, err := runtime.Encode(e.embeddedEncoder, event.Object)
51 // FIXME: get rid of json.RawMessage.
52 return e.encoder.Encode(&metav1.WatchEvent{
53 Type: string(event.Type),
54 Object: runtime.RawExtension{Raw: json.RawMessage(data)},