Skip to content

Commit

Permalink
feat: playbook agents
Browse files Browse the repository at this point in the history
* playbook run listener support playbook_run as the parent not just
  notification_send
  • Loading branch information
adityathebe committed Jan 9, 2025
1 parent 43cd2bd commit d861117
Show file tree
Hide file tree
Showing 20 changed files with 599 additions and 76 deletions.
2 changes: 2 additions & 0 deletions api/v1/playbook_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ type AIAction struct {
// When enabled, the prompt is simply saved without passing it on to the LLM.
DryRun bool `json:"dryRun,omitempty"`

PlaybookAgents []types.ResourceSelector `json:"playbookAgents,omitempty"`

// Specify selectors for playbooks. The LLM will recommend the best suited playbooks
// in response to the prompt.
RecommendPlaybooks []types.ResourceSelector `json:"recommendPlaybooks,omitempty"`
Expand Down
7 changes: 7 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions config/crds/mission-control.flanksource.com_playbooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,66 @@ spec:
Model name based on the backend chosen.
Example: gpt-4o for openai, claude-3-5-sonnet-latest for Anthropic, llama3.1:8b for Ollama
type: string
playbookAgents:
items:
properties:
agent:
description: |-
Agent can be the agent id or the name of the agent.
Additionally, the special "self" value can be used to select resources without an agent.
type: string
cache:
description: |-
Cache directives
'no-cache' (should not fetch from cache but can be cached)
'no-store' (should not cache)
'max-age=X' (cache for X duration)
type: string
fieldSelector:
type: string
health:
description: |-
Health filters resources by the health.
Multiple healths can be provided separated by comma.
type: string
id:
type: string
includeDeleted:
type: boolean
labelSelector:
type: string
limit:
type: integer
name:
type: string
namespace:
type: string
scope:
description: |-
Scope is the reference for parent of the resource to select.
For config items, the scope is the scraper id
For checks, it's canaries and
For components, it's topology.
It can either be a uuid or namespace/name
type: string
search:
description: Search query that applies to the resource
name, tag & labels.
type: string
statuses:
description: Statuses filter resources by the status
items:
type: string
type: array
tagSelector:
type: string
types:
description: Types filter resources by the type
items:
type: string
type: array
type: object
type: array
prompt:
description: Prompt is the human prompt
type: string
Expand Down
6 changes: 6 additions & 0 deletions config/schemas/playbook-spec.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
"dryRun": {
"type": "boolean"
},
"playbookAgents": {
"items": {
"$ref": "#/$defs/ResourceSelector"
},
"type": "array"
},
"recommendPlaybooks": {
"items": {
"$ref": "#/$defs/ResourceSelector"
Expand Down
6 changes: 6 additions & 0 deletions config/schemas/playbook.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
"dryRun": {
"type": "boolean"
},
"playbookAgents": {
"items": {
"$ref": "#/$defs/ResourceSelector"
},
"type": "array"
},
"recommendPlaybooks": {
"items": {
"$ref": "#/$defs/ResourceSelector"
Expand Down
13 changes: 13 additions & 0 deletions events/event_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"gorm.io/gorm/clause"
)

// events received on this channel are saved to DB.
// NOTE: Not sure about this one. will probably remove it.
var EventChan = make(chan models.Event)

type asyncHandlerData struct {
fn func(ctx context.Context, e models.Events) models.Events
batchSize int
Expand Down Expand Up @@ -67,6 +71,15 @@ func StartConsumers(ctx context.Context) {
for _, fn := range registers {
fn(ctx)
}

go func() {
for e := range EventChan {
if err := ctx.DB().Create(&e).Error; err != nil {
log.Errorf("failed to create event: %w", err)
}
}
}()

// We listen to all PG Notifications on one channel and distribute it to other consumers
// based on the events.
notifyRouter := pg.NewNotifyRouter()
Expand Down
47 changes: 47 additions & 0 deletions fixtures/playbooks/ai-agents.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
# yaml-language-server: $schema=../../config/schemas/playbook.schema.json
apiVersion: mission-control.flanksource.com/v1
kind: Playbook
metadata:
name: use-agents
spec:
description: Use AI to diagnose unhealthy config items and send a notification slack
category: AI
configs:
- name: '*'
parameters:
- name: prompt
label: Prompt
default: Find out why {{.config.name}} is unhealthy and report in one short sentence.
properties:
multiline: 'true'
actions:
- name: query LLM
ai:
connection: connection://mc/anthropic
playbookAgents:
- namespace: mc
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.
4. **One-Time Diagnosis:** Aim to diagnose the issue in a single response without requiring follow-up questions.
prompt: '{{.params.prompt}}'
changes:
since: 2d
analysis:
since: 2d
relationships:
- depth: 3
direction: outgoing
changes:
since: 24h
- depth: 5
direction: incoming
changes:
since: 24h
1 change: 1 addition & 0 deletions fixtures/playbooks/ai-diagnose-aws-ollama.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ metadata:
name: diagnose-aws-resource
spec:
description: Use AI to diagnose AWS resources
category: AI
configs:
- types:
- AWS::CloudFormation::Stack
Expand Down
1 change: 1 addition & 0 deletions fixtures/playbooks/ai-diagnose-kubernetes-resource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ metadata:
name: diagnose-kubernetes-resource
spec:
description: Use AI to diagnose unhealthy kubernetes resources
category: AI
configs:
- types:
- Kubernetes::DaemonSet
Expand Down
3 changes: 2 additions & 1 deletion fixtures/playbooks/ai-diagnose-slack-notification.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ metadata:
name: diagnose-and-send-to-slack
spec:
description: Use AI to diagnose unhealthy config items and send a notification slack
category: AI
configs:
- health: unhealthy,warning
- health: unhealthy,warning,unknown,healthy
parameters:
- name: prompt
label: Prompt
Expand Down
10 changes: 2 additions & 8 deletions fixtures/playbooks/ai-recommend-playbook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ metadata:
spec:
description: Use AI to diagnose unhealthy config items and send a notification slack
configs:
<<<<<<< HEAD
- health: unhealthy,warning
||||||| parent of 4e54e54 (feat(ai playbook action): formats support & playbook recommender)
=======
- healths:
- unhealthy
- warning
>>>>>>> 4e54e54 (feat(ai playbook action): formats support & playbook recommender)
- health: unhealthy,warning,unknown,healthy
category: AI
parameters:
- name: prompt
label: Prompt
Expand Down
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/containrrr/shoutrrr v0.8.0
github.com/fergusstrange/embedded-postgres v1.29.0 // indirect
github.com/flanksource/commons v1.35.3
github.com/flanksource/duty v1.0.800
github.com/flanksource/duty v1.0.802
github.com/flanksource/gomplate/v3 v3.24.50
github.com/flanksource/kopper v1.0.11
github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2
Expand Down Expand Up @@ -92,6 +92,7 @@ require (
dario.cat/mergo v1.0.0 // indirect
github.com/GehirnInc/crypt v0.0.0-20200316065508-bb7000b8a962 // indirect
github.com/Masterminds/semver/v3 v3.3.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/RaveNoX/go-jsonmerge v1.0.0 // indirect
Expand Down Expand Up @@ -167,13 +168,15 @@ require (
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect
github.com/google/s2a-go v0.1.8 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/goph/emperror v0.17.2 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/hairyhenderson/yaml v0.0.0-20220618171115-2d35fca545ce // indirect
github.com/hashicorp/hcl/v2 v2.21.0 // indirect
github.com/hirochachacha/go-smb2 v1.1.0 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/invopop/jsonschema v0.12.0 // indirect
github.com/itchyny/gojq v0.12.17 // indirect
github.com/itchyny/timefmt-go v0.1.6 // indirect
Expand Down Expand Up @@ -204,10 +207,12 @@ require (
github.com/moby/spdystream v0.4.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/nikolalohinski/gonja v1.5.3 // indirect
github.com/ohler55/ojg v1.25.0 // indirect
github.com/oklog/ulid/v2 v2.1.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/orcaman/concurrent-map/v2 v2.0.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/sftp v1.13.6 // indirect
Expand All @@ -223,10 +228,12 @@ require (
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/shurcooL/githubv4 v0.0.0-20190718010115-4ba037080260 // indirect
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
github.com/tidwall/gjson v1.17.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
Expand All @@ -243,6 +250,7 @@ require (
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/yargevad/filepathx v1.0.0 // indirect
github.com/yuin/gopher-lua v1.1.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
github.com/zclconf/go-cty v1.15.0 // indirect
Expand All @@ -251,6 +259,7 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.22.0 // indirect
go.opentelemetry.io/otel/metric v1.29.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.starlark.net v0.0.0-20231121155337-90ade8b19d09 // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/sync v0.10.0 // indirect
Expand Down
Loading

0 comments on commit d861117

Please sign in to comment.