From fca17d365aafb230ec084b685ebef5b5a6d3e981 Mon Sep 17 00:00:00 2001 From: Caleb Doxsey Date: Mon, 1 Jun 2020 08:58:28 -0600 Subject: [PATCH] xds: force ipv4 for localhost to workaround ipv6 issue in docker compose (#819) --- internal/controlplane/xds_cluster_test.go | 30 +++++++++++++++++++++++ internal/controlplane/xds_clusters.go | 8 ++++++ 2 files changed, 38 insertions(+) diff --git a/internal/controlplane/xds_cluster_test.go b/internal/controlplane/xds_cluster_test.go index 6361b0e4a..57e64a42d 100644 --- a/internal/controlplane/xds_cluster_test.go +++ b/internal/controlplane/xds_cluster_test.go @@ -271,4 +271,34 @@ func Test_buildCluster(t *testing.T) { } `, cluster) }) + t.Run("localhost", func(t *testing.T) { + cluster := buildCluster("example", mustParseURL("http://localhost"), nil, true) + testutil.AssertProtoJSONEqual(t, ` + { + "name": "example", + "type": "STATIC", + "connectTimeout": "10s", + "respectDnsTtl": true, + "http2ProtocolOptions": { + "allowConnect": true + }, + "loadAssignment": { + "clusterName": "example", + "endpoints": [{ + "lbEndpoints": [{ + "endpoint": { + "address": { + "socketAddress": { + "address": "127.0.0.1", + "ipv4Compat": true, + "portValue": 80 + } + } + } + }] + }] + } + } + `, cluster) + }) } diff --git a/internal/controlplane/xds_clusters.go b/internal/controlplane/xds_clusters.go index bab567c91..51dd802c5 100644 --- a/internal/controlplane/xds_clusters.go +++ b/internal/controlplane/xds_clusters.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "net" "net/url" + "strings" "time" envoy_config_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3" @@ -185,6 +186,13 @@ func buildCluster( defaultPort = 443 } + if endpoint.Hostname() == "localhost" { + u := new(url.URL) + *u = *endpoint + u.Host = strings.Replace(endpoint.Host, "localhost", "127.0.0.1", -1) + endpoint = u + } + cluster := &envoy_config_cluster_v3.Cluster{ Name: name, ConnectTimeout: ptypes.DurationProto(time.Second * 10),