44 "context"
55 "fmt"
66 "os"
7+ "regexp"
8+ "strings"
79 "time"
810
911 "github.com/kyma-project/cli.v3/internal/clierror"
@@ -14,6 +16,7 @@ import (
1416 "github.com/kyma-project/cli.v3/internal/out"
1517 "github.com/spf13/cobra"
1618 istioformatting "istio.io/istio/istioctl/pkg/util/formatting"
19+ istioanalysisdiag "istio.io/istio/pkg/config/analysis/diag"
1720 istioresource "istio.io/istio/pkg/config/resource"
1821 istiolog "istio.io/istio/pkg/log"
1922 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -28,6 +31,7 @@ type diagnoseIstioConfig struct {
2831 namespace string
2932 allNamespaces bool
3033 outputFormat types.Format
34+ outputLevel types.IstioLevel
3135 outputPath string
3236 verbose bool
3337 timeout time.Duration
@@ -49,6 +53,9 @@ func NewDiagnoseIstioCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
4953 # Analyze Istio configuration in a specific namespace
5054 kyma alpha diagnose istio --namespace my-namespace
5155
56+ # Print only warnings and errors
57+ kyma alpha diagnose istio --level warning
58+
5259 # Output as JSON to a file
5360 kyma alpha diagnose istio --format json --output istio-diagnostics.json` ,
5461
@@ -66,6 +73,7 @@ func NewDiagnoseIstioCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
6673 cmd .Flags ().BoolVarP (& cfg .allNamespaces , "all-namespaces" , "A" , false , "Analyzes all namespaces" )
6774 cmd .Flags ().StringVarP (& cfg .namespace , "namespace" , "n" , "" , "The namespace that the workload instances belongs to" )
6875 cmd .Flags ().VarP (& cfg .outputFormat , "format" , "f" , "Output format (possible values: json, yaml)" )
76+ cmd .Flags ().Var (& cfg .outputLevel , "level" , "Output message level (possible values: info, warning, error)" )
6977 cmd .Flags ().StringVarP (& cfg .outputPath , "output" , "o" , "" , "Path to the diagnostic output file. If not provided the output is printed to stdout" )
7078 cmd .Flags ().BoolVar (& cfg .verbose , "verbose" , false , "Displays verbose output, including error details during diagnostics collection" )
7179 cmd .Flags ().DurationVar (& cfg .timeout , "timeout" , 30 * time .Second , "Timeout for diagnosis" )
@@ -95,13 +103,25 @@ func diagnoseIstio(cfg *diagnoseIstioConfig) clierror.Error {
95103 return err
96104 }
97105
106+ diagnosticData .Messages = filterDataByLevel (diagnosticData .Messages , cfg .outputLevel .ToInternalIstioLevel ())
107+
98108 err = printIstioOutput (diagnosticData , cfg .outputFormat , cfg .outputPath )
99109 if err != nil {
100110 return err
101111 }
102112 return nil
103113}
104114
115+ func filterDataByLevel (messages istioanalysisdiag.Messages , minLevel istioanalysisdiag.Level ) istioanalysisdiag.Messages {
116+ var filtered []istioanalysisdiag.Message
117+ for _ , msg := range messages {
118+ if msg .Type .Level ().IsWorseThanOrEqualTo (minLevel ) {
119+ filtered = append (filtered , msg )
120+ }
121+ }
122+ return filtered
123+ }
124+
105125func calculateNamespace (allNamespaces bool , namespace string ) string {
106126 if allNamespaces {
107127 return metav1 .NamespaceAll
@@ -190,6 +210,10 @@ func printIstioOutput(analysisResult *istioanalysislocal.AnalysisResult, format
190210 if err != nil {
191211 return clierror .Wrap (err , clierror .New ("failed to format output" ))
192212 }
213+ re := regexp .MustCompile (`(?m)^\t+` )
214+ output = re .ReplaceAllStringFunc (output , func (match string ) string {
215+ return strings .Repeat (" " , len (match ))
216+ })
193217 printer .Msgfln (output )
194218 return nil
195219}
0 commit comments