Skip to content

Commit

Permalink
add support for entity mgmt annotations; updated readme+changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
Caleb Hailey committed Apr 14, 2021
1 parent 9120482 commit aef010a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 55 deletions.
26 changes: 23 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic
Versioning](http://semver.org/spec/v2.0.0.html).
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

### Added

- NEW `--add-labels` command, accepts newline separated list of key=value pairs
- NEW `--add-annotations` command, accepts newline separated list of key=value pairs
- NEW `--add-all` command, accepts newline separated list of commands + arguments; e.g.:

```
add-subscription postgres
add-label region=us-west-1
add-annotation foo=bar
```

This enables support for discovery plugins that may modify multiple entity attributes (e.g. subscriptions + labels).

### Fixed

- The `sensu.io/plugins/sensu-entity-manager/config/patch/annotations` and `sensu.io/plugins/sensu-entity-manager/config/patch/subscriptions` annotations are now supported.
These were documented in the README previously but there were never implemented.

### Changed

- Q1 '21 handler maintenance:
- Updated modules (go get -u && go mod tidy)
- Add pull_request to lint and test GitHub Actions
Expand All @@ -18,4 +37,5 @@ Versioning](http://semver.org/spec/v2.0.0.html).
## [0.0.1] - 2000-01-01

### Added

- Initial release
75 changes: 35 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

## Overview

Event-based Sensu entity management for service-discovery (add/remove
subscriptions) and other automation workflows.
Event-based Sensu entity management for service-discovery (add/remove subscriptions) and other automation workflows.

## Usage examples

Expand All @@ -41,6 +40,9 @@ Available Commands:
Flags:
-t, --access-token string Sensu Access Token
--add-all Checks event.Check.Output for a newline-separated list of entity management commands to execute
--add-annotations Checks event.Check.Output for a newline-separated list of annotation key=value pairs to add
--add-labels Checks event.Check.Output for a newline-separated list of label key=value pairs to add
--add-subscriptions Checks event.Check.Output for a newline-separated list of subscriptions to add
-k, --api-key string Sensu API Key
-a, --api-url string Sensu API URL (default "http://127.0.0.1:8080")
Expand All @@ -52,26 +54,24 @@ Use "sensu-entity-manager [command] --help" for more information about a command

### Environment variables

|Argument |Environment Variable |
|------------------|----------------------|
|--api-url |SENSU_API_URL |
|--api-key |SENSU_API_KEY |
|--access-token |SENSU_ACCESS_TOKEN |
|--trusted-ca-file |SENSU_TRUSTED_CA_FILE |
| Argument | Environment Variable |
|-------------------|-----------------------|
| --api-url | SENSU_API_URL |
| --api-key | SENSU_API_KEY |
| --access-token | SENSU_ACCESS_TOKEN |
| --trusted-ca-file | SENSU_TRUSTED_CA_FILE |

**Security Note:** Care should be taken to not expose the API key or access token for this handler
by specifying either on the command line or by directly setting the environment variable(s) in the
handler definition. It is suggested to make use of [secrets management][3] to surface either as an
environment variable. The [handler definition shown below](#handler-definition) references the API Key as a secret
using the built-in [env secrets provider][4].
**Security Note:** Care should be taken to not expose the API key or access token for this handler by explicitly specifying either on the command line or by directly setting the environment variable(s) in the handler definition.
It is suggested to make use of [secrets management][3] to provide the API key or access token as environment variables.
The [handler definition shown below](#handler-definition) references the API Key as a secret using the built-in [env secrets provider][4].

## Configuration

### Asset registration

[Sensu Assets][10] are the best way to make use of this plugin. If you're not using an asset, please
consider doing so! If you're using sensuctl 5.13 with Sensu Backend 5.13 or later, you can use the
following command to add the asset:
[Sensu Assets][10] are the best way to make use of this plugin.
If you're not using an asset, please consider doing so!
If you're using sensuctl 5.13 with Sensu Backend 5.13 or later, you can use the following command to add the asset:

```
sensuctl asset add sensu/sensu-entity-manager
Expand All @@ -89,9 +89,9 @@ metadata:
name: sensu-entity-manager
spec:
type: pipe
command: >-
sensu-entity-manager
--add-subscriptions
command: >-
sensu-entity-manager
--add-all
timeout: 5
runtime_assets:
- sensu/sensu-entity-manager:0.1.1
Expand All @@ -103,39 +103,35 @@ type: Secret
api_version: secrets/v1
metadata:
name: entity-manager-api-key
spec:
spec:
provider: env
id: SENSU_ENTITY_MANAGER_API_KEY
```
#### Proxy Support
This handler supports the use of the environment variables HTTP_PROXY,
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.
This handler supports the use of the environment variables HTTP_PROXY, 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.
### Supported Annotations
### Supported Annotations
The following _event-scoped annotations_ are supported.
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`).
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`).
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._
> _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._


#### Examples
Expand All @@ -153,9 +149,8 @@ metadata:

## Installation from source

The preferred way of installing and deploying this plugin is to use it as an Asset. If you would
like to compile and install the plugin from source or contribute to it, download the latest version
or create an executable from this source.
The preferred way of installing and deploying this plugin is to use it as an Asset.
If you would like to compile and install the plugin from source or contribute to it, download the latest version or create an executable from this source.

From the local path of the sensu-entity-manager repository:

Expand All @@ -166,9 +161,9 @@ go build
## 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.
- [x] Add support for adding/modifying entity labels
- [x] 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 @@ -180,4 +175,4 @@ For more information about contributing to this plugin, see [Contributing][1].
[3]: https://docs.sensu.io/sensu-go/latest/guides/secrets-management/
[4]: https://docs.sensu.io/sensu-go/latest/guides/secrets-management/#use-env-for-secrets-management
[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
[11]: https://docs.sensu.io/sensu-go/latest/api/entities/#update-an-entity-with-patch
49 changes: 37 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,31 @@ func checkArgs(event *types.Event) error {
plugin.ApiUrl = os.Getenv("SENSU_API_URL")
}
if plugin.AddSubscriptions {
fmt.Printf("Adding subscriptions from \"event.check.output\"\n")
addSubscriptions(strings.Split(event.Check.Output, "\n"))
}
if len(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/subscriptions"]) > 0 {
fmt.Printf("Adding subscriptions from the \"sensu.io/plugins/sensu-entity-manager/config/patch/subscriptions\" event annotation\n")
addSubscriptions(strings.Split(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/subscriptions"], ","))
}
if plugin.AddLabels {
fmt.Printf("Adding labels from \"event.check.output\"\n")
addLabels(strings.Split(event.Check.Output, "\n"))
}
if len(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/labels"]) > 0 {
fmt.Printf("Adding labels from the \"sensu.io/plugins/sensu-entity-manager/config/patch/labels\" event annotation\n")
addLabels(strings.Split(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/labels"], ","))
}
if plugin.AddAnnotations {
fmt.Printf("Adding annotations from \"event.check.output\"\n")
addAnnotations(strings.Split(event.Check.Output, "\n"))
}
if len(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/annotations"]) > 0 {
fmt.Printf("Adding annotations from the \"sensu.io/plugins/sensu-entity-manager/config/patch/annotations\" event annotation\n")
addAnnotations(strings.Split(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/annotations"], ","))
}
if plugin.AddAll {
fmt.Printf("Adding entity properties from \"event.check.output\"\n")
parseCommands(strings.Split(event.Check.Output, "\n"))
}
return nil
Expand Down Expand Up @@ -311,7 +327,11 @@ func parseKvStringSlice(s []string) map[string]string {
if len(i) > 1 {
k := strings.TrimSpace(i[0])
v := strings.TrimSpace(i[1])
m[k] = v
if len(strings.Split(k, " ")) > 1 {
fmt.Printf("WARNING: invalid key name: \"%s\" (did you mean to use --add-all?)\n", k)
} else {
m[k] = v
}
}
}
return m
Expand Down Expand Up @@ -353,17 +373,22 @@ func patchEntity(event *types.Event) *EntityPatch {
// Parse commands
func parseCommands(s []string) error {
for _, str := range s {
command := strings.TrimSpace(strings.Split(str, " ")[0])
argument := strings.TrimSpace(strings.Split(str, " ")[1])
switch command {
case "add-subscription":
addSubscriptions([]string{argument})
case "add-label":
addLabels([]string{argument})
case "add-annotation":
addAnnotations([]string{argument})
default:
fmt.Printf("WARNING: nothing to do for command: \"%v\" (argument: \"%s\").\n", command, argument)
instructions := strings.Split(str, " ")
if len(instructions) < 2 {
fmt.Printf("WARNING: invalid command: \"%s\"\n", str)
} else {
command := strings.TrimSpace(instructions[0])
argument := strings.TrimSpace(instructions[1])
switch command {
case "add-subscription":
addSubscriptions([]string{argument})
case "add-label":
addLabels([]string{argument})
case "add-annotation":
addAnnotations([]string{argument})
default:
fmt.Printf("WARNING: nothing to do for command: \"%v\" (argument: \"%s\").\n", command, argument)
}
}
}
return nil
Expand Down

0 comments on commit aef010a

Please sign in to comment.