From 1b7e47424b9f48c9fe194ab24b1d4ccdc7a1a005 Mon Sep 17 00:00:00 2001 From: chenk Date: Mon, 26 Feb 2024 08:58:47 +0200 Subject: [PATCH] fix: add context to target finding on k8s table view (#6099) Signed-off-by: chenk --- pkg/k8s/report/table.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/k8s/report/table.go b/pkg/k8s/report/table.go index 524a1c389b03..1edb44879156 100644 --- a/pkg/k8s/report/table.go +++ b/pkg/k8s/report/table.go @@ -2,7 +2,9 @@ package report import ( "context" + "fmt" "io" + "strings" "golang.org/x/xerrors" @@ -50,8 +52,9 @@ func (tw TableWriter) Write(ctx context.Context, report Report) error { Output: tw.Output, Severities: tw.Severities, } - for _, r := range report.Resources { + for i, r := range report.Resources { if r.Report.Results.Failed() { + updateTargetContext(&report.Resources[i]) err := t.Write(ctx, r.Report) if err != nil { return err @@ -67,3 +70,14 @@ func (tw TableWriter) Write(ctx context.Context, report Report) error { return nil } + +// updateTargetContext add context namespace, kind and name to the target +func updateTargetContext(r *Resource) { + targetName := fmt.Sprintf("namespace: %s, %s: %s", r.Namespace, strings.ToLower(r.Kind), r.Name) + if r.Kind == "NodeComponents" || r.Kind == "NodeInfo" { + targetName = fmt.Sprintf("node: %s", r.Name) + } + for i := range r.Report.Results { + r.Report.Results[i].Target = targetName + } +}