diff --git a/mission-control/docs/playbooks/concepts/playbook.md b/mission-control/docs/playbooks/concepts/playbook.md index 0d96e4d4..3d4cf381 100644 --- a/mission-control/docs/playbooks/concepts/playbook.md +++ b/mission-control/docs/playbooks/concepts/playbook.md @@ -60,10 +60,16 @@ Filters can define what resources (checks, configs or components) are permitted Playbook parameter defines a parameter that a playbook needs to run. -| Field | Description | Scheme | Required | -| ------- | --------------------------- | -------- | -------- | -| `name` | Specify name of parameter. | `string` | `true` | -| `label` | Specify label of parameter. | `string` | `true` | +| Field | Description | Scheme | Required | +| ------------- | ------------------------------------------------------------------------------------------------- | ------------------- | -------- | +| `name` | Name of parameter. | `string` | `true` | +| `default` | Default value of the parameter. | `string` | | +| `label` | Label of the parameter. | `string` | `true` | +| `required` | Specify if the parameter is required | `bool` | | +| `icon` | Icon of parameter. See [icons](https://github.com/flanksource/flanksource-ui/tree/main/src/icons) | `string` | | +| `description` | Description of the parameter. | `string` | | +| `type` | Type of parameter. | `string` | | +| `properties` | Properties of parameter. | `map[string]string` | | ## Run @@ -88,17 +94,17 @@ Start time can be in future or even in the past. Actions are the fundamental tasks executed by a playbook. A playbook can comprise multiple actions, which are executed sequentially. If any action encounters an error and fails, the execution of the playbook is halted. -| Field | Description | Scheme | Required | -| -------------- | --------------------------------------------------------------- | -------------------------------------------------- | -------- | -| `name` | Name of action. | `string` | `true` | -| `delay` | Delay the execution of the action. _(Sensitive to the minute.)_ | [`DurationString`](#duration-string) | | -| `timeout` | Timeout on this action. | [`DurationString`](#duration-string) | | -| `exec` | Specify exec of action. | [`ExecAction`](../actions/exec.md) | | -| `gitops` | Specify gitops of action. | [`GitopsAction`](../actions/gitops.md) | | -| `http` | Specify http of action. | [`HttpAction`](../actions/http.md) | | -| `sql` | Specify sql of action. | [`SqlAction`](../actions/sql.md) | | -| `pod` | Specify pod of action. | [`PodAction`](../actions/pod.md) | | -| `notification` | Specify notification of action. | [`NotificationAction`](../actions/notification.md) | | +| Field | Description | Scheme | Required | +| -------------- | ---------------------------------------------------------------------------------------------------------------- | -------------------------------------------------- | -------- | +| `name` | Name of action. | `string` | `true` | +| `delay` | A CEL expression that evaluates to a duration to delay the execution of the action. _(Sensitive to the minute.)_ | `string` | | +| `timeout` | Timeout on this action. | [`DurationString`](#duration-string) | | +| `exec` | Specify exec of action. | [`ExecAction`](../actions/exec.md) | | +| `gitops` | Specify gitops of action. | [`GitopsAction`](../actions/gitops.md) | | +| `http` | Specify http of action. | [`HttpAction`](../actions/http.md) | | +| `sql` | Specify sql of action. | [`SqlAction`](../actions/sql.md) | | +| `pod` | Specify pod of action. | [`PodAction`](../actions/pod.md) | | +| `notification` | Specify notification of action. | [`NotificationAction`](../actions/notification.md) | | :::note Specify one or more actions; but at least one. @@ -107,11 +113,23 @@ Specify one or more actions; but at least one. ![Playbook Action Logs](../../images/playbook-action-logs.png) _Fig: Playbooks Action Logs_ -### Duration String +### Delaying actions -Duration strings specify duration in a human readable format. +Actions can be delayed by a fixed duration or conditionally by a CEL expression. +It's only sensitive to the minute. i.e. if you delay by 20s it can take upto a minute to execute. -**Examples:** +#### Templating + +The CEL expression receives a environment variable that contain details about the corresponding config, check or component and the parameter (if applicable). + +| Field | Description | Schema | +| ----------- | ---------------------------------------- | -------------------------------------------- | +| `config` | Config passed to the playbook | [`ConfigItem`](../references/config_item.md) | +| `component` | Component passed to the playbook | [`Component`](../references/component.md) | +| `check` | Canary Check passed to the playbook | [`Check`](../references/check.md) | +| `params` | User provided parameters to the playbook | `map[string]string` | + +Valid time units are "s", "m", "h", "d", "w", "y". Eg: - `1m15s` - `1h5m` @@ -119,3 +137,25 @@ Duration strings specify duration in a human readable format. - `1d8h` - `1w6d8h` - `19w0d8h` + +### Conditionally running actions + +Playbook actions can be conditionally run using a CEL Expression. The expressions should either return + +- a boolean value _(`true` indicating run the action & skip the action otherwise)_ +- or a special function among the ones listed below + +#### Functions + +| Function | Description | +| ----------- | ----------------------------------------------------------- | +| `always()` | run no matter what; even if the playbook is cancelled/fails | +| `failure()` | run if any of the previous actions failed | +| `skip()` | skip running this action | +| `success()` | run only if all previous actions succeeded (default) | +| `timeout()` | run only if any of the previous actions timed out | + +#### Examples + +- `if: config.deleted_at ? true: false` +- `if: always()` diff --git a/mission-control/docs/playbooks/examples/action-filter.md b/mission-control/docs/playbooks/examples/action-filter.md new file mode 100644 index 00000000..0a9f75f0 --- /dev/null +++ b/mission-control/docs/playbooks/examples/action-filter.md @@ -0,0 +1,23 @@ +# Playbook Action Filter + +```yaml title="delete-kubernetes-pod.yaml" +--- +apiVersion: mission-control.flanksource.com/v1 +kind: Playbook +metadata: + name: delete kubernetes pod +spec: + configs: + - type: Kubernetes::Pod + actions: + - name: Delete the pod + exec: + script: kubectl delete pod {{.config.name}} + - name: Log Maintenance Success + delay: 2m + if: failure() + notification: + connection: connection://telegram/aditya + title: 'Failed to delete kubernetes pod {{.config.name}}' + message: 'Something went wrong with deleting kubernetes pod {{.config.name}}' +``` diff --git a/mission-control/docs/playbooks/examples/exec-delay.md b/mission-control/docs/playbooks/examples/exec-delay.md new file mode 100644 index 00000000..2d665ff2 --- /dev/null +++ b/mission-control/docs/playbooks/examples/exec-delay.md @@ -0,0 +1,23 @@ +# Delay Actions + +```yaml title="delay-deletion.yaml" +apiVersion: mission-control.flanksource.com/v1 +kind: Playbook +metadata: + name: write-config-to-file +spec: + description: Creates a file with the content of the config + configs: + - type: Kubernetes::Pod + actions: + - name: Create the config file + exec: + script: echo -n '{{.config.config}}' > /tmp/{{.config.id}} + - name: Delete the file + delay: 'config.name.startsWith("actual") ? "1m" : "0s"' + exec: + script: rm /tmp/{{.config.id}} +``` + +This playbook saves the config of a `Kubernetes::Pod` to a temporary file. A delay is introduced before deleting that file. If the name of the pod beings with +`actual` then the deletion is delayed by a minute, else it is deleted immediately. diff --git a/mission-control/docs/playbooks/triggers/events.md b/mission-control/docs/playbooks/triggers/events.md index 04bfd8cb..0509a7d1 100644 --- a/mission-control/docs/playbooks/triggers/events.md +++ b/mission-control/docs/playbooks/triggers/events.md @@ -17,8 +17,6 @@ Filters can give you fine-grained control over the events that can trigger the p ## Types -At this point, there's only support for events on canaries and components. - ### Canary events Canary events relate to activities on health checks. @@ -83,3 +81,34 @@ spec: title: 'Database {{.component.name}} has become unhealthy' message: 'Description: {{.component.description}}' ``` + +### Config events + +Config events relate to activities on config items. + +| Event | Description | +| --------- | ----------------------------- | +| `created` | when a config item is created | +| `updated` | when a config item is updated | +| `deleted` | when a config item is deleted | + +#### Example + +```yaml title="notify-newly-scraped-pod.yaml" +apiVersion: mission-control.flanksource.com/v1 +kind: Playbook +metadata: + name: Notify on pod config creation +spec: + description: Notify when a new pod is discovered + on: + component: + - event: config.created + filter: config.class == 'Pod' + actions: + - name: Send notification + notification: + connection: connection://telegram/playbook-alerts + title: 'A new kubernetes pod {{.config.name}} was scraped' + message: 'Namespace: {{.config.namespace}}' +``` diff --git a/mission-control/sidebars.js b/mission-control/sidebars.js index 8e06cca2..beebf9ff 100644 --- a/mission-control/sidebars.js +++ b/mission-control/sidebars.js @@ -592,6 +592,16 @@ const sidebars = { id: 'playbooks/examples/approval', label: 'Approval' }, + { + type: 'doc', + id: 'playbooks/examples/exec-delay', + label: 'Delaying actions' + }, + { + type: 'doc', + id: 'playbooks/examples/action-filter', + label: 'Conditionally running actions' + }, ], }, {