api/types/events: add "Type" type for event-type enum

Currently just an alias for string, but we can change it to be  an
actual type.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2021-08-21 20:38:44 +02:00
parent 2561e33771
commit 247f4796d2
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
4 changed files with 25 additions and 32 deletions

View File

@ -1,33 +1,26 @@
package events // import "github.com/docker/docker/api/types/events" package events // import "github.com/docker/docker/api/types/events"
// Type is used for event-types.
type Type = string
// List of known event types.
const ( const (
// BuilderEventType is the event type that the builder generates BuilderEventType Type = "builder" // BuilderEventType is the event type that the builder generates.
BuilderEventType = "builder" ConfigEventType Type = "config" // ConfigEventType is the event type that configs generate.
// ContainerEventType is the event type that containers generate ContainerEventType Type = "container" // ContainerEventType is the event type that containers generate.
ContainerEventType = "container" DaemonEventType Type = "daemon" // DaemonEventType is the event type that daemon generate.
// DaemonEventType is the event type that daemon generate ImageEventType Type = "image" // ImageEventType is the event type that images generate.
DaemonEventType = "daemon" NetworkEventType Type = "network" // NetworkEventType is the event type that networks generate.
// ImageEventType is the event type that images generate NodeEventType Type = "node" // NodeEventType is the event type that nodes generate.
ImageEventType = "image" PluginEventType Type = "plugin" // PluginEventType is the event type that plugins generate.
// NetworkEventType is the event type that networks generate SecretEventType Type = "secret" // SecretEventType is the event type that secrets generate.
NetworkEventType = "network" ServiceEventType Type = "service" // ServiceEventType is the event type that services generate.
// PluginEventType is the event type that plugins generate VolumeEventType Type = "volume" // VolumeEventType is the event type that volumes generate.
PluginEventType = "plugin"
// VolumeEventType is the event type that volumes generate
VolumeEventType = "volume"
// ServiceEventType is the event type that services generate
ServiceEventType = "service"
// NodeEventType is the event type that nodes generate
NodeEventType = "node"
// SecretEventType is the event type that secrets generate
SecretEventType = "secret"
// ConfigEventType is the event type that configs generate
ConfigEventType = "config"
) )
// Actor describes something that generates events, // Actor describes something that generates events,
// like a container, or a network, or a volume. // like a container, or a network, or a volume.
// It has a defined name and a set or attributes. // It has a defined name and a set of attributes.
// The container attributes are its labels, other actors // The container attributes are its labels, other actors
// can generate these attributes from other properties. // can generate these attributes from other properties.
type Actor struct { type Actor struct {
@ -39,11 +32,11 @@ type Actor struct {
type Message struct { type Message struct {
// Deprecated information from JSONMessage. // Deprecated information from JSONMessage.
// With data only in container events. // With data only in container events.
Status string `json:"status,omitempty"` Status string `json:"status,omitempty"` // Deprecated: use Action instead.
ID string `json:"id,omitempty"` ID string `json:"id,omitempty"` // Deprecated: use Actor.ID instead.
From string `json:"from,omitempty"` From string `json:"from,omitempty"` // Deprecated: use Actor.Attributes["image"] instead.
Type string Type Type
Action string Action string
Actor Actor Actor Actor
// Engine events are local scope. Cluster events are swarm scope. // Engine events are local scope. Cluster events are swarm scope.

View File

@ -91,17 +91,17 @@ func TestEvents(t *testing.T) {
}, },
events: []events.Message{ events: []events.Message{
{ {
Type: "container", Type: events.BuilderEventType,
ID: "1", ID: "1",
Action: "create", Action: "create",
}, },
{ {
Type: "container", Type: events.BuilderEventType,
ID: "2", ID: "2",
Action: "die", Action: "die",
}, },
{ {
Type: "container", Type: events.BuilderEventType,
ID: "3", ID: "3",
Action: "create", Action: "create",
}, },

View File

@ -79,7 +79,7 @@ func (e *Events) Evict(l chan interface{}) {
} }
// Log creates a local scope message and publishes it // Log creates a local scope message and publishes it
func (e *Events) Log(action, eventType string, actor eventtypes.Actor) { func (e *Events) Log(action string, eventType eventtypes.Type, actor eventtypes.Actor) {
now := time.Now().UTC() now := time.Now().UTC()
jm := eventtypes.Message{ jm := eventtypes.Message{
Action: action, Action: action,

View File

@ -102,7 +102,7 @@ func (ef *Filter) matchConfig(ev events.Message) bool {
return ef.fuzzyMatchName(ev, events.ConfigEventType) return ef.fuzzyMatchName(ev, events.ConfigEventType)
} }
func (ef *Filter) fuzzyMatchName(ev events.Message, eventType string) bool { func (ef *Filter) fuzzyMatchName(ev events.Message, eventType events.Type) bool {
return ef.filter.FuzzyMatch(eventType, ev.Actor.ID) || return ef.filter.FuzzyMatch(eventType, ev.Actor.ID) ||
ef.filter.FuzzyMatch(eventType, ev.Actor.Attributes["name"]) ef.filter.FuzzyMatch(eventType, ev.Actor.Attributes["name"])
} }