8000 feat: track resource replacements when claiming a prebuilt workspace by dannykopping · Pull Request #17571 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: track resource replacements when claiming a prebuilt workspace #17571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 38 commits into from
May 14, 2025
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 no 8000 tification
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: review feedback
Signed-off-by: Danny Kopping <dannykopping@gmail.com>
  • Loading branch information
dannykopping committed May 13, 2025
commit d9c906a49331da79f2ee8af982b563f448ac28b7
5 changes: 3 additions & 2 deletions coderd/provisionerdserver/provisionerdserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
protobuf "google.golang.org/protobuf/proto"

"cdr.dev/slog"

"github.com/coder/coder/v2/codersdk/drpcsdk"

"github.com/coder/quartz"
Expand Down Expand Up @@ -1729,8 +1730,8 @@ func (s *server) CompleteJob(ctx context.Context, completed *proto.CompletedJob)
// Track resource replacements, if there are any.
orchestrator := s.PrebuildsOrchestrator.Load()
if resourceReplacements := completed.GetWorkspaceBuild().GetResourceReplacements(); orchestrator != nil && len(resourceReplacements) > 0 {
// Fire and forget.
go (*orchestrator).TrackResourceReplacement(context.Background(), workspace.ID, workspaceBuild.ID, resourceReplacements)
// Fire and forget. Bind to the lifecycle of the server so shutdowns are handled gracefully.
go (*orchestrator).TrackResourceReplacement(s.lifecycleCtx, workspace.ID, workspaceBuild.ID, resourceReplacements)
}
}

Expand Down
9 changes: 4 additions & 5 deletions enterprise/coderd/prebuilds/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package prebuilds
import (
"context"
"database/sql"
"errors"
"fmt"
"math"
"strings"
Expand Down Expand Up @@ -631,7 +632,6 @@ func (c *StoreReconciler) provision(
}

func (c *StoreReconciler) TrackResourceReplacement(ctx context.Context, workspaceID, buildID uuid.UUID, replacements []*sdkproto.ResourceReplacement) {
// Set authorization context since this may be called in the background (i.e. with a bare context).
// nolint:gocritic // Necessary to query all the required data.
ctx = dbauthz.AsSystemRestricted(ctx)
// Since this may be called in a fire-and-forget fashion, we need to give up at some point.
Expand Down Expand Up @@ -716,8 +716,7 @@ func (c *StoreReconciler) trackResourceReplacement(ctx context.Context, workspac
return xerrors.Errorf("fetch template admins: %w", err)
}

var errs multierror.Error

var notifErr error
for _, templateAdmin := range templateAdmins {
if _, err := (*c.notifEnq.Load()).EnqueueWithData(ctx, templateAdmin.ID, notifications.TemplateWorkspaceResourceReplaced,
map[string]string{
Expand All @@ -735,10 +734,10 @@ func (c *StoreReconciler) trackResourceReplacement(ctx context.Context, workspac
// Associate this notification with all the related entities.
workspace.ID, workspace.OwnerID, workspace.TemplateID, templateVersion.ID, prebuildPreset.ID, workspace.OrganizationID,
); err != nil {
errs.Errors = append(errs.Errors, xerrors.Errorf("send notification to %q: %w", templateAdmin.ID.String(), err))
notifErr = errors.Join(xerrors.Errorf("send notification to %q: %w", templateAdmin.ID.String(), err))
continue
}
}

return errs.ErrorOrNil()
return notifErr
}
9 changes: 6 additions & 3 deletions provisioner/terraform/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func (e *executor) parsePlan(ctx, killCtx context.Context, planfilePath string)
// logDrift must only be called while the lock is held.
// It will log the output of `terraform show`, which will show which resources have drifted from the known state.
func (e *executor) logDrift(ctx, killCtx context.Context, planfilePath string, logr logSink) {
stdout, stdoutDone := resourceReplaceLogWriter(logr)
stdout, stdoutDone := resourceReplaceLogWriter(logr, e.logger)
stderr, stderrDone := logWriter(logr, proto.LogLevel_ERROR)
defer func() {
_ = stdout.Close()
Expand All @@ -436,9 +436,9 @@ func (e *executor) logDrift(ctx, killCtx context.Context, planfilePath string, l
//
// The WriteCloser must be closed by the caller to end logging, after which the returned channel will be closed to
// indicate that logging of the written data has finished. Failure to close the WriteCloser will leak a goroutine.
func resourceReplaceLogWriter(sink logSink) (io.WriteCloser, <-chan any) {
func resourceReplaceLogWriter(sink logSink, logger slog.Logger) (io.WriteCloser, <-chan struct{}) {
r, w := io.Pipe()
done := make(chan any)
done := make(chan struct{})

go func() {
defer close(done)
Expand All @@ -455,6 +455,9 @@ func resourceReplaceLogWriter(sink logSink) (io.WriteCloser, <-chan any) {

sink.ProvisionLog(level, string(line))
}
if err := scanner.Err(); err != nil {
logger.Error(context.Background(), "failed to read terraform log", slog.Error(err))
}
}()
return w, done
}
Expand Down
21 changes: 11 additions & 10 deletions provisioner/terraform/resource_replacements.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,22 @@ func findResourceReplacements(plan *tfjson.Plan) resourceReplacements {
}

// Replacements found, problem!
for _, p := range ch.Change.ReplacePaths {
var path string
switch p := p.(type) {
for _, val := range ch.Change.ReplacePaths {
var pathStr string
// Each path needs to be coerced into a string. All types except []interface{} can be coerced using fmt.Sprintf.
switch path := val.(type) {
case []interface{}:
segs := p
list := make([]string, 0, len(segs))
for _, s := range segs {
list = append(list, fmt.Sprintf("%v", s))
// Found a slice of paths; coerce to string and join by ".".
segments := make([]string, 0, len(path))
for _, seg := range path {
segments = append(segments, fmt.Sprintf("%v", seg))
}
path = strings.Join(list, ".")
pathStr = strings.Join(segments, ".")
default:
path = fmt.Sprintf("%v", p)
pathStr = fmt.Sprintf("%v", path)
}

replacements[ch.Address] = append(replacements[ch.Address], path)
replacements[ch.Address] = append(replacements[ch.Address], pathStr)
}
}

Expand Down
Loading
0