Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: bitliu <bitliu@tencent.com>
  • Loading branch information
Xunzhuo committed Feb 28, 2023
1 parent dfe2513 commit 19ccd11
Showing 1 changed file with 35 additions and 130 deletions.
165 changes: 35 additions & 130 deletions internal/cmd/egctl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,33 +92,7 @@ func allConfigCmd() *cobra.Command {
}

func runAllConfig(c *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("pod name is required")
}

podName = args[0]

if podName == "" {
return fmt.Errorf("pod name is required")
}

if podNamespace == "" {
return fmt.Errorf("pod namespace is required")
}

fw, err := portForwarder(types.NamespacedName{
Namespace: podNamespace,
Name: podName,
})
if err != nil {
return err
}
if err := fw.Start(); err != nil {
return err
}
defer fw.Stop()

configDump, err := extractConfigDump(fw)
configDump, err := retrieveConfigDump(args)
if err != nil {
return err
}
Expand Down Expand Up @@ -146,7 +120,7 @@ func bootstrapConfigCmd() *cobra.Command {
egctl config envoy-proxy bootstrap <pod-name> -n <pod-namespace> -o yaml
# Retrieve full configuration dump with short syntax
egctl c proxy bootstrap <pod-name> -n <pod-namespace>
egctl c proxy b <pod-name> -n <pod-namespace>
`,
Run: func(c *cobra.Command, args []string) {
cmdutil.CheckErr(runBootstrapConfig(c, args))
Expand All @@ -157,33 +131,7 @@ func bootstrapConfigCmd() *cobra.Command {
}

func runBootstrapConfig(c *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("pod name is required")
}

podName = args[0]

if podName == "" {
return fmt.Errorf("pod name is required")
}

if podNamespace == "" {
return fmt.Errorf("pod namespace is required")
}

fw, err := portForwarder(types.NamespacedName{
Namespace: podNamespace,
Name: podName,
})
if err != nil {
return err
}
if err := fw.Start(); err != nil {
return err
}
defer fw.Stop()

configDump, err := extractConfigDump(fw)
configDump, err := retrieveConfigDump(args)
if err != nil {
return err
}
Expand Down Expand Up @@ -216,7 +164,7 @@ func clusterConfigCmd() *cobra.Command {
egctl config envoy-proxy cluster <pod-name> -n <pod-namespace> -o yaml
# Retrieve full configuration dump with short syntax
egctl c proxy cluster <pod-name> -n <pod-namespace>
egctl c proxy c <pod-name> -n <pod-namespace>
`,
Run: func(c *cobra.Command, args []string) {
cmdutil.CheckErr(runClusterConfig(c, args))
Expand All @@ -227,33 +175,7 @@ func clusterConfigCmd() *cobra.Command {
}

func runClusterConfig(c *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("pod name is required")
}

podName = args[0]

if podName == "" {
return fmt.Errorf("pod name is required")
}

if podNamespace == "" {
return fmt.Errorf("pod namespace is required")
}

fw, err := portForwarder(types.NamespacedName{
Namespace: podNamespace,
Name: podName,
})
if err != nil {
return err
}
if err := fw.Start(); err != nil {
return err
}
defer fw.Stop()

configDump, err := extractConfigDump(fw)
configDump, err := retrieveConfigDump(args)
if err != nil {
return err
}
Expand Down Expand Up @@ -286,7 +208,7 @@ func listenerConfigCmd() *cobra.Command {
egctl config envoy-proxy listener <pod-name> -n <pod-namespace> -o yaml
# Retrieve full configuration dump with short syntax
egctl c proxy listener <pod-name> -n <pod-namespace>
egctl c proxy l <pod-name> -n <pod-namespace>
`,
Run: func(c *cobra.Command, args []string) {
cmdutil.CheckErr(runListenerConfig(c, args))
Expand All @@ -297,33 +219,7 @@ func listenerConfigCmd() *cobra.Command {
}

func runListenerConfig(c *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("pod name is required")
}

podName = args[0]

if podName == "" {
return fmt.Errorf("pod name is required")
}

if podNamespace == "" {
return fmt.Errorf("pod namespace is required")
}

fw, err := portForwarder(types.NamespacedName{
Namespace: podNamespace,
Name: podName,
})
if err != nil {
return err
}
if err := fw.Start(); err != nil {
return err
}
defer fw.Stop()

configDump, err := extractConfigDump(fw)
configDump, err := retrieveConfigDump(args)
if err != nil {
return err
}
Expand Down Expand Up @@ -356,7 +252,7 @@ func routeConfigCmd() *cobra.Command {
egctl config envoy-proxy route <pod-name> -n <pod-namespace> -o yaml
# Retrieve full configuration dump with short syntax
egctl c proxy route <pod-name> -n <pod-namespace>
egctl c proxy r <pod-name> -n <pod-namespace>
`,
Run: func(c *cobra.Command, args []string) {
cmdutil.CheckErr(runRouteConfig(c, args))
Expand All @@ -367,49 +263,58 @@ func routeConfigCmd() *cobra.Command {
}

func runRouteConfig(c *cobra.Command, args []string) error {
configDump, err := retrieveConfigDump(args)
if err != nil {
return err
}

route, err := findXDSResourceFromConfigDump(RouteEnvoyConfigType, configDump)
if err != nil {
return err
}

out, err := marshalEnvoyProxyConfig(route, output)
if err != nil {
return err
}

_, err = fmt.Fprintln(c.OutOrStdout(), string(out))
return err
}

func retrieveConfigDump(args []string) (*adminv3.ConfigDump, error) {
if len(args) == 0 {
return fmt.Errorf("pod name is required")
return nil, fmt.Errorf("pod name is required")
}

podName = args[0]

if podName == "" {
return fmt.Errorf("pod name is required")
return nil, fmt.Errorf("pod name is required")
}

if podNamespace == "" {
return fmt.Errorf("pod namespace is required")
return nil, fmt.Errorf("pod namespace is required")
}

fw, err := portForwarder(types.NamespacedName{
Namespace: podNamespace,
Name: podName,
})
if err != nil {
return err
return nil, err
}
if err := fw.Start(); err != nil {
return err
return nil, err
}
defer fw.Stop()

configDump, err := extractConfigDump(fw)
if err != nil {
return err
}

route, err := findXDSResourceFromConfigDump(RouteEnvoyConfigType, configDump)
if err != nil {
return err
}

out, err := marshalEnvoyProxyConfig(route, output)
if err != nil {
return err
return nil, err
}

_, err = fmt.Fprintln(c.OutOrStdout(), string(out))
return err
return configDump, nil
}

func portForwarder(nn types.NamespacedName) (kube.PortForwarder, error) {
Expand Down

0 comments on commit 19ccd11

Please sign in to comment.