fix: use interpolated address when performing health checks (#18584)

* fix: use interpolated address when performing health checks

* Fix tests, add changelog

* Update .changelog/18584.txt

Co-authored-by: Seth Hoenig <shoenig@duck.com>

---------

Co-authored-by: Seth Hoenig <shoenig@duck.com>
This commit is contained in:
Matthew Salsamendi
2023-10-04 05:58:55 -07:00
committed by GitHub
parent fb7582d596
commit aa9ff3a5b3
4 changed files with 19 additions and 5 deletions

3
.changelog/18584.txt Normal file
View File

@@ -0,0 +1,3 @@
```release-note:bug
services: use interpolated address when performing nomad service health checks
```

View File

@@ -110,7 +110,7 @@ func (ar *allocRunner) initRunnerHooks(config *clientconfig.Config) error {
} }
// Create a taskenv.TaskEnv which is used for read only purposes by the // Create a taskenv.TaskEnv which is used for read only purposes by the
// newNetworkHook. // newNetworkHook and newChecksHook.
builtTaskEnv := newEnvBuilder().Build() builtTaskEnv := newEnvBuilder().Build()
// Create the alloc directory hook. This is run first to ensure the // Create the alloc directory hook. This is run first to ensure the
@@ -139,7 +139,7 @@ func (ar *allocRunner) initRunnerHooks(config *clientconfig.Config) error {
newConsulGRPCSocketHook(hookLogger, alloc, ar.allocDir, config.ConsulConfig, config.Node.Attributes), newConsulGRPCSocketHook(hookLogger, alloc, ar.allocDir, config.ConsulConfig, config.Node.Attributes),
newConsulHTTPSocketHook(hookLogger, alloc, ar.allocDir, config.ConsulConfig), newConsulHTTPSocketHook(hookLogger, alloc, ar.allocDir, config.ConsulConfig),
newCSIHook(alloc, hookLogger, ar.csiManager, ar.rpcClient, ar, ar.hookResources, ar.clientConfig.Node.SecretID), newCSIHook(alloc, hookLogger, ar.csiManager, ar.rpcClient, ar, ar.hookResources, ar.clientConfig.Node.SecretID),
newChecksHook(hookLogger, alloc, ar.checkStore, ar), newChecksHook(hookLogger, alloc, ar.checkStore, ar, builtTaskEnv),
} }
if config.ExtraAllocHooks != nil { if config.ExtraAllocHooks != nil {
ar.runnerHooks = append(ar.runnerHooks, config.ExtraAllocHooks...) ar.runnerHooks = append(ar.runnerHooks, config.ExtraAllocHooks...)

View File

@@ -12,6 +12,7 @@ import (
"github.com/hashicorp/nomad/client/allocrunner/interfaces" "github.com/hashicorp/nomad/client/allocrunner/interfaces"
"github.com/hashicorp/nomad/client/serviceregistration/checks" "github.com/hashicorp/nomad/client/serviceregistration/checks"
"github.com/hashicorp/nomad/client/serviceregistration/checks/checkstore" "github.com/hashicorp/nomad/client/serviceregistration/checks/checkstore"
"github.com/hashicorp/nomad/client/taskenv"
"github.com/hashicorp/nomad/helper" "github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs"
) )
@@ -82,6 +83,7 @@ type checksHook struct {
shim checkstore.Shim shim checkstore.Shim
checker checks.Checker checker checks.Checker
allocID string allocID string
taskEnv *taskenv.TaskEnv
// fields that get re-initialized on allocation update // fields that get re-initialized on allocation update
lock sync.RWMutex lock sync.RWMutex
@@ -96,6 +98,7 @@ func newChecksHook(
alloc *structs.Allocation, alloc *structs.Allocation,
shim checkstore.Shim, shim checkstore.Shim,
network structs.NetworkStatus, network structs.NetworkStatus,
taskEnv *taskenv.TaskEnv,
) *checksHook { ) *checksHook {
h := &checksHook{ h := &checksHook{
logger: logger.Named(checksHookName), logger: logger.Named(checksHookName),
@@ -104,6 +107,7 @@ func newChecksHook(
shim: shim, shim: shim,
network: network, network: network,
checker: checks.New(logger), checker: checks.New(logger),
taskEnv: taskEnv,
} }
h.initialize(alloc) h.initialize(alloc)
return h return h
@@ -208,8 +212,10 @@ func (h *checksHook) Prerun() error {
return nil return nil
} }
interpolatedServices := taskenv.InterpolateServices(h.taskEnv, group.NomadServices())
// create and start observers of nomad service checks in alloc // create and start observers of nomad service checks in alloc
h.observe(h.alloc, group.NomadServices()) h.observe(h.alloc, interpolatedServices)
return nil return nil
} }

View File

@@ -17,6 +17,7 @@ import (
"github.com/hashicorp/nomad/client/allocrunner/interfaces" "github.com/hashicorp/nomad/client/allocrunner/interfaces"
"github.com/hashicorp/nomad/client/serviceregistration/checks/checkstore" "github.com/hashicorp/nomad/client/serviceregistration/checks/checkstore"
"github.com/hashicorp/nomad/client/state" "github.com/hashicorp/nomad/client/state"
"github.com/hashicorp/nomad/client/taskenv"
"github.com/hashicorp/nomad/helper/testlog" "github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/nomad/mock" "github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs"
@@ -169,7 +170,9 @@ func TestCheckHook_Checks_ResultsSet(t *testing.T) {
alloc := allocWithNomadChecks(addr, port, tc.onGroup) alloc := allocWithNomadChecks(addr, port, tc.onGroup)
h := newChecksHook(logger, alloc, checkStore, network) envBuilder := taskenv.NewBuilder(mock.Node(), alloc, nil, alloc.Job.Region)
h := newChecksHook(logger, alloc, checkStore, network, envBuilder.Build())
// initialize is called; observers are created but not started yet // initialize is called; observers are created but not started yet
must.MapEmpty(t, h.observers) must.MapEmpty(t, h.observers)
@@ -234,7 +237,9 @@ func TestCheckHook_Checks_UpdateSet(t *testing.T) {
alloc := allocWithNomadChecks(addr, port, true) alloc := allocWithNomadChecks(addr, port, true)
h := newChecksHook(logger, alloc, shim, network) envBuilder := taskenv.NewBuilder(mock.Node(), alloc, nil, alloc.Job.Region)
h := newChecksHook(logger, alloc, shim, network, envBuilder.Build())
// calling pre-run starts the observers // calling pre-run starts the observers
err := h.Prerun() err := h.Prerun()