mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 10:25:42 +03:00
docs: add docs and tests for tagged_addresses
This commit is contained in:
@@ -1029,17 +1029,9 @@ func (c *ServiceClient) serviceRegs(
|
||||
}
|
||||
}
|
||||
|
||||
var taggedAddresses map[string]api.ServiceAddress
|
||||
for k, v := range service.TaggedAddresses {
|
||||
sa, err := parseAddress(v, port)
|
||||
if err != nil {
|
||||
c.logger.Warn("failed to parse advertise address", "name", k, "adrress", v)
|
||||
continue
|
||||
}
|
||||
if taggedAddresses == nil {
|
||||
taggedAddresses = make(map[string]api.ServiceAddress)
|
||||
}
|
||||
taggedAddresses[k] = sa
|
||||
taggedAddresses, err := parseTaggedAddresses(service.TaggedAddresses, port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Build the Consul Service registration request
|
||||
@@ -1708,3 +1700,16 @@ func parseAddress(raw string, port int) (api.ServiceAddress, error) {
|
||||
result.Port = port
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// morph the tagged_addresses map into the structure consul api wants
|
||||
func parseTaggedAddresses(m map[string]string, port int) (map[string]api.ServiceAddress, error) {
|
||||
result := make(map[string]api.ServiceAddress, len(m))
|
||||
for k, v := range m {
|
||||
sa, err := parseAddress(v, port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result[k] = sa
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/hashicorp/nomad/helper/testlog"
|
||||
"github.com/hashicorp/nomad/helper/uuid"
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
"github.com/shoenig/test/must"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@@ -28,6 +29,9 @@ func TestSyncLogic_agentServiceUpdateRequired(t *testing.T) {
|
||||
Address: "1.1.1.1",
|
||||
EnableTagOverride: true,
|
||||
Meta: map[string]string{"foo": "1"},
|
||||
TaggedAddresses: map[string]api.ServiceAddress{
|
||||
"public_wan": {Address: "1.2.3.4", Port: 8080},
|
||||
},
|
||||
Connect: &api.AgentServiceConnect{
|
||||
Native: false,
|
||||
SidecarService: &api.AgentServiceRegistration{
|
||||
@@ -56,6 +60,9 @@ func TestSyncLogic_agentServiceUpdateRequired(t *testing.T) {
|
||||
Address: "1.1.1.1",
|
||||
EnableTagOverride: true,
|
||||
Meta: map[string]string{"foo": "1"},
|
||||
TaggedAddresses: map[string]api.ServiceAddress{
|
||||
"public_wan": {Address: "1.2.3.4", Port: 8080},
|
||||
},
|
||||
}
|
||||
|
||||
sidecar := &api.AgentService{
|
||||
@@ -212,6 +219,15 @@ func TestSyncLogic_agentServiceUpdateRequired(t *testing.T) {
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("different tagged addresses", func(t *testing.T) {
|
||||
try(t, true, syncNewOps, func(w asr) *asr {
|
||||
w.TaggedAddresses = map[string]api.ServiceAddress{
|
||||
"public_wan": {Address: "5.6.7.8", Port: 8080},
|
||||
}
|
||||
return &w
|
||||
})
|
||||
})
|
||||
|
||||
// for remaining tests, EnableTagOverride = false
|
||||
existing.EnableTagOverride = false
|
||||
|
||||
@@ -648,3 +664,44 @@ func TestSyncLogic_maybeSidecarProxyCheck(t *testing.T) {
|
||||
try("service:_nomad-task-2f5fb517-57d4-44ee-7780-dc1cb6e103cd-group-api-count-api-9001-sidecar-proxy: ", false)
|
||||
try("service", false)
|
||||
}
|
||||
|
||||
func TestSyncLogic_parseTaggedAddresses(t *testing.T) {
|
||||
ci.Parallel(t)
|
||||
|
||||
t.Run("nil", func(t *testing.T) {
|
||||
m, err := parseTaggedAddresses(nil, 0)
|
||||
must.NoError(t, err)
|
||||
must.MapEmpty(t, m)
|
||||
})
|
||||
|
||||
t.Run("parse fail", func(t *testing.T) {
|
||||
ta := map[string]string{
|
||||
"public_wan": "not an address",
|
||||
}
|
||||
result, err := parseTaggedAddresses(ta, 8080)
|
||||
must.Error(t, err)
|
||||
must.MapEmpty(t, result)
|
||||
})
|
||||
|
||||
t.Run("parse address", func(t *testing.T) {
|
||||
ta := map[string]string{
|
||||
"public_wan": "1.2.3.4",
|
||||
}
|
||||
result, err := parseTaggedAddresses(ta, 8080)
|
||||
must.NoError(t, err)
|
||||
must.MapEq(t, map[string]api.ServiceAddress{
|
||||
"public_wan": {Address: "1.2.3.4", Port: 8080},
|
||||
}, result)
|
||||
})
|
||||
|
||||
t.Run("parse address and port", func(t *testing.T) {
|
||||
ta := map[string]string{
|
||||
"public_wan": "1.2.3.4:9999",
|
||||
}
|
||||
result, err := parseTaggedAddresses(ta, 8080)
|
||||
must.NoError(t, err)
|
||||
must.MapEq(t, map[string]api.ServiceAddress{
|
||||
"public_wan": {Address: "1.2.3.4", Port: 9999},
|
||||
}, result)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2518,6 +2518,9 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
|
||||
Meta: map[string]string{
|
||||
"servicemeta": "foobar",
|
||||
},
|
||||
TaggedAddresses: map[string]string{
|
||||
"wan": "1.2.3.4",
|
||||
},
|
||||
CheckRestart: &api.CheckRestart{
|
||||
Limit: 4,
|
||||
Grace: helper.TimeToPtr(11 * time.Second),
|
||||
@@ -2915,6 +2918,9 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
|
||||
Meta: map[string]string{
|
||||
"servicemeta": "foobar",
|
||||
},
|
||||
TaggedAddresses: map[string]string{
|
||||
"wan": "1.2.3.4",
|
||||
},
|
||||
OnUpdate: "require_healthy",
|
||||
Checks: []*structs.ServiceCheck{
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user