From 951795f36c675a1536ad019598579e9787efa7bf Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Wed, 1 Jan 2025 12:34:50 +0545 Subject: [PATCH] add playbook to send diagnostic report to Slack --- charts/playbooks-ai/README.md | 16 ++-- .../diagnose-kubernetes-notification.yaml | 83 +++++++++++++++++++ .../templates/diagnose-kubernetes.yaml | 10 +-- charts/playbooks-ai/values.schema.json | 69 ++++++++++++--- charts/playbooks-ai/values.yaml | 45 ++++++++-- .../playbooks-kubernetes/values.schema.json | 1 - charts/playbooks-kubernetes/values.yaml | 1 + 7 files changed, 193 insertions(+), 32 deletions(-) create mode 100644 charts/playbooks-ai/templates/diagnose-kubernetes-notification.yaml diff --git a/charts/playbooks-ai/README.md b/charts/playbooks-ai/README.md index fae5df9..be9f282 100644 --- a/charts/playbooks-ai/README.md +++ b/charts/playbooks-ai/README.md @@ -4,12 +4,16 @@ Flanksource Mission Control Playbooks that uses AI action ## Values -| Key | Type | Default | Description | -| --------------------- | ------ | ------- | ----------------------------------------------------------------------------------- | -| connection | string | `""` | LLM connection: one of ollama, openai or anthropic | -| diagnose.cluster | string | `""` | name of the cluster | -| diagnose.enabled | bool | `true` | enable kubernetes diagnosing playbook | -| diagnose.systemPrompt | string | `""` | Optional system prompt for the LLM. If not provided, a default prompt will be used. | +| Key | Type | Default | Description | +| ------------------------------- | ------ | ------- | ------------------------------------------------------------------------------------------ | +| diagnose.cluster | string | `""` | name of the cluster | +| diagnose.enabled | bool | `true` | create a playbook that can diagnose an unhealthy kubernetes resource in the given cluster. | +| diagnose.systemPrompt | string | `""` | Optional system prompt for the LLM. If not provided, a default prompt will be used. | +| diganoseToSlack.cluster | string | `""` | name of the cluster | +| diganoseToSlack.enabled | bool | `true` | and send the diagnosis to slack. | +| diganoseToSlack.slackConnection | string | `""` | connection string for slack | +| diganoseToSlack.systemPrompt | string | `""` | Optional system prompt for the LLM. If not provided, a default prompt will be used. | +| global.connection | string | `""` | LLM connection: one of ollama, openai or anthropic | ## Maintainers diff --git a/charts/playbooks-ai/templates/diagnose-kubernetes-notification.yaml b/charts/playbooks-ai/templates/diagnose-kubernetes-notification.yaml new file mode 100644 index 0000000..e081101 --- /dev/null +++ b/charts/playbooks-ai/templates/diagnose-kubernetes-notification.yaml @@ -0,0 +1,83 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/flanksource/duty/main/schema/openapi/playbook.schema.json +{{- if and .Values.diganoseToSlack.enabled }} +--- +apiVersion: mission-control.flanksource.com/v1 +kind: Playbook +metadata: + name: send-diagnostics-to-slack +spec: + description: Use AI to diganoseToSlack unhealthy kubernetes resources and send notification to slack + configs: + - tagSelector: 'cluster={{.Values.diganoseToSlack.cluster}}' + healths: + - unhealthy + - warning + # TODO: Limit this selector to kubernetes types + parameters: + - name: prompt + label: Prompt + default: Find out why $(.config.name) is unhealthy + properties: + multiline: 'true' + actions: + - name: query + ai: + connection: '{{.Values.global.connection}}' + systemPrompt: |- + {{- if .Values.diganoseToSlack.systemPrompt }} + {{- .Values.diganoseToSlack.systemPrompt | nindent 10 }} + {{- else }} + **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. **One-Time Diagnosis:** Aim to diganoseToSlack the issue in a single response without requiring follow-up questions. + + **Output:** Provide a concise diagnosis and potential solutions based on the analysis. + The output should be in json using Block Kit(https://api.slack.com/block-kit) - a UI framework for Slack apps. + Do not wrap the json block in a code block (like ```json```). The output should be a valid json. + Example: output + { + "blocks": [ + { + "type": "section", + "fields": [ + { + "type": "mrkdwn", + "text": "Statefulset: alertmanager" + }, + { + "type": "mrkdwn", + "text": "*Namespace*: mc" + }, + { + "type": "mrkdwn", + "text": "Deployment has pods that are in a crash loop." + } + ] + }, + ] + } + {{- end }} + prompt: '$(.params.prompt)' + changes: + since: 2d + analysis: + since: 2d + relationships: + - depth: 5 + direction: all + changes: + since: 24h + analysis: + since: 30d + - name: send notificaiton + notification: + connection: {{.Values.diganoseToSlack.slackConnection}} + message: "$(getLastAction.result.markdown)" + title: Diagnosis report +{{- end}} diff --git a/charts/playbooks-ai/templates/diagnose-kubernetes.yaml b/charts/playbooks-ai/templates/diagnose-kubernetes.yaml index 1aeaeac..5d6c46e 100644 --- a/charts/playbooks-ai/templates/diagnose-kubernetes.yaml +++ b/charts/playbooks-ai/templates/diagnose-kubernetes.yaml @@ -22,7 +22,7 @@ spec: actions: - name: query ai: - connection: '{{.Values.connection}}' + connection: '{{.Values.global.connection}}' systemPrompt: |- {{- if .Values.diagnose.systemPrompt }} {{- .Values.diagnose.systemPrompt | nindent 10 }} @@ -47,11 +47,9 @@ spec: since: 2d relationships: - depth: 5 - direction: outgoing - changes: - since: 24h - - depth: 5 - direction: incoming + direction: all changes: since: 24h + analysis: + since: 30d {{- end}} diff --git a/charts/playbooks-ai/values.schema.json b/charts/playbooks-ai/values.schema.json index 7d042b5..28af005 100644 --- a/charts/playbooks-ai/values.schema.json +++ b/charts/playbooks-ai/values.schema.json @@ -2,13 +2,6 @@ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { - "connection": { - "default": "", - "description": "yaml-language-server: $schema=values.schema.json\nLLM connection: one of ollama, openai or anthropic", - "required": [], - "title": "connection", - "type": "string" - }, "diagnose": { "additionalProperties": false, "properties": { @@ -21,7 +14,7 @@ }, "enabled": { "default": true, - "description": "enable kubernetes diagnosing playbook", + "description": "create a playbook that can diagnose an unhealthy kubernetes resource in the given cluster.", "required": [], "title": "enabled", "type": "boolean" @@ -40,16 +33,68 @@ "title": "diagnose", "type": "object" }, + "diganoseToSlack": { + "additionalProperties": false, + "properties": { + "cluster": { + "default": "", + "description": "name of the cluster", + "required": [], + "title": "cluster", + "type": "string" + }, + "enabled": { + "default": true, + "description": "create a playbook that can diagnose an unhealthy kubernetes resource in the given cluster\nand send the diagnosis to slack.", + "required": [], + "title": "enabled", + "type": "boolean" + }, + "slackConnection": { + "default": "", + "description": "connection string for slack", + "required": [], + "title": "slackConnection", + "type": "string" + }, + "systemPrompt": { + "default": "", + "description": "Optional system prompt for the LLM. If not provided, a default prompt will be used.", + "required": [], + "title": "systemPrompt", + "type": "string" + } + }, + "required": [ + "slackConnection", + "cluster" + ], + "title": "diganoseToSlack", + "type": "object" + }, "global": { - "description": "Global values are values that can be accessed from any chart or subchart by exactly the same name.", - "required": [], + "additionalProperties": false, + "description": "yaml-language-server: $schema=values.schema.json", + "properties": { + "connection": { + "default": "", + "description": "LLM connection: one of ollama, openai or anthropic", + "required": [], + "title": "connection", + "type": "string" + } + }, + "required": [ + "connection" + ], "title": "global", "type": "object" } }, "required": [ - "connection", - "diagnose" + "global", + "diagnose", + "diganoseToSlack" ], "type": "object" } \ No newline at end of file diff --git a/charts/playbooks-ai/values.yaml b/charts/playbooks-ai/values.yaml index d377b9b..81dd715 100644 --- a/charts/playbooks-ai/values.yaml +++ b/charts/playbooks-ai/values.yaml @@ -1,17 +1,18 @@ # yaml-language-server: $schema=values.schema.json -# @schema -# type: string -# required: true -# @schema -# -- LLM connection: one of ollama, openai or anthropic -connection: "" +global: + # @schema + # type: string + # required: true + # @schema + # -- LLM connection: one of ollama, openai or anthropic + connection: "" diagnose: # @schema # type: boolean # required: false # @schema - # -- enable kubernetes diagnosing playbook + # -- create a playbook that can diagnose an unhealthy kubernetes resource in the given cluster. enabled: true # @schema @@ -27,3 +28,33 @@ diagnose: # @schema # -- Optional system prompt for the LLM. If not provided, a default prompt will be used. systemPrompt: "" + +diganoseToSlack: + # @schema + # type: boolean + # required: false + # @schema + # -- create a playbook that can diagnose an unhealthy kubernetes resource in the given cluster + # -- and send the diagnosis to slack. + enabled: true + + # @schema + # type: string + # required: true + # @schema + # -- connection string for slack + slackConnection: "" + + # @schema + # type: string + # required: true + # @schema + # -- name of the cluster + cluster: "" + + # @schema + # type: string + # required: false + # @schema + # -- Optional system prompt for the LLM. If not provided, a default prompt will be used. + systemPrompt: "" diff --git a/charts/playbooks-kubernetes/values.schema.json b/charts/playbooks-kubernetes/values.schema.json index f904b99..99af4dc 100644 --- a/charts/playbooks-kubernetes/values.schema.json +++ b/charts/playbooks-kubernetes/values.schema.json @@ -62,7 +62,6 @@ }, "playbooks": { "additionalProperties": false, - "description": "yaml-language-server: $schema=values.schema.json", "properties": { "cleanupFailedPods": { "default": true, diff --git a/charts/playbooks-kubernetes/values.yaml b/charts/playbooks-kubernetes/values.yaml index babfe32..d62c0de 100644 --- a/charts/playbooks-kubernetes/values.yaml +++ b/charts/playbooks-kubernetes/values.yaml @@ -14,6 +14,7 @@ playbooks: updateImage: true updateResources: true deployHelmChart: true + delete: types: - Kubernetes::Pod