github.com/lusis/distribution@v2.0.1+incompatible/notifications/event.go (about)

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