Add alloc_id to sidecar bootstrap

This commit is contained in:
Ian Drennan 2022-04-11 17:27:14 +00:00 committed by Seth Hoenig
parent 7351f45672
commit 70bd32df83
3 changed files with 45 additions and 30 deletions

View File

@ -13,7 +13,7 @@ import (
// newConnect creates a new Consul AgentServiceConnect struct based on a Nomad
// Connect struct. If the nomad Connect struct is nil, nil will be returned to
// disable Connect for this service.
func newConnect(serviceId string, serviceName string, nc *structs.ConsulConnect, networks structs.Networks, ports structs.AllocatedPorts) (*api.AgentServiceConnect, error) {
func newConnect(serviceId string, serviceName string, nc *structs.ConsulConnect, networks structs.Networks, ports structs.AllocatedPorts, allocId string) (*api.AgentServiceConnect, error) {
switch {
case nc == nil:
// no connect stanza means there is no connect service to register
@ -32,7 +32,7 @@ func newConnect(serviceId string, serviceName string, nc *structs.ConsulConnect,
if nc.SidecarService.Port == "" {
nc.SidecarService.Port = fmt.Sprintf("%s-%s", structs.ConnectProxyPrefix, serviceName)
}
sidecarReg, err := connectSidecarRegistration(serviceId, nc.SidecarService, networks, ports)
sidecarReg, err := connectSidecarRegistration(serviceId, nc.SidecarService, networks, ports, allocId)
if err != nil {
return nil, err
}
@ -89,7 +89,7 @@ func newConnectGateway(serviceName string, connect *structs.ConsulConnect) *api.
return &api.AgentServiceConnectProxyConfig{Config: envoyConfig}
}
func connectSidecarRegistration(serviceId string, css *structs.ConsulSidecarService, networks structs.Networks, ports structs.AllocatedPorts) (*api.AgentServiceRegistration, error) {
func connectSidecarRegistration(serviceId string, css *structs.ConsulSidecarService, networks structs.Networks, ports structs.AllocatedPorts, allocId string) (*api.AgentServiceRegistration, error) {
if css == nil {
// no sidecar stanza means there is no sidecar service to register
return nil, nil
@ -100,7 +100,7 @@ func connectSidecarRegistration(serviceId string, css *structs.ConsulSidecarServ
return nil, err
}
proxy, err := connectSidecarProxy(css.Proxy, cMapping.To, networks)
proxy, err := connectSidecarProxy(css.Proxy, cMapping.To, networks, allocId)
if err != nil {
return nil, err
}
@ -129,7 +129,7 @@ func connectSidecarRegistration(serviceId string, css *structs.ConsulSidecarServ
}, nil
}
func connectSidecarProxy(proxy *structs.ConsulProxy, cPort int, networks structs.Networks) (*api.AgentServiceConnectProxyConfig, error) {
func connectSidecarProxy(proxy *structs.ConsulProxy, cPort int, networks structs.Networks, allocId string) (*api.AgentServiceConnectProxyConfig, error) {
if proxy == nil {
proxy = new(structs.ConsulProxy)
}
@ -142,7 +142,7 @@ func connectSidecarProxy(proxy *structs.ConsulProxy, cPort int, networks structs
return &api.AgentServiceConnectProxyConfig{
LocalServiceAddress: proxy.LocalServiceAddress,
LocalServicePort: proxy.LocalServicePort,
Config: connectProxyConfig(proxy.Config, cPort),
Config: connectProxyConfig(proxy.Config, cPort, allocId),
Upstreams: connectUpstreams(proxy.Upstreams),
Expose: expose,
}, nil
@ -228,12 +228,16 @@ func connectMeshGateway(in *structs.ConsulMeshGateway) api.MeshGatewayConfig {
return gw
}
func connectProxyConfig(cfg map[string]interface{}, port int) map[string]interface{} {
func connectProxyConfig(cfg map[string]interface{}, port int, allocId string) map[string]interface{} {
if cfg == nil {
cfg = make(map[string]interface{})
}
cfg["bind_address"] = "0.0.0.0"
cfg["bind_port"] = port
if allocId != "" {
cfg["envoy_stats_tags"] = []string{"alloc_id=" + allocId}
}
return cfg
}

View File

@ -34,7 +34,7 @@ func TestConnect_newConnect(t *testing.T) {
ci.Parallel(t)
t.Run("nil", func(t *testing.T) {
asr, err := newConnect("", "", nil, nil, nil)
asr, err := newConnect("", "", nil, nil, nil, "")
require.NoError(t, err)
require.Nil(t, asr)
})
@ -42,7 +42,7 @@ func TestConnect_newConnect(t *testing.T) {
t.Run("native", func(t *testing.T) {
asr, err := newConnect("", "", &structs.ConsulConnect{
Native: true,
}, nil, nil)
}, nil, nil, "")
require.NoError(t, err)
require.True(t, asr.Native)
require.Nil(t, asr.SidecarService)
@ -55,7 +55,7 @@ func TestConnect_newConnect(t *testing.T) {
Tags: []string{"foo", "bar"},
Port: "connect-proxy-redis",
},
}, testConnectNetwork, testConnectPorts)
}, testConnectNetwork, testConnectPorts, "redis-alloc-id")
require.NoError(t, err)
require.Equal(t, &api.AgentServiceRegistration{
Tags: []string{"foo", "bar"},
@ -63,8 +63,9 @@ func TestConnect_newConnect(t *testing.T) {
Address: "192.168.30.1",
Proxy: &api.AgentServiceConnectProxyConfig{
Config: map[string]interface{}{
"bind_address": "0.0.0.0",
"bind_port": 3000,
"bind_address": "0.0.0.0",
"bind_port": 3000,
"envoy_stats_tags": []string{"alloc_id=redis-alloc-id"},
},
},
Checks: api.AgentServiceChecks{
@ -89,7 +90,7 @@ func TestConnect_newConnect(t *testing.T) {
Port: "connect-proxy-redis",
DisableDefaultTCPCheck: true,
},
}, testConnectNetwork, testConnectPorts)
}, testConnectNetwork, testConnectPorts, "redis-alloc-id")
require.NoError(t, err)
require.Equal(t, &api.AgentServiceRegistration{
Tags: []string{"foo", "bar"},
@ -97,8 +98,9 @@ func TestConnect_newConnect(t *testing.T) {
Address: "192.168.30.1",
Proxy: &api.AgentServiceConnectProxyConfig{
Config: map[string]interface{}{
"bind_address": "0.0.0.0",
"bind_port": 3000,
"bind_address": "0.0.0.0",
"bind_port": 3000,
"envoy_stats_tags": []string{"alloc_id=redis-alloc-id"},
},
},
Checks: api.AgentServiceChecks{
@ -115,7 +117,7 @@ func TestConnect_connectSidecarRegistration(t *testing.T) {
ci.Parallel(t)
t.Run("nil", func(t *testing.T) {
sidecarReg, err := connectSidecarRegistration("", nil, testConnectNetwork, testConnectPorts)
sidecarReg, err := connectSidecarRegistration("", nil, testConnectNetwork, testConnectPorts, "")
require.NoError(t, err)
require.Nil(t, sidecarReg)
})
@ -123,7 +125,7 @@ func TestConnect_connectSidecarRegistration(t *testing.T) {
t.Run("no service port", func(t *testing.T) {
_, err := connectSidecarRegistration("unknown-id", &structs.ConsulSidecarService{
Port: "unknown-label",
}, testConnectNetwork, testConnectPorts)
}, testConnectNetwork, testConnectPorts, "")
require.EqualError(t, err, `No port of label "unknown-label" defined`)
})
@ -137,7 +139,7 @@ func TestConnect_connectSidecarRegistration(t *testing.T) {
}},
},
},
}, testConnectNetwork, testConnectPorts)
}, testConnectNetwork, testConnectPorts, "")
require.EqualError(t, err, `No port of label "badPort" defined`)
})
@ -145,7 +147,7 @@ func TestConnect_connectSidecarRegistration(t *testing.T) {
proxy, err := connectSidecarRegistration("redis-service-id", &structs.ConsulSidecarService{
Tags: []string{"foo", "bar"},
Port: "connect-proxy-redis",
}, testConnectNetwork, testConnectPorts)
}, testConnectNetwork, testConnectPorts, "")
require.NoError(t, err)
require.Equal(t, &api.AgentServiceRegistration{
Tags: []string{"foo", "bar"},
@ -178,7 +180,7 @@ func TestConnect_connectProxy(t *testing.T) {
// If the input proxy is nil, we expect the output to be a proxy with its
// config set to default values.
t.Run("nil proxy", func(t *testing.T) {
proxy, err := connectSidecarProxy(nil, 2000, testConnectNetwork)
proxy, err := connectSidecarProxy(nil, 2000, testConnectNetwork, "")
require.NoError(t, err)
require.Equal(t, &api.AgentServiceConnectProxyConfig{
LocalServiceAddress: "",
@ -203,7 +205,7 @@ func TestConnect_connectProxy(t *testing.T) {
}},
},
Config: nil,
}, 2000, testConnectNetwork)
}, 2000, testConnectNetwork, "")
require.EqualError(t, err, `No port of label "badPort" defined`)
})
@ -221,7 +223,7 @@ func TestConnect_connectProxy(t *testing.T) {
}},
},
Config: nil,
}, 2000, testConnectNetwork)
}, 2000, testConnectNetwork, "")
require.NoError(t, err)
require.Equal(t, &api.AgentServiceConnectProxyConfig{
LocalServiceAddress: "0.0.0.0",
@ -368,19 +370,28 @@ func TestConnect_connectProxyConfig(t *testing.T) {
t.Run("nil map", func(t *testing.T) {
require.Equal(t, map[string]interface{}{
"bind_address": "0.0.0.0",
"bind_port": 42,
}, connectProxyConfig(nil, 42))
"bind_address": "0.0.0.0",
"bind_port": 42,
"envoy_stats_tags": []string{"alloc_id=test_alloc1"},
}, connectProxyConfig(nil, 42, "test_alloc1"))
})
t.Run("pre-existing map", func(t *testing.T) {
require.Equal(t, map[string]interface{}{
"bind_address": "0.0.0.0",
"bind_port": 42,
"foo": "bar",
"bind_address": "0.0.0.0",
"bind_port": 42,
"foo": "bar",
"envoy_stats_tags": []string{"alloc_id=test_alloc2"},
}, connectProxyConfig(map[string]interface{}{
"foo": "bar",
}, 42))
}, 42, "test_alloc2"))
})
t.Run("emtpy alloc id", func(t *testing.T) {
require.Equal(t, map[string]interface{}{
"bind_address": "0.0.0.0",
"bind_port": 42,
}, connectProxyConfig(nil, 42, ""))
})
}

View File

@ -964,7 +964,7 @@ func (c *ServiceClient) serviceRegs(
}
// newConnect returns (nil, nil) if there's no Connect-enabled service.
connect, err := newConnect(id, service.Name, service.Connect, workload.Networks, workload.Ports)
connect, err := newConnect(id, service.Name, service.Connect, workload.Networks, workload.Ports, workload.AllocID)
if err != nil {
return nil, fmt.Errorf("invalid Consul Connect configuration for service %q: %v", service.Name, err)
}