template: add RunOnFirstRender option to change_script#27819
Open
VedantMadane wants to merge 1 commit intohashicorp:mainfrom
Open
template: add RunOnFirstRender option to change_script#27819VedantMadane wants to merge 1 commit intohashicorp:mainfrom
VedantMadane wants to merge 1 commit intohashicorp:mainfrom
Conversation
Author
|
I have signed the CLA. |
Contributor
|
@VedantMadane Thanks for pulling this issue and signing the CLA! I'm taking a look and hope to have some feedback for you tomorrow. |
tehut
requested changes
Apr 14, 2026
Add a RunOnFirstRender field to the ChangeScript configuration that allows template change_script to fire after the initial template render, not only on subsequent re-renders. When enabled, the template manager waits for the task to reach the running state after unblocking, then executes the configured scripts. This is useful for initialization tasks that need to run once the first template content is available. Replace the runFirstRenderScripts polling goroutine with a RunFirstRenderScripts public method that is called from the template hook Poststart handler. The task runner framework guarantees Poststart fires once the task reaches running state, so the polling loop is unnecessary. Also adds TestTaskTemplateManager_FirstRenderScript covering the new code path. Fixes hashicorp#27429
5a7b326 to
ba098ed
Compare
tehut
reviewed
Apr 21, 2026
Contributor
tehut
left a comment
There was a problem hiding this comment.
@VedantMadane This looks great. It was neat to see the template execute at task start up.
Since this PR will close out an open issue, would you mind submitting a changelog entry?
https://github.com/hashicorp/nomad/blob/main/contributing/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Add a
run_on_first_renderoption to thechange_scripttemplate configuration block. When set totrue, the configured script will also fire after the initial template render, not only on subsequent re-renders.Currently,
change_mode = "script"only triggers when a template is re-rendered after the initial render. This means initialization logic that depends on the first template content being available has no built-in hook. The newrun_on_first_renderflag fills that gap.When enabled, the template manager collects scripts that should fire on first render, then launches a goroutine that polls for the task to reach the running state (since
Execrequires a running task driver handle). Once running, the scripts are executed concurrently, following the sameprocessScriptpath used for re-render scripts.Example usage in a job spec:
Testing & Reproduction steps
The change adds the
RunOnFirstRenderfield across the full config stack:nomad/structs.ChangeScript(internal struct)api.ChangeScript(API struct with mapstructure/HCL tags)command/agent/job_endpoint.gojobspec2/parse_job.goclient/allocrunner/taskrunner/template/template.goUpdated tests:
TestChangeScript_Equalcovers the new field in equality checksdiff_test.goexpectations updated for the new field in plan diffsjob_endpoint_test.goexpectations updated for API conversionLinks
Fixes #27429
Implementation follows the design direction given by @tgross in the issue discussion: modify
handleFirstRender/Run()to trigger scripts on first render, pollingIsRunning()until the task driver handle is available.Contributor Checklist
make clif requested.web-unified-docsif the approach is accepted. Thenomad-docsteam can advise on placement.Changes to Security Controls
No changes to security controls. The new field is a boolean configuration option that controls when an already-configured script executes.