From 5bcfe930c67534baf9457244b8eede5865cae574 Mon Sep 17 00:00:00 2001 From: "R.B. Boyer" <4903+rboyer@users.noreply.github.com> Date: Wed, 7 Apr 2021 14:22:52 -0500 Subject: [PATCH] command: when generating envoy bootstrap configs to stdout do not mix informational logs into the json (#9980) Fixes #9921 --- .changelog/9980.txt | 3 +++ command/connect/envoy/envoy.go | 25 ++++++------------- command/connect/envoy/envoy_test.go | 13 +++------- ..._ADDR-with-https-scheme-enables-tls.golden | 1 + .../envoy/testdata/access-log-path.golden | 1 + .../envoy/testdata/custom-bootstrap.golden | 2 +- .../connect/envoy/testdata/defaults.golden | 1 + .../envoy/testdata/existing-ca-file.golden | 1 + .../envoy/testdata/existing-ca-path.golden | 1 + .../envoy/testdata/extra_-multiple.golden | 1 + .../envoy/testdata/extra_-single.golden | 1 + .../envoy/testdata/grpc-addr-config.golden | 1 + .../envoy/testdata/grpc-addr-env.golden | 1 + .../envoy/testdata/grpc-addr-flag.golden | 1 + .../envoy/testdata/grpc-addr-unix.golden | 1 + .../ingress-gateway-address-specified.golden | 1 + .../ingress-gateway-no-auto-register.golden | 1 + ...-register-with-service-and-proxy-id.golden | 1 + ...ister-with-service-without-proxy-id.golden | 1 + .../envoy/testdata/ingress-gateway.golden | 1 + .../envoy/testdata/prometheus-metrics.golden | 1 + .../testdata/stats-config-override.golden | 1 + .../connect/envoy/testdata/token-arg.golden | 1 + .../connect/envoy/testdata/token-env.golden | 1 + .../envoy/testdata/token-file-arg.golden | 1 + .../envoy/testdata/token-file-env.golden | 1 + .../testdata/zipkin-tracing-config.golden | 1 + 27 files changed, 38 insertions(+), 28 deletions(-) create mode 100644 .changelog/9980.txt diff --git a/.changelog/9980.txt b/.changelog/9980.txt new file mode 100644 index 0000000000..9a5d7b3e58 --- /dev/null +++ b/.changelog/9980.txt @@ -0,0 +1,3 @@ +```release-note:bug +command: when generating envoy bootstrap configs to stdout do not mix informational logs into the json +``` diff --git a/command/connect/envoy/envoy.go b/command/connect/envoy/envoy.go index a53867e7f0..1547085237 100644 --- a/command/connect/envoy/envoy.go +++ b/command/connect/envoy/envoy.go @@ -4,7 +4,6 @@ import ( "errors" "flag" "fmt" - "io" "net" "os" "os/exec" @@ -24,17 +23,7 @@ import ( ) func New(ui cli.Ui) *cmd { - ui = &cli.PrefixedUi{ - OutputPrefix: "==> ", - InfoPrefix: " ", - ErrorPrefix: "==> ", - Ui: ui, - } - - c := &cmd{ - UI: ui, - directOut: os.Stdout, - } + c := &cmd{UI: ui} c.init() return c } @@ -47,9 +36,6 @@ type cmd struct { http *flags.HTTPFlags help string client *api.Client - // DirectOut defaults to os.stdout but is a property to allow capture during - // tests to have more useful output. - directOut io.Writer // flags meshGateway bool @@ -380,7 +366,11 @@ func (c *cmd) run(args []string) int { return 1 } - c.UI.Output(fmt.Sprintf("Registered service: %s", svc.Name)) + if !c.bootstrap { + // We need stdout to be reserved exclusively for the JSON blob, so + // we omit logging this to Info which also writes to stdout. + c.UI.Info(fmt.Sprintf("Registered service: %s", svc.Name)) + } } // Generate config @@ -392,7 +382,7 @@ func (c *cmd) run(args []string) int { if c.bootstrap { // Just output it and we are done - c.directOut.Write(bootstrapJson) + c.UI.Output(string(bootstrapJson)) return 0 } @@ -578,7 +568,6 @@ func (c *cmd) grpcAddress(httpCfg *api.Config) (GRPC, error) { // This is the dev mode default and recommended production setting if // enabled. port = 8502 - c.UI.Info(fmt.Sprintf("Defaulting to grpc port = %d", port)) } addr = fmt.Sprintf("localhost:%v", port) } diff --git a/command/connect/envoy/envoy_test.go b/command/connect/envoy/envoy_test.go index 471688c000..67698a7c65 100644 --- a/command/connect/envoy/envoy_test.go +++ b/command/connect/envoy/envoy_test.go @@ -1,7 +1,6 @@ package envoy import ( - "bytes" "encoding/json" "flag" "io/ioutil" @@ -15,12 +14,13 @@ import ( "github.com/stretchr/testify/assert" + "github.com/mitchellh/cli" + "github.com/stretchr/testify/require" + "github.com/hashicorp/consul/agent" "github.com/hashicorp/consul/agent/xds" "github.com/hashicorp/consul/api" "github.com/hashicorp/consul/sdk/testutil" - "github.com/mitchellh/cli" - "github.com/stretchr/testify/require" ) var update = flag.Bool("update", false, "update golden files") @@ -874,11 +874,6 @@ func TestGenerateConfig(t *testing.T) { // explicitly set the client to one which can connect to the httptest.Server c.client = client - var outBuf bytes.Buffer - // Capture output since it clutters test output and we can assert on what - // was actually printed this way. - c.directOut = &outBuf - // Run the command myFlags := copyAndReplaceAll(tc.Flags, "@@TEMPDIR@@", testDirPrefix) args := append([]string{"-bootstrap"}, myFlags...) @@ -899,7 +894,7 @@ func TestGenerateConfig(t *testing.T) { require.NoError(err) // Error cases should have returned above require.Equal(&tc.WantArgs, got) - actual := outBuf.Bytes() + actual := ui.OutputWriter.Bytes() // If we got the arg handling write, verify output golden := filepath.Join("testdata", tc.Name+".golden") diff --git a/command/connect/envoy/testdata/CONSUL_HTTP_ADDR-with-https-scheme-enables-tls.golden b/command/connect/envoy/testdata/CONSUL_HTTP_ADDR-with-https-scheme-enables-tls.golden index c549aa9821..b13b4da043 100644 --- a/command/connect/envoy/testdata/CONSUL_HTTP_ADDR-with-https-scheme-enables-tls.golden +++ b/command/connect/envoy/testdata/CONSUL_HTTP_ADDR-with-https-scheme-enables-tls.golden @@ -190,3 +190,4 @@ } } } + diff --git a/command/connect/envoy/testdata/access-log-path.golden b/command/connect/envoy/testdata/access-log-path.golden index c729985d01..21c827d815 100644 --- a/command/connect/envoy/testdata/access-log-path.golden +++ b/command/connect/envoy/testdata/access-log-path.golden @@ -177,3 +177,4 @@ } } } + diff --git a/command/connect/envoy/testdata/custom-bootstrap.golden b/command/connect/envoy/testdata/custom-bootstrap.golden index 98746ead1e..fa7e553988 100644 --- a/command/connect/envoy/testdata/custom-bootstrap.golden +++ b/command/connect/envoy/testdata/custom-bootstrap.golden @@ -13,4 +13,4 @@ "id": "test-proxy" }, "custom_field": "foo" -} \ No newline at end of file +} diff --git a/command/connect/envoy/testdata/defaults.golden b/command/connect/envoy/testdata/defaults.golden index bd0d2ac8f4..1bb73227a1 100644 --- a/command/connect/envoy/testdata/defaults.golden +++ b/command/connect/envoy/testdata/defaults.golden @@ -177,3 +177,4 @@ } } } + diff --git a/command/connect/envoy/testdata/existing-ca-file.golden b/command/connect/envoy/testdata/existing-ca-file.golden index da29d6d4da..04e268b291 100644 --- a/command/connect/envoy/testdata/existing-ca-file.golden +++ b/command/connect/envoy/testdata/existing-ca-file.golden @@ -190,3 +190,4 @@ } } } + diff --git a/command/connect/envoy/testdata/existing-ca-path.golden b/command/connect/envoy/testdata/existing-ca-path.golden index 8f7cd86fb9..29b05398a0 100644 --- a/command/connect/envoy/testdata/existing-ca-path.golden +++ b/command/connect/envoy/testdata/existing-ca-path.golden @@ -190,3 +190,4 @@ } } } + diff --git a/command/connect/envoy/testdata/extra_-multiple.golden b/command/connect/envoy/testdata/extra_-multiple.golden index 31e4fad806..5fffe62eee 100644 --- a/command/connect/envoy/testdata/extra_-multiple.golden +++ b/command/connect/envoy/testdata/extra_-multiple.golden @@ -199,3 +199,4 @@ } } } + diff --git a/command/connect/envoy/testdata/extra_-single.golden b/command/connect/envoy/testdata/extra_-single.golden index deac15295e..e839bc811d 100644 --- a/command/connect/envoy/testdata/extra_-single.golden +++ b/command/connect/envoy/testdata/extra_-single.golden @@ -190,3 +190,4 @@ } } } + diff --git a/command/connect/envoy/testdata/grpc-addr-config.golden b/command/connect/envoy/testdata/grpc-addr-config.golden index 2b1b081582..6e1b003822 100644 --- a/command/connect/envoy/testdata/grpc-addr-config.golden +++ b/command/connect/envoy/testdata/grpc-addr-config.golden @@ -177,3 +177,4 @@ } } } + diff --git a/command/connect/envoy/testdata/grpc-addr-env.golden b/command/connect/envoy/testdata/grpc-addr-env.golden index 2b1b081582..6e1b003822 100644 --- a/command/connect/envoy/testdata/grpc-addr-env.golden +++ b/command/connect/envoy/testdata/grpc-addr-env.golden @@ -177,3 +177,4 @@ } } } + diff --git a/command/connect/envoy/testdata/grpc-addr-flag.golden b/command/connect/envoy/testdata/grpc-addr-flag.golden index 2b1b081582..6e1b003822 100644 --- a/command/connect/envoy/testdata/grpc-addr-flag.golden +++ b/command/connect/envoy/testdata/grpc-addr-flag.golden @@ -177,3 +177,4 @@ } } } + diff --git a/command/connect/envoy/testdata/grpc-addr-unix.golden b/command/connect/envoy/testdata/grpc-addr-unix.golden index 9fabd35363..20730ff9b4 100644 --- a/command/connect/envoy/testdata/grpc-addr-unix.golden +++ b/command/connect/envoy/testdata/grpc-addr-unix.golden @@ -176,3 +176,4 @@ } } } + diff --git a/command/connect/envoy/testdata/ingress-gateway-address-specified.golden b/command/connect/envoy/testdata/ingress-gateway-address-specified.golden index 97f6e68860..9676c0de3b 100644 --- a/command/connect/envoy/testdata/ingress-gateway-address-specified.golden +++ b/command/connect/envoy/testdata/ingress-gateway-address-specified.golden @@ -263,3 +263,4 @@ } } } + diff --git a/command/connect/envoy/testdata/ingress-gateway-no-auto-register.golden b/command/connect/envoy/testdata/ingress-gateway-no-auto-register.golden index c848b17f45..d454e8caa9 100644 --- a/command/connect/envoy/testdata/ingress-gateway-no-auto-register.golden +++ b/command/connect/envoy/testdata/ingress-gateway-no-auto-register.golden @@ -263,3 +263,4 @@ } } } + diff --git a/command/connect/envoy/testdata/ingress-gateway-register-with-service-and-proxy-id.golden b/command/connect/envoy/testdata/ingress-gateway-register-with-service-and-proxy-id.golden index 87f534df7e..345d22e343 100644 --- a/command/connect/envoy/testdata/ingress-gateway-register-with-service-and-proxy-id.golden +++ b/command/connect/envoy/testdata/ingress-gateway-register-with-service-and-proxy-id.golden @@ -263,3 +263,4 @@ } } } + diff --git a/command/connect/envoy/testdata/ingress-gateway-register-with-service-without-proxy-id.golden b/command/connect/envoy/testdata/ingress-gateway-register-with-service-without-proxy-id.golden index 40f0aab2de..1976535c65 100644 --- a/command/connect/envoy/testdata/ingress-gateway-register-with-service-without-proxy-id.golden +++ b/command/connect/envoy/testdata/ingress-gateway-register-with-service-without-proxy-id.golden @@ -263,3 +263,4 @@ } } } + diff --git a/command/connect/envoy/testdata/ingress-gateway.golden b/command/connect/envoy/testdata/ingress-gateway.golden index 8a918196ce..05a7e2f8e9 100644 --- a/command/connect/envoy/testdata/ingress-gateway.golden +++ b/command/connect/envoy/testdata/ingress-gateway.golden @@ -263,3 +263,4 @@ } } } + diff --git a/command/connect/envoy/testdata/prometheus-metrics.golden b/command/connect/envoy/testdata/prometheus-metrics.golden index 328671e294..8d1e1be750 100644 --- a/command/connect/envoy/testdata/prometheus-metrics.golden +++ b/command/connect/envoy/testdata/prometheus-metrics.golden @@ -263,3 +263,4 @@ } } } + diff --git a/command/connect/envoy/testdata/stats-config-override.golden b/command/connect/envoy/testdata/stats-config-override.golden index 1e3becd70e..674cf1adf3 100644 --- a/command/connect/envoy/testdata/stats-config-override.golden +++ b/command/connect/envoy/testdata/stats-config-override.golden @@ -75,3 +75,4 @@ } } } + diff --git a/command/connect/envoy/testdata/token-arg.golden b/command/connect/envoy/testdata/token-arg.golden index 1ba256fd15..0a5e88b54b 100644 --- a/command/connect/envoy/testdata/token-arg.golden +++ b/command/connect/envoy/testdata/token-arg.golden @@ -177,3 +177,4 @@ } } } + diff --git a/command/connect/envoy/testdata/token-env.golden b/command/connect/envoy/testdata/token-env.golden index 1ba256fd15..0a5e88b54b 100644 --- a/command/connect/envoy/testdata/token-env.golden +++ b/command/connect/envoy/testdata/token-env.golden @@ -177,3 +177,4 @@ } } } + diff --git a/command/connect/envoy/testdata/token-file-arg.golden b/command/connect/envoy/testdata/token-file-arg.golden index 1ba256fd15..0a5e88b54b 100644 --- a/command/connect/envoy/testdata/token-file-arg.golden +++ b/command/connect/envoy/testdata/token-file-arg.golden @@ -177,3 +177,4 @@ } } } + diff --git a/command/connect/envoy/testdata/token-file-env.golden b/command/connect/envoy/testdata/token-file-env.golden index 1ba256fd15..0a5e88b54b 100644 --- a/command/connect/envoy/testdata/token-file-env.golden +++ b/command/connect/envoy/testdata/token-file-env.golden @@ -177,3 +177,4 @@ } } } + diff --git a/command/connect/envoy/testdata/zipkin-tracing-config.golden b/command/connect/envoy/testdata/zipkin-tracing-config.golden index a9880a295d..8ffaec2ce4 100644 --- a/command/connect/envoy/testdata/zipkin-tracing-config.golden +++ b/command/connect/envoy/testdata/zipkin-tracing-config.golden @@ -210,3 +210,4 @@ } } } +