Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
b32923a
feat: log resource replacements
dannykopping Apr 25, 2025
0b0830f
feat: show terraform state drift diff in build logs
dannykopping Apr 25, 2025
256395a
feat: only highlight lines which mention replacement
dannykopping Apr 25, 2025
61ef61a
feat: notify template admins when prebuild claim results in resource …
dannykopping Apr 25, 2025
a66559f
chore: appease linter
dannykopping Apr 25, 2025
222892b
chore: fix notifications test
dannykopping Apr 25, 2025
f34e011
fix: don't panic
dannykopping Apr 28, 2025
5168c01
fix: renaming type
dannykopping Apr 28, 2025
41e5e0c
chore: updating migration numbers
dannykopping May 6, 2025
b29e8fa
chore: minor touch-ups
dannykopping May 6, 2025
b31ed5e
feat: add resource replacements metric
dannykopping May 7, 2025
adf98d2
feat: add resource replacement notification
dannykopping May 7, 2025
f24aef0
make lint; make fmt
dannykopping May 7, 2025
70f9a53
chore: adding tests
dannykopping May 8, 2025
1e8385d
feat: pass flag to terraform provider when prebuilt workspace claimed
dannykopping May 9, 2025
d0f00ce
chore: update provider, add test for is_prebuild_claim
dannykopping May 12, 2025
11a2c5a
Merge branch 'main' of github.com:/coder/coder into dk/logreplacements
dannykopping May 12, 2025
ce63b24
Merge branch 'dk/is-prebuild-claim' of github.com:/coder/coder into d…
dannykopping May 12, 2025
d2c5d43
chore: replace GetTemplatePresetsByID with GetPresetByID
dannykopping May 12, 2025
22d82a4
chore: correcting docs link
dannykopping May 12, 2025
5209aae
Merge branch 'main' of github.com:/coder/coder into dk/logreplacement
dannykopping May 12, 2025
39ce658
Merge branch 'main' of github.com:/coder/coder into dk/logreplacements
dannykopping May 12, 2025
ac5655f
Merge branch 'main' of github.com:/coder/coder into dk/logreplacements
dannykopping May 12, 2025
82c3f58
chore: note provisioner API change
dannykopping May 12, 2025
7577a90
chore: fixups
dannykopping May 13, 2025
a893b79
chore: adding note about immutable resources
dannykopping May 13, 2025
d9c906a
chore: review feedback
dannykopping May 13, 2025
471198a
Merge branch 'main' of github.com:/coder/coder into dk/logreplacements
dannykopping May 13, 2025
7d694e6
chore: merge conflicts
dannykopping May 13, 2025
6b7a8b7
chore: fix 'is not iterable' bullshit
dannykopping May 13, 2025
5df2cb3
Merge branch 'main' of github.com:/coder/coder into dk/logreplacements
dannykopping May 14, 2025
6d1c3ea
chore: rename migrations
dannykopping May 14, 2025
5f62702
chore: set notifications manager before enterprise server initializes…
dannykopping May 14, 2025
f74d799
chore: completing refactor since https://github.com/coder/coder/pull/…
dannykopping May 14, 2025
971f65c
chore: remove unnecessary atomicity since map is protected by mutex a…
dannykopping May 14, 2025
bc362b0
chore: appeasing linter's Very Important Suggestion
dannykopping May 14, 2025
4fbd356
Merge branch 'main' of github.com:/coder/coder into dk/logreplacements
dannykopping May 14, 2025
b9eb8be
chore: remove old replacement logging
dannykopping May 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: minor touch-ups
Signed-off-by: Danny Kopping <dannykopping@gmail.com>
  • Loading branch information
dannykopping committed May 8, 2025
commit b29e8fa22f7afa1298c27e77f9c37f10d9bfc960
7 changes: 4 additions & 3 deletions coderd/provisionerdserver/provisionerdserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1841,7 +1841,7 @@ func (s *server) notifyWorkspaceDeleted(ctx context.Context, workspace database.
}

func (s *server) notifyPrebuiltWorkspaceResourceReplacement(ctx context.Context, workspace database.Workspace, build database.WorkspaceBuild, claimantID uuid.UUID, replacements []*sdkproto.ResourceReplacement) {
if claimantID == uuid.Nil {
if claimantID == uuid.Nil || len(replacements) == 0 {
// This is not a prebuild claim.
return
}
Expand Down Expand Up @@ -1878,8 +1878,9 @@ func (s *server) notifyPrebuiltWorkspaceResourceReplacement(ctx context.Context,
// Associate this notification with all the related entities.
workspace.ID, workspace.OwnerID, workspace.TemplateID, workspace.OrganizationID,
); err != nil {
s.Logger.Warn(ctx, "failed to notify of prebuilt workspace resource replacement", slog.Error(err))
break
s.Logger.Warn(ctx, "failed to notify of prebuilt workspace resource replacement", slog.Error(err),
slog.F("user_id", templateAdmin.ID.String()), slog.F("user_email", templateAdmin.Email))
continue
}
}
}
Expand Down
52 changes: 28 additions & 24 deletions provisioner/terraform/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,14 @@ func (e *executor) plan(ctx, killCtx context.Context, env, vars []string, logr l
graphTimings := newTimingAggregator(database.ProvisionerJobTimingStageGraph)
graphTimings.ingest(createGraphTimingsEvent(timingGraphStart))

state, plan, replacements, err := e.planResources(ctx, killCtx, planfilePath)
state, plan, err := e.planResources(ctx, killCtx, planfilePath)
if err != nil {
graphTimings.ingest(createGraphTimingsEvent(timingGraphErrored))
return nil, err
return nil, xerrors.Errorf("plan resources: %w", err)
}
planJSON, err := json.Marshal(plan)
if err != nil {
return nil, xerrors.Errorf("marshal plan: %w", err)
}

graphTimings.ingest(createGraphTimingsEvent(timingGraphComplete))
Expand All @@ -312,17 +316,22 @@ func (e *executor) plan(ctx, killCtx context.Context, env, vars []string, logr l
// the point of prebuilding if the expensive resource is replaced once claimed!
var (
isPrebuildClaimAttempt = !destroy && metadata.GetPrebuildClaimForUserId() != ""
reps []*proto.ResourceReplacement
resReps []*proto.ResourceReplacement
)
if count := len(replacements); count > 0 && isPrebuildClaimAttempt {
// TODO(dannyk): we should log drift always (not just during prebuild claim attempts); we're validating that this output
// will not be overwhelming for end-users, but it'll certainly be super valuable for template admins
// to diagnose this resource replacement issue, at least.
e.logDrift(ctx, killCtx, planfilePath, logr)

reps = make([]*proto.ResourceReplacement, 0, len(replacements))
for n, p := range replacements {
reps = append(reps, &proto.ResourceReplacement{
if repsFromPlan := findResourceReplacements(plan); len(repsFromPlan) > 0 {
if isPrebuildClaimAttempt {
// TODO(dannyk): we should log drift always (not just during prebuild claim attempts); we're validating that this output
// will not be overwhelming for end-users, but it'll certainly be super valuable for template admins
// to diagnose this resource replacement issue, at least.
// Once prebuilds moves out of beta, consider deleting this condition.

// Lock held before calling (see top of method).
e.logDrift(ctx, killCtx, planfilePath, logr)
}

resReps = make([]*proto.ResourceReplacement, 0, len(repsFromPlan))
for n, p := range repsFromPlan {
resReps = append(resReps, &proto.ResourceReplacement{
Resource: n,
Paths: p,
})
Expand All @@ -335,8 +344,8 @@ func (e *executor) plan(ctx, killCtx context.Context, env, vars []string, logr l
ExternalAuthProviders: state.ExternalAuthProviders,
Timings: append(e.timings.aggregate(), graphTimings.aggregate()...),
Presets: state.Presets,
Plan: plan,
ResourceReplacements: reps,
Plan: planJSON,
ResourceReplacements: resReps,
}, nil
}

Expand All @@ -358,18 +367,18 @@ func onlyDataResources(sm tfjson.StateModule) tfjson.StateModule {
}

// planResources must only be called while the lock is held.
func (e *executor) planResources(ctx, killCtx context.Context, planfilePath string) (*State, json.RawMessage, resourceReplacements, error) {
func (e *executor) planResources(ctx, killCtx context.Context, planfilePath string) (*State, *tfjson.Plan, error) {
ctx, span := e.server.startTrace(ctx, tracing.FuncName())
defer span.End()

plan, err := e.parsePlan(ctx, killCtx, planfilePath)
if err != nil {
return nil, nil, nil, xerrors.Errorf("show terraform plan file: %w", err)
return nil, nil, xerrors.Errorf("show terraform plan file: %w", err)
}

rawGraph, err := e.graph(ctx, killCtx)
if err != nil {
return nil, nil, nil, xerrors.Errorf("graph: %w", err)
return nil, nil, xerrors.Errorf("graph: %w", err)
}
modules := []*tfjson.StateModule{}
if plan.PriorState != nil {
Expand All @@ -387,15 +396,10 @@ func (e *executor) planResources(ctx, killCtx context.Context, planfilePath stri

state, err := ConvertState(ctx, modules, rawGraph, e.server.logger)
if err != nil {
return nil, nil, nil, err
}

planJSON, err := json.Marshal(plan)
if err != nil {
return nil, nil, nil, err
return nil, nil, err
}

return state, planJSON, findResourceReplacements(plan), nil
return state, plan, nil
}

// parsePlan must only be called while the lock is held.
Expand Down
13 changes: 12 additions & 1 deletion provisioner/terraform/resource_replacements.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,18 @@ func findResourceReplacements(plan *tfjson.Plan) resourceReplacements {
continue
}

// Replacing our resources, no problem!
// Replacing our resources: could be a problem - but we ignore since they're "virtual" resources. If any of these
// resources' attributes are referenced by non-coder resources, those will show up as transitive changes there.
// i.e. if the coder_agent.id attribute is used in docker_container.env
//
// Replacing our resources is not strictly a problem in and of itself.
//
// NOTE:
// We may need to special-case coder_agent in the future. Currently, coder_agent is replaced on every build
// because it only supports Create but not Update: https://github.com/coder/terraform-provider-coder/blob/5648efb/provider/agent.go#L28
// When we can modify an agent's attributes, some of which may be immutable (like "arch") and some may not (like "env"),
// then we'll have to handle this specifically.
// This will only become relevant once we support multiple agents: https://github.com/coder/coder/issues/17388
if strings.Index(ch.Type, "coder_") == 0 {
continue
}
Expand Down