script_check_hook: handle task-level Consul namespace (#19241)

The `script_check_hook` runs at the task level but can create script checks for
both task-level services and group-level services. Now that we allow the Consul
namespace to be set at the task-level `consul.namespace`, we need to have both
possible namespaces handy when creating and updating checks.
This commit is contained in:
Tim Gross
2023-11-30 11:13:30 -05:00
committed by GitHub
parent 1a2d41d30b
commit ae403dcb4b

View File

@@ -37,13 +37,18 @@ type scriptCheckHookConfig struct {
// scriptCheckHook implements a task runner hook for running script
// checks in the context of a task
type scriptCheckHook struct {
consul serviceregistration.Handler
consulNamespace string
alloc *structs.Allocation
task *structs.Task
logger log.Logger
shutdownWait time.Duration // max time to wait for scripts to shutdown
shutdownCh chan struct{} // closed when all scripts should shutdown
consul serviceregistration.Handler
// a script check hook can create checks for both group-level and task-level
// services, so we track both possible namespaces we require
groupConsulNamespace string
taskConsulNamespace string
alloc *structs.Allocation
task *structs.Task
logger log.Logger
shutdownWait time.Duration // max time to wait for scripts to shutdown
shutdownCh chan struct{} // closed when all scripts should shutdown
// The following fields can be changed by Update()
driverExec tinterfaces.ScriptExecutor
@@ -63,14 +68,15 @@ type scriptCheckHook struct {
// in Poststart() or Update()
func newScriptCheckHook(c scriptCheckHookConfig) *scriptCheckHook {
h := &scriptCheckHook{
consul: c.consul,
consulNamespace: c.alloc.Job.LookupTaskGroup(c.alloc.TaskGroup).Consul.GetNamespace(),
alloc: c.alloc,
task: c.task,
scripts: make(map[string]*scriptCheck),
runningScripts: make(map[string]*taskletHandle),
shutdownWait: defaultShutdownWait,
shutdownCh: make(chan struct{}),
consul: c.consul,
groupConsulNamespace: c.alloc.ConsulNamespace(),
taskConsulNamespace: c.alloc.ConsulNamespaceForTask(c.task.Name),
alloc: c.alloc,
task: c.task,
scripts: make(map[string]*scriptCheck),
runningScripts: make(map[string]*taskletHandle),
shutdownWait: defaultShutdownWait,
shutdownCh: make(chan struct{}),
}
if c.shutdownWait != 0 {
@@ -124,6 +130,8 @@ func (h *scriptCheckHook) Update(ctx context.Context, req *interfaces.TaskUpdate
h.alloc = req.Alloc
h.task = task
h.taskEnv = req.TaskEnv
h.groupConsulNamespace = req.Alloc.ConsulNamespace()
h.taskConsulNamespace = req.Alloc.ConsulNamespaceForTask(task.Name)
return h.upsertChecks()
}
@@ -188,7 +196,7 @@ func (h *scriptCheckHook) newScriptChecks() map[string]*scriptCheck {
serviceID := serviceregistration.MakeAllocServiceID(
h.alloc.ID, h.task.Name, service)
sc := newScriptCheck(&scriptCheckConfig{
consulNamespace: h.consulNamespace,
consulNamespace: h.taskConsulNamespace,
allocID: h.alloc.ID,
taskName: h.task.Name,
check: check,
@@ -228,7 +236,7 @@ func (h *scriptCheckHook) newScriptChecks() map[string]*scriptCheck {
serviceID := serviceregistration.MakeAllocServiceID(
h.alloc.ID, groupTaskName, service)
sc := newScriptCheck(&scriptCheckConfig{
consulNamespace: h.consulNamespace,
consulNamespace: h.groupConsulNamespace,
allocID: h.alloc.ID,
taskName: groupTaskName,
check: check,