Files
helmfile/pkg/agent/doctor/config.go
T
yxxhero 9b943adc9e feat: add helmfile doctor command for AI-assisted diff analysis (#2660)
* 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>
2026-06-22 16:52:35 +08:00

44 lines
1.1 KiB
Go

package doctor
import (
"fmt"
"os"
"time"
"github.com/helmfile/helmfile/pkg/agent/llm"
)
// EnvConfig reads LLM configuration from process environment variables.
// These are the lowest-precedence source (env < yaml < flag).
func EnvConfig() llm.Config {
cfg := llm.Config{
BaseURL: os.Getenv("HELMFILE_LLM_BASE_URL"),
APIKey: os.Getenv("HELMFILE_LLM_API_KEY"),
Model: os.Getenv("HELMFILE_LLM_MODEL"),
}
if t := os.Getenv("HELMFILE_LLM_TIMEOUT"); t != "" {
if d, err := time.ParseDuration(t); err == nil {
cfg.Timeout = d
}
}
if mt := os.Getenv("HELMFILE_LLM_MAX_TOKENS"); mt != "" {
var n int
if _, err := fmt.Sscanf(mt, "%d", &n); err == nil && n > 0 {
cfg.MaxTokens = n
}
}
return cfg
}
// ResolveConfig merges env < yaml < flag into a single Config.
//
// Any non-zero field at a higher precedence layer overrides the lower one.
// Returns the merged config (caller can call IsConfigured() to decide whether
// to wire up an LLM client at all).
func ResolveConfig(env, yamlCfg, flag llm.Config) llm.Config {
merged := env
merged = llm.Merge(merged, yamlCfg)
merged = llm.Merge(merged, flag)
return merged
}