From 59d44c079370c4a0ff02e926ce83bd3fcabbcce9 Mon Sep 17 00:00:00 2001 From: Matthew Salsamendi Date: Wed, 4 Oct 2023 12:58:55 +0000 Subject: [PATCH] backport of commit aa9ff3a5b306645311dc4687e97561c0dfccbf90 --- .changelog/18584.txt | 3 +++ client/allocrunner/alloc_runner_hooks.go | 4 ++-- client/allocrunner/checks_hook.go | 8 +++++++- client/allocrunner/checks_hook_test.go | 9 +++++++-- 4 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 .changelog/18584.txt diff --git a/.changelog/18584.txt b/.changelog/18584.txt new file mode 100644 index 00000000000..2a9f693a51b --- /dev/null +++ b/.changelog/18584.txt @@ -0,0 +1,3 @@ +```release-note:bug +services: use interpolated address when performing nomad service health checks +``` diff --git a/client/allocrunner/alloc_runner_hooks.go b/client/allocrunner/alloc_runner_hooks.go index 2288f697fce..b5a365b4201 100644 --- a/client/allocrunner/alloc_runner_hooks.go +++ b/client/allocrunner/alloc_runner_hooks.go @@ -108,7 +108,7 @@ func (ar *allocRunner) initRunnerHooks(config *clientconfig.Config) error { } // Create a taskenv.TaskEnv which is used for read only purposes by the - // newNetworkHook. + // newNetworkHook and newChecksHook. builtTaskEnv := newEnvBuilder().Build() // Create the alloc directory hook. This is run first to ensure the @@ -134,7 +134,7 @@ func (ar *allocRunner) initRunnerHooks(config *clientconfig.Config) error { newConsulGRPCSocketHook(hookLogger, alloc, ar.allocDir, config.ConsulConfig, config.Node.Attributes), newConsulHTTPSocketHook(hookLogger, alloc, ar.allocDir, config.ConsulConfig), 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 { ar.runnerHooks = append(ar.runnerHooks, config.ExtraAllocHooks...) diff --git a/client/allocrunner/checks_hook.go b/client/allocrunner/checks_hook.go index 1705cc4cbe9..55fa86661e3 100644 --- a/client/allocrunner/checks_hook.go +++ b/client/allocrunner/checks_hook.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/nomad/client/allocrunner/interfaces" "github.com/hashicorp/nomad/client/serviceregistration/checks" "github.com/hashicorp/nomad/client/serviceregistration/checks/checkstore" + "github.com/hashicorp/nomad/client/taskenv" "github.com/hashicorp/nomad/helper" "github.com/hashicorp/nomad/nomad/structs" ) @@ -79,6 +80,7 @@ type checksHook struct { shim checkstore.Shim checker checks.Checker allocID string + taskEnv *taskenv.TaskEnv // fields that get re-initialized on allocation update lock sync.RWMutex @@ -93,6 +95,7 @@ func newChecksHook( alloc *structs.Allocation, shim checkstore.Shim, network structs.NetworkStatus, + taskEnv *taskenv.TaskEnv, ) *checksHook { h := &checksHook{ logger: logger.Named(checksHookName), @@ -101,6 +104,7 @@ func newChecksHook( shim: shim, network: network, checker: checks.New(logger), + taskEnv: taskEnv, } h.initialize(alloc) return h @@ -205,8 +209,10 @@ func (h *checksHook) Prerun() error { return nil } + interpolatedServices := taskenv.InterpolateServices(h.taskEnv, group.NomadServices()) + // create and start observers of nomad service checks in alloc - h.observe(h.alloc, group.NomadServices()) + h.observe(h.alloc, interpolatedServices) return nil } diff --git a/client/allocrunner/checks_hook_test.go b/client/allocrunner/checks_hook_test.go index 2a85560fe9d..bee0a2bf807 100644 --- a/client/allocrunner/checks_hook_test.go +++ b/client/allocrunner/checks_hook_test.go @@ -14,6 +14,7 @@ import ( "github.com/hashicorp/nomad/client/allocrunner/interfaces" "github.com/hashicorp/nomad/client/serviceregistration/checks/checkstore" "github.com/hashicorp/nomad/client/state" + "github.com/hashicorp/nomad/client/taskenv" "github.com/hashicorp/nomad/helper/testlog" "github.com/hashicorp/nomad/nomad/mock" "github.com/hashicorp/nomad/nomad/structs" @@ -166,7 +167,9 @@ func TestCheckHook_Checks_ResultsSet(t *testing.T) { 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 must.MapEmpty(t, h.observers) @@ -231,7 +234,9 @@ func TestCheckHook_Checks_UpdateSet(t *testing.T) { 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 err := h.Prerun()