event.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package notifications
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/docker/distribution"
  6. )
  7. // EventAction constants used in action field of Event.
  8. const (
  9. EventActionPull = "pull"
  10. EventActionPush = "push"
  11. EventActionMount = "mount"
  12. EventActionDelete = "delete"
  13. )
  14. const (
  15. // EventsMediaType is the mediatype for the json event envelope. If the
  16. // Event, ActorRecord, SourceRecord or Envelope structs change, the version
  17. // number should be incremented.
  18. EventsMediaType = "application/vnd.docker.distribution.events.v1+json"
  19. // LayerMediaType is the media type for image rootfs diffs (aka "layers")
  20. // used by Docker. We don't expect this to change for quite a while.
  21. layerMediaType = "application/vnd.docker.container.image.rootfs.diff+x-gtar"
  22. )
  23. // Envelope defines the fields of a json event envelope message that can hold
  24. // one or more events.
  25. type Envelope struct {
  26. // Events make up the contents of the envelope. Events present in a single
  27. // envelope are not necessarily related.
  28. Events []Event `json:"events,omitempty"`
  29. }
  30. // TODO(stevvooe): The event type should be separate from the json format. It
  31. // should be defined as an interface. Leaving as is for now since we don't
  32. // need that at this time. If we make this change, the struct below would be
  33. // called "EventRecord".
  34. // Event provides the fields required to describe a registry event.
  35. type Event struct {
  36. // ID provides a unique identifier for the event.
  37. ID string `json:"id,omitempty"`
  38. // Timestamp is the time at which the event occurred.
  39. Timestamp time.Time `json:"timestamp,omitempty"`
  40. // Action indicates what action encompasses the provided event.
  41. Action string `json:"action,omitempty"`
  42. // Target uniquely describes the target of the event.
  43. Target struct {
  44. // TODO(stevvooe): Use http.DetectContentType for layers, maybe.
  45. distribution.Descriptor
  46. // Length in bytes of content. Same as Size field in Descriptor.
  47. // Provided for backwards compatibility.
  48. Length int64 `json:"length,omitempty"`
  49. // Repository identifies the named repository.
  50. Repository string `json:"repository,omitempty"`
  51. // FromRepository identifies the named repository which a blob was mounted
  52. // from if appropriate.
  53. FromRepository string `json:"fromRepository,omitempty"`
  54. // URL provides a direct link to the content.
  55. URL string `json:"url,omitempty"`
  56. // Tag provides the tag
  57. Tag string `json:"tag,omitempty"`
  58. } `json:"target,omitempty"`
  59. // Request covers the request that generated the event.
  60. Request RequestRecord `json:"request,omitempty"`
  61. // Actor specifies the agent that initiated the event. For most
  62. // situations, this could be from the authorizaton context of the request.
  63. Actor ActorRecord `json:"actor,omitempty"`
  64. // Source identifies the registry node that generated the event. Put
  65. // differently, while the actor "initiates" the event, the source
  66. // "generates" it.
  67. Source SourceRecord `json:"source,omitempty"`
  68. }
  69. // ActorRecord specifies the agent that initiated the event. For most
  70. // situations, this could be from the authorizaton context of the request.
  71. // Data in this record can refer to both the initiating client and the
  72. // generating request.
  73. type ActorRecord struct {
  74. // Name corresponds to the subject or username associated with the
  75. // request context that generated the event.
  76. Name string `json:"name,omitempty"`
  77. // TODO(stevvooe): Look into setting a session cookie to get this
  78. // without docker daemon.
  79. // SessionID
  80. // TODO(stevvooe): Push the "Docker-Command" header to replace cookie and
  81. // get the actual command.
  82. // Command
  83. }
  84. // RequestRecord covers the request that generated the event.
  85. type RequestRecord struct {
  86. // ID uniquely identifies the request that initiated the event.
  87. ID string `json:"id"`
  88. // Addr contains the ip or hostname and possibly port of the client
  89. // connection that initiated the event. This is the RemoteAddr from
  90. // the standard http request.
  91. Addr string `json:"addr,omitempty"`
  92. // Host is the externally accessible host name of the registry instance,
  93. // as specified by the http host header on incoming requests.
  94. Host string `json:"host,omitempty"`
  95. // Method has the request method that generated the event.
  96. Method string `json:"method"`
  97. // UserAgent contains the user agent header of the request.
  98. UserAgent string `json:"useragent"`
  99. }
  100. // SourceRecord identifies the registry node that generated the event. Put
  101. // differently, while the actor "initiates" the event, the source "generates"
  102. // it.
  103. type SourceRecord struct {
  104. // Addr contains the ip or hostname and the port of the registry node
  105. // that generated the event. Generally, this will be resolved by
  106. // os.Hostname() along with the running port.
  107. Addr string `json:"addr,omitempty"`
  108. // InstanceID identifies a running instance of an application. Changes
  109. // after each restart.
  110. InstanceID string `json:"instanceID,omitempty"`
  111. }
  112. var (
  113. // ErrSinkClosed is returned if a write is issued to a sink that has been
  114. // closed. If encountered, the error should be considered terminal and
  115. // retries will not be successful.
  116. ErrSinkClosed = fmt.Errorf("sink: closed")
  117. )
  118. // Sink accepts and sends events.
  119. type Sink interface {
  120. // Write writes one or more events to the sink. If no error is returned,
  121. // the caller will assume that all events have been committed and will not
  122. // try to send them again. If an error is received, the caller may retry
  123. // sending the event. The caller should cede the slice of memory to the
  124. // sink and not modify it after calling this method.
  125. Write(events ...Event) error
  126. // Close the sink, possibly waiting for pending events to flush.
  127. Close() error
  128. }