blob: 58f077380334fb244743c6f4befc0485e0464eb6 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2Copyright 2015 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package v1
18
19import (
20 "k8s.io/apimachinery/pkg/conversion"
21 "k8s.io/apimachinery/pkg/runtime"
22 "k8s.io/apimachinery/pkg/runtime/schema"
23 "k8s.io/apimachinery/pkg/watch"
24)
25
26// Event represents a single event to a watched resource.
27//
28// +protobuf=true
29// +k8s:deepcopy-gen=true
30// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
31type WatchEvent struct {
32 Type string `json:"type" protobuf:"bytes,1,opt,name=type"`
33
34 // Object is:
35 // * If Type is Added or Modified: the new state of the object.
36 // * If Type is Deleted: the state of the object immediately before deletion.
37 // * If Type is Error: *Status is recommended; other types may make sense
38 // depending on context.
39 Object runtime.RawExtension `json:"object" protobuf:"bytes,2,opt,name=object"`
40}
41
42func Convert_watch_Event_To_v1_WatchEvent(in *watch.Event, out *WatchEvent, s conversion.Scope) error {
43 out.Type = string(in.Type)
44 switch t := in.Object.(type) {
45 case *runtime.Unknown:
46 // TODO: handle other fields on Unknown and detect type
47 out.Object.Raw = t.Raw
48 case nil:
49 default:
50 out.Object.Object = in.Object
51 }
52 return nil
53}
54
55func Convert_v1_InternalEvent_To_v1_WatchEvent(in *InternalEvent, out *WatchEvent, s conversion.Scope) error {
56 return Convert_watch_Event_To_v1_WatchEvent((*watch.Event)(in), out, s)
57}
58
59func Convert_v1_WatchEvent_To_watch_Event(in *WatchEvent, out *watch.Event, s conversion.Scope) error {
60 out.Type = watch.EventType(in.Type)
61 if in.Object.Object != nil {
62 out.Object = in.Object.Object
63 } else if in.Object.Raw != nil {
64 // TODO: handle other fields on Unknown and detect type
65 out.Object = &runtime.Unknown{
66 Raw: in.Object.Raw,
67 ContentType: runtime.ContentTypeJSON,
68 }
69 }
70 return nil
71}
72
73func Convert_v1_WatchEvent_To_v1_InternalEvent(in *WatchEvent, out *InternalEvent, s conversion.Scope) error {
74 return Convert_v1_WatchEvent_To_watch_Event(in, (*watch.Event)(out), s)
75}
76
77// InternalEvent makes watch.Event versioned
78// +protobuf=false
79type InternalEvent watch.Event
80
81func (e *InternalEvent) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
82func (e *WatchEvent) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
83func (e *InternalEvent) DeepCopyObject() runtime.Object {
84 if c := e.DeepCopy(); c != nil {
85 return c
86 } else {
87 return nil
88 }
89}