Skip to content

Commit

Permalink
refactored subscription logic so stdout and annotations are not mutua…
Browse files Browse the repository at this point in the history
…lly exclusive
  • Loading branch information
Caleb Hailey committed Nov 14, 2020
1 parent 1bb4452 commit 374c92b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 27 deletions.
54 changes: 46 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- [Configuration](#configuration)
- [Asset registration](#asset-registration)
- [Handler definition](#handler-definition)
- [Annotations](#annotations)
- [Supported Annotations](#supported-annotations)
- [Installation from source](#installation-from-source)
- [Additional notes](#additional-notes)
- [Contributing](#contributing)
Expand Down Expand Up @@ -68,12 +68,25 @@ type: Handler
api_version: core/v2
metadata:
name: sensu-entity-manager
namespace: default
spec:
command: sensu-entity-manager --example example_arg
type: pipe
command: >-
sensu-entity-manager
--add-subscriptions
timeout: 5
runtime_assets:
- calebhailey/sensu-entity-manager
- calebhailey/sensu-entity-manager:0.1.1
secrets:
- name: SENSU_API_KEY
secret: entity-manager-api-key
---
type: Secret
api_version: secrets/v1
metadata:
name: entity-manager-api-key
spec:
provider: env
id: SENSU_ENTITY_MANAGER_API_KEY
```
#### Proxy Support
Expand All @@ -83,10 +96,28 @@ HTTPS_PROXY, and NO_PROXY (or the lowercase versions thereof). HTTPS_PROXY takes
precedence over HTTP_PROXY for https requests. The environment values may be
either a complete URL or a "host[:port]", in which case the "http" scheme is assumed.
### Annotations
### Supported Annotations
The following _event-scoped annotations_ are supported.
- `sensu.io/plugins/sensu-entity-manager/config/patch/subscriptions`

Comma-separated list of subscriptions to add (e.g. `nginx,http-service`).

- `sensu.io/plugins/sensu-entity-manager/config/patch/labels`

Comma-separated list of key=value pairs to add (e.g. `region=us-west-1,app=example`).

- `sensu.io/plugins/sensu-entity-manager/config/patch/annotations`

Semicolon-separated list of key=value pairs to add (e.g.
`scrape_config="{\"ports\": [9091,9093]}";service_account=sensu`).

> _NOTE: event-scoped annotations are set at the root-level of the event
> (i.e. `event.Annotations`). Entity-scoped (`event.Entity.Annotations`) and
> Check-scoped (`event.Check.Annotations`) annotations are currently not
> supported._

All arguments for this handler are tunable on a per entity or check basis based on annotations. The
annotations keyspace for this handler is `sensu.io/plugins/sensu-entity-manager/config`.

#### Examples

Expand All @@ -113,7 +144,13 @@ From the local path of the sensu-entity-manager repository:
go build
```

## Additional notes
## Roadmap

- [x] Add support for adding/modifying entity subscriptions
- [ ] Add support for adding/modifying entity labels
- [ ] Add support for adding/modifying entity annotations
- [ ] Add support for modifying other [entity-patchable fields][11] (e.g.
`created_by`, `entity_class`, `deregister`, etc).

## Contributing

Expand All @@ -129,3 +166,4 @@ For more information about contributing to this plugin, see [Contributing][1].
[8]: https://bonsai.sensu.io/
[9]: https://github.com/sensu-community/sensu-plugin-tool
[10]: https://docs.sensu.io/sensu-go/latest/reference/assets/
[11]: https://docs.sensu.io/sensu-go/latest/api/entities/#update-an-entity-with-patch
58 changes: 39 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type Config struct {
Annotations map[string]string
Subscriptions []string
AddSubscriptions bool
AddLabels bool
AddAnnotations bool
}

// EntitySubscriptions is a partial Entity definition for use with the
Expand All @@ -39,16 +41,17 @@ type Deregistration struct {
// EntityPatch is a shell of an Entity object for use with the
// PATCH /entities API
type EntityPatch struct {
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
CreatedBy string `json:"created_by,omitempty"`
EntityClass string `json:"entity_class,omitempty"`
User string `json:"user,omitempty"`
Subscriptions []string `json:"subscriptions,omitempty"`
Deregister string `json:"deregister,omitempty"`
Deregistration Deregistration `json:"deregistration,omitempty"`
Redact []string `json:"redact"`
KeepaliveHandler string `json:"keepalive_handler,omitempty"`
Subscriptions []string `json:"subscriptions,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
// TBD if we want to support other Entity-patchable fields:
// CreatedBy string `json:"created_by,omitempty"`
// EntityClass string `json:"entity_class,omitempty"`
// User string `json:"user,omitempty"`
// Deregister string `json:"deregister,omitempty"`
// Deregistration Deregistration `json:"deregistration,omitempty"`
// Redact []string `json:"redact"`
// KeepaliveHandler string `json:"keepalive_handler,omitempty"`
}

var (
Expand Down Expand Up @@ -108,6 +111,24 @@ var (
Usage: "Checks event.Check.Output for a newline-separated list of subscriptions to add",
Value: &plugin.AddSubscriptions,
},
&sensu.PluginConfigOption{
Path: "",
Env: "",
Argument: "add-labels",
Shorthand: "",
Default: false,
Usage: "Checks event.Check.Output for a newline-separated list of label key=value pairs to add",
Value: &plugin.AddLabels,
},
&sensu.PluginConfigOption{
Path: "",
Env: "",
Argument: "add-annotations",
Shorthand: "",
Default: false,
Usage: "Checks event.Check.Output for a newline-separated list of annotation key=value pairs to add",
Value: &plugin.AddAnnotations,
},
&sensu.PluginConfigOption{
Path: "patch/subscriptions",
Env: "",
Expand Down Expand Up @@ -165,12 +186,14 @@ func checkArgs(event *types.Event) error {
plugin.ApiUrl = os.Getenv("SENSU_API_URL")
}
if plugin.AddSubscriptions {
plugin.Subscriptions = strings.Split(event.Check.Output, "\n")
fmt.Printf("Added %v subscriptions from event.Check.Output\n", len(plugin.Subscriptions))
checkOutputSubs := strings.Split(event.Check.Output, "\n")
plugin.Subscriptions = mergeStringSlices(plugin.Subscriptions, checkOutputSubs)
fmt.Printf("Added %v subscriptions from event.Check.Output\n", len(checkOutputSubs))
}
if len(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/subscriptions"]) > 0 {
plugin.Subscriptions = strings.Split(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/subscriptions"], ",")
fmt.Printf("Added %v subscriptions from the \"sensu.io/plugins/sensu-entity-manager/config/patch/subscriptions\" event annotation\n", len(plugin.Subscriptions))
annotationSubs := strings.Split(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/subscriptions"], ",")
plugin.Subscriptions = mergeStringSlices(plugin.Subscriptions, annotationSubs)
fmt.Printf("Added %v subscriptions from the \"sensu.io/plugins/sensu-entity-manager/config/patch/subscriptions\" event annotation\n", len(annotationSubs))
}
return nil
}
Expand Down Expand Up @@ -222,7 +245,7 @@ func indexOf(s []string, k string) int {
return -1
}

func mergeSlices(a []string, b []string) []string {
func mergeStringSlices(a []string, b []string) []string {
for _, v := range b {
if indexOf(a, v) < 0 {
a = append(a, v)
Expand All @@ -235,10 +258,7 @@ func patchEntity(event *types.Event) *EntityPatch {
entity := new(EntityPatch)

// Merge subscriptions
if len(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/subscriptions"]) > 0 {
plugin.Subscriptions = strings.Split(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/subscriptions"], ",")
}
entity.Subscriptions = mergeSlices(event.Entity.Subscriptions, plugin.Subscriptions)
entity.Subscriptions = mergeStringSlices(event.Entity.Subscriptions, plugin.Subscriptions)

return entity
}
Expand Down

0 comments on commit 374c92b

Please sign in to comment.