Skip to content

Commit b79d014

Browse files
committed
feat: playbook agents
* playbook run listener support playbook_run as the parent not just notification_send
1 parent e56be18 commit b79d014

20 files changed

Lines changed: 601 additions & 71 deletions

api/v1/playbook_actions.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func (t *AIActionClient) Populate(ctx context.Context) error {
300300
if err != nil {
301301
return err
302302
} else if conn == nil {
303-
return fmt.Errorf("connection(%s) was not found: %w", *t.Connection, err)
303+
return fmt.Errorf("connection(%s) was not found", *t.Connection)
304304
}
305305

306306
if err := t.APIKey.Scan(conn.Password); err != nil {
@@ -385,6 +385,8 @@ type AIAction struct {
385385
// When enabled, the prompt is simply saved without passing it on to the LLM.
386386
DryRun bool `json:"dryRun,omitempty"`
387387

388+
PlaybookAgents []types.ResourceSelector `json:"playbookAgents,omitempty"`
389+
388390
// Specify selectors for playbooks. The LLM will recommend the best suited playbooks
389391
// in response to the prompt.
390392
RecommendPlaybooks []types.ResourceSelector `json:"recommendPlaybooks,omitempty"`

api/v1/zz_generated.deepcopy.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crds/mission-control.flanksource.com_playbooks.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,66 @@ spec:
140140
Model name based on the backend chosen.
141141
Example: gpt-4o for openai, claude-3-5-sonnet-latest for Anthropic, llama3.1:8b for Ollama
142142
type: string
143+
playbookAgents:
144+
items:
145+
properties:
146+
agent:
147+
description: |-
148+
Agent can be the agent id or the name of the agent.
149+
Additionally, the special "self" value can be used to select resources without an agent.
150+
type: string
151+
cache:
152+
description: |-
153+
Cache directives
154+
'no-cache' (should not fetch from cache but can be cached)
155+
'no-store' (should not cache)
156+
'max-age=X' (cache for X duration)
157+
type: string
158+
fieldSelector:
159+
type: string
160+
health:
161+
description: |-
162+
Health filters resources by the health.
163+
Multiple healths can be provided separated by comma.
164+
type: string
165+
id:
166+
type: string
167+
includeDeleted:
168+
type: boolean
169+
labelSelector:
170+
type: string
171+
limit:
172+
type: integer
173+
name:
174+
type: string
175+
namespace:
176+
type: string
177+
scope:
178+
description: |-
179+
Scope is the reference for parent of the resource to select.
180+
For config items, the scope is the scraper id
181+
For checks, it's canaries and
182+
For components, it's topology.
183+
It can either be a uuid or namespace/name
184+
type: string
185+
search:
186+
description: Search query that applies to the resource
187+
name, tag & labels.
188+
type: string
189+
statuses:
190+
description: Statuses filter resources by the status
191+
items:
192+
type: string
193+
type: array
194+
tagSelector:
195+
type: string
196+
types:
197+
description: Types filter resources by the type
198+
items:
199+
type: string
200+
type: array
201+
type: object
202+
type: array
143203
prompt:
144204
description: Prompt is the human prompt
145205
type: string

config/schemas/playbook-spec.schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
"dryRun": {
3939
"type": "boolean"
4040
},
41+
"playbookAgents": {
42+
"items": {
43+
"$ref": "#/$defs/ResourceSelector"
44+
},
45+
"type": "array"
46+
},
4147
"recommendPlaybooks": {
4248
"items": {
4349
"$ref": "#/$defs/ResourceSelector"

config/schemas/playbook.schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
"dryRun": {
3939
"type": "boolean"
4040
},
41+
"playbookAgents": {
42+
"items": {
43+
"$ref": "#/$defs/ResourceSelector"
44+
},
45+
"type": "array"
46+
},
4147
"recommendPlaybooks": {
4248
"items": {
4349
"$ref": "#/$defs/ResourceSelector"

events/event_queue.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import (
1212
"gorm.io/gorm/clause"
1313
)
1414

15+
// events received on this channel are saved to DB.
16+
// NOTE: Not sure about this one. will probably remove it.
17+
var EventChan = make(chan models.Event)
18+
1519
type asyncHandlerData struct {
1620
fn func(ctx context.Context, e models.Events) models.Events
1721
batchSize int
@@ -67,6 +71,15 @@ func StartConsumers(ctx context.Context) {
6771
for _, fn := range registers {
6872
fn(ctx)
6973
}
74+
75+
go func() {
76+
for e := range EventChan {
77+
if err := ctx.DB().Create(&e).Error; err != nil {
78+
log.Errorf("failed to create event: %w", err)
79+
}
80+
}
81+
}()
82+
7083
// We listen to all PG Notifications on one channel and distribute it to other consumers
7184
// based on the events.
7285
notifyRouter := pg.NewNotifyRouter()
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
# yaml-language-server: $schema=../../../config/schemas/playbook.schema.json
3+
apiVersion: mission-control.flanksource.com/v1
4+
kind: Playbook
5+
metadata:
6+
name: use-agents
7+
spec:
8+
description: Use AI to diagnose unhealthy config items and send a notification slack
9+
category: AI
10+
configs:
11+
- name: '*'
12+
parameters:
13+
- name: prompt
14+
label: Prompt
15+
default: Find out why {{.config.name}} is unhealthy and report in one short sentence.
16+
properties:
17+
multiline: 'true'
18+
actions:
19+
- name: query LLM
20+
ai:
21+
connection: connection://mc/anthropic
22+
playbookAgents:
23+
- namespace: mc
24+
systemPrompt: |
25+
**Role:** Seasoned Kubernetes engineer and Diagnostic Expert
26+
27+
**Objective:** Assist users in diagnosing issues with unhealthy Kubernetes resources by analyzing provided manifests and related resources.
28+
29+
**Instructions:**
30+
31+
1. **Resource Analysis:** Examine the manifest of the unhealthy resource thoroughly.
32+
2. **Contextual Investigation:** Consider additional related resources provided (e.g., pods, replica sets, namespaces) to gain a comprehensive understanding of the issue.
33+
4. **One-Time Diagnosis:** Aim to diagnose the issue in a single response without requiring follow-up questions.
34+
prompt: '{{.params.prompt}}'
35+
changes:
36+
since: 2d
37+
analysis:
38+
since: 2d
39+
relationships:
40+
- depth: 3
41+
direction: outgoing
42+
changes:
43+
since: 24h
44+
- depth: 5
45+
direction: incoming
46+
changes:
47+
since: 24h

fixtures/playbooks/ai-diagnose-aws-ollama.yaml renamed to fixtures/playbooks/ai/ai-diagnose-aws-ollama.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
---
2-
# yaml-language-server: $schema=../../config/schemas/playbook.schema.json
2+
# yaml-language-server: $schema=../../../config/schemas/playbook.schema.json
33
apiVersion: mission-control.flanksource.com/v1
44
kind: Playbook
55
metadata:
66
name: diagnose-aws-resource
77
spec:
88
description: Use AI to diagnose AWS resources
9+
category: AI
910
configs:
1011
- types:
1112
- AWS::CloudFormation::Stack

fixtures/playbooks/ai-diagnose-kubernetes-resource.yaml renamed to fixtures/playbooks/ai/ai-diagnose-kubernetes-resource.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
---
2-
# yaml-language-server: $schema=../../config/schemas/playbook.schema.json
2+
# yaml-language-server: $schema=../../../config/schemas/playbook.schema.json
33
apiVersion: mission-control.flanksource.com/v1
44
kind: Playbook
55
metadata:
66
name: diagnose-kubernetes-resource
77
spec:
88
description: Use AI to diagnose unhealthy kubernetes resources
9+
category: AI
910
configs:
1011
- types:
1112
- Kubernetes::DaemonSet

fixtures/playbooks/ai-diagnose-slack-notification.yaml renamed to fixtures/playbooks/ai/ai-diagnose-slack-notification.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
---
2-
# yaml-language-server: $schema=../../config/schemas/playbook.schema.json
2+
# yaml-language-server: $schema=../../../config/schemas/playbook.schema.json
33
apiVersion: mission-control.flanksource.com/v1
44
kind: Playbook
55
metadata:
66
name: diagnose-and-send-to-slack
77
spec:
88
description: Use AI to diagnose unhealthy config items and send a notification slack
9+
category: AI
910
configs:
10-
- health: unhealthy,warning
11+
- health: unhealthy,warning,unknown,healthy
1112
parameters:
1213
- name: prompt
1314
label: Prompt

0 commit comments

Comments
 (0)