agent/config: add managed proxy upstreams config to skip

agent/config will turn [{}] into {} (single element maps into a single
map) to work around HCL issues. These are resolved in HCL2 which I'm
sure Consul will switch to eventually.

This breaks the connect proxy configuration in service definition FILES
since we call this patch function. For now, let's just special-case skip
this. In the future we maybe Consul will adopt HCL2 and fix it, or we
can do something else if we want. This works and is tested.
This commit is contained in:
Mitchell Hashimoto 2018-05-05 11:10:24 -07:00
parent 28e6f800d8
commit 37dde6d64a
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 95 additions and 1 deletions

View File

@ -84,6 +84,7 @@ func Parse(data string, format string) (c Config, err error) {
"services", "services",
"services.checks", "services.checks",
"watches", "watches",
"service.connect.proxy.config.upstreams",
}) })
// There is a difference of representation of some fields depending on // There is a difference of representation of some fields depending on

View File

@ -31,6 +31,7 @@ type configTest struct {
pre, post func() pre, post func()
json, jsontail []string json, jsontail []string
hcl, hcltail []string hcl, hcltail []string
skipformat bool
privatev4 func() ([]*net.IPAddr, error) privatev4 func() ([]*net.IPAddr, error)
publicv6 func() ([]*net.IPAddr, error) publicv6 func() ([]*net.IPAddr, error)
patch func(rt *RuntimeConfig) patch func(rt *RuntimeConfig)
@ -2069,6 +2070,92 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
rt.DataDir = dataDir rt.DataDir = dataDir
}, },
}, },
{
desc: "HCL service managed proxy 'upstreams'",
args: []string{
`-data-dir=` + dataDir,
},
hcl: []string{
`service {
name = "web"
port = 8080
connect {
proxy {
config {
upstreams {
local_bind_port = 1234
}
}
}
}
}`,
},
skipformat: true, // skipping JSON cause we get slightly diff types (okay)
patch: func(rt *RuntimeConfig) {
rt.DataDir = dataDir
rt.Services = []*structs.ServiceDefinition{
&structs.ServiceDefinition{
Name: "web",
Port: 8080,
Connect: &structs.ServiceDefinitionConnect{
Proxy: &structs.ServiceDefinitionConnectProxy{
Config: map[string]interface{}{
"upstreams": []map[string]interface{}{
map[string]interface{}{
"local_bind_port": 1234,
},
},
},
},
},
},
}
},
},
{
desc: "JSON service managed proxy 'upstreams'",
args: []string{
`-data-dir=` + dataDir,
},
json: []string{
`{
"service": {
"name": "web",
"port": 8080,
"connect": {
"proxy": {
"config": {
"upstreams": [{
"local_bind_port": 1234
}]
}
}
}
}
}`,
},
skipformat: true, // skipping HCL cause we get slightly diff types (okay)
patch: func(rt *RuntimeConfig) {
rt.DataDir = dataDir
rt.Services = []*structs.ServiceDefinition{
&structs.ServiceDefinition{
Name: "web",
Port: 8080,
Connect: &structs.ServiceDefinitionConnect{
Proxy: &structs.ServiceDefinitionConnectProxy{
Config: map[string]interface{}{
"upstreams": []interface{}{
map[string]interface{}{
"local_bind_port": float64(1234),
},
},
},
},
},
},
}
},
},
} }
testConfig(t, tests, dataDir) testConfig(t, tests, dataDir)
@ -2090,7 +2177,7 @@ func testConfig(t *testing.T, tests []configTest, dataDir string) {
// json and hcl sources need to be in sync // json and hcl sources need to be in sync
// to make sure we're generating the same config // to make sure we're generating the same config
if len(tt.json) != len(tt.hcl) { if len(tt.json) != len(tt.hcl) && !tt.skipformat {
t.Fatal(tt.desc, ": JSON and HCL test case out of sync") t.Fatal(tt.desc, ": JSON and HCL test case out of sync")
} }
@ -2100,6 +2187,12 @@ func testConfig(t *testing.T, tests []configTest, dataDir string) {
srcs, tails = tt.hcl, tt.hcltail srcs, tails = tt.hcl, tt.hcltail
} }
// If we're skipping a format and the current format is empty,
// then skip it!
if tt.skipformat && len(srcs) == 0 {
continue
}
// build the description // build the description
var desc []string var desc []string
if !flagsOnly { if !flagsOnly {