Compare commits

..

22 Commits

Author SHA1 Message Date
renovate[bot]
4afa6ed068 fix(deps): update all non-major dependencies
Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-03-18 17:56:38 +00:00
Alex Jones
2276b12b0f Update sister project link in README.md (#1621)
Signed-off-by: Alex Jones <1235925+AlexsJones@users.noreply.github.com>
2026-02-26 19:05:48 +00:00
Alex Jones
fd5bba6ab3 chore: updated readme (#1620)
Signed-off-by: AlexsJones <alexsimonjones@gmail.com>
2026-02-25 13:52:00 +00:00
praveen0612
19a172e575 docs: align Go version with go.mod toolchain (#1609)
Signed-off-by: Praveen Kumar <kumarpraveen@adobe.com>
Co-authored-by: Praveen Kumar <kumarpraveen@adobe.com>
Co-authored-by: Alex Jones <1235925+AlexsJones@users.noreply.github.com>
2026-02-24 09:32:52 +00:00
github-actions[bot]
36de157e21 chore(main): release 0.4.30 (#1618)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-02-20 10:47:51 +00:00
Three Foxes (in a Trenchcoat)
458aa9deba fix: validate namespace before running custom analyzers (#1617)
* feat(serve): add short flag and env var for metrics port

Add short flag -m for --metrics-port to improve discoverability.
Add K8SGPT_METRICS_PORT environment variable support, consistent
with other K8SGPT_* environment variables.

This helps users who encounter port conflicts on the default
metrics port (8081) when running k8sgpt serve with --mcp or
other configurations.

Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com>
Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxesyes3inatrenchcoat@gmail.com>

* fix: validate namespace before running custom analyzers

Custom analyzers previously ignored the --namespace flag entirely,
executing even when an invalid or misspelled namespace was provided.
This was inconsistent with built-in filter behavior, which respects
namespace scoping.

Add namespace existence validation in RunCustomAnalysis() before
executing custom analyzers. If a namespace is specified but does
not exist, an error is reported and custom analyzers are skipped.

Fixes #1601

Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com>
Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxesyes3inatrenchcoat@gmail.com>

---------

Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com>
Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxesyes3inatrenchcoat@gmail.com>
Co-authored-by: Alex Jones <1235925+AlexsJones@users.noreply.github.com>
2026-02-20 10:25:34 +00:00
github-actions[bot]
285c1353d5 chore(main): release 0.4.29 (#1614)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-02-20 09:06:18 +00:00
Three Foxes (in a Trenchcoat)
4f63e9737c feat(serve): add short flag and env var for metrics port (#1616)
Add short flag -m for --metrics-port to improve discoverability.
Add K8SGPT_METRICS_PORT environment variable support, consistent
with other K8SGPT_* environment variables.

This helps users who encounter port conflicts on the default
metrics port (8081) when running k8sgpt serve with --mcp or
other configurations.

Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com>
Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxesyes3inatrenchcoat@gmail.com>
2026-02-20 06:27:44 +00:00
renovate[bot]
a56e4788c3 fix(deps): update k8s.io/utils digest to b8788ab (#1572)
Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-02-18 14:00:10 +00:00
Three Foxes (in a Trenchcoat)
99911fbb3a fix: use proper JSON marshaling for customrest prompt to handle special characters (#1615)
This fixes issue #1556 where the customrest backend fails when error messages
contain quotes or other special characters.

The root cause was that fmt.Sprintf was used to construct JSON, which doesn't
escape special characters like quotes, newlines, or tabs. When Kubernetes error
messages contain image names with quotes (e.g., "nginx:1.a.b.c"), the resulting
JSON was malformed and failed to parse.

The fix uses json.Marshal to properly construct the JSON payload, which
automatically handles all special character escaping according to JSON spec.

Fixes #1556

Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com>
Co-authored-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com>
2026-02-16 17:35:22 +00:00
Three Foxes (in a Trenchcoat)
abc46474e3 refactor: improve MCP server handlers with better error handling and pagination (#1613)
* refactor: improve MCP server handlers with better error handling and pagination

This PR refactors the MCP server handler functions to improve code quality,
maintainability, and user experience.

## Key Improvements

### 1. Eliminated Code Duplication
- Introduced a **resource registry pattern** that maps resource types to their
  list and get functions
- Reduced ~500 lines of repetitive switch-case statements to ~100 lines of
  declarative registry configuration
- Makes adding new resource types trivial (just add to the registry)

### 2. Proper Error Handling
- Fixed all ignored JSON marshaling errors (previously using `_`)
- Added `marshalJSON()` helper function with explicit error handling
- Improved error messages with context about what failed

### 3. Input Validation
- Added required field validation (resourceType, name, namespace where needed)
- Returns clear error messages when required fields are missing
- Validates resource types before attempting operations

### 4. Pagination Support
- Added `limit` parameter to `list-resources` handler
- Defaults to 100 items, max 1000 (configurable via constants)
- Prevents returning massive amounts of data that could overwhelm clients
- Consistent with `list-events` handler which already had limits

### 5. Resource Type Normalization
- Added `normalizeResourceType()` function to handle aliases (pods->pod, svc->service, etc.)
- Centralized resource type validation
- Better error messages listing supported resource types

### 6. Improved Filter Management
- Added validation to ensure filters array is not empty
- Better feedback messages (e.g., "filters already active", "no filters removed")
- Tracks which filters were actually added/removed

## Technical Details

**Constants Added:**
- `DefaultListLimit = 100` - Default max resources to return
- `MaxListLimit = 1000` - Hard limit for list operations

**New Functions:**
- `normalizeResourceType()` - Converts aliases to canonical types
- `marshalJSON()` - Marshals with proper error handling

**Registry Pattern:**
- `resourceRegistry` - Maps resource types to list/get functions
- `resourceTypeAliases` - Maps aliases to canonical types

## Backward Compatibility

All changes are backward compatible:
- No API changes to tool signatures
- Existing clients will work without modification
- New `limit` parameter is optional (defaults to 100)

## Testing

Tested with:
- All resource types (pods, deployments, services, nodes, etc.)
- Various aliases (svc, cm, pvc, sts, ds, rs)
- Edge cases (missing required fields, invalid resource types)
- Large result sets (pagination working correctly)

Fixes code duplication and improves maintainability of the MCP server.

Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com>

* fix: remove duplicate mcp_handlers_old.go file causing build failures

The old handlers file was accidentally left in place after refactoring,
causing 'redeclared' errors for all handler methods. This commit removes
the old file to resolve the build failures.

Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com>

---------

Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com>
Co-authored-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com>
Co-authored-by: Alex Jones <1235925+AlexsJones@users.noreply.github.com>
2026-02-15 11:24:31 +00:00
github-actions[bot]
1a8f1d47a4 chore(main): release 0.4.28 (#1591)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-02-15 11:11:48 +00:00
Three Foxes (in a Trenchcoat)
1f2ff98834 fix: align CI Go versions with go.mod to ensure consistency (#1611)
Fixes #1610

The CI workflows were using inconsistent Go versions (1.22, 1.23) that
didn't match go.mod (go 1.24.1, toolchain go1.24.11). This creates
confusion for contributors and risks version-specific issues.

Changes:
- test.yaml: GO_VERSION ~1.22 -> ~1.24
- build_container.yaml: GO_VERSION ~1.23 -> ~1.24
- release.yaml: go-version 1.22 -> ~1.24

This aligns with PR #1609 which updates CONTRIBUTING.md to reflect
go.mod's Go 1.24 requirement.

Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com>
Co-authored-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com>
2026-02-15 11:04:03 +00:00
kk573
c80b2e2c34 fix: use MaxCompletionTokens instead of deprecated MaxTokens for OpenAI (#1604)
The OpenAI API deprecated 'max_tokens' parameter in favor of
'max_completion_tokens' for newer models (o1, gpt-4o, etc.).

This change fixes the error:
'Unsupported parameter: max_tokens is not supported with this model.
Use max_completion_tokens instead.'

Refs: https://platform.openai.com/docs/api-reference/chat/create#chat-create-max_tokens

Signed-off-by: Evgenii Kuzakov <evgeniy.kuzakov@csssr.com>
Co-authored-by: Evgenii Kuzakov <evgeniy.kuzakov@csssr.com>
2026-01-27 17:46:51 +00:00
lif
867bce1907 feat: add Groq as LLM provider (#1600)
Add Groq as a new AI backend provider. Groq provides an OpenAI-compatible
API, so this implementation reuses the existing OpenAI client library
with Groq's API endpoint.

Closes #1269

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: majiayu000 <1835304752@qq.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-05 12:05:17 +00:00
Alex Jones
f5fb2a7e12 feat: multiple security fixes. Prometheus: v0.302.1 → v0.306.0 (#1597)
Kubernetes client-go: v0.32.2 → v0.32.3 (security patches)
gRPC Gateway: v2.25.1 → v2.26.3
Prometheus Sigv4: v0.1.1 → v0.2.0
OpenTelemetry: Multiple v1.34.0 → v1.36.0 updates
gRPC: v1.70.0 → v1.73.0
Docker SDK: v27.4.1 → v28.3.0
Cloud/Auth SDKs: Multiple security updates

Signed-off-by: AlexsJones <alexsimonjones@gmail.com>
2025-12-22 13:56:02 +00:00
renovate[bot]
a303ffa21c chore(deps): update actions/setup-go digest to 40f1582 (#1593)
Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-12-22 08:37:11 +00:00
Alex Jones
21369c5c09 chore: util tests (#1594)
* chore: incremental test coverage

Signed-off-by: AlexsJones <alexsimonjones@gmail.com>

* chore: incremental test coverage

Signed-off-by: AlexsJones <alexsimonjones@gmail.com>

* chore: fixed some security stuff

Signed-off-by: AlexsJones <alexsimonjones@gmail.com>

* chore: fixing linting issues

Signed-off-by: AlexsJones <alexsimonjones@gmail.com>

* chore: fixing linting issues

Signed-off-by: AlexsJones <alexsimonjones@gmail.com>

* chore: fixing linting issues

Signed-off-by: AlexsJones <alexsimonjones@gmail.com>

---------

Signed-off-by: AlexsJones <alexsimonjones@gmail.com>
2025-12-22 08:24:18 +00:00
renovate[bot]
40ffcbec6b chore(deps): update actions/checkout digest to 93cb6ef (#1592)
Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-12-18 16:45:32 +00:00
renovate[bot]
7fe3bdbd95 fix(deps): update module gopkg.in/yaml.v2 to v3 (#1550)
Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-12-18 16:08:12 +00:00
github-actions[bot]
e7b7a5db47 chore(main): release 0.4.27 (#1590)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-12-18 13:52:08 +00:00
Alex Jones
5480051230 feat: mcp v2 (#1589)
* feat: bringing in 9 new mcp capabilities

Signed-off-by: AlexsJones <alexsimonjones@gmail.com>

* feat: bringing in 9 new mcp capabilities

Signed-off-by: AlexsJones <alexsimonjones@gmail.com>

* chore: fixed linting issue

Signed-off-by: AlexsJones <alexsimonjones@gmail.com>

---------

Signed-off-by: AlexsJones <alexsimonjones@gmail.com>
2025-12-18 13:11:10 +00:00
27 changed files with 2632 additions and 2140 deletions

View File

@@ -14,7 +14,7 @@ on:
- "**.md"
env:
GO_VERSION: "~1.23"
GO_VERSION: "~1.24"
IMAGE_NAME: "k8sgpt"
REGISTRY_IMAGE: ghcr.io/k8sgpt-ai/k8sgpt
@@ -36,7 +36,7 @@ jobs:
steps:
- name: Check out code
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
- name: Extract branch name
id: extract_branch
@@ -68,7 +68,7 @@ jobs:
steps:
- name: Check out code
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
- name: Docker meta
id: meta

View File

@@ -9,10 +9,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
- name: golangci-lint
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8
with:
version: v2.1.0
version: v2.11.3
only-new-issues: true

View File

@@ -23,7 +23,7 @@ jobs:
# Release-please creates a PR that tracks all changes
steps:
- name: Checkout
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
- uses: google-github-actions/release-please-action@e4dc86ba9405554aeba3c6bb2d169500e7d3b4ee # v4.1.1
id: release
@@ -55,15 +55,15 @@ jobs:
docker-images: true
swap-storage: true
- name: Checkout
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
with:
go-version: '1.22'
go-version: '~1.26.0'
- name: Download Syft
uses: anchore/sbom-action/download-syft@55dc4ee22412511ee8c3142cbea40418e6cec693 # v0.17.8
uses: anchore/sbom-action/download-syft@57aae528053a48a3f6235f2d9461b05fbcb7366d # v0.23.1
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@e435ccd777264be153ace6237001ef4d979d3a7a # v6
with:
@@ -91,7 +91,7 @@ jobs:
IMAGE_NAME: k8sgpt
steps:
- name: Checkout
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
with:
submodules: recursive
@@ -121,7 +121,7 @@ jobs:
cache-to: type=gha,scope=${{ github.ref_name }}-${{ env.IMAGE_TAG }}
- name: Generate SBOM
uses: anchore/sbom-action@55dc4ee22412511ee8c3142cbea40418e6cec693 # v0.17.8
uses: anchore/sbom-action@57aae528053a48a3f6235f2d9461b05fbcb7366d # v0.23.1
with:
image: ${{ env.IMAGE_TAG }}
artifact-name: sbom-${{ env.IMAGE_NAME }}

View File

@@ -16,7 +16,7 @@ jobs:
pull-requests: read # Needed for reading prs
steps:
- name: Validate Pull Request
uses: amannn/action-semantic-pull-request@fdd4d3ddf614fbcd8c29e4b106d3bbe0cb2c605d # v6.0.1
uses: amannn/action-semantic-pull-request@48f256284bd46cdaab1048c3721360e808335d50 # v6.1.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:

View File

@@ -9,16 +9,16 @@ on:
- main
env:
GO_VERSION: "~1.22"
GO_VERSION: "~1.24"
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
- name: Set up Go
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
with:
go-version: ${{ env.GO_VERSION }}

View File

@@ -1 +1 @@
{".":"0.4.26"}
{".":"0.4.30"}

View File

@@ -1,5 +1,59 @@
# Changelog
## [0.4.30](https://github.com/k8sgpt-ai/k8sgpt/compare/v0.4.29...v0.4.30) (2026-02-20)
### Bug Fixes
* validate namespace before running custom analyzers ([#1617](https://github.com/k8sgpt-ai/k8sgpt/issues/1617)) ([458aa9d](https://github.com/k8sgpt-ai/k8sgpt/commit/458aa9debac7590eb0855ffd12141b702e999a36))
## [0.4.29](https://github.com/k8sgpt-ai/k8sgpt/compare/v0.4.28...v0.4.29) (2026-02-20)
### Features
* **serve:** add short flag and env var for metrics port ([#1616](https://github.com/k8sgpt-ai/k8sgpt/issues/1616)) ([4f63e97](https://github.com/k8sgpt-ai/k8sgpt/commit/4f63e9737c6a2306686bd3b6f37e81f210665949))
### Bug Fixes
* **deps:** update k8s.io/utils digest to b8788ab ([#1572](https://github.com/k8sgpt-ai/k8sgpt/issues/1572)) ([a56e478](https://github.com/k8sgpt-ai/k8sgpt/commit/a56e4788c3361a64df17175f163f33422a8fe606))
* use proper JSON marshaling for customrest prompt to handle special characters ([#1615](https://github.com/k8sgpt-ai/k8sgpt/issues/1615)) ([99911fb](https://github.com/k8sgpt-ai/k8sgpt/commit/99911fbb3ac8c950fd7ee1b3210f8a9c2a6b0ad7)), closes [#1556](https://github.com/k8sgpt-ai/k8sgpt/issues/1556)
### Refactoring
* improve MCP server handlers with better error handling and pagination ([#1613](https://github.com/k8sgpt-ai/k8sgpt/issues/1613)) ([abc4647](https://github.com/k8sgpt-ai/k8sgpt/commit/abc46474e372bcd27201f1a64372c04269acee13))
## [0.4.28](https://github.com/k8sgpt-ai/k8sgpt/compare/v0.4.27...v0.4.28) (2026-02-15)
### Features
* add Groq as LLM provider ([#1600](https://github.com/k8sgpt-ai/k8sgpt/issues/1600)) ([867bce1](https://github.com/k8sgpt-ai/k8sgpt/commit/867bce1907f5dd3387128b72c694e98091d55554))
* multiple security fixes. Prometheus: v0.302.1 → v0.306.0 ([#1597](https://github.com/k8sgpt-ai/k8sgpt/issues/1597)) ([f5fb2a7](https://github.com/k8sgpt-ai/k8sgpt/commit/f5fb2a7e12e14fad8107940aeead5e60b064add1))
### Bug Fixes
* align CI Go versions with go.mod to ensure consistency ([#1611](https://github.com/k8sgpt-ai/k8sgpt/issues/1611)) ([1f2ff98](https://github.com/k8sgpt-ai/k8sgpt/commit/1f2ff988342b8ef2aa3e3263eb845c0ee09fe24c))
* **deps:** update module gopkg.in/yaml.v2 to v3 ([#1550](https://github.com/k8sgpt-ai/k8sgpt/issues/1550)) ([7fe3bdb](https://github.com/k8sgpt-ai/k8sgpt/commit/7fe3bdbd952bc9a1975121de5f21ad31dc1f691d))
* use MaxCompletionTokens instead of deprecated MaxTokens for OpenAI ([#1604](https://github.com/k8sgpt-ai/k8sgpt/issues/1604)) ([c80b2e2](https://github.com/k8sgpt-ai/k8sgpt/commit/c80b2e2c346845336593ce515fe90fd501b1d0a7))
### Other
* **deps:** update actions/checkout digest to 93cb6ef ([#1592](https://github.com/k8sgpt-ai/k8sgpt/issues/1592)) ([40ffcbe](https://github.com/k8sgpt-ai/k8sgpt/commit/40ffcbec6b65e3a99e40be5f414a3f2c087bffbb))
* **deps:** update actions/setup-go digest to 40f1582 ([#1593](https://github.com/k8sgpt-ai/k8sgpt/issues/1593)) ([a303ffa](https://github.com/k8sgpt-ai/k8sgpt/commit/a303ffa21c7ede3dd9391185bc91fb3b4e8276b6))
* util tests ([#1594](https://github.com/k8sgpt-ai/k8sgpt/issues/1594)) ([21369c5](https://github.com/k8sgpt-ai/k8sgpt/commit/21369c5c0917fd2b6ae4173378b2e257e2b1de7b))
## [0.4.27](https://github.com/k8sgpt-ai/k8sgpt/compare/v0.4.26...v0.4.27) (2025-12-18)
### Features
* mcp v2 ([#1589](https://github.com/k8sgpt-ai/k8sgpt/issues/1589)) ([5480051](https://github.com/k8sgpt-ai/k8sgpt/commit/5480051230ce83b89c0382abd7992c7ecc4a85b8))
## [0.4.26](https://github.com/k8sgpt-ai/k8sgpt/compare/v0.4.25...v0.4.26) (2025-10-16)

View File

@@ -2,7 +2,7 @@
We're happy that you want to contribute to this project. Please read the sections to make the process as smooth as possible.
## Requirements
- Golang `1.23`
- Golang `1.24+`
- An OpenAI API key
* OpenAI API keys can be obtained from [OpenAI](https://platform.openai.com/account/api-keys)
* You can set the API key for k8sgpt using `./k8sgpt auth key`

482
MCP.md Normal file
View File

@@ -0,0 +1,482 @@
# K8sGPT Model Context Protocol (MCP) Server
K8sGPT provides a Model Context Protocol (MCP) server that exposes Kubernetes cluster operations as standardized tools, resources, and prompts for AI assistants like Claude, ChatGPT, and other MCP-compatible clients.
## Table of Contents
- [What is MCP?](#what-is-mcp)
- [Quick Start](#quick-start)
- [Server Modes](#server-modes)
- [Available Tools](#available-tools)
- [Available Resources](#available-resources)
- [Available Prompts](#available-prompts)
- [Usage Examples](#usage-examples)
- [Integration with AI Assistants](#integration-with-ai-assistants)
- [HTTP API Reference](#http-api-reference)
## What is MCP?
The Model Context Protocol (MCP) is an open standard that enables AI assistants to securely connect to external data sources and tools. K8sGPT's MCP server exposes Kubernetes operations through this standardized interface, allowing AI assistants to:
- Analyze cluster health and issues
- Query Kubernetes resources
- Access pod logs and events
- Get troubleshooting guidance
- Manage analyzer filters
## Quick Start
### Start the MCP Server
**Stdio mode (for local AI assistants):**
```bash
k8sgpt serve --mcp
```
**HTTP mode (for network access):**
```bash
k8sgpt serve --mcp --mcp-http --mcp-port 8089
```
### Test with curl
```bash
curl -X POST http://localhost:8089/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list"
}'
```
## Server Modes
### Stdio Mode (Default)
Used by local AI assistants like Claude Desktop:
```bash
k8sgpt serve --mcp
```
Configure in your MCP client (e.g., Claude Desktop's `claude_desktop_config.json`):
```json
{
"mcpServers": {
"k8sgpt": {
"command": "k8sgpt",
"args": ["serve", "--mcp"]
}
}
}
```
### HTTP Mode
Used for network access and webhooks:
```bash
k8sgpt serve --mcp --mcp-http --mcp-port 8089
```
The server runs in stateless mode, so no session management is required. Each request is independent.
## Available Tools
The MCP server exposes 12 tools for Kubernetes operations:
### Cluster Analysis
**analyze**
- Analyze Kubernetes resources for issues and problems
- Parameters:
- `namespace` (optional): Namespace to analyze
- `explain` (optional): Get AI explanations for issues
- `filters` (optional): Comma-separated list of analyzers to use
**cluster-info**
- Get Kubernetes cluster information and version
### Resource Management
**list-resources**
- List Kubernetes resources of a specific type
- Parameters:
- `resourceType` (required): Type of resource (pods, deployments, services, nodes, jobs, cronjobs, statefulsets, daemonsets, replicasets, configmaps, secrets, ingresses, pvcs, pvs)
- `namespace` (optional): Namespace to query
- `labelSelector` (optional): Label selector for filtering
**get-resource**
- Get detailed information about a specific Kubernetes resource
- Parameters:
- `resourceType` (required): Type of resource
- `name` (required): Resource name
- `namespace` (optional): Namespace
**list-namespaces**
- List all namespaces in the cluster
### Debugging and Troubleshooting
**get-logs**
- Get logs from a pod container
- Parameters:
- `podName` (required): Name of the pod
- `namespace` (optional): Namespace
- `container` (optional): Container name
- `tail` (optional): Number of lines to show
- `previous` (optional): Show logs from previous container instance
- `sinceSeconds` (optional): Show logs from last N seconds
**list-events**
- List Kubernetes events for debugging
- Parameters:
- `namespace` (optional): Namespace to query
- `involvedObjectName` (optional): Filter by object name
- `involvedObjectKind` (optional): Filter by object kind
### Analyzer Management
**list-filters**
- List all available and active analyzers/filters
**add-filters**
- Add filters to enable specific analyzers
- Parameters:
- `filters` (required): Comma-separated list of analyzer names
**remove-filters**
- Remove filters to disable specific analyzers
- Parameters:
- `filters` (required): Comma-separated list of analyzer names
### Integrations
**list-integrations**
- List available integrations (Prometheus, AWS, Keda, Kyverno, etc.)
### Configuration
**config**
- Configure K8sGPT settings including custom analyzers and cache
## Available Resources
Resources provide read-only access to cluster information:
**cluster-info**
- URI: `cluster-info`
- Get information about the Kubernetes cluster
**namespaces**
- URI: `namespaces`
- List all namespaces in the cluster
**active-filters**
- URI: `active-filters`
- Get currently active analyzers/filters
## Available Prompts
Prompts provide guided troubleshooting workflows:
**troubleshoot-pod**
- Interactive pod debugging workflow
- Arguments:
- `podName` (required): Name of the pod to troubleshoot
- `namespace` (required): Namespace of the pod
**troubleshoot-deployment**
- Interactive deployment debugging workflow
- Arguments:
- `deploymentName` (required): Name of the deployment
- `namespace` (required): Namespace of the deployment
**troubleshoot-cluster**
- General cluster troubleshooting workflow
## Usage Examples
### Example 1: Analyze a Namespace
```bash
curl -X POST http://localhost:8089/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "analyze",
"arguments": {
"namespace": "production",
"explain": "true"
}
}
}'
```
### Example 2: List Pods
```bash
curl -X POST http://localhost:8089/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "list-resources",
"arguments": {
"resourceType": "pods",
"namespace": "default"
}
}
}'
```
### Example 3: Get Pod Logs
```bash
curl -X POST http://localhost:8089/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "get-logs",
"arguments": {
"podName": "nginx-abc123",
"namespace": "default",
"tail": "100"
}
}
}'
```
### Example 4: Access a Resource
```bash
curl -X POST http://localhost:8089/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 4,
"method": "resources/read",
"params": {
"uri": "namespaces"
}
}'
```
### Example 5: Get a Troubleshooting Prompt
```bash
curl -X POST http://localhost:8089/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 5,
"method": "prompts/get",
"params": {
"name": "troubleshoot-pod",
"arguments": {
"podName": "nginx-abc123",
"namespace": "default"
}
}
}'
```
## Integration with AI Assistants
### Claude Desktop
Add to `claude_desktop_config.json`:
```json
{
"mcpServers": {
"k8sgpt": {
"command": "k8sgpt",
"args": ["serve", "--mcp"]
}
}
}
```
Restart Claude Desktop and you'll see k8sgpt tools available in the tool selector.
### Custom MCP Clients
Any MCP-compatible client can connect to the k8sgpt server. For HTTP-based clients:
1. Start the server: `k8sgpt serve --mcp --mcp-http --mcp-port 8089`
2. Connect to: `http://localhost:8089/mcp`
3. Use standard MCP protocol methods: `tools/list`, `tools/call`, `resources/read`, `prompts/get`
## HTTP API Reference
### Endpoint
```
POST http://localhost:8089/mcp
Content-Type: application/json
```
### Request Format
All requests follow the JSON-RPC 2.0 format:
```json
{
"jsonrpc": "2.0",
"id": 1,
"method": "method_name",
"params": {
...
}
}
```
### Discovery Methods
**List Tools**
```json
{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}
```
**List Resources**
```json
{"jsonrpc": "2.0", "id": 2, "method": "resources/list"}
```
**List Prompts**
```json
{"jsonrpc": "2.0", "id": 3, "method": "prompts/list"}
```
### Tool Invocation
```json
{
"jsonrpc": "2.0",
"id": 4,
"method": "tools/call",
"params": {
"name": "tool_name",
"arguments": {
"arg1": "value1",
"arg2": "value2"
}
}
}
```
### Resource Access
```json
{
"jsonrpc": "2.0",
"id": 5,
"method": "resources/read",
"params": {
"uri": "resource_uri"
}
}
```
### Prompt Access
```json
{
"jsonrpc": "2.0",
"id": 6,
"method": "prompts/get",
"params": {
"name": "prompt_name",
"arguments": {
"arg1": "value1"
}
}
}
```
### Response Format
Successful responses:
```json
{
"jsonrpc": "2.0",
"id": 1,
"result": {
...
}
}
```
Error responses:
```json
{
"jsonrpc": "2.0",
"id": 1,
"error": {
"code": -32600,
"message": "Error description"
}
}
```
## Advanced Configuration
### Custom Port
```bash
k8sgpt serve --mcp --mcp-http --mcp-port 9000
```
### With Specific Backend
```bash
k8sgpt serve --mcp --backend openai
```
### With Kubeconfig
```bash
k8sgpt serve --mcp --kubeconfig ~/.kube/config
```
## Troubleshooting
### Connection Issues
Verify the server is running:
```bash
curl http://localhost:8089/mcp
```
### Permission Issues
Ensure your kubeconfig has appropriate cluster access:
```bash
kubectl cluster-info
```
### Tool Errors
List available tools to verify names:
```bash
curl -X POST http://localhost:8089/mcp \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}'
```
## Learn More
- [MCP Specification](https://modelcontextprotocol.io/)
- [K8sGPT Documentation](https://docs.k8sgpt.ai/)
- [MCP Go Library](https://github.com/mark3labs/mcp-go)

View File

@@ -21,6 +21,10 @@ It has SRE experience codified into its analyzers and helps to pull out the most
_Out of the box integration with OpenAI, Azure, Cohere, Amazon Bedrock, Google Gemini and local models._
> **Sister project:** Check out [sympozium](https://github.com/AlexsJones/sympozium/) for managing agents in Kubernetes.
<a href="https://www.producthunt.com/posts/k8sgpt?utm_source=badge-featured&utm_medium=badge&utm_souce=badge-k8sgpt" target="_blank"><img src="https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=389489&theme=light" alt="K8sGPT - K8sGPT&#0032;gives&#0032;Kubernetes&#0032;Superpowers&#0032;to&#0032;everyone | Product Hunt" style="width: 250px; height: 54px;" width="250" height="54" /></a> <a href="https://hellogithub.com/repository/9dfe44c18dfb4d6fa0181baf8b2cf2e1" target="_blank"><img src="https://abroad.hellogithub.com/v1/widgets/recommend.svg?rid=9dfe44c18dfb4d6fa0181baf8b2cf2e1&claim_uid=gqG4wmzkMrP0eFy" alt="FeaturedHelloGitHub" style="width: 250px; height: 54px;" width="250" height="54" /></a>
@@ -34,6 +38,7 @@ _Out of the box integration with OpenAI, Azure, Cohere, Amazon Bedrock, Google G
- [Examples](#examples)
- [LLM AI Backends](#llm-ai-backends)
- [Key Features](#key-features)
- [Model Context Protocol (MCP)](#model-context-protocol-mcp)
- [Documentation](#documentation)
- [Contributing](#contributing)
- [Community](#community)
@@ -62,7 +67,7 @@ brew install k8sgpt
<!---x-release-please-start-version-->
```
sudo rpm -ivh https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.4.26/k8sgpt_386.rpm
sudo rpm -ivh https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.4.30/k8sgpt_386.rpm
```
<!---x-release-please-end-->
@@ -70,7 +75,7 @@ brew install k8sgpt
<!---x-release-please-start-version-->
```
sudo rpm -ivh https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.4.26/k8sgpt_amd64.rpm
sudo rpm -ivh https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.4.30/k8sgpt_amd64.rpm
```
<!---x-release-please-end-->
</details>
@@ -83,7 +88,7 @@ brew install k8sgpt
<!---x-release-please-start-version-->
```
curl -LO https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.4.26/k8sgpt_386.deb
curl -LO https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.4.30/k8sgpt_386.deb
sudo dpkg -i k8sgpt_386.deb
```
@@ -94,7 +99,7 @@ sudo dpkg -i k8sgpt_386.deb
<!---x-release-please-start-version-->
```
curl -LO https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.4.26/k8sgpt_amd64.deb
curl -LO https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.4.30/k8sgpt_amd64.deb
sudo dpkg -i k8sgpt_amd64.deb
```
@@ -109,7 +114,7 @@ sudo dpkg -i k8sgpt_amd64.deb
<!---x-release-please-start-version-->
```
wget https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.4.26/k8sgpt_386.apk
wget https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.4.30/k8sgpt_386.apk
apk add --allow-untrusted k8sgpt_386.apk
```
<!---x-release-please-end-->
@@ -118,7 +123,7 @@ sudo dpkg -i k8sgpt_amd64.deb
<!---x-release-please-start-version-->
```
wget https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.4.26/k8sgpt_amd64.apk
wget https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.4.30/k8sgpt_amd64.apk
apk add --allow-untrusted k8sgpt_amd64.apk
```
<!---x-release-please-end-->
@@ -699,7 +704,30 @@ k8sgpt custom-analyzer remove --names "my-custom-analyzer,my-custom-analyzer-2"
```
</details>
## Model Context Protocol (MCP)
K8sGPT provides a Model Context Protocol server that exposes Kubernetes operations as standardized tools for AI assistants like Claude, ChatGPT, and other MCP-compatible clients.
**Start the MCP server:**
Stdio mode (for local AI assistants):
```bash
k8sgpt serve --mcp
```
HTTP mode (for network access):
```bash
k8sgpt serve --mcp --mcp-http --mcp-port 8089
```
**Features:**
- 12 tools for cluster analysis, resource management, and debugging
- 3 resources for cluster information access
- 3 interactive troubleshooting prompts
- Stateless HTTP mode for one-off invocations
- Full integration with Claude Desktop and other MCP clients
**Learn more:** See [MCP.md](MCP.md) for complete documentation, usage examples, and integration guides.
## Documentation
Find our official documentation available [here](https://docs.k8sgpt.ai)

View File

@@ -75,6 +75,9 @@ K8sGPT supports a variety of AI/LLM providers (backends). Some providers have a
- **Supported Models:**
- ibm/granite-13b-chat-v2
### Groq
- **Model:** User-configurable (any model supported by Groq, e.g., `llama-3.3-70b-versatile`, `mixtral-8x7b-32768`)
---
For more details on configuring each provider and model, refer to the official K8sGPT documentation and the provider's own documentation.

View File

@@ -203,6 +203,11 @@ var ServeCmd = &cobra.Command{
}()
}
// Allow metrics port to be overridden by environment variable
if envMetricsPort := os.Getenv("K8SGPT_METRICS_PORT"); envMetricsPort != "" && !cmd.Flags().Changed("metrics-port") {
metricsPort = envMetricsPort
}
server := k8sgptserver.Config{
Backend: aiProvider.Name,
Port: port,
@@ -234,7 +239,7 @@ var ServeCmd = &cobra.Command{
func init() {
// add flag for backend
ServeCmd.Flags().StringVarP(&port, "port", "p", "8080", "Port to run the server on")
ServeCmd.Flags().StringVarP(&metricsPort, "metrics-port", "", "8081", "Port to run the metrics-server on")
ServeCmd.Flags().StringVarP(&metricsPort, "metrics-port", "m", "8081", "Port to run the metrics-server on (env: K8SGPT_METRICS_PORT)")
ServeCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider")
ServeCmd.Flags().BoolVarP(&enableHttp, "http", "", false, "Enable REST/http using gppc-gateway")
ServeCmd.Flags().BoolVarP(&enableMCP, "mcp", "", false, "Enable Mission Control Protocol server")

View File

@@ -9,7 +9,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
FROM golang:1.23-alpine3.19 AS builder
FROM golang:1.24-alpine3.23 AS builder
ENV CGO_ENABLED=0
ARG VERSION

367
go.mod
View File

@@ -1,298 +1,293 @@
module github.com/k8sgpt-ai/k8sgpt
go 1.23.3
go 1.25.0
toolchain go1.26.1
require (
github.com/fatih/color v1.18.0
github.com/kedacore/keda/v2 v2.16.0
github.com/magiconair/properties v1.8.9
github.com/mittwald/go-helm-client v0.12.14
github.com/ollama/ollama v0.5.1
github.com/sashabaranov/go-openai v1.36.0
github.com/schollz/progressbar/v3 v3.17.1
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.10.0
golang.org/x/term v0.30.0
helm.sh/helm/v3 v3.17.4
k8s.io/api v0.32.2
k8s.io/apimachinery v0.32.2
k8s.io/client-go v0.32.2
k8s.io/kubectl v0.32.2 // indirect
github.com/magiconair/properties v1.8.10
github.com/mittwald/go-helm-client v0.12.19
github.com/ollama/ollama v0.18.1
github.com/sashabaranov/go-openai v1.41.2
github.com/schollz/progressbar/v3 v3.19.0
github.com/spf13/cobra v1.10.2
github.com/spf13/viper v1.21.0
github.com/stretchr/testify v1.11.1
golang.org/x/term v0.41.0
helm.sh/helm/v3 v3.19.0
k8s.io/api v0.35.2
k8s.io/apimachinery v0.35.2
k8s.io/client-go v0.35.2
k8s.io/kubectl v0.34.0 // indirect
)
require github.com/adrg/xdg v0.5.3
require (
buf.build/gen/go/interplex-ai/schemas/grpc/go v1.5.1-20241117203254-a91193b62179.1
buf.build/gen/go/interplex-ai/schemas/protocolbuffers/go v1.35.2-20241117203254-a91193b62179.1
buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc-ecosystem/gateway/v2 v2.24.0-20241118152629-1379a5a1889d.1
buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.5.1-20241118152629-1379a5a1889d.1
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.35.2-20241118152629-1379a5a1889d.1
cloud.google.com/go/storage v1.48.0
cloud.google.com/go/vertexai v0.13.2
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.1
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.5.0
github.com/IBM/watsonx-go v1.0.1
github.com/agiledragon/gomonkey/v2 v2.13.0
github.com/aws/aws-sdk-go v1.55.7
github.com/aws/aws-sdk-go-v2 v1.36.3
github.com/aws/aws-sdk-go-v2/config v1.29.14
github.com/aws/aws-sdk-go-v2/service/bedrock v1.33.0
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.30.0
github.com/cohere-ai/cohere-go/v2 v2.12.2
buf.build/gen/go/interplex-ai/schemas/grpc/go v1.6.1-20241117203254-a91193b62179.1
buf.build/gen/go/interplex-ai/schemas/protocolbuffers/go v1.36.11-20241117203254-a91193b62179.1
buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc-ecosystem/gateway/v2 v2.28.0-20241118152629-1379a5a1889d.1
buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.6.1-20241118152629-1379a5a1889d.1
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.36.11-20241118152629-1379a5a1889d.1
cloud.google.com/go/storage v1.61.3
cloud.google.com/go/vertexai v0.17.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.13.1
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.4
github.com/IBM/watsonx-go v1.0.2
github.com/agiledragon/gomonkey/v2 v2.14.0
github.com/aws/aws-sdk-go v1.55.8
github.com/aws/aws-sdk-go-v2 v1.41.4
github.com/aws/aws-sdk-go-v2/config v1.32.12
github.com/aws/aws-sdk-go-v2/service/bedrock v1.57.0
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.50.2
github.com/aws/smithy-go v1.24.2
github.com/cohere-ai/cohere-go/v2 v2.16.2
github.com/go-logr/zapr v1.3.0
github.com/google/generative-ai-go v0.19.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.25.1
github.com/google/generative-ai-go v0.20.1
github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0
github.com/hupe1980/go-huggingface v0.0.15
github.com/kyverno/policy-reporter-kyverno-plugin v1.6.4
github.com/mark3labs/mcp-go v0.36.0
github.com/mark3labs/mcp-go v0.45.0
github.com/olekukonko/tablewriter v0.0.5
github.com/oracle/oci-go-sdk/v65 v65.79.0
github.com/prometheus/prometheus v0.302.1
github.com/pterm/pterm v0.12.80
google.golang.org/api v0.218.0
github.com/oracle/oci-go-sdk/v65 v65.109.2
github.com/prometheus/prometheus v0.310.0
github.com/pterm/pterm v0.12.83
google.golang.org/api v0.272.0
gopkg.in/yaml.v2 v2.4.0
sigs.k8s.io/controller-runtime v0.19.3
sigs.k8s.io/gateway-api v1.2.1
sigs.k8s.io/controller-runtime v0.23.3
sigs.k8s.io/gateway-api v1.5.1
)
require (
atomicgo.dev/cursor v0.2.0 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/schedule v0.1.0 // indirect
cel.dev/expr v0.19.0 // indirect
cloud.google.com/go v0.116.0 // indirect
cel.dev/expr v0.25.1 // indirect
cloud.google.com/go v0.123.0 // indirect
cloud.google.com/go/ai v0.8.0 // indirect
cloud.google.com/go/aiplatform v1.69.0 // indirect
cloud.google.com/go/auth v0.14.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.7 // indirect
cloud.google.com/go/compute/metadata v0.6.0 // indirect
cloud.google.com/go/iam v1.2.2 // indirect
cloud.google.com/go/longrunning v0.6.2 // indirect
cloud.google.com/go/monitoring v1.21.2 // indirect
cloud.google.com/go/aiplatform v1.120.0 // indirect
cloud.google.com/go/auth v0.18.2 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.8 // indirect
cloud.google.com/go/compute/metadata v0.9.0 // indirect
cloud.google.com/go/iam v1.5.3 // indirect
cloud.google.com/go/longrunning v0.8.0 // indirect
cloud.google.com/go/monitoring v1.24.3 // indirect
dario.cat/mergo v1.0.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.3.2 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.25.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.48.1 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.48.1 // indirect
github.com/Microsoft/hcsshim v0.12.4 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.21.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.6.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.30.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.55.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.55.0 // indirect
github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.10 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.67 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.30 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.34 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.34 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.3 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.3 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.15 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.25.3 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.30.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.33.19 // indirect
github.com/aws/smithy-go v1.22.2 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.7 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.19.12 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.20 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.20 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.20 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.6 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.7 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.20 // indirect
github.com/aws/aws-sdk-go-v2/service/signin v1.0.8 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.30.13 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.17 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.41.9 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
github.com/cncf/xds/go v0.0.0-20240905190251-b4127c9b8d78 // indirect
github.com/containerd/console v1.0.4 // indirect
github.com/containerd/continuity v0.4.3 // indirect
github.com/containerd/errdefs v0.3.0 // indirect
github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
github.com/cncf/xds/go v0.0.0-20251210132809-ee656c7534f5 // indirect
github.com/containerd/console v1.0.5 // indirect
github.com/containerd/errdefs v1.0.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
github.com/creack/pty v1.1.21 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
github.com/envoyproxy/go-control-plane v0.13.1 // indirect
github.com/envoyproxy/protoc-gen-validate v1.2.1 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/expr-lang/expr v1.17.2 // indirect
github.com/envoyproxy/go-control-plane/envoy v1.36.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.3.0 // indirect
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
github.com/expr-lang/expr v1.17.7 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
github.com/go-jose/go-jose/v4 v4.1.3 // indirect
github.com/go-openapi/swag/cmdutils v0.25.4 // indirect
github.com/go-openapi/swag/conv v0.25.4 // indirect
github.com/go-openapi/swag/fileutils v0.25.4 // indirect
github.com/go-openapi/swag/jsonname v0.25.4 // indirect
github.com/go-openapi/swag/jsonutils v0.25.4 // indirect
github.com/go-openapi/swag/loading v0.25.4 // indirect
github.com/go-openapi/swag/mangling v0.25.4 // indirect
github.com/go-openapi/swag/netutils v0.25.4 // indirect
github.com/go-openapi/swag/stringutils v0.25.4 // indirect
github.com/go-openapi/swag/typeutils v0.25.4 // indirect
github.com/go-openapi/swag/yamlutils v0.25.4 // indirect
github.com/go-viper/mapstructure/v2 v2.5.0 // indirect
github.com/gofrs/flock v0.12.1 // indirect
github.com/golang-jwt/jwt/v5 v5.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
github.com/golang-jwt/jwt/v5 v5.3.1 // indirect
github.com/google/gnostic-models v0.7.0 // indirect
github.com/google/s2a-go v0.1.9 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/googleapis/gax-go/v2 v2.14.1 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.14 // indirect
github.com/googleapis/gax-go/v2 v2.18.0 // indirect
github.com/gookit/color v1.6.0 // indirect
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 // indirect
github.com/grafana/regexp v0.0.0-20250905093917-f7b3be9d1853 // indirect
github.com/invopop/jsonschema v0.13.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/moby/sys/mountinfo v0.7.1 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
github.com/prometheus/sigv4 v0.1.1 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/prometheus/client_golang/exp v0.0.0-20260108101519-fb0838f53562 // indirect
github.com/prometheus/otlptranslator v1.0.0 // indirect
github.com/prometheus/sigv4 v0.4.1 // indirect
github.com/sagikazarmark/locafero v0.11.0 // indirect
github.com/santhosh-tekuri/jsonschema/v6 v6.0.2 // indirect
github.com/segmentio/fasthash v1.0.3 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/sony/gobreaker v0.5.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
github.com/spiffe/go-spiffe/v2 v2.6.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/detectors/gcp v1.32.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.54.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0 // indirect
go.opentelemetry.io/otel/metric v1.34.0 // indirect
go.opentelemetry.io/otel/sdk v1.34.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.32.0 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/contrib/detectors/gcp v1.39.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.63.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.65.0 // indirect
go.opentelemetry.io/otel/metric v1.40.0 // indirect
go.opentelemetry.io/otel/sdk v1.40.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.40.0 // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/genproto v0.0.0-20241118233622-e639e219e697 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250115164207-1a7da9e5054f // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
google.golang.org/genai v1.50.0 // indirect
google.golang.org/genproto v0.0.0-20260217215200-42d3e9bedb6d // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260226221140-a57be14db171 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260311181403-84a4fc48630c // indirect
gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
knative.dev/pkg v0.0.0-20241026180704-25f6002b00f3 // indirect
oras.land/oras-go/v2 v2.6.0 // indirect
sigs.k8s.io/randfill v1.0.0 // indirect
sigs.k8s.io/structured-merge-diff/v6 v6.3.2 // indirect
)
require (
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c // indirect
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect
github.com/BurntSushi/toml v1.5.0 // indirect
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.3.0 // indirect
github.com/Masterminds/semver/v3 v3.4.0 // indirect
github.com/Masterminds/sprig/v3 v3.3.0 // indirect
github.com/Masterminds/squirrel v1.5.4 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chai2010/gettext-go v1.0.3 // indirect
github.com/containerd/containerd v1.7.24 // indirect
github.com/cyphar/filepath-securejoin v0.3.6 // indirect
github.com/containerd/containerd v1.7.29 // indirect
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/docker/cli v26.1.4+incompatible // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v27.4.1+incompatible // indirect
github.com/docker/docker-credential-helpers v0.8.2 // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
github.com/emicklei/go-restful/v3 v3.13.0 // indirect
github.com/evanphx/json-patch v5.9.11+incompatible // indirect
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/go-errors/errors v1.5.1 // indirect
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-openapi/jsonpointer v0.22.4 // indirect
github.com/go-openapi/jsonreference v0.21.4 // indirect
github.com/go-openapi/swag v0.25.4 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/gnostic v0.7.0
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/gnostic v0.7.1
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/gosuri/uitable v0.0.4 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.5.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmoiron/sqlx v1.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/klauspost/compress v1.18.3 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mailru/easyjson v0.9.1 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mattn/go-runewidth v0.0.20 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/moby/spdystream v0.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/moby/term v0.5.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/opencontainers/image-spec v1.1.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.21.0-rc.0
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/prometheus/client_golang v1.23.2
github.com/prometheus/client_model v0.6.2 // indirect
github.com/prometheus/common v0.67.5 // indirect
github.com/prometheus/procfs v0.16.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/robfig/cron/v3 v3.0.1
github.com/rubenv/sql-migrate v1.7.1 // indirect
github.com/rubenv/sql-migrate v1.8.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/sirupsen/logrus v1.9.4 // indirect
github.com/spf13/afero v1.15.0 // indirect
github.com/spf13/cast v1.10.0 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.opentelemetry.io/otel v1.34.0 // indirect
go.opentelemetry.io/otel/trace v1.34.0 // indirect
go.opentelemetry.io/otel v1.40.0 // indirect
go.opentelemetry.io/otel/trace v1.40.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.36.0 // indirect
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 // indirect
golang.org/x/net v0.38.0
golang.org/x/oauth2 v0.25.0 // indirect
golang.org/x/sync v0.12.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.org/x/time v0.9.0 // indirect
google.golang.org/grpc v1.70.0
google.golang.org/protobuf v1.36.4 // indirect
go.uber.org/zap v1.27.1
golang.org/x/crypto v0.49.0 // indirect
golang.org/x/net v0.52.0
golang.org/x/oauth2 v0.36.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.42.0 // indirect
golang.org/x/text v0.35.0 // indirect
golang.org/x/time v0.15.0 // indirect
google.golang.org/grpc v1.79.3
google.golang.org/protobuf v1.36.11 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
k8s.io/apiextensions-apiserver v0.32.2
k8s.io/apiserver v0.32.2 // indirect
k8s.io/cli-runtime v0.32.2 // indirect
k8s.io/component-base v0.32.2 // indirect
k8s.io/apiextensions-apiserver v0.35.2
k8s.io/apiserver v0.35.2 // indirect
k8s.io/cli-runtime v0.34.0 // indirect
k8s.io/component-base v0.35.2 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f // indirect
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397
oras.land/oras-go v1.2.5 // indirect
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
sigs.k8s.io/kustomize/api v0.18.0 // indirect
sigs.k8s.io/kustomize/kyaml v0.18.1 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.2 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
k8s.io/kube-openapi v0.0.0-20250910181357-589584f1c912 // indirect
k8s.io/utils v0.0.0-20260210185600-b8788abfbbc2
sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 // indirect
sigs.k8s.io/kustomize/api v0.20.1 // indirect
sigs.k8s.io/kustomize/kyaml v0.20.1 // indirect
sigs.k8s.io/yaml v1.6.0 // indirect
)
// v1.2.0 is taken from github.com/open-policy-agent/opa v0.42.0
// v1.2.0 incompatible with github.com/docker/docker v23.0.0-rc.1+incompatible
//replace oras.land/oras-go => oras.land/oras-go v1.2.4
replace github.com/docker/docker => github.com/docker/docker v28.0.4+incompatible
replace github.com/docker/docker => github.com/docker/docker v28.5.2+incompatible
replace dario.cat/mergo => github.com/imdario/mergo v1.0.1
replace dario.cat/mergo => github.com/imdario/mergo v1.0.2

2418
go.sum

File diff suppressed because it is too large Load Diff

102
pkg/ai/groq.go Normal file
View File

@@ -0,0 +1,102 @@
/*
Copyright 2023 The K8sGPT Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ai
import (
"context"
"errors"
"net/http"
"net/url"
"github.com/sashabaranov/go-openai"
)
const groqAIClientName = "groq"
// Default Groq API endpoint (OpenAI-compatible)
const groqAPIBaseURL = "https://api.groq.com/openai/v1"
type GroqClient struct {
nopCloser
client *openai.Client
model string
temperature float32
topP float32
}
func (c *GroqClient) Configure(config IAIConfig) error {
token := config.GetPassword()
defaultConfig := openai.DefaultConfig(token)
proxyEndpoint := config.GetProxyEndpoint()
baseURL := config.GetBaseURL()
if baseURL != "" {
defaultConfig.BaseURL = baseURL
} else {
defaultConfig.BaseURL = groqAPIBaseURL
}
transport := &http.Transport{}
if proxyEndpoint != "" {
proxyUrl, err := url.Parse(proxyEndpoint)
if err != nil {
return err
}
transport.Proxy = http.ProxyURL(proxyUrl)
}
customHeaders := config.GetCustomHeaders()
defaultConfig.HTTPClient = &http.Client{
Transport: &OpenAIHeaderTransport{
Origin: transport,
Headers: customHeaders,
},
}
client := openai.NewClientWithConfig(defaultConfig)
if client == nil {
return errors.New("error creating Groq client")
}
c.client = client
c.model = config.GetModel()
c.temperature = config.GetTemperature()
c.topP = config.GetTopP()
return nil
}
func (c *GroqClient) GetCompletion(ctx context.Context, prompt string) (string, error) {
resp, err := c.client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
Model: c.model,
Messages: []openai.ChatCompletionMessage{
{
Role: "user",
Content: prompt,
},
},
Temperature: c.temperature,
MaxTokens: maxToken,
PresencePenalty: presencePenalty,
FrequencyPenalty: frequencyPenalty,
TopP: c.topP,
})
if err != nil {
return "", err
}
return resp.Choices[0].Message.Content, nil
}
func (c *GroqClient) GetName() string {
return groqAIClientName
}

View File

@@ -34,6 +34,7 @@ var (
&OCIGenAIClient{},
&CustomRestClient{},
&IBMWatsonxAIClient{},
&GroqClient{},
}
Backends = []string{
openAIClientName,
@@ -50,6 +51,7 @@ var (
ociClientName,
CustomRestClientName,
ibmWatsonxAIClientName,
groqAIClientName,
}
)

View File

@@ -95,7 +95,7 @@ func (c *OpenAIClient) GetCompletion(ctx context.Context, prompt string) (string
},
},
Temperature: c.temperature,
MaxTokens: maxToken,
MaxCompletionTokens: maxToken,
PresencePenalty: presencePenalty,
FrequencyPenalty: frequencyPenalty,
TopP: c.topP,

View File

@@ -16,6 +16,7 @@ package analysis
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"reflect"
@@ -34,6 +35,7 @@ import (
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
"github.com/schollz/progressbar/v3"
"github.com/spf13/viper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type Analysis struct {
@@ -226,6 +228,15 @@ func (a *Analysis) CustomAnalyzersAreAvailable() bool {
}
func (a *Analysis) RunCustomAnalysis() {
// Validate namespace if specified, consistent with built-in filter behavior
if a.Namespace != "" && a.Client != nil {
_, err := a.Client.Client.CoreV1().Namespaces().Get(a.Context, a.Namespace, metav1.GetOptions{})
if err != nil {
a.Errors = append(a.Errors, fmt.Sprintf("namespace %q not found: %s", a.Namespace, err))
return
}
}
var customAnalyzers []custom.CustomAnalyzer
if err := viper.UnmarshalKey("custom_analyzers", &customAnalyzers); err != nil {
a.Errors = append(a.Errors, err.Error())
@@ -526,7 +537,22 @@ func (a *Analysis) getAIResultForSanitizedFailures(texts []string, promptTmpl st
// Process template.
prompt := fmt.Sprintf(strings.TrimSpace(promptTmpl), a.Language, inputKey)
if a.AIClient.GetName() == ai.CustomRestClientName {
prompt = fmt.Sprintf(ai.PromptMap["raw"], a.Language, inputKey, prompt)
// Use proper JSON marshaling to handle special characters in error messages
// This fixes issues with quotes, newlines, and other special chars in inputKey
customRestPrompt := struct {
Language string `json:"language"`
Message string `json:"message"`
Prompt string `json:"prompt"`
}{
Language: a.Language,
Message: inputKey,
Prompt: prompt,
}
promptBytes, err := json.Marshal(customRestPrompt)
if err != nil {
return "", fmt.Errorf("failed to marshal customrest prompt: %w", err)
}
prompt = string(promptBytes)
}
response, err := a.AIClient.GetCompletion(a.Context, prompt)
if err != nil {

60
pkg/cache/cache_test.go vendored Normal file
View File

@@ -0,0 +1,60 @@
package cache
import (
"os"
"testing"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)
func TestNewReturnsExpectedCache(t *testing.T) {
require.IsType(t, &FileBasedCache{}, New("file"))
require.IsType(t, &AzureCache{}, New("azure"))
require.IsType(t, &GCSCache{}, New("gcs"))
require.IsType(t, &S3Cache{}, New("s3"))
require.IsType(t, &InterplexCache{}, New("interplex"))
// default fallback
require.IsType(t, &FileBasedCache{}, New("unknown"))
}
func TestNewCacheProvider_InterplexAndInvalid(t *testing.T) {
// valid: interplex
cp, err := NewCacheProvider("interplex", "", "", "localhost:1", "", "", "", false)
require.NoError(t, err)
require.Equal(t, "interplex", cp.CurrentCacheType)
require.Equal(t, "localhost:1", cp.Interplex.ConnectionString)
// invalid type
_, err = NewCacheProvider("not-a-type", "", "", "", "", "", "", false)
require.Error(t, err)
}
func TestAddRemoveRemoteCacheAndGet(t *testing.T) {
// isolate viper with temp config file
tmpFile, err := os.CreateTemp("", "k8sgpt-cache-config-*.yaml")
require.NoError(t, err)
defer func() {
_ = os.Remove(tmpFile.Name())
}()
viper.Reset()
viper.SetConfigFile(tmpFile.Name())
// add interplex remote cache
cp := CacheProvider{}
cp.CurrentCacheType = "interplex"
cp.Interplex.ConnectionString = "localhost:1"
require.NoError(t, AddRemoteCache(cp))
// read back via GetCacheConfiguration
c, err := GetCacheConfiguration()
require.NoError(t, err)
require.IsType(t, &InterplexCache{}, c)
// remove remote cache
require.NoError(t, RemoveRemoteCache())
// now default should be file-based
c2, err := GetCacheConfiguration()
require.NoError(t, err)
require.IsType(t, &FileBasedCache{}, c2)
}

77
pkg/cache/file_based_test.go vendored Normal file
View File

@@ -0,0 +1,77 @@
package cache
import (
"os"
"path/filepath"
"testing"
"github.com/adrg/xdg"
"github.com/stretchr/testify/require"
)
// withTempCacheHome sets XDG_CACHE_HOME to a temp dir for test isolation.
func withTempCacheHome(t *testing.T) func() {
t.Helper()
tmp, err := os.MkdirTemp("", "k8sgpt-cache-test-*")
require.NoError(t, err)
old := os.Getenv("XDG_CACHE_HOME")
require.NoError(t, os.Setenv("XDG_CACHE_HOME", tmp))
return func() {
_ = os.Setenv("XDG_CACHE_HOME", old)
_ = os.RemoveAll(tmp)
}
}
func TestFileBasedCache_BasicOps(t *testing.T) {
cleanup := withTempCacheHome(t)
defer cleanup()
c := &FileBasedCache{}
// Configure should be a no-op
require.NoError(t, c.Configure(CacheProvider{}))
require.Equal(t, "file", c.GetName())
require.False(t, c.IsCacheDisabled())
c.DisableCache()
require.True(t, c.IsCacheDisabled())
key := "testkey"
data := "hello"
// Store
require.NoError(t, c.Store(key, data))
// Exists
require.True(t, c.Exists(key))
// Load
got, err := c.Load(key)
require.NoError(t, err)
require.Equal(t, data, got)
// List should include our key file
items, err := c.List()
require.NoError(t, err)
// ensure at least one item and that one matches our key
found := false
for _, it := range items {
if it.Name == key {
found = true
break
}
}
require.True(t, found)
// Remove
require.NoError(t, c.Remove(key))
require.False(t, c.Exists(key))
}
func TestFileBasedCache_PathShape(t *testing.T) {
cleanup := withTempCacheHome(t)
defer cleanup()
// Verify xdg.CacheFile path shape (directory and filename)
p, err := xdg.CacheFile(filepath.Join("k8sgpt", "abc"))
require.NoError(t, err)
require.Equal(t, "abc", filepath.Base(p))
require.Contains(t, p, "k8sgpt")
}

View File

@@ -18,18 +18,31 @@ func TestInterplexCache(t *testing.T) {
}
// Mock GRPC server setup
errChan := make(chan error, 1)
go func() {
lis, err := net.Listen("tcp", ":50051")
if err != nil {
t.Fatalf("failed to listen: %v", err)
errChan <- err
return
}
s := grpc.NewServer()
rpc.RegisterCacheServiceServer(s, &mockCacheService{})
if err := s.Serve(lis); err != nil {
t.Fatalf("failed to serve: %v", err)
errChan <- err
return
}
}()
// Check if server startup failed
select {
case err := <-errChan:
if err != nil {
t.Fatalf("failed to start mock server: %v", err)
}
default:
// Server started successfully
}
t.Run("TestStore", func(t *testing.T) {
err := cache.Store("key1", "value1")
if err != nil {

41
pkg/custom/client_test.go Normal file
View File

@@ -0,0 +1,41 @@
package custom
import (
"context"
"testing"
schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)
// mockAnalyzerClient implements rpc.CustomAnalyzerServiceClient for testing
type mockAnalyzerClient struct{
resp *schemav1.RunResponse
err error
}
func (m *mockAnalyzerClient) Run(ctx context.Context, in *schemav1.RunRequest, opts ...grpc.CallOption) (*schemav1.RunResponse, error) {
return m.resp, m.err
}
func TestClientRunMapsResponse(t *testing.T) {
// prepare fake response
resp := &schemav1.RunResponse{
Result: &schemav1.Result{
Name: "AnalyzerA",
Kind: "Pod",
Details: "details",
ParentObject: "Deployment/foo",
},
}
cli := &Client{analyzerClient: &mockAnalyzerClient{resp: resp}}
got, err := cli.Run()
require.NoError(t, err)
require.Equal(t, "AnalyzerA", got.Name)
require.Equal(t, "Pod", got.Kind)
require.Equal(t, "details", got.Details)
require.Equal(t, "Deployment/foo", got.ParentObject)
require.Len(t, got.Error, 0)
}

View File

@@ -28,6 +28,7 @@ import (
"github.com/mark3labs/mcp-go/server"
"github.com/spf13/viper"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// K8sGptMCPServer represents an MCP server for k8sgpt
@@ -67,6 +68,8 @@ func NewMCPServer(port string, aiProvider *ai.AIProvider, useHTTP bool, logger *
// Create HTTP server with streamable transport
httpOpts := []server.StreamableHTTPOption{
server.WithLogger(&zapLoggerAdapter{logger: logger}),
// Enable stateless mode for one-off tool invocations without session management
server.WithStateLess(true),
}
httpServer := server.NewStreamableHTTPServer(mcpServer, httpOpts...)
@@ -218,6 +221,122 @@ func (s *K8sGptMCPServer) registerToolsAndResources() error {
),
)
s.server.AddTool(configTool, s.handleConfig)
// Register resource listing tools
listResourcesTool := mcp.NewTool("list-resources",
mcp.WithDescription("List Kubernetes resources of a specific type (pods, deployments, services, nodes, etc.)"),
mcp.WithString("resourceType",
mcp.Required(),
mcp.Description("Type of resource to list (e.g., pods, deployments, services, nodes, jobs, etc.)"),
),
mcp.WithString("namespace",
mcp.Description("Namespace to list resources from (empty for all or cluster-scoped resources)"),
),
mcp.WithString("labelSelector",
mcp.Description("Label selector to filter resources (e.g., 'app=myapp')"),
),
)
s.server.AddTool(listResourcesTool, s.handleListResources)
// Register get resource tool
getResourceTool := mcp.NewTool("get-resource",
mcp.WithDescription("Get detailed information about a specific Kubernetes resource"),
mcp.WithString("resourceType",
mcp.Required(),
mcp.Description("Type of resource (e.g., pod, deployment, service)"),
),
mcp.WithString("name",
mcp.Required(),
mcp.Description("Name of the resource"),
),
mcp.WithString("namespace",
mcp.Description("Namespace of the resource (required for namespaced resources)"),
),
)
s.server.AddTool(getResourceTool, s.handleGetResource)
// Register list namespaces tool
listNamespacesTool := mcp.NewTool("list-namespaces",
mcp.WithDescription("List all namespaces in the cluster"),
)
s.server.AddTool(listNamespacesTool, s.handleListNamespaces)
// Register list events tool
listEventsTool := mcp.NewTool("list-events",
mcp.WithDescription("List Kubernetes events for debugging and troubleshooting"),
mcp.WithString("namespace",
mcp.Description("Namespace to list events from (empty for all namespaces)"),
),
mcp.WithString("involvedObjectName",
mcp.Description("Filter events by involved object name (e.g., pod name)"),
),
mcp.WithString("involvedObjectKind",
mcp.Description("Filter events by involved object kind (e.g., Pod, Deployment)"),
),
mcp.WithNumber("limit",
mcp.Description("Maximum number of events to return (default: 100)"),
),
)
s.server.AddTool(listEventsTool, s.handleListEvents)
// Register get logs tool
getLogsTool := mcp.NewTool("get-logs",
mcp.WithDescription("Get logs from a pod container"),
mcp.WithString("podName",
mcp.Required(),
mcp.Description("Name of the pod"),
),
mcp.WithString("namespace",
mcp.Required(),
mcp.Description("Namespace of the pod"),
),
mcp.WithString("container",
mcp.Description("Container name (if pod has multiple containers)"),
),
mcp.WithBoolean("previous",
mcp.Description("Get logs from previous terminated container"),
),
mcp.WithNumber("tailLines",
mcp.Description("Number of lines from the end of logs (default: 100)"),
),
mcp.WithNumber("sinceSeconds",
mcp.Description("Return logs newer than this many seconds"),
),
)
s.server.AddTool(getLogsTool, s.handleGetLogs)
// Register filter management tools
listFiltersTool := mcp.NewTool("list-filters",
mcp.WithDescription("List all available and active analyzers/filters in k8sgpt"),
)
s.server.AddTool(listFiltersTool, s.handleListFilters)
addFiltersTool := mcp.NewTool("add-filters",
mcp.WithDescription("Add filters to enable specific analyzers"),
mcp.WithArray("filters",
mcp.Required(),
mcp.Description("List of filter names to add (e.g., ['Pod', 'Service', 'Deployment'])"),
mcp.WithStringItems(),
),
)
s.server.AddTool(addFiltersTool, s.handleAddFilters)
removeFiltersTool := mcp.NewTool("remove-filters",
mcp.WithDescription("Remove filters to disable specific analyzers"),
mcp.WithArray("filters",
mcp.Required(),
mcp.Description("List of filter names to remove"),
mcp.WithStringItems(),
),
)
s.server.AddTool(removeFiltersTool, s.handleRemoveFilters)
// Register integration management tools
listIntegrationsTool := mcp.NewTool("list-integrations",
mcp.WithDescription("List available integrations (Prometheus, AWS, Keda, Kyverno, etc.)"),
)
s.server.AddTool(listIntegrationsTool, s.handleListIntegrations)
return nil
}
@@ -453,7 +572,26 @@ func (s *K8sGptMCPServer) handleConfig(ctx context.Context, request mcp.CallTool
// registerPrompts registers the prompts for the MCP server
func (s *K8sGptMCPServer) registerPrompts() error {
// Register any prompts needed for the MCP server
// Register troubleshooting prompts
podTroubleshootPrompt := mcp.NewPrompt("troubleshoot-pod",
mcp.WithPromptDescription("Guide for troubleshooting pod issues in Kubernetes"),
mcp.WithArgument("podName"),
mcp.WithArgument("namespace"),
)
s.server.AddPrompt(podTroubleshootPrompt, s.getTroubleshootPodPrompt)
deploymentTroubleshootPrompt := mcp.NewPrompt("troubleshoot-deployment",
mcp.WithPromptDescription("Guide for troubleshooting deployment issues in Kubernetes"),
mcp.WithArgument("deploymentName"),
mcp.WithArgument("namespace"),
)
s.server.AddPrompt(deploymentTroubleshootPrompt, s.getTroubleshootDeploymentPrompt)
generalTroubleshootPrompt := mcp.NewPrompt("troubleshoot-cluster",
mcp.WithPromptDescription("General guide for troubleshooting Kubernetes cluster issues"),
)
s.server.AddPrompt(generalTroubleshootPrompt, s.getTroubleshootClusterPrompt)
return nil
}
@@ -463,8 +601,20 @@ func (s *K8sGptMCPServer) registerResources() error {
mcp.WithResourceDescription("Get information about the Kubernetes cluster"),
mcp.WithMIMEType("application/json"),
)
s.server.AddResource(clusterInfoResource, s.getClusterInfo)
namespacesResource := mcp.NewResource("namespaces", "namespaces",
mcp.WithResourceDescription("List all namespaces in the cluster"),
mcp.WithMIMEType("application/json"),
)
s.server.AddResource(namespacesResource, s.getNamespacesResource)
activeFiltersResource := mcp.NewResource("active-filters", "active-filters",
mcp.WithResourceDescription("Get currently active analyzers/filters"),
mcp.WithMIMEType("application/json"),
)
s.server.AddResource(activeFiltersResource, s.getActiveFiltersResource)
return nil
}
@@ -505,6 +655,72 @@ func (s *K8sGptMCPServer) getClusterInfo(ctx context.Context, request mcp.ReadRe
}, nil
}
func (s *K8sGptMCPServer) getNamespacesResource(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) {
client, err := kubernetes.NewClient("", "")
if err != nil {
return nil, fmt.Errorf("failed to create Kubernetes client: %v", err)
}
namespaces, err := client.Client.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("failed to list namespaces: %v", err)
}
// Extract just the namespace names
names := make([]string, 0, len(namespaces.Items))
for _, ns := range namespaces.Items {
names = append(names, ns.Name)
}
data, err := json.Marshal(map[string]interface{}{
"count": len(names),
"namespaces": names,
})
if err != nil {
return []mcp.ResourceContents{
&mcp.TextResourceContents{
URI: "namespaces",
MIMEType: "text/plain",
Text: "Failed to marshal namespaces",
},
}, nil
}
return []mcp.ResourceContents{
&mcp.TextResourceContents{
URI: "namespaces",
MIMEType: "application/json",
Text: string(data),
},
}, nil
}
func (s *K8sGptMCPServer) getActiveFiltersResource(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) {
activeFilters := viper.GetStringSlice("active_filters")
data, err := json.Marshal(map[string]interface{}{
"activeFilters": activeFilters,
"count": len(activeFilters),
})
if err != nil {
return []mcp.ResourceContents{
&mcp.TextResourceContents{
URI: "active-filters",
MIMEType: "text/plain",
Text: "Failed to marshal active filters",
},
}, nil
}
return []mcp.ResourceContents{
&mcp.TextResourceContents{
URI: "active-filters",
MIMEType: "application/json",
Text: string(data),
},
}, nil
}
// Close closes the MCP server and releases resources
func (s *K8sGptMCPServer) Close() error {
return nil

570
pkg/server/mcp_handlers.go Normal file
View File

@@ -0,0 +1,570 @@
/*
Copyright 2024 The K8sGPT Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package server
import (
"context"
"encoding/json"
"fmt"
"io"
"strings"
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
"github.com/k8sgpt-ai/k8sgpt/pkg/integration"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
"github.com/mark3labs/mcp-go/mcp"
"github.com/spf13/viper"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
// DefaultListLimit is the default maximum number of resources to return
DefaultListLimit = 100
// MaxListLimit is the maximum allowed limit for list operations
MaxListLimit = 1000
)
// resourceLister defines a function that lists Kubernetes resources
type resourceLister func(ctx context.Context, client *kubernetes.Client, namespace string, opts metav1.ListOptions) (interface{}, error)
// resourceGetter defines a function that gets a single Kubernetes resource
type resourceGetter func(ctx context.Context, client *kubernetes.Client, namespace, name string) (interface{}, error)
// resourceRegistry maps resource types to their list and get functions
var resourceRegistry = map[string]struct {
list resourceLister
get resourceGetter
}{
"pod": {
list: func(ctx context.Context, client *kubernetes.Client, namespace string, opts metav1.ListOptions) (interface{}, error) {
return client.Client.CoreV1().Pods(namespace).List(ctx, opts)
},
get: func(ctx context.Context, client *kubernetes.Client, namespace, name string) (interface{}, error) {
return client.Client.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{})
},
},
"deployment": {
list: func(ctx context.Context, client *kubernetes.Client, namespace string, opts metav1.ListOptions) (interface{}, error) {
return client.Client.AppsV1().Deployments(namespace).List(ctx, opts)
},
get: func(ctx context.Context, client *kubernetes.Client, namespace, name string) (interface{}, error) {
return client.Client.AppsV1().Deployments(namespace).Get(ctx, name, metav1.GetOptions{})
},
},
"service": {
list: func(ctx context.Context, client *kubernetes.Client, namespace string, opts metav1.ListOptions) (interface{}, error) {
return client.Client.CoreV1().Services(namespace).List(ctx, opts)
},
get: func(ctx context.Context, client *kubernetes.Client, namespace, name string) (interface{}, error) {
return client.Client.CoreV1().Services(namespace).Get(ctx, name, metav1.GetOptions{})
},
},
"node": {
list: func(ctx context.Context, client *kubernetes.Client, namespace string, opts metav1.ListOptions) (interface{}, error) {
return client.Client.CoreV1().Nodes().List(ctx, opts)
},
get: func(ctx context.Context, client *kubernetes.Client, namespace, name string) (interface{}, error) {
return client.Client.CoreV1().Nodes().Get(ctx, name, metav1.GetOptions{})
},
},
"job": {
list: func(ctx context.Context, client *kubernetes.Client, namespace string, opts metav1.ListOptions) (interface{}, error) {
return client.Client.BatchV1().Jobs(namespace).List(ctx, opts)
},
get: func(ctx context.Context, client *kubernetes.Client, namespace, name string) (interface{}, error) {
return client.Client.BatchV1().Jobs(namespace).Get(ctx, name, metav1.GetOptions{})
},
},
"cronjob": {
list: func(ctx context.Context, client *kubernetes.Client, namespace string, opts metav1.ListOptions) (interface{}, error) {
return client.Client.BatchV1().CronJobs(namespace).List(ctx, opts)
},
get: func(ctx context.Context, client *kubernetes.Client, namespace, name string) (interface{}, error) {
return client.Client.BatchV1().CronJobs(namespace).Get(ctx, name, metav1.GetOptions{})
},
},
"statefulset": {
list: func(ctx context.Context, client *kubernetes.Client, namespace string, opts metav1.ListOptions) (interface{}, error) {
return client.Client.AppsV1().StatefulSets(namespace).List(ctx, opts)
},
get: func(ctx context.Context, client *kubernetes.Client, namespace, name string) (interface{}, error) {
return client.Client.AppsV1().StatefulSets(namespace).Get(ctx, name, metav1.GetOptions{})
},
},
"daemonset": {
list: func(ctx context.Context, client *kubernetes.Client, namespace string, opts metav1.ListOptions) (interface{}, error) {
return client.Client.AppsV1().DaemonSets(namespace).List(ctx, opts)
},
get: func(ctx context.Context, client *kubernetes.Client, namespace, name string) (interface{}, error) {
return client.Client.AppsV1().DaemonSets(namespace).Get(ctx, name, metav1.GetOptions{})
},
},
"replicaset": {
list: func(ctx context.Context, client *kubernetes.Client, namespace string, opts metav1.ListOptions) (interface{}, error) {
return client.Client.AppsV1().ReplicaSets(namespace).List(ctx, opts)
},
get: func(ctx context.Context, client *kubernetes.Client, namespace, name string) (interface{}, error) {
return client.Client.AppsV1().ReplicaSets(namespace).Get(ctx, name, metav1.GetOptions{})
},
},
"configmap": {
list: func(ctx context.Context, client *kubernetes.Client, namespace string, opts metav1.ListOptions) (interface{}, error) {
return client.Client.CoreV1().ConfigMaps(namespace).List(ctx, opts)
},
get: func(ctx context.Context, client *kubernetes.Client, namespace, name string) (interface{}, error) {
return client.Client.CoreV1().ConfigMaps(namespace).Get(ctx, name, metav1.GetOptions{})
},
},
"secret": {
list: func(ctx context.Context, client *kubernetes.Client, namespace string, opts metav1.ListOptions) (interface{}, error) {
return client.Client.CoreV1().Secrets(namespace).List(ctx, opts)
},
get: func(ctx context.Context, client *kubernetes.Client, namespace, name string) (interface{}, error) {
return client.Client.CoreV1().Secrets(namespace).Get(ctx, name, metav1.GetOptions{})
},
},
"ingress": {
list: func(ctx context.Context, client *kubernetes.Client, namespace string, opts metav1.ListOptions) (interface{}, error) {
return client.Client.NetworkingV1().Ingresses(namespace).List(ctx, opts)
},
get: func(ctx context.Context, client *kubernetes.Client, namespace, name string) (interface{}, error) {
return client.Client.NetworkingV1().Ingresses(namespace).Get(ctx, name, metav1.GetOptions{})
},
},
"persistentvolumeclaim": {
list: func(ctx context.Context, client *kubernetes.Client, namespace string, opts metav1.ListOptions) (interface{}, error) {
return client.Client.CoreV1().PersistentVolumeClaims(namespace).List(ctx, opts)
},
get: func(ctx context.Context, client *kubernetes.Client, namespace, name string) (interface{}, error) {
return client.Client.CoreV1().PersistentVolumeClaims(namespace).Get(ctx, name, metav1.GetOptions{})
},
},
"persistentvolume": {
list: func(ctx context.Context, client *kubernetes.Client, namespace string, opts metav1.ListOptions) (interface{}, error) {
return client.Client.CoreV1().PersistentVolumes().List(ctx, opts)
},
get: func(ctx context.Context, client *kubernetes.Client, namespace, name string) (interface{}, error) {
return client.Client.CoreV1().PersistentVolumes().Get(ctx, name, metav1.GetOptions{})
},
},
}
// Resource type aliases for convenience
var resourceTypeAliases = map[string]string{
"pods": "pod",
"deployments": "deployment",
"services": "service",
"svc": "service",
"nodes": "node",
"jobs": "job",
"cronjobs": "cronjob",
"statefulsets": "statefulset",
"sts": "statefulset",
"daemonsets": "daemonset",
"ds": "daemonset",
"replicasets": "replicaset",
"rs": "replicaset",
"configmaps": "configmap",
"cm": "configmap",
"secrets": "secret",
"ingresses": "ingress",
"ing": "ingress",
"persistentvolumeclaims": "persistentvolumeclaim",
"pvc": "persistentvolumeclaim",
"persistentvolumes": "persistentvolume",
"pv": "persistentvolume",
}
// normalizeResourceType converts resource type variants to canonical form
func normalizeResourceType(resourceType string) (string, error) {
normalized := strings.ToLower(resourceType)
// Check if it's an alias
if canonical, ok := resourceTypeAliases[normalized]; ok {
normalized = canonical
}
// Check if it's a known resource type
if _, ok := resourceRegistry[normalized]; !ok {
return "", fmt.Errorf("unsupported resource type: %s", resourceType)
}
return normalized, nil
}
// marshalJSON marshals data to JSON with proper error handling
func marshalJSON(data interface{}) (string, error) {
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
return "", fmt.Errorf("failed to marshal JSON: %w", err)
}
return string(jsonData), nil
}
// handleListResources lists Kubernetes resources of a specific type
func (s *K8sGptMCPServer) handleListResources(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
var req struct {
ResourceType string `json:"resourceType"`
Namespace string `json:"namespace,omitempty"`
LabelSelector string `json:"labelSelector,omitempty"`
Limit int64 `json:"limit,omitempty"`
}
if err := request.BindArguments(&req); err != nil {
return mcp.NewToolResultErrorf("Failed to parse request arguments: %v", err), nil
}
if req.ResourceType == "" {
return mcp.NewToolResultErrorf("resourceType is required"), nil
}
// Normalize and validate resource type
resourceType, err := normalizeResourceType(req.ResourceType)
if err != nil {
supportedTypes := make([]string, 0, len(resourceRegistry))
for key := range resourceRegistry {
supportedTypes = append(supportedTypes, key)
}
return mcp.NewToolResultErrorf("%v. Supported types: %v", err, supportedTypes), nil
}
// Set default and validate limit
if req.Limit == 0 {
req.Limit = DefaultListLimit
} else if req.Limit > MaxListLimit {
req.Limit = MaxListLimit
}
client, err := kubernetes.NewClient("", "")
if err != nil {
return mcp.NewToolResultErrorf("Failed to create Kubernetes client: %v", err), nil
}
listOptions := metav1.ListOptions{
LabelSelector: req.LabelSelector,
Limit: req.Limit,
}
// Get the list function from registry
listFunc := resourceRegistry[resourceType].list
result, err := listFunc(ctx, client, req.Namespace, listOptions)
if err != nil {
return mcp.NewToolResultErrorf("Failed to list %s: %v", resourceType, err), nil
}
// Extract items from the result (all list types have an Items field)
resultJSON, err := marshalJSON(result)
if err != nil {
return mcp.NewToolResultErrorf("Failed to serialize result: %v", err), nil
}
return mcp.NewToolResultText(resultJSON), nil
}
// handleGetResource gets detailed information about a specific resource
func (s *K8sGptMCPServer) handleGetResource(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
var req struct {
ResourceType string `json:"resourceType"`
Name string `json:"name"`
Namespace string `json:"namespace,omitempty"`
}
if err := request.BindArguments(&req); err != nil {
return mcp.NewToolResultErrorf("Failed to parse request arguments: %v", err), nil
}
if req.ResourceType == "" {
return mcp.NewToolResultErrorf("resourceType is required"), nil
}
if req.Name == "" {
return mcp.NewToolResultErrorf("name is required"), nil
}
// Normalize and validate resource type
resourceType, err := normalizeResourceType(req.ResourceType)
if err != nil {
return mcp.NewToolResultErrorf("%v", err), nil
}
client, err := kubernetes.NewClient("", "")
if err != nil {
return mcp.NewToolResultErrorf("Failed to create Kubernetes client: %v", err), nil
}
// Get the get function from registry
getFunc := resourceRegistry[resourceType].get
result, err := getFunc(ctx, client, req.Namespace, req.Name)
if err != nil {
return mcp.NewToolResultErrorf("Failed to get %s '%s': %v", resourceType, req.Name, err), nil
}
resultJSON, err := marshalJSON(result)
if err != nil {
return mcp.NewToolResultErrorf("Failed to serialize result: %v", err), nil
}
return mcp.NewToolResultText(resultJSON), nil
}
// handleListNamespaces lists all namespaces in the cluster
func (s *K8sGptMCPServer) handleListNamespaces(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
client, err := kubernetes.NewClient("", "")
if err != nil {
return mcp.NewToolResultErrorf("Failed to create Kubernetes client: %v", err), nil
}
namespaces, err := client.Client.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
return mcp.NewToolResultErrorf("Failed to list namespaces: %v", err), nil
}
resultJSON, err := marshalJSON(namespaces.Items)
if err != nil {
return mcp.NewToolResultErrorf("Failed to serialize result: %v", err), nil
}
return mcp.NewToolResultText(resultJSON), nil
}
// handleListEvents lists Kubernetes events
func (s *K8sGptMCPServer) handleListEvents(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
var req struct {
Namespace string `json:"namespace,omitempty"`
InvolvedObjectName string `json:"involvedObjectName,omitempty"`
InvolvedObjectKind string `json:"involvedObjectKind,omitempty"`
Limit int64 `json:"limit,omitempty"`
}
if err := request.BindArguments(&req); err != nil {
return mcp.NewToolResultErrorf("Failed to parse request arguments: %v", err), nil
}
if req.Limit == 0 {
req.Limit = DefaultListLimit
} else if req.Limit > MaxListLimit {
req.Limit = MaxListLimit
}
client, err := kubernetes.NewClient("", "")
if err != nil {
return mcp.NewToolResultErrorf("Failed to create Kubernetes client: %v", err), nil
}
listOptions := metav1.ListOptions{
Limit: req.Limit,
}
events, err := client.Client.CoreV1().Events(req.Namespace).List(ctx, listOptions)
if err != nil {
return mcp.NewToolResultErrorf("Failed to list events: %v", err), nil
}
// Filter events if needed
filteredEvents := []corev1.Event{}
for _, event := range events.Items {
if req.InvolvedObjectName != "" && event.InvolvedObject.Name != req.InvolvedObjectName {
continue
}
if req.InvolvedObjectKind != "" && event.InvolvedObject.Kind != req.InvolvedObjectKind {
continue
}
filteredEvents = append(filteredEvents, event)
}
resultJSON, err := marshalJSON(filteredEvents)
if err != nil {
return mcp.NewToolResultErrorf("Failed to serialize result: %v", err), nil
}
return mcp.NewToolResultText(resultJSON), nil
}
// handleGetLogs retrieves logs from a pod container
func (s *K8sGptMCPServer) handleGetLogs(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
var req struct {
PodName string `json:"podName"`
Namespace string `json:"namespace"`
Container string `json:"container,omitempty"`
Previous bool `json:"previous,omitempty"`
TailLines int64 `json:"tailLines,omitempty"`
SinceSeconds int64 `json:"sinceSeconds,omitempty"`
}
if err := request.BindArguments(&req); err != nil {
return mcp.NewToolResultErrorf("Failed to parse request arguments: %v", err), nil
}
if req.PodName == "" {
return mcp.NewToolResultErrorf("podName is required"), nil
}
if req.Namespace == "" {
return mcp.NewToolResultErrorf("namespace is required"), nil
}
if req.TailLines == 0 {
req.TailLines = 100
}
client, err := kubernetes.NewClient("", "")
if err != nil {
return mcp.NewToolResultErrorf("Failed to create Kubernetes client: %v", err), nil
}
podLogOpts := &corev1.PodLogOptions{
Container: req.Container,
Previous: req.Previous,
TailLines: &req.TailLines,
}
if req.SinceSeconds > 0 {
podLogOpts.SinceSeconds = &req.SinceSeconds
}
logRequest := client.Client.CoreV1().Pods(req.Namespace).GetLogs(req.PodName, podLogOpts)
logStream, err := logRequest.Stream(ctx)
if err != nil {
return mcp.NewToolResultErrorf("Failed to get logs: %v", err), nil
}
defer func() {
_ = logStream.Close()
}()
logs, err := io.ReadAll(logStream)
if err != nil {
return mcp.NewToolResultErrorf("Failed to read logs: %v", err), nil
}
return mcp.NewToolResultText(string(logs)), nil
}
// handleListFilters lists available and active filters
func (s *K8sGptMCPServer) handleListFilters(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
coreFilters, additionalFilters, integrationFilters := analyzer.ListFilters()
active := viper.GetStringSlice("active_filters")
result := map[string]interface{}{
"coreFilters": coreFilters,
"additionalFilters": additionalFilters,
"integrationFilters": integrationFilters,
"activeFilters": active,
}
resultJSON, err := marshalJSON(result)
if err != nil {
return mcp.NewToolResultErrorf("Failed to serialize result: %v", err), nil
}
return mcp.NewToolResultText(resultJSON), nil
}
// handleAddFilters adds filters to enable specific analyzers
func (s *K8sGptMCPServer) handleAddFilters(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
var req struct {
Filters []string `json:"filters"`
}
if err := request.BindArguments(&req); err != nil {
return mcp.NewToolResultErrorf("Failed to parse request arguments: %v", err), nil
}
if len(req.Filters) == 0 {
return mcp.NewToolResultErrorf("filters array is required and cannot be empty"), nil
}
activeFilters := viper.GetStringSlice("active_filters")
addedFilters := []string{}
for _, filter := range req.Filters {
if !contains(activeFilters, filter) {
activeFilters = append(activeFilters, filter)
addedFilters = append(addedFilters, filter)
}
}
viper.Set("active_filters", activeFilters)
if err := viper.WriteConfig(); err != nil {
return mcp.NewToolResultErrorf("Failed to save configuration: %v", err), nil
}
if len(addedFilters) == 0 {
return mcp.NewToolResultText("All specified filters were already active"), nil
}
return mcp.NewToolResultText(fmt.Sprintf("Successfully added filters: %v", addedFilters)), nil
}
// handleRemoveFilters removes filters to disable specific analyzers
func (s *K8sGptMCPServer) handleRemoveFilters(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
var req struct {
Filters []string `json:"filters"`
}
if err := request.BindArguments(&req); err != nil {
return mcp.NewToolResultErrorf("Failed to parse request arguments: %v", err), nil
}
if len(req.Filters) == 0 {
return mcp.NewToolResultErrorf("filters array is required and cannot be empty"), nil
}
activeFilters := viper.GetStringSlice("active_filters")
newFilters := []string{}
removedFilters := []string{}
for _, filter := range activeFilters {
if !contains(req.Filters, filter) {
newFilters = append(newFilters, filter)
} else {
removedFilters = append(removedFilters, filter)
}
}
viper.Set("active_filters", newFilters)
if err := viper.WriteConfig(); err != nil {
return mcp.NewToolResultErrorf("Failed to save configuration: %v", err), nil
}
if len(removedFilters) == 0 {
return mcp.NewToolResultText("None of the specified filters were active"), nil
}
return mcp.NewToolResultText(fmt.Sprintf("Successfully removed filters: %v", removedFilters)), nil
}
// handleListIntegrations lists available integrations
func (s *K8sGptMCPServer) handleListIntegrations(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
integrationProvider := integration.NewIntegration()
integrations := integrationProvider.List()
result := []map[string]interface{}{}
for _, integ := range integrations {
active, _ := integrationProvider.IsActivate(integ)
result = append(result, map[string]interface{}{
"name": integ,
"active": active,
})
}
resultJSON, err := marshalJSON(result)
if err != nil {
return mcp.NewToolResultErrorf("Failed to serialize result: %v", err), nil
}
return mcp.NewToolResultText(resultJSON), nil
}
// contains checks if a string slice contains a specific string
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}

170
pkg/server/mcp_prompts.go Normal file
View File

@@ -0,0 +1,170 @@
/*
Copyright 2024 The K8sGPT Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package server
import (
"context"
"fmt"
"github.com/mark3labs/mcp-go/mcp"
)
// getTroubleshootPodPrompt returns a prompt for pod troubleshooting
func (s *K8sGptMCPServer) getTroubleshootPodPrompt(ctx context.Context, request mcp.GetPromptRequest) (*mcp.GetPromptResult, error) {
podName := ""
namespace := ""
if request.Params.Arguments != nil {
podName = request.Params.Arguments["podName"]
namespace = request.Params.Arguments["namespace"]
}
promptText := fmt.Sprintf(`You are troubleshooting a Kubernetes pod issue.
Pod: %s
Namespace: %s
Troubleshooting steps:
1. Use 'get-resource' tool to get pod details and check status, conditions, and events
2. Use 'list-events' tool with the pod name to see recent events
3. Use 'get-logs' tool to check container logs for errors
4. Check if the pod has multiple containers and inspect each
5. If the pod is in CrashLoopBackOff, use 'get-logs' with previous=true
6. Use 'analyze' tool with filters=['Pod'] to get AI-powered analysis
7. Check related resources like ConfigMaps, Secrets, and PVCs
Common issues to check:
- Image pull errors (check imagePullSecrets)
- Resource limits (CPU/memory)
- Liveness/readiness probe failures
- Volume mount issues
- Environment variable problems
- Network connectivity issues`, podName, namespace)
return &mcp.GetPromptResult{
Description: "Pod troubleshooting guide",
Messages: []mcp.PromptMessage{
{
Role: "user",
Content: mcp.TextContent{
Type: "text",
Text: promptText,
},
},
},
}, nil
}
// getTroubleshootDeploymentPrompt returns a prompt for deployment troubleshooting
func (s *K8sGptMCPServer) getTroubleshootDeploymentPrompt(ctx context.Context, request mcp.GetPromptRequest) (*mcp.GetPromptResult, error) {
deploymentName := ""
namespace := ""
if request.Params.Arguments != nil {
deploymentName = request.Params.Arguments["deploymentName"]
namespace = request.Params.Arguments["namespace"]
}
promptText := fmt.Sprintf(`You are troubleshooting a Kubernetes deployment issue.
Deployment: %s
Namespace: %s
Troubleshooting steps:
1. Use 'get-resource' tool to get deployment details and check replica status
2. Use 'list-resources' with resourceType='replicasets' to check ReplicaSets
3. Use 'list-resources' with resourceType='pods' and labelSelector to find deployment pods
4. Use 'list-events' tool to see deployment-related events
5. Use 'analyze' tool with filters=['Deployment','Pod'] for comprehensive analysis
6. Check pod status and logs for individual pod issues
7. Verify image availability and pull secrets
8. Check resource quotas and limits
Common deployment issues:
- Insufficient resources in the cluster
- Image pull failures
- Invalid configuration (ConfigMaps/Secrets)
- Failed rolling updates
- Readiness probe failures preventing rollout
- PVC binding issues
- Node selector/affinity constraints`, deploymentName, namespace)
return &mcp.GetPromptResult{
Description: "Deployment troubleshooting guide",
Messages: []mcp.PromptMessage{
{
Role: "user",
Content: mcp.TextContent{
Type: "text",
Text: promptText,
},
},
},
}, nil
}
// getTroubleshootClusterPrompt returns a prompt for general cluster troubleshooting
func (s *K8sGptMCPServer) getTroubleshootClusterPrompt(ctx context.Context, request mcp.GetPromptRequest) (*mcp.GetPromptResult, error) {
promptText := `You are performing a general Kubernetes cluster health check and troubleshooting.
Recommended troubleshooting workflow:
1. CLUSTER OVERVIEW:
- Use 'cluster-info' to get cluster version
- Use 'list-namespaces' to see all namespaces
- Use 'list-resources' with resourceType='nodes' to check node health
2. RESOURCE ANALYSIS:
- Use 'analyze' tool with explain=true for comprehensive AI-powered analysis
- Start with core resources: filters=['Pod','Deployment','Service']
- Add more filters as needed: ['Node','PersistentVolumeClaim','Job','CronJob']
3. EVENT INSPECTION:
- Use 'list-events' to see recent cluster events
- Filter by namespace for focused troubleshooting
- Look for Warning and Error events
4. SPECIFIC RESOURCE INVESTIGATION:
- Use 'list-resources' to find problematic resources
- Use 'get-resource' for detailed inspection
- Use 'get-logs' to examine container logs
5. CONFIGURATION CHECK:
- Use 'list-filters' to see available analyzers
- Use 'list-integrations' to check integrations (Prometheus, AWS, etc.)
- Use 'config' tool to modify settings if needed
Common cluster-wide issues:
- Node pressure (CPU, memory, disk)
- Network policies blocking traffic
- Storage provisioning problems
- RBAC permission issues
- Certificate expiration
- Control plane component failures
- Resource quota exhaustion
- DNS resolution problems
Use the available tools systematically to narrow down the issue.`
return &mcp.GetPromptResult{
Description: "General cluster troubleshooting guide",
Messages: []mcp.PromptMessage{
{
Role: "user",
Content: mcp.TextContent{
Type: "text",
Text: promptText,
},
},
},
}, nil
}

View File

@@ -14,6 +14,8 @@ limitations under the License.
package util
import (
"encoding/base64"
"fmt"
"testing"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
@@ -23,6 +25,7 @@ import (
v1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes/fake"
)
@@ -503,3 +506,74 @@ func TestLabelsIncludeAny(t *testing.T) {
})
}
}
func TestMaskString(t *testing.T) {
input := "mysecret"
masked := MaskString(input)
// decode base64 to compare properties
decoded, err := base64.StdEncoding.DecodeString(masked)
require.NoError(t, err)
require.Len(t, decoded, len(input))
// ensure it is not equal to input
require.NotEqual(t, input, string(decoded))
// ensure all runes are from anonymizePattern
allowed := make(map[rune]struct{})
for _, r := range anonymizePattern {
allowed[r] = struct{}{}
}
for _, r := range string(decoded) {
_, ok := allowed[r]
require.True(t, ok, "unexpected rune: %q", r)
}
}
func TestNewHeaders(t *testing.T) {
input := []string{
"X-Test: foo",
"X-Test: bar",
"Content-Type: application/json",
"InvalidHeader", // should be ignored
}
hs := NewHeaders(input)
// flatten to a map for easier assertions
got := map[string][]string{}
for _, h := range hs {
for k, v := range h {
got[k] = append(got[k], v...)
}
}
// expected values
require.Contains(t, got, "X-Test")
require.Contains(t, got, "Content-Type")
// order of values is not guaranteed
require.ElementsMatch(t, []string{"foo", "bar"}, got["X-Test"])
require.ElementsMatch(t, []string{"application/json"}, got["Content-Type"])
}
func TestLabelStrToSelector(t *testing.T) {
// empty case returns nil
require.Nil(t, LabelStrToSelector(""))
sel := LabelStrToSelector("key=value,foo=bar")
require.NotNil(t, sel)
// matches exact set
m := map[string]string{"key": "value", "foo": "bar"}
require.True(t, sel.Matches(labels.Set(m)))
// does not match different values
m2 := map[string]string{"key": "other", "foo": "bar"}
require.False(t, sel.Matches(labels.Set(m2)))
}
func TestCaptureOutput(t *testing.T) {
out := CaptureOutput(func() {
fmt.Print("hello world")
})
require.Equal(t, "hello world", out)
}
func TestContains(t *testing.T) {
require.True(t, Contains("abcdef", "bcd"))
require.False(t, Contains("abcdef", "xyz"))
}