Skip to main content

AI Action

AI Action allows you to integrate AI capabilities into your playbooks by leveraging the rich context that Mission Control maintains about your infrastructure. When executed against configs or components, the AI action automatically injects:

  • Component/config manifests and specifications
  • Related configurations with configurable relationship depth
  • Historical analysis data within the specified time period
  • Change history across related infrastructure within defined time ranges

This comprehensive context enables AI models to provide more informed analysis and insights about your infrastructure state and relationships. For example, when a Kubernetes pod fails, it examines the pod spec, ConfigMap changes, service logs together, revealing patterns that single-component analysis might overlook.

diagnose-kubernetes-resource.yaml
---
# yaml-language-server: $schema=../../config/schemas/playbook.schema.json
apiVersion: mission-control.flanksource.com/v1
kind: Playbook
metadata:
name: diagnose-kubernetes-resource
spec:
description: Use AI to diagnose unhealthy kubernetes resources
configs:
- types:
- Kubernetes::DaemonSet
- Kubernetes::Deployment
- Kubernetes::Pod
- Kubernetes::StatefulSet
parameters:
- name: prompt
label: Prompt
default: Find out why {{.config.name}} is unhealthy
properties:
multiline: 'true'
actions:
- name: query
ai:
connection: connection://mc/anthropic
systemPrompt: |
**Role:** Seasoned Kubernetes engineer and Diagnostic Expert

**Objective:** Assist users in diagnosing issues with unhealthy Kubernetes resources by analyzing provided manifests and related resources.

**Instructions:**

1. **Resource Analysis:** Examine the manifest of the unhealthy resource thoroughly.
2. **Contextual Investigation:** Consider additional related resources provided (e.g., pods, replica sets, namespaces) to gain a comprehensive understanding of the issue.
3. **Diagnostic Steps:** Clearly outline the steps taken during the investigation, focusing on precision and brevity.
4. **One-Time Diagnosis:** Aim to diagnose the issue in a single response without requiring follow-up questions.

**Output:** Provide a concise diagnosis and potential solutions based on the analysis.
prompt: '{{.params.prompt}}'
changes:
since: 2d
analysis:
since: 2d
relationships:
- depth: 3
direction: outgoing
changes:
since: 24h
- depth: 5
direction: incoming
changes:
since: 24h
FieldDescriptionScheme
name*

Step Name

string

prompt*

Main prompt

string

systemPrompt*

Context-setting system prompt

string

analysis.since

Select the analysis of the playbook resource to feed into the AI

Duration

apiKey

AI service API key

apiURL

Custom API endpoint (applicable when behind a proxy or using Ollama)

string

backend

LLM provider

changes.since

Select the changes of the playbook resource to feed into the AI

Duration

connection

Connection string for the LLM

string

formats

Output format. (Only markdown supported as of now)

model

LLM model (e.g. gpt-4)

string

relationships

Select the related configs and their changes and analysis to feed into the AI

delay

A delay before running the action e.g. 8h

filter

Conditionally run an action

runsOn

Which runner (agent) to run the action on

templatesOn

Where templating (and secret management) of actions should occur

timeout

Timeout on this action.

Duration

Relationship

The AI Action can maintain relationships with other elements:

FieldDescriptionScheme
analysis.since

Select the analysis of the related resources to feed into the AI

Duration

changes.since

Select the changes of the related resources to feed into the AI

Duration

depth

Depth of the relationship

integer

direction

Direction of the relationship

Templating

CEL Expressions

The following variables can be used within the CEL expressions of filter, if, delays and parameters.default:

FieldDescriptionSchema
configConfig passed to the playbookConfigItem
componentComponent passed to the playbookComponent
checkCanary Check passed to the playbookCheck
playbookPlaybook passed to the playbookPlaybook
runCurrent runRun
paramsUser provided parameters to the playbookmap[string]any
requestWebhook requestWebhook Request
envEnvironment variables defined on the playbookmap[string]any
user.nameName of the user who invoked the actionstring
user.emailEmail of the user who invoked the actionstring
agent.idID of the agent the resource belongs to.string
agent.nameName of the agent the resource belongs to.string
Conditionally Running Actions

Playbook actions can be selectively executed based on CEL expressions. These expressions must either return

  • a boolean value (true indicating run the action & skip the action otherwise)
  • or a special function among the ones listed below
FunctionDescription
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
delete-kubernetes-pod.yaml
---
apiVersion: mission-control.flanksource.com/v1
kind: Playbook
metadata:
name: notify-send-with-filter
spec:
parameters:
- name: message
label: The message for notification
default: '{{.config.name}}'
configs:
- types:
- Kubernetes::Pod
actions:
- name: Send notification
exec:
script: notify-send "{{.config.name}} was created"
- name: Bad script
exec:
script: deltaforce
- name: Send all success notification
if: success() # this filter practically skips this action as the second action above always fails
exec:
script: notify-send "Everything went successfully"
- name: Send notification regardless
if: always()
exec:
script: notify-send "a Pod config was created"
Defaulting Parameters
delete-kubernetes-pod.yaml
apiVersion:
mission-control.flanksource.com/v1
kind: Playbook
metadata:
name: edit
spec:
title: 'Edit Kustomize Resource'
icon: flux
parameters:
- default: 'chore: update $(.config.type)/$(.config.name)'
name: commit_message

Go Templating

When templating actions with Go Templates, the context variables are available as fields of the template's context object . eg .config, .user.email

Templating Actions
delete-kubernetes-pod.yaml
apiVersion: mission-control.flanksource.com/v1
kind: Playbook
metadata:
name: scale-deployment
spec:
description: Scale Deployment
configs:
- types:
- Kubernetes::Deployment
parameters:
- name: replicas
label: The new desired number of replicas.
actions:
- name: kubectl scale
exec:
script: |
kubectl scale --replicas={{.params.replicas}} \
--namespace={{.config.tags.namespace}} \
deployment {{.config.name}}

Functions

FunctionDescriptionReturn
getLastAction()Returns the result of the action that just runAction Specific
getAction({action})Return the result of a specific actionAction Specific
Printing out Results
Reusing Action Results
action-results.yaml
apiVersion: mission-control.flanksource.com/v1
kind: Playbook
metadata:
name: use-previous-action-result
spec:
description: Creates a file with the content of the config
configs:
- types:
- Kubernetes::Pod
actions:
- name: Fetch all changes
sql:
query: SELECT id FROM config_changes WHERE config_id = '{{.config.id}}'
driver: postgres
connection: connection://postgres/local
- name: Send notification
if: 'last_result().count > 0'
notification:
title: 'Changes summary for {{.config.name}}'
connection: connection://slack/flanksource
message: |
{{$rows:=index last_result "count"}}
Found {{$rows}} changes