Skip to content

Commit

Permalink
cr: followup to fix cause of extra consul logging
Browse files Browse the repository at this point in the history
  • Loading branch information
shoenig committed Oct 19, 2022
1 parent 1660905 commit 0f730ed
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 39 deletions.
2 changes: 1 addition & 1 deletion client/serviceregistration/service_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ type ServiceRegistration struct {
// services/checks registered in Consul. It is used to materialize the other
// fields when queried.
ServiceID string
CheckIDs map[string]struct{}
CheckIDs map[string]struct{} // todo: use a Set?

// CheckOnUpdate is a map of checkIDs and the associated OnUpdate value
// from the ServiceCheck It is used to determine how a reported checks
Expand Down
19 changes: 19 additions & 0 deletions command/agent/consul/catalog_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ func (c *MockAgent) CheckRegs() []*api.AgentCheckRegistration {
func (c *MockAgent) CheckRegister(check *api.AgentCheckRegistration) error {
c.mu.Lock()
defer c.mu.Unlock()
return c.checkRegister(check)
}

// checkRegister registers a check; c.mu must be held.
func (c *MockAgent) checkRegister(check *api.AgentCheckRegistration) error {
c.hits++

// Consul will set empty Namespace to default, do the same here
Expand Down Expand Up @@ -330,6 +335,20 @@ func (c *MockAgent) ServiceRegister(service *api.AgentServiceRegistration) error
c.services[service.Namespace] = make(map[string]*api.AgentServiceRegistration)
}
c.services[service.Namespace][service.ID] = service

// as of Nomad v1.4.x registering service now also registers its checks
for _, check := range service.Checks {
if err := c.checkRegister(&api.AgentCheckRegistration{
ID: check.CheckID,
Name: check.Name,
ServiceID: service.ID,
AgentServiceCheck: *check,
Namespace: service.Namespace,
}); err != nil {
return err
}
}

return nil
}

Expand Down
8 changes: 4 additions & 4 deletions command/agent/consul/service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,10 @@ func (c *ServiceClient) serviceRegs(
for _, registration := range checkRegs {
sreg.CheckIDs[registration.ID] = struct{}{}
ops.regChecks = append(ops.regChecks, registration)
serviceReg.Checks = append(serviceReg.Checks, apiCheckRegistrationToCheck(registration))
serviceReg.Checks = append(
serviceReg.Checks,
apiCheckRegistrationToCheck(registration),
)
}

return sreg, nil
Expand Down Expand Up @@ -1597,9 +1600,6 @@ func createCheckReg(serviceID, checkID string, check *structs.ServiceCheck, host
ServiceID: serviceID,
Namespace: normalizeNamespace(namespace),
}
chkReg.AgentServiceCheck.CheckID = checkID
chkReg.AgentServiceCheck.Name = check.Name

chkReg.Status = check.InitialStatus
chkReg.Timeout = check.Timeout.String()
chkReg.Interval = check.Interval.String()
Expand Down
62 changes: 28 additions & 34 deletions command/agent/consul/unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ func TestConsul_ChangePorts(t *testing.T) {
ci.Parallel(t)

ctx := setupFake(t)
require := require.New(t)

ctx.Workload.Services[0].Checks = []*structs.ServiceCheck{
{
Expand All @@ -239,17 +238,17 @@ func TestConsul_ChangePorts(t *testing.T) {
},
}

require.NoError(ctx.ServiceClient.RegisterWorkload(ctx.Workload))
require.NoError(ctx.syncOnce(syncNewOps))
require.Equal(1, len(ctx.FakeConsul.services["default"]), "Expected 1 service to be registered with Consul")
must.NoError(t, ctx.ServiceClient.RegisterWorkload(ctx.Workload))
must.NoError(t, ctx.syncOnce(syncNewOps))
must.MapLen(t, 1, ctx.FakeConsul.services["default"])

for _, v := range ctx.FakeConsul.services["default"] {
require.Equal(ctx.Workload.Services[0].Name, v.Name)
require.Equal(ctx.Workload.Services[0].Tags, v.Tags)
require.Equal(xPort, v.Port)
must.Eq(t, ctx.Workload.Services[0].Name, v.Name)
must.Eq(t, ctx.Workload.Services[0].Tags, v.Tags)
must.Eq(t, xPort, v.Port)
}

require.Len(ctx.FakeConsul.checks["default"], 3)
must.MapLen(t, 3, ctx.FakeConsul.checks["default"], must.Sprintf("checks %#v", ctx.FakeConsul.checks))

origTCPKey := ""
origScriptKey := ""
Expand All @@ -258,20 +257,20 @@ func TestConsul_ChangePorts(t *testing.T) {
switch v.Name {
case "c1":
origTCPKey = k
require.Equal(fmt.Sprintf(":%d", xPort), v.TCP)
must.Eq(t, fmt.Sprintf(":%d", xPort), v.TCP)
case "c2":
origScriptKey = k
case "c3":
origHTTPKey = k
require.Equal(fmt.Sprintf("http://:%d/", yPort), v.HTTP)
must.Eq(t, fmt.Sprintf("http://:%d/", yPort), v.HTTP)
default:
t.Fatalf("unexpected check: %q", v.Name)
}
}

require.NotEmpty(origTCPKey)
require.NotEmpty(origScriptKey)
require.NotEmpty(origHTTPKey)
must.StrHasPrefix(t, origTCPKey, "_nomad-check-")
must.StrHasPrefix(t, origScriptKey, "_nomad-check-")
must.StrHasPrefix(t, origHTTPKey, "_nomad-check-")

// Now update the PortLabel on the Service and Check c3
origWorkload := ctx.Workload.Copy()
Expand Down Expand Up @@ -301,32 +300,31 @@ func TestConsul_ChangePorts(t *testing.T) {
},
}

require.NoError(ctx.ServiceClient.UpdateWorkload(origWorkload, ctx.Workload))
require.NoError(ctx.syncOnce(syncNewOps))
require.Equal(1, len(ctx.FakeConsul.services["default"]), "Expected 1 service to be registered with Consul")
must.NoError(t, ctx.ServiceClient.UpdateWorkload(origWorkload, ctx.Workload))
must.NoError(t, ctx.syncOnce(syncNewOps))
must.MapLen(t, 1, ctx.FakeConsul.services["default"])

for _, v := range ctx.FakeConsul.services["default"] {
require.Equal(ctx.Workload.Services[0].Name, v.Name)
require.Equal(ctx.Workload.Services[0].Tags, v.Tags)
require.Equal(yPort, v.Port)
must.Eq(t, ctx.Workload.Services[0].Name, v.Name)
must.Eq(t, ctx.Workload.Services[0].Tags, v.Tags)
must.Eq(t, yPort, v.Port)
}

require.Equal(3, len(ctx.FakeConsul.checks["default"]))
must.MapLen(t, 3, ctx.FakeConsul.checks["default"])

for k, v := range ctx.FakeConsul.checks["default"] {
switch v.Name {
case "c1":
// C1 is changed because the service was re-registered
require.NotEqual(origTCPKey, k)
require.Equal(fmt.Sprintf(":%d", xPort), v.TCP)
must.NotEq(t, origTCPKey, k)
must.Eq(t, fmt.Sprintf(":%d", xPort), v.TCP)
case "c2":
// C2 is changed because the service was re-registered
require.NotEqual(origScriptKey, k)
must.NotEq(t, origScriptKey, k)
case "c3":
require.NotEqual(origHTTPKey, k)
require.Equal(fmt.Sprintf("http://:%d/", yPort), v.HTTP)
must.NotEq(t, origHTTPKey, k)
must.Eq(t, fmt.Sprintf("http://:%d/", yPort), v.HTTP)
default:
t.Errorf("Unknown check: %q", k)
must.Unreachable(t, must.Sprintf("unknown check: %q", k))
}
}
}
Expand Down Expand Up @@ -940,8 +938,6 @@ func TestCreateCheckReg_HTTP(t *testing.T) {
Name: "name",
ServiceID: serviceID,
AgentServiceCheck: api.AgentServiceCheck{
CheckID: checkID,
Name: "name",
Timeout: "0s",
Interval: "0s",
HTTP: fmt.Sprintf("http://%s:%d/path", host, port),
Expand Down Expand Up @@ -984,11 +980,9 @@ func TestCreateCheckReg_GRPC(t *testing.T) {
expected := &api.AgentCheckRegistration{
Namespace: "",
ID: checkID,
Name: "name",
Name: check.Name,
ServiceID: serviceID,
AgentServiceCheck: api.AgentServiceCheck{
CheckID: checkID,
Name: "name",
Timeout: "1s",
Interval: "1m0s",
GRPC: "localhost:8080/foo.Bar",
Expand All @@ -998,8 +992,8 @@ func TestCreateCheckReg_GRPC(t *testing.T) {
}

actual, err := createCheckReg(serviceID, checkID, check, "localhost", 8080, "default")
require.NoError(t, err)
require.Equal(t, expected, actual)
must.NoError(t, err)
must.Eq(t, expected, actual)
}

func TestConsul_ServiceName_Duplicates(t *testing.T) {
Expand Down

0 comments on commit 0f730ed

Please sign in to comment.