mirror of
https://github.com/helmfile/helmfile.git
synced 2026-10-01 04:37:17 +02:00
* feat: add `helmfile doctor` command for AI-assisted diff analysis `helmfile doctor` runs `helmfile diff` and asks an OpenAI-compatible LLM to summarize the changes and flag risks (data loss, security exposure, breaking changes, downtime, performance, best-practice issues). Key design decisions: - When no LLM is configured, doctor is equivalent to `helmfile diff` with one exception: --show-secrets is always forced off (secrets never reach stdout, even without an LLM). - Secrets are ALWAYS redacted via two layers: (1) ShowSecrets() forced to false so helm-diff emits <REDACTED> placeholders; (2) a defense-in-depth text redactor strips residual secret-looking content (Secret YAML blocks, sensitive key/value lines, base64 blobs, JWT tokens) before LLM transmission. - LLM configuration precedence: env (HELMFILE_LLM_*) < helmfile.yaml (llm:) < CLI flags (--llm-*). - Supports any OpenAI-compatible backend (OpenAI, Azure, One-API, LiteLLM, Ollama, etc.) with automatic response_format fallback for backends that don't support JSON mode. - Prompt injection defense: release names and environment values are JSON-encoded before insertion into the LLM prompt. - Exit codes: 0 (success/low-risk), 2 (high-risk gate, bypass with --force), 1 (other errors). Helm-diff's 'detected changes' exit-2 is swallowed. New packages: - pkg/agent/llm: OpenAI-compatible client with JSON response parsing, mock client for testing, prompt builder with injection defense. - pkg/agent/doctor: secret redactor (state machine + regex), report renderer (markdown + JSON), config resolver (env < yaml < flag merge). Testing: 70+ unit tests covering redaction patterns, prompt injection, response_format fallback, JSON parsing, yaml roundtrip, concurrency safety, panic recovery, and error propagation. go test -race passes. Documentation: full doctor section in docs/cli.md, llm: block reference in docs/configuration.md, updated skills/helmfile for AI agents. Signed-off-by: yxxhero <aiopsclub@163.com> * docs: fix doctor equivalence wording per PR review Per review feedback (PR #2660): the docs claimed doctor is 'equivalent to helmfile diff — same flags, same output, same exit codes' in the unconfigured path, but this over-promises because: 1. doctor --output is the report format (not helm-diff's output format) 2. helm-diff's --output is exposed as --diff-output in doctor 3. --show-secrets is silently ignored Updated all three locations (cli.md, cmd/doctor.go Long + godoc, pkg/app/doctor.go godoc) to say 'falls back to helmfile diff with --show-secrets forced off' and explicitly note the --output / --diff-output flag difference. Signed-off-by: yxxhero <aiopsclub@163.com> --------- Signed-off-by: yxxhero <aiopsclub@163.com>
33 lines
1.2 KiB
Go
33 lines
1.2 KiB
Go
package llm
|
|
|
|
import goContext "context"
|
|
|
|
// Client is the abstraction used by `helmfile doctor` to talk to whatever
|
|
// OpenAI-compatible backend the user configured. The default implementation
|
|
// is the OpenAI client (see openai.go). Tests inject a mock implementation.
|
|
type Client interface {
|
|
// Analyze asks the LLM to review the given helm diff and return a
|
|
// structured Analysis. ctx is used for cancellation/timeout.
|
|
Analyze(ctx goContext.Context, diff string, extras AnalyzeInput) (Analysis, error)
|
|
}
|
|
|
|
// AnalyzeInput carries optional context that helps the LLM produce a more
|
|
// grounded analysis (release names, environment name, etc.). All fields are
|
|
// optional.
|
|
type AnalyzeInput struct {
|
|
// Environment is the helmfile --environment value (e.g. "prod").
|
|
Environment string
|
|
// Releases is the list of release names that appear in the diff.
|
|
Releases []string
|
|
}
|
|
|
|
// NewClient returns a Client backed by the OpenAI Chat Completions protocol.
|
|
// Returns nil when cfg.IsConfigured() is false so callers can short-circuit
|
|
// to the plain-diff fallback path.
|
|
func NewClient(cfg Config) Client {
|
|
if !cfg.IsConfigured() {
|
|
return nil
|
|
}
|
|
return newOpenAIClient(cfg.WithDefaults())
|
|
}
|