watch.go 2.8 KB

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