From 458aa9debac7590eb0855ffd12141b702e999a36 Mon Sep 17 00:00:00 2001 From: "Three Foxes (in a Trenchcoat)" Date: Fri, 20 Feb 2026 10:25:34 +0000 Subject: [PATCH] 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) Signed-off-by: Three Foxes (in a Trenchcoat) * 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) Signed-off-by: Three Foxes (in a Trenchcoat) --------- Signed-off-by: Three Foxes (in a Trenchcoat) Signed-off-by: Three Foxes (in a Trenchcoat) Co-authored-by: Alex Jones <1235925+AlexsJones@users.noreply.github.com> --- pkg/analysis/analysis.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/analysis/analysis.go b/pkg/analysis/analysis.go index 20ced22e..cfd78ce7 100644 --- a/pkg/analysis/analysis.go +++ b/pkg/analysis/analysis.go @@ -35,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 { @@ -227,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())