Skip to content

Commit 5d2192e

Browse files
anoipmgrego952
authored andcommitted
add filtering istio messages by level (kyma-project#2756)
1 parent 358325b commit 5d2192e

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

docs/user/gen-docs/kyma_alpha_diagnose_istio.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ kyma alpha diagnose istio [flags]
2121
# Analyze Istio configuration in a specific namespace
2222
kyma alpha diagnose istio --namespace my-namespace
2323

24+
# Print only warnings and errors
25+
kyma alpha diagnose istio --level warning
26+
2427
# Output as JSON to a file
2528
kyma alpha diagnose istio --format json --output istio-diagnostics.json
2629
```
@@ -30,6 +33,7 @@ kyma alpha diagnose istio [flags]
3033
```text
3134
-A, --all-namespaces Analyzes all namespaces
3235
-f, --format string Output format (possible values: json, yaml)
36+
--level string Output message level (possible values: info, warning, error)
3337
-n, --namespace string The namespace that the workload instances belongs to
3438
-o, --output string Path to the diagnostic output file. If not provided the output is printed to stdout
3539
--timeout duration Timeout for diagnosis (default "30s")

internal/cmd/alpha/diagnose/istio.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
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+
105125
func 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
}

internal/cmdcommon/types/format.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ func (f *Format) String() string {
3838

3939
func (f *Format) Set(v string) error {
4040
for _, format := range availableFormats {
41-
if *f == format {
41+
if v == format.String() {
4242
*f = Format(v)
4343
return nil
4444
}
4545
}
4646

47-
return errors.New(fmt.Sprintf("invalid output format '%s'", *f))
47+
return errors.New(fmt.Sprintf("invalid output format '%s'", v))
4848
}
4949

5050
func (f *Format) Type() string {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package types
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/pkg/errors"
8+
istioanalysisdiag "istio.io/istio/pkg/config/analysis/diag"
9+
)
10+
11+
const (
12+
InfoIstioLevel IstioLevel = "info"
13+
WarningIstioLevel IstioLevel = "warning"
14+
ErrorIstioLevel IstioLevel = "error"
15+
)
16+
17+
var (
18+
availableIstioLevels = []IstioLevel{
19+
InfoIstioLevel,
20+
WarningIstioLevel,
21+
ErrorIstioLevel,
22+
}
23+
)
24+
25+
type IstioLevel string
26+
27+
func (f *IstioLevel) String() string {
28+
return string(*f)
29+
}
30+
31+
func (f *IstioLevel) Set(v string) error {
32+
for _, format := range availableIstioLevels {
33+
if v == format.String() {
34+
*f = IstioLevel(v)
35+
return nil
36+
}
37+
}
38+
39+
return errors.New(fmt.Sprintf("invalid istio level '%s'", v))
40+
}
41+
42+
func (f *IstioLevel) Type() string {
43+
return "string"
44+
}
45+
46+
func (f *IstioLevel) ToInternalIstioLevel() istioanalysisdiag.Level {
47+
levelName := f.String()
48+
if levelName == "" {
49+
return istioanalysisdiag.Info
50+
}
51+
return istioanalysisdiag.GetUppercaseStringToLevelMap()[strings.ToUpper(levelName)]
52+
}

0 commit comments

Comments
 (0)