Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/user/gen-docs/kyma_alpha_diagnose_istio.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ kyma alpha diagnose istio [flags]
# Analyze Istio configuration in a specific namespace
kyma alpha diagnose istio --namespace my-namespace

# Print only warnings and errors
kyma alpha diagnose istio --level warning

# Output as JSON to a file
kyma alpha diagnose istio --format json --output istio-diagnostics.json
```
Expand All @@ -30,6 +33,7 @@ kyma alpha diagnose istio [flags]
```text
-A, --all-namespaces Analyzes all namespaces
-f, --format string Output format (possible values: json, yaml)
--level string Output message level (possible values: info, warning, error)
-n, --namespace string The namespace that the workload instances belongs to
-o, --output string Path to the diagnostic output file. If not provided the output is printed to stdout
--timeout duration Timeout for diagnosis (default "30s")
Expand Down
24 changes: 24 additions & 0 deletions internal/cmd/alpha/diagnose/istio.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"os"
"regexp"
"strings"
"time"

"github.com/kyma-project/cli.v3/internal/clierror"
Expand All @@ -14,6 +16,7 @@ import (
"github.com/kyma-project/cli.v3/internal/out"
"github.com/spf13/cobra"
istioformatting "istio.io/istio/istioctl/pkg/util/formatting"
istioanalysisdiag "istio.io/istio/pkg/config/analysis/diag"
istioresource "istio.io/istio/pkg/config/resource"
istiolog "istio.io/istio/pkg/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -28,6 +31,7 @@ type diagnoseIstioConfig struct {
namespace string
allNamespaces bool
outputFormat types.Format
outputLevel types.IstioLevel
outputPath string
verbose bool
timeout time.Duration
Expand All @@ -49,6 +53,9 @@ func NewDiagnoseIstioCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
# Analyze Istio configuration in a specific namespace
kyma alpha diagnose istio --namespace my-namespace

# Print only warnings and errors
kyma alpha diagnose istio --level warning

# Output as JSON to a file
kyma alpha diagnose istio --format json --output istio-diagnostics.json`,

Expand All @@ -66,6 +73,7 @@ func NewDiagnoseIstioCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
cmd.Flags().BoolVarP(&cfg.allNamespaces, "all-namespaces", "A", false, "Analyzes all namespaces")
cmd.Flags().StringVarP(&cfg.namespace, "namespace", "n", "", "The namespace that the workload instances belongs to")
cmd.Flags().VarP(&cfg.outputFormat, "format", "f", "Output format (possible values: json, yaml)")
cmd.Flags().Var(&cfg.outputLevel, "level", "Output message level (possible values: info, warning, error)")
cmd.Flags().StringVarP(&cfg.outputPath, "output", "o", "", "Path to the diagnostic output file. If not provided the output is printed to stdout")
cmd.Flags().BoolVar(&cfg.verbose, "verbose", false, "Displays verbose output, including error details during diagnostics collection")
cmd.Flags().DurationVar(&cfg.timeout, "timeout", 30*time.Second, "Timeout for diagnosis")
Expand Down Expand Up @@ -95,13 +103,25 @@ func diagnoseIstio(cfg *diagnoseIstioConfig) clierror.Error {
return err
}

diagnosticData.Messages = filterDataByLevel(diagnosticData.Messages, cfg.outputLevel.ToInternalIstioLevel())

err = printIstioOutput(diagnosticData, cfg.outputFormat, cfg.outputPath)
if err != nil {
return err
}
return nil
}

func filterDataByLevel(messages istioanalysisdiag.Messages, minLevel istioanalysisdiag.Level) istioanalysisdiag.Messages {
var filtered []istioanalysisdiag.Message
for _, msg := range messages {
if msg.Type.Level().IsWorseThanOrEqualTo(minLevel) {
filtered = append(filtered, msg)
}
}
return filtered
}

func calculateNamespace(allNamespaces bool, namespace string) string {
if allNamespaces {
return metav1.NamespaceAll
Expand Down Expand Up @@ -190,6 +210,10 @@ func printIstioOutput(analysisResult *istioanalysislocal.AnalysisResult, format
if err != nil {
return clierror.Wrap(err, clierror.New("failed to format output"))
}
re := regexp.MustCompile(`(?m)^\t+`)
output = re.ReplaceAllStringFunc(output, func(match string) string {
return strings.Repeat(" ", len(match))
})
printer.Msgfln(output)
return nil
}
4 changes: 2 additions & 2 deletions internal/cmdcommon/types/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ func (f *Format) String() string {

func (f *Format) Set(v string) error {
for _, format := range availableFormats {
if *f == format {
if v == format.String() {
*f = Format(v)
return nil
}
}

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

func (f *Format) Type() string {
Expand Down
52 changes: 52 additions & 0 deletions internal/cmdcommon/types/istiolevel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package types

import (
"fmt"
"strings"

"github.com/pkg/errors"
istioanalysisdiag "istio.io/istio/pkg/config/analysis/diag"
)

const (
InfoIstioLevel IstioLevel = "info"
WarningIstioLevel IstioLevel = "warning"
ErrorIstioLevel IstioLevel = "error"
)

var (
availableIstioLevels = []IstioLevel{
InfoIstioLevel,
WarningIstioLevel,
ErrorIstioLevel,
}
)

type IstioLevel string

func (f *IstioLevel) String() string {
return string(*f)
}

func (f *IstioLevel) Set(v string) error {
for _, format := range availableIstioLevels {
if v == format.String() {
*f = IstioLevel(v)
return nil
}
}

return errors.New(fmt.Sprintf("invalid istio level '%s'", v))
}

func (f *IstioLevel) Type() string {
return "string"
}

func (f *IstioLevel) ToInternalIstioLevel() istioanalysisdiag.Level {
levelName := f.String()
if levelName == "" {
return istioanalysisdiag.Info
}
return istioanalysisdiag.GetUppercaseStringToLevelMap()[strings.ToUpper(levelName)]
}