Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2015 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 v1 |
| 18 | |
| 19 | import ( |
| 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 |
| 31 | type 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 | |
| 42 | func 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 | |
| 55 | func 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 | |
| 59 | func 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 | |
| 73 | func 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 |
| 79 | type InternalEvent watch.Event |
| 80 | |
| 81 | func (e *InternalEvent) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind } |
| 82 | func (e *WatchEvent) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind } |
| 83 | func (e *InternalEvent) DeepCopyObject() runtime.Object { |
| 84 | if c := e.DeepCopy(); c != nil { |
| 85 | return c |
| 86 | } else { |
| 87 | return nil |
| 88 | } |
| 89 | } |