forked from sensu/sensu-go
/
entity.go
52 lines (43 loc) · 1.73 KB
/
entity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package agentd
import (
"context"
"fmt"
"github.com/sensu/sensu-go/types"
)
// addEntitySubscription appends the entity subscription (using the format
// "entity:entityName") to the subscriptions of an entity
func addEntitySubscription(entityName string, subscriptions []string) []string {
entityKey := types.GetEntitySubscription(entityName)
return append(subscriptions, entityKey)
}
// getProxyEntity verifies if a proxy entity name was provided in the given event and if
// so, retrieves the corresponding entity in the store in order to replace the
// event's entity with it. In case no entity exists, we create an entity with
// the proxy class
func getProxyEntity(event *types.Event, s SessionStore) error {
ctx := context.WithValue(context.Background(), types.NamespaceKey, event.Entity.Namespace)
// Verify if a proxy entity name, representing a proxy entity, is defined in the check
if event.HasCheck() && event.Check.ProxyEntityName != "" {
// Query the store for an entity using the given proxy entity name
entity, err := s.GetEntityByName(ctx, event.Check.ProxyEntityName)
if err != nil {
return fmt.Errorf("could not query the store for a proxy entity: %s", err)
}
// Check if an entity was found for this proxy entity. If not, we need to create it
if entity == nil {
entity = &types.Entity{
EntityClass: types.EntityProxyClass,
Subscriptions: addEntitySubscription(event.Check.ProxyEntityName, []string{}),
ObjectMeta: types.ObjectMeta{
Namespace: event.Entity.Namespace,
Name: event.Check.ProxyEntityName,
},
}
if err := s.UpdateEntity(ctx, entity); err != nil {
return fmt.Errorf("could not create a proxy entity: %s", err)
}
}
event.Entity = entity
}
return nil
}