cmd: use env vars as defaults
Insted of setting them afterward in Run. This change required a small re-ordering of the test to patch the environment before calling New()
This commit is contained in:
parent
f994bc9157
commit
1021a06181
|
@ -74,13 +74,13 @@ const defaultEnvoyVersion = "1.13.1"
|
||||||
func (c *cmd) init() {
|
func (c *cmd) init() {
|
||||||
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
|
|
||||||
c.flags.StringVar(&c.proxyID, "proxy-id", "",
|
c.flags.StringVar(&c.proxyID, "proxy-id", os.Getenv("CONNECT_PROXY_ID"),
|
||||||
"The proxy's ID on the local agent.")
|
"The proxy's ID on the local agent.")
|
||||||
|
|
||||||
c.flags.BoolVar(&c.meshGateway, "mesh-gateway", false,
|
c.flags.BoolVar(&c.meshGateway, "mesh-gateway", false,
|
||||||
"Configure Envoy as a Mesh Gateway.")
|
"Configure Envoy as a Mesh Gateway.")
|
||||||
|
|
||||||
c.flags.StringVar(&c.sidecarFor, "sidecar-for", "",
|
c.flags.StringVar(&c.sidecarFor, "sidecar-for", os.Getenv("CONNECT_SIDECAR_FOR"),
|
||||||
"The ID of a service instance on the local agent that this proxy should "+
|
"The ID of a service instance on the local agent that this proxy should "+
|
||||||
"become a sidecar for. It requires that the proxy service is registered "+
|
"become a sidecar for. It requires that the proxy service is registered "+
|
||||||
"with the agent as a connect-proxy with Proxy.DestinationServiceID set "+
|
"with the agent as a connect-proxy with Proxy.DestinationServiceID set "+
|
||||||
|
@ -110,7 +110,7 @@ func (c *cmd) init() {
|
||||||
"cases where either assumption is violated this flag will prevent the "+
|
"cases where either assumption is violated this flag will prevent the "+
|
||||||
"command attempting to resolve config from the local agent.")
|
"command attempting to resolve config from the local agent.")
|
||||||
|
|
||||||
c.flags.StringVar(&c.grpcAddr, "grpc-addr", "",
|
c.flags.StringVar(&c.grpcAddr, "grpc-addr", os.Getenv(api.GRPCAddrEnvName),
|
||||||
"Set the agent's gRPC address and port (in http(s)://host:port format). "+
|
"Set the agent's gRPC address and port (in http(s)://host:port format). "+
|
||||||
"Alternatively, you can specify CONSUL_GRPC_ADDR in ENV.")
|
"Alternatively, you can specify CONSUL_GRPC_ADDR in ENV.")
|
||||||
|
|
||||||
|
@ -222,17 +222,6 @@ func (c *cmd) Run(args []string) int {
|
||||||
}
|
}
|
||||||
passThroughArgs := c.flags.Args()
|
passThroughArgs := c.flags.Args()
|
||||||
|
|
||||||
// Load the proxy ID and token from env vars if they're set
|
|
||||||
if c.proxyID == "" {
|
|
||||||
c.proxyID = os.Getenv("CONNECT_PROXY_ID")
|
|
||||||
}
|
|
||||||
if c.sidecarFor == "" {
|
|
||||||
c.sidecarFor = os.Getenv("CONNECT_SIDECAR_FOR")
|
|
||||||
}
|
|
||||||
if c.grpcAddr == "" {
|
|
||||||
c.grpcAddr = os.Getenv(api.GRPCAddrEnvName)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup Consul client
|
// Setup Consul client
|
||||||
client, err := c.http.APIClient()
|
client, err := c.http.APIClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -81,7 +81,6 @@ func TestGenerateConfig(t *testing.T) {
|
||||||
{
|
{
|
||||||
Name: "defaults",
|
Name: "defaults",
|
||||||
Flags: []string{"-proxy-id", "test-proxy"},
|
Flags: []string{"-proxy-id", "test-proxy"},
|
||||||
Env: []string{},
|
|
||||||
WantArgs: BootstrapTplArgs{
|
WantArgs: BootstrapTplArgs{
|
||||||
EnvoyVersion: defaultEnvoyVersion,
|
EnvoyVersion: defaultEnvoyVersion,
|
||||||
ProxyCluster: "test-proxy",
|
ProxyCluster: "test-proxy",
|
||||||
|
@ -98,7 +97,6 @@ func TestGenerateConfig(t *testing.T) {
|
||||||
Name: "token-arg",
|
Name: "token-arg",
|
||||||
Flags: []string{"-proxy-id", "test-proxy",
|
Flags: []string{"-proxy-id", "test-proxy",
|
||||||
"-token", "c9a52720-bf6c-4aa6-b8bc-66881a5ade95"},
|
"-token", "c9a52720-bf6c-4aa6-b8bc-66881a5ade95"},
|
||||||
Env: []string{},
|
|
||||||
WantArgs: BootstrapTplArgs{
|
WantArgs: BootstrapTplArgs{
|
||||||
EnvoyVersion: defaultEnvoyVersion,
|
EnvoyVersion: defaultEnvoyVersion,
|
||||||
ProxyCluster: "test-proxy",
|
ProxyCluster: "test-proxy",
|
||||||
|
@ -136,7 +134,6 @@ func TestGenerateConfig(t *testing.T) {
|
||||||
Flags: []string{"-proxy-id", "test-proxy",
|
Flags: []string{"-proxy-id", "test-proxy",
|
||||||
"-token-file", "@@TEMPDIR@@token.txt",
|
"-token-file", "@@TEMPDIR@@token.txt",
|
||||||
},
|
},
|
||||||
Env: []string{},
|
|
||||||
Files: map[string]string{
|
Files: map[string]string{
|
||||||
"token.txt": "c9a52720-bf6c-4aa6-b8bc-66881a5ade95",
|
"token.txt": "c9a52720-bf6c-4aa6-b8bc-66881a5ade95",
|
||||||
},
|
},
|
||||||
|
@ -179,7 +176,6 @@ func TestGenerateConfig(t *testing.T) {
|
||||||
Name: "grpc-addr-flag",
|
Name: "grpc-addr-flag",
|
||||||
Flags: []string{"-proxy-id", "test-proxy",
|
Flags: []string{"-proxy-id", "test-proxy",
|
||||||
"-grpc-addr", "localhost:9999"},
|
"-grpc-addr", "localhost:9999"},
|
||||||
Env: []string{},
|
|
||||||
WantArgs: BootstrapTplArgs{
|
WantArgs: BootstrapTplArgs{
|
||||||
EnvoyVersion: defaultEnvoyVersion,
|
EnvoyVersion: defaultEnvoyVersion,
|
||||||
ProxyCluster: "test-proxy",
|
ProxyCluster: "test-proxy",
|
||||||
|
@ -220,7 +216,6 @@ func TestGenerateConfig(t *testing.T) {
|
||||||
Name: "grpc-addr-unix",
|
Name: "grpc-addr-unix",
|
||||||
Flags: []string{"-proxy-id", "test-proxy",
|
Flags: []string{"-proxy-id", "test-proxy",
|
||||||
"-grpc-addr", "unix:///var/run/consul.sock"},
|
"-grpc-addr", "unix:///var/run/consul.sock"},
|
||||||
Env: []string{},
|
|
||||||
WantArgs: BootstrapTplArgs{
|
WantArgs: BootstrapTplArgs{
|
||||||
EnvoyVersion: defaultEnvoyVersion,
|
EnvoyVersion: defaultEnvoyVersion,
|
||||||
ProxyCluster: "test-proxy",
|
ProxyCluster: "test-proxy",
|
||||||
|
@ -254,7 +249,6 @@ func TestGenerateConfig(t *testing.T) {
|
||||||
{
|
{
|
||||||
Name: "access-log-path",
|
Name: "access-log-path",
|
||||||
Flags: []string{"-proxy-id", "test-proxy", "-admin-access-log-path", "/some/path/access.log"},
|
Flags: []string{"-proxy-id", "test-proxy", "-admin-access-log-path", "/some/path/access.log"},
|
||||||
Env: []string{},
|
|
||||||
WantArgs: BootstrapTplArgs{
|
WantArgs: BootstrapTplArgs{
|
||||||
EnvoyVersion: defaultEnvoyVersion,
|
EnvoyVersion: defaultEnvoyVersion,
|
||||||
ProxyCluster: "test-proxy",
|
ProxyCluster: "test-proxy",
|
||||||
|
@ -273,7 +267,6 @@ func TestGenerateConfig(t *testing.T) {
|
||||||
{
|
{
|
||||||
Name: "missing-ca-file",
|
Name: "missing-ca-file",
|
||||||
Flags: []string{"-proxy-id", "test-proxy", "-ca-file", "some/path"},
|
Flags: []string{"-proxy-id", "test-proxy", "-ca-file", "some/path"},
|
||||||
Env: []string{},
|
|
||||||
WantArgs: BootstrapTplArgs{
|
WantArgs: BootstrapTplArgs{
|
||||||
EnvoyVersion: defaultEnvoyVersion,
|
EnvoyVersion: defaultEnvoyVersion,
|
||||||
ProxyCluster: "test-proxy",
|
ProxyCluster: "test-proxy",
|
||||||
|
@ -310,7 +303,6 @@ func TestGenerateConfig(t *testing.T) {
|
||||||
{
|
{
|
||||||
Name: "custom-bootstrap",
|
Name: "custom-bootstrap",
|
||||||
Flags: []string{"-proxy-id", "test-proxy"},
|
Flags: []string{"-proxy-id", "test-proxy"},
|
||||||
Env: []string{},
|
|
||||||
ProxyConfig: map[string]interface{}{
|
ProxyConfig: map[string]interface{}{
|
||||||
// Add a completely custom bootstrap template. Never mind if this is
|
// Add a completely custom bootstrap template. Never mind if this is
|
||||||
// invalid envoy config just as long as it works and gets the variables
|
// invalid envoy config just as long as it works and gets the variables
|
||||||
|
@ -348,7 +340,6 @@ func TestGenerateConfig(t *testing.T) {
|
||||||
{
|
{
|
||||||
Name: "extra_-single",
|
Name: "extra_-single",
|
||||||
Flags: []string{"-proxy-id", "test-proxy"},
|
Flags: []string{"-proxy-id", "test-proxy"},
|
||||||
Env: []string{},
|
|
||||||
ProxyConfig: map[string]interface{}{
|
ProxyConfig: map[string]interface{}{
|
||||||
// Add a custom sections with interpolated variables. These are all
|
// Add a custom sections with interpolated variables. These are all
|
||||||
// invalid config syntax too but we are just testing they have the right
|
// invalid config syntax too but we are just testing they have the right
|
||||||
|
@ -381,7 +372,6 @@ func TestGenerateConfig(t *testing.T) {
|
||||||
{
|
{
|
||||||
Name: "extra_-multiple",
|
Name: "extra_-multiple",
|
||||||
Flags: []string{"-proxy-id", "test-proxy"},
|
Flags: []string{"-proxy-id", "test-proxy"},
|
||||||
Env: []string{},
|
|
||||||
ProxyConfig: map[string]interface{}{
|
ProxyConfig: map[string]interface{}{
|
||||||
// Add a custom sections with interpolated variables. These are all
|
// Add a custom sections with interpolated variables. These are all
|
||||||
// invalid config syntax too but we are just testing they have the right
|
// invalid config syntax too but we are just testing they have the right
|
||||||
|
@ -419,7 +409,6 @@ func TestGenerateConfig(t *testing.T) {
|
||||||
{
|
{
|
||||||
Name: "stats-config-override",
|
Name: "stats-config-override",
|
||||||
Flags: []string{"-proxy-id", "test-proxy"},
|
Flags: []string{"-proxy-id", "test-proxy"},
|
||||||
Env: []string{},
|
|
||||||
ProxyConfig: map[string]interface{}{
|
ProxyConfig: map[string]interface{}{
|
||||||
// Add a custom sections with interpolated variables. These are all
|
// Add a custom sections with interpolated variables. These are all
|
||||||
// invalid config syntax too but we are just testing they have the right
|
// invalid config syntax too but we are just testing they have the right
|
||||||
|
@ -444,7 +433,6 @@ func TestGenerateConfig(t *testing.T) {
|
||||||
{
|
{
|
||||||
Name: "zipkin-tracing-config",
|
Name: "zipkin-tracing-config",
|
||||||
Flags: []string{"-proxy-id", "test-proxy"},
|
Flags: []string{"-proxy-id", "test-proxy"},
|
||||||
Env: []string{},
|
|
||||||
ProxyConfig: map[string]interface{}{
|
ProxyConfig: map[string]interface{}{
|
||||||
// Add a custom sections with interpolated variables. These are all
|
// Add a custom sections with interpolated variables. These are all
|
||||||
// invalid config syntax too but we are just testing they have the right
|
// invalid config syntax too but we are just testing they have the right
|
||||||
|
@ -520,9 +508,6 @@ func TestGenerateConfig(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ui := cli.NewMockUi()
|
|
||||||
c := New(ui)
|
|
||||||
|
|
||||||
// Run a mock agent API that just always returns the proxy config in the
|
// Run a mock agent API that just always returns the proxy config in the
|
||||||
// test.
|
// test.
|
||||||
srv := httptest.NewServer(testMockAgent(tc.ProxyConfig, tc.GRPCPort))
|
srv := httptest.NewServer(testMockAgent(tc.ProxyConfig, tc.GRPCPort))
|
||||||
|
@ -530,15 +515,15 @@ func TestGenerateConfig(t *testing.T) {
|
||||||
|
|
||||||
// Set the agent HTTP address in ENV to be our mock
|
// Set the agent HTTP address in ENV to be our mock
|
||||||
tc.Env = append(tc.Env, "CONSUL_HTTP_ADDR="+srv.URL)
|
tc.Env = append(tc.Env, "CONSUL_HTTP_ADDR="+srv.URL)
|
||||||
|
|
||||||
testDirPrefix := testDir + string(filepath.Separator)
|
testDirPrefix := testDir + string(filepath.Separator)
|
||||||
|
|
||||||
myFlags := copyAndReplaceAll(tc.Flags, "@@TEMPDIR@@", testDirPrefix)
|
|
||||||
myEnv := copyAndReplaceAll(tc.Env, "@@TEMPDIR@@", testDirPrefix)
|
myEnv := copyAndReplaceAll(tc.Env, "@@TEMPDIR@@", testDirPrefix)
|
||||||
|
|
||||||
defer testSetAndResetEnv(t, myEnv)()
|
defer testSetAndResetEnv(t, myEnv)()
|
||||||
|
|
||||||
|
ui := cli.NewMockUi()
|
||||||
|
c := New(ui)
|
||||||
|
|
||||||
// Run the command
|
// Run the command
|
||||||
|
myFlags := copyAndReplaceAll(tc.Flags, "@@TEMPDIR@@", testDirPrefix)
|
||||||
args := append([]string{"-bootstrap"}, myFlags...)
|
args := append([]string{"-bootstrap"}, myFlags...)
|
||||||
code := c.Run(args)
|
code := c.Run(args)
|
||||||
if tc.WantErr == "" {
|
if tc.WantErr == "" {
|
||||||
|
|
Loading…
Reference in a new issue