template: restore driver handle on update (#15915)

When the template hook Update() method is called it may recreate the
template manager if the Nomad or Vault token has been updated.

This caused the new template manager did not have a driver handler
because this was only being set on the Poststart hook, which is not
called for inplace updates.
This commit is contained in:
Luiz Aoqui 2023-01-27 10:55:59 -05:00 committed by GitHub
parent 881a4cfaff
commit de87cdc816
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

3
.changelog/15915.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
template: Fixed a bug that caused the chage script to fail to run
```

View File

@ -54,6 +54,12 @@ type templateHook struct {
templateManager *template.TaskTemplateManager templateManager *template.TaskTemplateManager
managerLock sync.Mutex managerLock sync.Mutex
// driverHandle is the task driver executor used by the template manager to
// run scripts when the template change mode is set to script.
//
// Must obtain a managerLock before changing. It may be nil.
driverHandle ti.ScriptExecutor
// consulNamespace is the current Consul namespace // consulNamespace is the current Consul namespace
consulNamespace string consulNamespace string
@ -124,7 +130,8 @@ func (h *templateHook) Poststart(ctx context.Context, req *interfaces.TaskPostst
} }
if req.DriverExec != nil { if req.DriverExec != nil {
h.templateManager.SetDriverHandle(req.DriverExec) h.driverHandle = req.DriverExec
h.templateManager.SetDriverHandle(h.driverHandle)
} else { } else {
for _, tmpl := range h.config.templates { for _, tmpl := range h.config.templates {
if tmpl.ChangeMode == structs.TemplateChangeModeScript { if tmpl.ChangeMode == structs.TemplateChangeModeScript {
@ -158,6 +165,9 @@ func (h *templateHook) newManager() (unblock chan struct{}, err error) {
} }
h.templateManager = m h.templateManager = m
if h.driverHandle != nil {
h.templateManager.SetDriverHandle(h.driverHandle)
}
return unblock, nil return unblock, nil
} }