diff --git a/cmd/hgctl/config/gateway_config.go b/cmd/hgctl/config/gateway_config.go index 6a4fd4be90..2aa9683209 100644 --- a/cmd/hgctl/config/gateway_config.go +++ b/cmd/hgctl/config/gateway_config.go @@ -22,6 +22,7 @@ import ( "github.com/alibaba/higress/pkg/cmd/hgctl/kubernetes" "github.com/alibaba/higress/pkg/cmd/options" + "istio.io/istio/istioctl/pkg/writer/envoy/configdump" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/yaml" ) @@ -61,6 +62,23 @@ func NewDefaultGetEnvoyConfigOptions() *GetEnvoyConfigOptions { } } +func setupConfigdumpEnvoyConfigWriter(debug []byte, stdout io.Writer) (*configdump.ConfigWriter, error) { + cw := &configdump.ConfigWriter{Stdout: stdout} + err := cw.Prime(debug) + if err != nil { + return nil, err + } + return cw, nil +} + +func GetEnvoyConfigWriter(config *GetEnvoyConfigOptions, stdout io.Writer) (*configdump.ConfigWriter, error) { + configDump, err := retrieveConfigDump(config.PodName, config.PodNamespace, config.BindAddress, config.IncludeEds) + if err != nil { + return nil, err + } + return setupConfigdumpEnvoyConfigWriter(configDump, stdout) +} + func GetEnvoyConfig(config *GetEnvoyConfigOptions) ([]byte, error) { configDump, err := retrieveConfigDump(config.PodName, config.PodNamespace, config.BindAddress, config.IncludeEds) if err != nil { @@ -144,14 +162,12 @@ func formatGatewayConfig(configDump any, output string) ([]byte, error) { if err != nil { return nil, err } - if output == "yaml" { out, err = yaml.JSONToYAML(out) if err != nil { return nil, err } } - return out, nil } diff --git a/pkg/cmd/hgctl/common.go b/pkg/cmd/hgctl/common.go index e08813998c..2ed51b815e 100644 --- a/pkg/cmd/hgctl/common.go +++ b/pkg/cmd/hgctl/common.go @@ -15,7 +15,8 @@ package hgctl const ( - yamlOutput = "yaml" - jsonOutput = "json" - flagsOutput = "flags" + summaryOutput = "short" + yamlOutput = "yaml" + jsonOutput = "json" + flagsOutput = "flags" ) diff --git a/pkg/cmd/hgctl/config_cluster.go b/pkg/cmd/hgctl/config_cluster.go index 2a8eab2564..074034ead0 100644 --- a/pkg/cmd/hgctl/config_cluster.go +++ b/pkg/cmd/hgctl/config_cluster.go @@ -19,6 +19,7 @@ import ( "github.com/alibaba/higress/cmd/hgctl/config" "github.com/spf13/cobra" + "istio.io/istio/istioctl/pkg/writer/envoy/configdump" cmdutil "k8s.io/kubectl/pkg/cmd/util" ) @@ -49,17 +50,23 @@ func runClusterConfig(c *cobra.Command, args []string) error { if len(args) != 0 { podName = args[0] } - envoyConfig, err := config.GetEnvoyConfig(&config.GetEnvoyConfigOptions{ + configWriter, err := config.GetEnvoyConfigWriter(&config.GetEnvoyConfigOptions{ PodName: podName, PodNamespace: podNamespace, BindAddress: bindAddress, Output: output, EnvoyConfigType: config.ClusterEnvoyConfigType, IncludeEds: true, - }) + }, c.OutOrStdout()) if err != nil { return err } - _, err = fmt.Fprintln(c.OutOrStdout(), string(envoyConfig)) - return err + switch output { + case summaryOutput: + return configWriter.PrintClusterSummary(configdump.ClusterFilter{}) + case jsonOutput, yamlOutput: + return configWriter.PrintClusterDump(configdump.ClusterFilter{}, output) + default: + return fmt.Errorf("output format %q not supported", output) + } } diff --git a/pkg/cmd/hgctl/config_cmd.go b/pkg/cmd/hgctl/config_cmd.go index 19f71078fd..42cd2bc5d9 100644 --- a/pkg/cmd/hgctl/config_cmd.go +++ b/pkg/cmd/hgctl/config_cmd.go @@ -52,7 +52,7 @@ func newConfigCommand() *cobra.Command { flags := cfgCommand.Flags() options.AddKubeConfigFlags(flags) - cfgCommand.PersistentFlags().StringVarP(&output, "output", "o", "json", "One of 'yaml' or 'json'") + cfgCommand.PersistentFlags().StringVarP(&output, "output", "o", "json", "Output format: one of json|yaml|short") cfgCommand.PersistentFlags().StringVarP(&podNamespace, "namespace", "n", "higress-system", "Namespace where envoy proxy pod are installed.") return cfgCommand diff --git a/pkg/cmd/hgctl/config_listener.go b/pkg/cmd/hgctl/config_listener.go index 143875fd29..22c5c25b6a 100644 --- a/pkg/cmd/hgctl/config_listener.go +++ b/pkg/cmd/hgctl/config_listener.go @@ -19,6 +19,7 @@ import ( "github.com/alibaba/higress/cmd/hgctl/config" "github.com/spf13/cobra" + "istio.io/istio/istioctl/pkg/writer/envoy/configdump" cmdutil "k8s.io/kubectl/pkg/cmd/util" ) @@ -49,17 +50,23 @@ func runListenerConfig(c *cobra.Command, args []string) error { if len(args) != 0 { podName = args[0] } - envoyConfig, err := config.GetEnvoyConfig(&config.GetEnvoyConfigOptions{ + configWriter, err := config.GetEnvoyConfigWriter(&config.GetEnvoyConfigOptions{ PodName: podName, PodNamespace: podNamespace, BindAddress: bindAddress, Output: output, EnvoyConfigType: config.ListenerEnvoyConfigType, IncludeEds: true, - }) + }, c.OutOrStdout()) if err != nil { return err } - _, err = fmt.Fprintln(c.OutOrStdout(), string(envoyConfig)) - return err + switch output { + case summaryOutput: + return configWriter.PrintListenerSummary(configdump.ListenerFilter{Verbose: true}) + case jsonOutput, yamlOutput: + return configWriter.PrintListenerDump(configdump.ListenerFilter{Verbose: true}, output) + default: + return fmt.Errorf("output format %q not supported", output) + } } diff --git a/pkg/cmd/hgctl/config_route.go b/pkg/cmd/hgctl/config_route.go index 66be5a11e5..cba78cb700 100644 --- a/pkg/cmd/hgctl/config_route.go +++ b/pkg/cmd/hgctl/config_route.go @@ -19,6 +19,7 @@ import ( "github.com/alibaba/higress/cmd/hgctl/config" "github.com/spf13/cobra" + "istio.io/istio/istioctl/pkg/writer/envoy/configdump" cmdutil "k8s.io/kubectl/pkg/cmd/util" ) @@ -49,17 +50,23 @@ func runRouteConfig(c *cobra.Command, args []string) error { if len(args) != 0 { podName = args[0] } - envoyConfig, err := config.GetEnvoyConfig(&config.GetEnvoyConfigOptions{ + configWriter, err := config.GetEnvoyConfigWriter(&config.GetEnvoyConfigOptions{ PodName: podName, PodNamespace: podNamespace, BindAddress: bindAddress, Output: output, EnvoyConfigType: config.RouteEnvoyConfigType, IncludeEds: true, - }) + }, c.OutOrStdout()) if err != nil { return err } - _, err = fmt.Fprintln(c.OutOrStdout(), string(envoyConfig)) - return err + switch output { + case summaryOutput: + return configWriter.PrintRouteSummary(configdump.RouteFilter{Verbose: true}) + case jsonOutput, yamlOutput: + return configWriter.PrintRouteDump(configdump.RouteFilter{Verbose: true}, output) + default: + return fmt.Errorf("output format %q not supported", output) + } }