command: when generating envoy bootstrap configs to stdout do not mix informational logs into the json (#9980)

Fixes #9921
This commit is contained in:
R.B. Boyer 2021-04-07 14:22:52 -05:00 committed by GitHub
parent 014547159c
commit 36c74bf865
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 38 additions and 28 deletions

3
.changelog/9980.txt Normal file
View file

@ -0,0 +1,3 @@
```release-note:bug
command: when generating envoy bootstrap configs to stdout do not mix informational logs into the json
```

View file

@ -4,7 +4,6 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"io"
"net" "net"
"os" "os"
"os/exec" "os/exec"
@ -24,17 +23,7 @@ import (
) )
func New(ui cli.Ui) *cmd { func New(ui cli.Ui) *cmd {
ui = &cli.PrefixedUi{ c := &cmd{UI: ui}
OutputPrefix: "==> ",
InfoPrefix: " ",
ErrorPrefix: "==> ",
Ui: ui,
}
c := &cmd{
UI: ui,
directOut: os.Stdout,
}
c.init() c.init()
return c return c
} }
@ -47,9 +36,6 @@ type cmd struct {
http *flags.HTTPFlags http *flags.HTTPFlags
help string help string
client *api.Client 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 // flags
meshGateway bool meshGateway bool
@ -380,7 +366,11 @@ func (c *cmd) run(args []string) int {
return 1 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 // Generate config
@ -392,7 +382,7 @@ func (c *cmd) run(args []string) int {
if c.bootstrap { if c.bootstrap {
// Just output it and we are done // Just output it and we are done
c.directOut.Write(bootstrapJson) c.UI.Output(string(bootstrapJson))
return 0 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 // This is the dev mode default and recommended production setting if
// enabled. // enabled.
port = 8502 port = 8502
c.UI.Info(fmt.Sprintf("Defaulting to grpc port = %d", port))
} }
addr = fmt.Sprintf("localhost:%v", port) addr = fmt.Sprintf("localhost:%v", port)
} }

View file

@ -1,7 +1,6 @@
package envoy package envoy
import ( import (
"bytes"
"encoding/json" "encoding/json"
"flag" "flag"
"io/ioutil" "io/ioutil"
@ -15,12 +14,13 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/mitchellh/cli"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/agent" "github.com/hashicorp/consul/agent"
"github.com/hashicorp/consul/agent/xds" "github.com/hashicorp/consul/agent/xds"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/sdk/testutil" "github.com/hashicorp/consul/sdk/testutil"
"github.com/mitchellh/cli"
"github.com/stretchr/testify/require"
) )
var update = flag.Bool("update", false, "update golden files") 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 // explicitly set the client to one which can connect to the httptest.Server
c.client = client 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 // Run the command
myFlags := copyAndReplaceAll(tc.Flags, "@@TEMPDIR@@", testDirPrefix) myFlags := copyAndReplaceAll(tc.Flags, "@@TEMPDIR@@", testDirPrefix)
args := append([]string{"-bootstrap"}, myFlags...) args := append([]string{"-bootstrap"}, myFlags...)
@ -899,7 +894,7 @@ func TestGenerateConfig(t *testing.T) {
require.NoError(err) // Error cases should have returned above require.NoError(err) // Error cases should have returned above
require.Equal(&tc.WantArgs, got) require.Equal(&tc.WantArgs, got)
actual := outBuf.Bytes() actual := ui.OutputWriter.Bytes()
// If we got the arg handling write, verify output // If we got the arg handling write, verify output
golden := filepath.Join("testdata", tc.Name+".golden") golden := filepath.Join("testdata", tc.Name+".golden")

View file

@ -177,3 +177,4 @@
} }
} }
} }

View file

@ -177,3 +177,4 @@
} }
} }
} }

View file

@ -190,3 +190,4 @@
} }
} }
} }

View file

@ -190,3 +190,4 @@
} }
} }
} }

View file

@ -199,3 +199,4 @@
} }
} }
} }

View file

@ -190,3 +190,4 @@
} }
} }
} }

View file

@ -177,3 +177,4 @@
} }
} }
} }

View file

@ -177,3 +177,4 @@
} }
} }
} }

View file

@ -177,3 +177,4 @@
} }
} }
} }

View file

@ -176,3 +176,4 @@
} }
} }
} }

View file

@ -263,3 +263,4 @@
} }
} }
} }

View file

@ -263,3 +263,4 @@
} }
} }
} }

View file

@ -263,3 +263,4 @@
} }
} }
} }

View file

@ -263,3 +263,4 @@
} }
} }
} }

View file

@ -75,3 +75,4 @@
} }
} }
} }

View file

@ -177,3 +177,4 @@
} }
} }
} }

View file

@ -177,3 +177,4 @@
} }
} }
} }

View file

@ -177,3 +177,4 @@
} }
} }
} }

View file

@ -177,3 +177,4 @@
} }
} }
} }

View file

@ -210,3 +210,4 @@
} }
} }
} }