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 9b91dd7 commit 8e48864
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 145 deletions.
196 changes: 52 additions & 144 deletions internal/cmd/egctl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (
"io"
"net/http"

adminv3 "github.com/envoyproxy/go-control-plane/envoy/admin/v3"
"github.com/spf13/cobra"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/reflect/protoreflect"
"k8s.io/apimachinery/pkg/types"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/yaml"

adminv3 "github.com/envoyproxy/go-control-plane/envoy/admin/v3"

"github.com/envoyproxy/gateway/internal/cmd/options"
kube "github.com/envoyproxy/gateway/internal/kubernetes"
)
Expand Down Expand Up @@ -93,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 All @@ -136,17 +109,18 @@ func runAllConfig(c *cobra.Command, args []string) error {
func bootstrapConfigCmd() *cobra.Command {

configCmd := &cobra.Command{
Use: "bootstrap <pod-name>",
Short: "Retrieves bootstrap Envoy xDS resources from the specified pod",
Long: `Retrieves information about bootstrap Envoy xDS resources from the Envoy instance in the specified pod.`,
Use: "bootstrap <pod-name>",
Aliases: []string{"b"},
Short: "Retrieves bootstrap Envoy xDS resources from the specified pod",
Long: `Retrieves information about bootstrap Envoy xDS resources from the Envoy instance in the specified pod.`,
Example: ` # Retrieve summary about bootstrap configuration for a given pod from Envoy.
egctl config envoy-proxy bootstrap <pod-name> -n <pod-namespace>
# Retrieve full configuration dump as YAML
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 All @@ -205,17 +153,18 @@ func runBootstrapConfig(c *cobra.Command, args []string) error {
func clusterConfigCmd() *cobra.Command {

configCmd := &cobra.Command{
Use: "cluster <pod-name>",
Short: "Retrieves cluster Envoy xDS resources from the specified pod",
Long: `Retrieves information about cluster Envoy xDS resources from the Envoy instance in the specified pod.`,
Use: "cluster <pod-name>",
Short: "Retrieves cluster Envoy xDS resources from the specified pod",
Aliases: []string{"c"},
Long: `Retrieves information about cluster Envoy xDS resources from the Envoy instance in the specified pod.`,
Example: ` # Retrieve summary about cluster configuration for a given pod from Envoy.
egctl config envoy-proxy cluster <pod-name> -n <pod-namespace>
# Retrieve full configuration dump as YAML
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 @@ -226,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 All @@ -274,17 +197,18 @@ func runClusterConfig(c *cobra.Command, args []string) error {
func listenerConfigCmd() *cobra.Command {

configCmd := &cobra.Command{
Use: "listener <pod-name>",
Short: "Retrieves listener Envoy xDS resources from the specified pod",
Long: `Retrieves information about listener Envoy xDS resources from the Envoy instance in the specified pod.`,
Use: "listener <pod-name>",
Aliases: []string{"l"},
Short: "Retrieves listener Envoy xDS resources from the specified pod",
Long: `Retrieves information about listener Envoy xDS resources from the Envoy instance in the specified pod.`,
Example: ` # Retrieve summary about listener configuration for a given pod from Envoy.
egctl config envoy-proxy listener <pod-name> -n <pod-namespace>
# Retrieve full configuration dump as YAML
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 @@ -295,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 All @@ -343,17 +241,18 @@ func runListenerConfig(c *cobra.Command, args []string) error {
func routeConfigCmd() *cobra.Command {

configCmd := &cobra.Command{
Use: "route <pod-name>",
Short: "Retrieves route Envoy xDS resources from the specified pod",
Long: `Retrieves information about route Envoy xDS resources from the Envoy instance in the specified pod.`,
Use: "route <pod-name>",
Aliases: []string{"r"},
Short: "Retrieves route Envoy xDS resources from the specified pod",
Long: `Retrieves information about route Envoy xDS resources from the Envoy instance in the specified pod.`,
Example: ` # Retrieve summary about route configuration for a given pod from Envoy.
egctl config envoy-proxy route <pod-name> -n <pod-namespace>
# Retrieve full configuration dump as YAML
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 @@ -364,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
1 change: 0 additions & 1 deletion internal/cmd/egctl/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ func TestExtractSubResourcesConfigDump(t *testing.T) {
resource, err := findXDSResourceFromConfigDump(tc.resourceType, configDump)
assert.NoError(t, err)
got, err := marshalEnvoyProxyConfig(resource, tc.output)
t.Log("GOT:-----------------\n\n", string(got), "\n\n\n")
assert.NoError(t, err)
out, err := readOutputConfig(tc.expected)
assert.NoError(t, err)
Expand Down

0 comments on commit 8e48864

Please sign in to comment.