Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d9c906a

Browse files
committed
chore: review feedback
Signed-off-by: Danny Kopping <[email protected]>
1 parent a893b79 commit d9c906a

File tree

9 files changed

+283
-279
lines changed

9 files changed

+283
-279
lines changed

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
protobuf "google.golang.org/protobuf/proto"
2727

2828
"cdr.dev/slog"
29+
2930
"github.com/coder/coder/v2/codersdk/drpcsdk"
3031

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

enterprise/coderd/prebuilds/reconcile.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package prebuilds
33
import (
44
"context"
55
"database/sql"
6+
"errors"
67
"fmt"
78
"math"
89
"strings"
@@ -631,7 +632,6 @@ func (c *StoreReconciler) provision(
631632
}
632633

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

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

743-
return errs.ErrorOrNil()
742+
return notifErr
744743
}

provisioner/terraform/executor.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ func (e *executor) parsePlan(ctx, killCtx context.Context, planfilePath string)
416416
// logDrift must only be called while the lock is held.
417417
// It will log the output of `terraform show`, which will show which resources have drifted from the known state.
418418
func (e *executor) logDrift(ctx, killCtx context.Context, planfilePath string, logr logSink) {
419-
stdout, stdoutDone := resourceReplaceLogWriter(logr)
419+
stdout, stdoutDone := resourceReplaceLogWriter(logr, e.logger)
420420
stderr, stderrDone := logWriter(logr, proto.LogLevel_ERROR)
421421
defer func() {
422422
_ = stdout.Close()
@@ -436,9 +436,9 @@ func (e *executor) logDrift(ctx, killCtx context.Context, planfilePath string, l
436436
//
437437
// The WriteCloser must be closed by the caller to end logging, after which the returned channel will be closed to
438438
// indicate that logging of the written data has finished. Failure to close the WriteCloser will leak a goroutine.
439-
func resourceReplaceLogWriter(sink logSink) (io.WriteCloser, <-chan any) {
439+
func resourceReplaceLogWriter(sink logSink, logger slog.Logger) (io.WriteCloser, <-chan struct{}) {
440440
r, w := io.Pipe()
441-
done := make(chan any)
441+
done := make(chan struct{})
442442

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

456456
sink.ProvisionLog(level, string(line))
457457
}
458+
if err := scanner.Err(); err != nil {
459+
logger.Error(context.Background(), "failed to read terraform log", slog.Error(err))
460+
}
458461
}()
459462
return w, done
460463
}

provisioner/terraform/resource_replacements.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,22 @@ func findResourceReplacements(plan *tfjson.Plan) resourceReplacements {
5959
}
6060

6161
// Replacements found, problem!
62-
for _, p := range ch.Change.ReplacePaths {
63-
var path string
64-
switch p := p.(type) {
62+
for _, val := range ch.Change.ReplacePaths {
63+
var pathStr string
64+
// Each path needs to be coerced into a string. All types except []interface{} can be coerced using fmt.Sprintf.
65+
switch path := val.(type) {
6566
case []interface{}:
66-
segs := p
67-
list := make([]string, 0, len(segs))
68-
for _, s := range segs {
69-
list = append(list, fmt.Sprintf("%v", s))
67+
// Found a slice of paths; coerce to string and join by ".".
68+
segments := make([]string, 0, len(path))
69+
for _, seg := range path {
70+
segments = append(segments, fmt.Sprintf("%v", seg))
7071
}
71-
path = strings.Join(list, ".")
72+
pathStr = strings.Join(segments, ".")
7273
default:
73-
path = fmt.Sprintf("%v", p)
74+
pathStr = fmt.Sprintf("%v", path)
7475
}
7576

76-
replacements[ch.Address] = append(replacements[ch.Address], path)
77+
replacements[ch.Address] = append(replacements[ch.Address], pathStr)
7778
}
7879
}
7980

0 commit comments

Comments
 (0)