Track the correct check id for idempotent service/check updates

This commit is contained in:
Matt Keeler 2019-11-14 10:59:06 -05:00
parent 8572ac135c
commit 8f1f15a827
No known key found for this signature in database
GPG Key ID: 04DBAE1857E0081B
2 changed files with 8 additions and 7 deletions

View File

@ -2248,8 +2248,6 @@ func (a *Agent) addServiceInternal(req *addServiceRequest) error {
// Create an associated health check // Create an associated health check
for i, chkType := range chkTypes { for i, chkType := range chkTypes {
existingChecks[chkType.CheckID] = true
checkID := string(chkType.CheckID) checkID := string(chkType.CheckID)
if checkID == "" { if checkID == "" {
checkID = fmt.Sprintf("service:%s", service.ID) checkID = fmt.Sprintf("service:%s", service.ID)
@ -2257,6 +2255,9 @@ func (a *Agent) addServiceInternal(req *addServiceRequest) error {
checkID += fmt.Sprintf(":%d", i+1) checkID += fmt.Sprintf(":%d", i+1)
} }
} }
existingChecks[types.CheckID(checkID)] = true
name := chkType.Name name := chkType.Name
if name == "" { if name == "" {
name = fmt.Sprintf("Service '%s' check", service.Service) name = fmt.Sprintf("Service '%s' check", service.Service)

View File

@ -2595,8 +2595,9 @@ func testAgent_RegisterService_ReRegister_ReplaceExistingChecks(t *testing.T, ex
Port: 8000, Port: 8000,
Checks: []*structs.CheckType{ Checks: []*structs.CheckType{
&structs.CheckType{ &structs.CheckType{
CheckID: types.CheckID("check_1"), // explicitly not setting the check id to let it be auto-generated
TTL: 20 * time.Second, // we want to ensure that we
TTL: 20 * time.Second,
}, },
&structs.CheckType{ &structs.CheckType{
CheckID: types.CheckID("check_2"), CheckID: types.CheckID("check_2"),
@ -2619,8 +2620,7 @@ func testAgent_RegisterService_ReRegister_ReplaceExistingChecks(t *testing.T, ex
Port: 8000, Port: 8000,
Checks: []*structs.CheckType{ Checks: []*structs.CheckType{
&structs.CheckType{ &structs.CheckType{
CheckID: types.CheckID("check_1"), TTL: 20 * time.Second,
TTL: 20 * time.Second,
}, },
&structs.CheckType{ &structs.CheckType{
CheckID: types.CheckID("check_3"), CheckID: types.CheckID("check_3"),
@ -2644,7 +2644,7 @@ func testAgent_RegisterService_ReRegister_ReplaceExistingChecks(t *testing.T, ex
checkIDs = append(checkIDs, string(id)) checkIDs = append(checkIDs, string(id))
} }
sort.Strings(checkIDs) sort.Strings(checkIDs)
require.Equal(t, []string{"check_1", "check_3"}, checkIDs) require.ElementsMatch(t, []string{"service:test:1", "check_3"}, checkIDs)
} }
func TestAgent_RegisterService_TranslateKeys(t *testing.T) { func TestAgent_RegisterService_TranslateKeys(t *testing.T) {