Skip to content

Commit

Permalink
fix endpoint cannot retrieve bug. (envoyproxy#1148)
Browse files Browse the repository at this point in the history
Signed-off-by: qicz <[email protected]>
  • Loading branch information
qicz authored Mar 16, 2023
1 parent 289c18c commit 73e38e0
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 20 deletions.
34 changes: 16 additions & 18 deletions internal/cmd/egctl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func allConfigCmd() *cobra.Command {
}

func runAllConfig(c *cobra.Command, args []string) error {
configDump, err := retrieveConfigDump(args)
configDump, err := retrieveConfigDump(args, true)
if err != nil {
return err
}
Expand All @@ -108,7 +108,6 @@ func runAllConfig(c *cobra.Command, args []string) error {
}

func bootstrapConfigCmd() *cobra.Command {

configCmd := &cobra.Command{
Use: "bootstrap <pod-name>",
Aliases: []string{"b"},
Expand All @@ -132,7 +131,7 @@ func bootstrapConfigCmd() *cobra.Command {
}

func runBootstrapConfig(c *cobra.Command, args []string) error {
configDump, err := retrieveConfigDump(args)
configDump, err := retrieveConfigDump(args, false)
if err != nil {
return err
}
Expand All @@ -152,7 +151,6 @@ 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",
Expand All @@ -176,7 +174,7 @@ func clusterConfigCmd() *cobra.Command {
}

func runClusterConfig(c *cobra.Command, args []string) error {
configDump, err := retrieveConfigDump(args)
configDump, err := retrieveConfigDump(args, false)
if err != nil {
return err
}
Expand All @@ -196,7 +194,6 @@ func runClusterConfig(c *cobra.Command, args []string) error {
}

func endpointConfigCmd() *cobra.Command {

configCmd := &cobra.Command{
Use: "endpoint <pod-name>",
Short: "Retrieves endpoint Envoy xDS resources from the specified pod",
Expand All @@ -220,7 +217,7 @@ func endpointConfigCmd() *cobra.Command {
}

func runEndpointConfig(c *cobra.Command, args []string) error {
configDump, err := retrieveConfigDump(args)
configDump, err := retrieveConfigDump(args, true)
if err != nil {
return err
}
Expand All @@ -240,7 +237,6 @@ func runEndpointConfig(c *cobra.Command, args []string) error {
}

func listenerConfigCmd() *cobra.Command {

configCmd := &cobra.Command{
Use: "listener <pod-name>",
Aliases: []string{"l"},
Expand All @@ -264,7 +260,7 @@ func listenerConfigCmd() *cobra.Command {
}

func runListenerConfig(c *cobra.Command, args []string) error {
configDump, err := retrieveConfigDump(args)
configDump, err := retrieveConfigDump(args, false)
if err != nil {
return err
}
Expand All @@ -284,7 +280,6 @@ func runListenerConfig(c *cobra.Command, args []string) error {
}

func routeConfigCmd() *cobra.Command {

configCmd := &cobra.Command{
Use: "route <pod-name>",
Aliases: []string{"r"},
Expand All @@ -308,7 +303,7 @@ func routeConfigCmd() *cobra.Command {
}

func runRouteConfig(c *cobra.Command, args []string) error {
configDump, err := retrieveConfigDump(args)
configDump, err := retrieveConfigDump(args, false)
if err != nil {
return err
}
Expand All @@ -327,7 +322,7 @@ func runRouteConfig(c *cobra.Command, args []string) error {
return err
}

func retrieveConfigDump(args []string) (*adminv3.ConfigDump, error) {
func retrieveConfigDump(args []string, includeEds bool) (*adminv3.ConfigDump, error) {
if len(args) == 0 {
return nil, fmt.Errorf("pod name is required")
}
Expand All @@ -354,7 +349,7 @@ func retrieveConfigDump(args []string) (*adminv3.ConfigDump, error) {
}
defer fw.Stop()

configDump, err := extractConfigDump(fw)
configDump, err := extractConfigDump(fw, includeEds)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -385,7 +380,6 @@ func portForwarder(nn types.NamespacedName) (kube.PortForwarder, error) {
}

func marshalEnvoyProxyConfig(configDump protoreflect.ProtoMessage, output string) ([]byte, error) {

out, err := protojson.MarshalOptions{
Multiline: true,
}.Marshal(configDump)
Expand All @@ -403,8 +397,8 @@ func marshalEnvoyProxyConfig(configDump protoreflect.ProtoMessage, output string
return out, nil
}

func extractConfigDump(fw kube.PortForwarder) (*adminv3.ConfigDump, error) {
out, err := configDumpRequest(fw.Address())
func extractConfigDump(fw kube.PortForwarder, includeEds bool) (*adminv3.ConfigDump, error) {
out, err := configDumpRequest(fw.Address(), includeEds)
if err != nil {
return nil, err
}
Expand All @@ -417,8 +411,12 @@ func extractConfigDump(fw kube.PortForwarder) (*adminv3.ConfigDump, error) {
return configDump, nil
}

func configDumpRequest(address string) ([]byte, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/config_dump", address), nil)
func configDumpRequest(address string, includeEds bool) ([]byte, error) {
url := fmt.Sprintf("http://%s/config_dump", address)
if includeEds {
url = fmt.Sprintf("%s?include_eds", url)
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
Expand Down
14 changes: 12 additions & 2 deletions internal/cmd/egctl/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestExtractAllConfigDump(t *testing.T) {

for _, tc := range cases {
t.Run(tc.output, func(t *testing.T) {
configDump, err := extractConfigDump(fw)
configDump, err := extractConfigDump(fw, true)
assert.NoError(t, err)
got, err := marshalEnvoyProxyConfig(configDump, tc.output)
assert.NoError(t, err)
Expand Down Expand Up @@ -162,11 +162,21 @@ func TestExtractSubResourcesConfigDump(t *testing.T) {
resourceType: RouteEnvoyConfigType,
expected: "out.route.yaml",
},
{
output: "json",
resourceType: EndpointEnvoyConfigType,
expected: "out.endpoints.json",
},
{
output: "yaml",
resourceType: EndpointEnvoyConfigType,
expected: "out.endpoints.yaml",
},
}

for _, tc := range cases {
t.Run(tc.output, func(t *testing.T) {
configDump, err := extractConfigDump(fw)
configDump, err := extractConfigDump(fw, false)
assert.NoError(t, err)
resource, err := findXDSResourceFromConfigDump(tc.resourceType, configDump)
assert.NoError(t, err)
Expand Down
36 changes: 36 additions & 0 deletions internal/cmd/egctl/testdata/config/in/in.all.json
Original file line number Diff line number Diff line change
Expand Up @@ -2271,6 +2271,42 @@
}
}
]
},
{
"@type": "type.googleapis.com/envoy.admin.v3.EndpointsConfigDump",
"staticEndpointConfigs": [
{
"endpointConfig": {
"@type": "type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment",
"clusterName": "xds_cluster",
"endpoints": [
{
"locality": {},
"lbEndpoints": [
{
"endpoint": {
"address": {
"socketAddress": {
"address": "10.106.37.54",
"portValue": 18000
}
},
"healthCheckConfig": {},
"hostname": "envoy-gateway"
},
"healthStatus": "HEALTHY",
"metadata": {},
"loadBalancingWeight": 1
}
]
}
],
"policy": {
"overprovisioningFactor": 140
}
}
}
]
}
]
}
35 changes: 35 additions & 0 deletions internal/cmd/egctl/testdata/config/out/out.all.json
Original file line number Diff line number Diff line change
Expand Up @@ -1470,5 +1470,40 @@
}
}
}]
}, {
"@type": "type.googleapis.com/envoy.admin.v3.EndpointsConfigDump",
"staticEndpointConfigs": [
{
"endpointConfig": {
"@type": "type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment",
"clusterName": "xds_cluster",
"endpoints": [
{
"locality": {},
"lbEndpoints": [
{
"endpoint": {
"address": {
"socketAddress": {
"address": "10.106.37.54",
"portValue": 18000
}
},
"healthCheckConfig": {},
"hostname": "envoy-gateway"
},
"healthStatus": "HEALTHY",
"metadata": {},
"loadBalancingWeight": 1
}
]
}
],
"policy": {
"overprovisioningFactor": 140
}
}
}
]
}]
}
20 changes: 20 additions & 0 deletions internal/cmd/egctl/testdata/config/out/out.all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1282,3 +1282,23 @@ configs:
sanType: DNS
trustedCa:
inlineBytes: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURHRENDQWdDZ0F3SUJBZ0lFSWFxd1VUQU5CZ2txaGtpRzl3MEJBUXNGQURBc01SWXdGQVlEVlFRREV3MWwKYm5admVTMW5ZWFJsZDJGNU1SSXdFQVlEVlFRRkV3azFOalE0TXpRek9EVXdIaGNOTWpNd01qRTNNRE0wTVRJNApXaGNOTWpRd01qRTRNRE0wTVRJNFdqQXNNUll3RkFZRFZRUURFdzFsYm5admVTMW5ZWFJsZDJ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kF5aGhzOXBHNERndkpSY1BLeFY4bndRdzZtSm55dkIxcExxTW1aQTVqZWhxbFNvUGVpWUlBMk1neU83cTVPYmMKL040bzBNTVdvZ1piRWR6aTBnTXJRT2lpNE41Q0ZlakVrYStIMmc9PQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
- '@type': type.googleapis.com/envoy.admin.v3.EndpointsConfigDump
staticEndpointConfigs:
- endpointConfig:
'@type': type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment
clusterName: xds_cluster
endpoints:
- lbEndpoints:
- endpoint:
address:
socketAddress:
address: 10.106.37.54
portValue: 18000
healthCheckConfig: {}
hostname: envoy-gateway
healthStatus: HEALTHY
loadBalancingWeight: 1
metadata: {}
locality: {}
policy:
overprovisioningFactor: 140
36 changes: 36 additions & 0 deletions internal/cmd/egctl/testdata/config/out/out.endpoints.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"@type": "type.googleapis.com/envoy.admin.v3.EndpointsConfigDump",
"staticEndpointConfigs": [
{
"endpointConfig": {
"@type": "type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment",
"clusterName": "xds_cluster",
"endpoints": [
{
"locality": {},
"lbEndpoints": [
{
"endpoint": {
"address": {
"socketAddress": {
"address": "10.106.37.54",
"portValue": 18000
}
},
"healthCheckConfig": {},
"hostname": "envoy-gateway"
},
"healthStatus": "HEALTHY",
"metadata": {},
"loadBalancingWeight": 1
}
]
}
],
"policy": {
"overprovisioningFactor": 140
}
}
}
]
}
20 changes: 20 additions & 0 deletions internal/cmd/egctl/testdata/config/out/out.endpoints.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'@type': type.googleapis.com/envoy.admin.v3.EndpointsConfigDump
staticEndpointConfigs:
- endpointConfig:
'@type': type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment
clusterName: xds_cluster
endpoints:
- lbEndpoints:
- endpoint:
address:
socketAddress:
address: 10.106.37.54
portValue: 18000
healthCheckConfig: {}
hostname: envoy-gateway
healthStatus: HEALTHY
loadBalancingWeight: 1
metadata: {}
locality: {}
policy:
overprovisioningFactor: 140

0 comments on commit 73e38e0

Please sign in to comment.