[1.8.x] command: when generating envoy bootstrap configs to stdout do not mix informational logs into the json (#9985)

Backport of #9980 to `1.8.x`

Conflicts:
- just the generated golden command/ files
- envoy.go and envoy_test.go
This commit is contained in:
R.B. Boyer 2021-04-07 15:00:33 -05:00 committed by GitHub
parent 0d0f14f901
commit 89e4e3d534
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 37 additions and 18 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

@ -23,13 +23,6 @@ import (
) )
func New(ui cli.Ui) *cmd { func New(ui cli.Ui) *cmd {
ui = &cli.PrefixedUi{
OutputPrefix: "==> ",
InfoPrefix: " ",
ErrorPrefix: "==> ",
Ui: ui,
}
c := &cmd{UI: ui} c := &cmd{UI: ui}
c.init() c.init()
return c return c
@ -352,7 +345,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
@ -364,7 +361,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
os.Stdout.Write(bootstrapJson) c.UI.Output(string(bootstrapJson))
return 0 return 0
} }
@ -523,7 +520,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

@ -3,7 +3,6 @@ package envoy
import ( import (
"encoding/json" "encoding/json"
"flag" "flag"
"github.com/stretchr/testify/assert"
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
@ -13,12 +12,14 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/mitchellh/cli"
"github.com/stretchr/testify/assert"
"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")
@ -770,10 +771,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 template output goes to stdout direct to avoid prefix in UI, so actual := ui.OutputWriter.Bytes()
// generate it again here to assert on.
actual, err := c.generateConfig()
require.NoError(err)
// 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

@ -114,3 +114,4 @@
] ]
} }
} }

View File

@ -13,4 +13,4 @@
"id": "test-proxy" "id": "test-proxy"
}, },
"custom_field": "foo" "custom_field": "foo"
} }

View File

@ -114,3 +114,4 @@
] ]
} }
} }

View File

@ -123,3 +123,4 @@
] ]
} }
} }

View File

@ -123,3 +123,4 @@
] ]
} }
} }

View File

@ -136,3 +136,4 @@
] ]
} }
} }

View File

@ -127,3 +127,4 @@
] ]
} }
} }

View File

@ -114,3 +114,4 @@
] ]
} }
} }

View File

@ -114,3 +114,4 @@
] ]
} }
} }

View File

@ -114,3 +114,4 @@
] ]
} }
} }

View File

@ -113,3 +113,4 @@
] ]
} }
} }

View File

@ -187,3 +187,4 @@
] ]
} }
} }

View File

@ -187,3 +187,4 @@
] ]
} }
} }

View File

@ -187,3 +187,4 @@
] ]
} }
} }

View File

@ -72,3 +72,4 @@
] ]
} }
} }

View File

@ -114,3 +114,4 @@
] ]
} }
} }

View File

@ -114,3 +114,4 @@
] ]
} }
} }

View File

@ -114,3 +114,4 @@
] ]
} }
} }

View File

@ -114,3 +114,4 @@
] ]
} }
} }

View File

@ -147,3 +147,4 @@
] ]
} }
} }