@@ -500,6 +500,17 @@ func (s *server) acquireProtoJob(ctx context.Context, job database.ProvisionerJo
500
500
for _ , group := range ownerGroups {
501
501
ownerGroupNames = append (ownerGroupNames , group .Group .Name )
502
502
}
503
+ var runningWorkspaceAgentToken string
504
+ if input .RunningWorkspaceAgentID != uuid .Nil {
505
+ agent , err := s .Database .GetWorkspaceAgentByID (ctx , input .RunningWorkspaceAgentID )
506
+ if err != nil {
507
+ s .Logger .Warn (ctx , "failed to retrieve running workspace agent by ID; this may affect prebuilds" ,
508
+ slog .F ("workspace_agent_id" , input .RunningWorkspaceAgentID ),
509
+ slog .F ("job_id" , job .ID ))
510
+ } else {
511
+ runningWorkspaceAgentToken = agent .AuthToken .String ()
512
+ }
513
+ }
503
514
504
515
msg , err := json .Marshal (wspubsub.WorkspaceEvent {
505
516
Kind : wspubsub .WorkspaceEventKindStateChange ,
@@ -622,6 +633,7 @@ func (s *server) acquireProtoJob(ctx context.Context, job database.ProvisionerJo
622
633
WorkspaceBuildId : workspaceBuild .ID .String (),
623
634
WorkspaceOwnerLoginType : string (owner .LoginType ),
624
635
IsPrebuild : input .IsPrebuild ,
636
+ RunningWorkspaceAgentToken : runningWorkspaceAgentToken ,
625
637
},
626
638
LogLevel : input .LogLevel ,
627
639
},
@@ -2347,7 +2359,14 @@ type WorkspaceProvisionJob struct {
2347
2359
WorkspaceBuildID uuid.UUID `json:"workspace_build_id"`
2348
2360
DryRun bool `json:"dry_run"`
2349
2361
IsPrebuild bool `json:"is_prebuild,omitempty"`
2350
- LogLevel string `json:"log_level,omitempty"`
2362
+ // RunningWorkspaceAgentID is *only* used for prebuilds. We pass it down when we want to rebuild a prebuilt workspace
2363
+ // but not generate a new agent token. The provisionerdserver will retrieve this token and push it down to
2364
+ // the provisioner (and ultimately to the `coder_agent` resource in the Terraform provider) where it will be
2365
+ // reused. Context: the agent token is often used in immutable attributes of workspace resource (e.g. VM/container)
2366
+ // to initialize the agent, so if that value changes it will necessitate a replacement of that resource, thus
2367
+ // obviating the whole point of the prebuild.
2368
+ RunningWorkspaceAgentID uuid.UUID `json:"running_workspace_agent_id"`
2369
+ LogLevel string `json:"log_level,omitempty"`
2351
2370
}
2352
2371
2353
2372
// TemplateVersionDryRunJob is the payload for the "template_version_dry_run" job type.
0 commit comments