- Notifications
You must be signed in to change notification settings - Fork1.2k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
b32923a0b0830f256395a61ef61aa66559f222892bf34e0115168c0141e5e0cb29e8fab31ed5eadf98d2f24aef070f9a531e8385dd0f00ce11a2c5ace63b24d2c5d4322d82a45209aae39ce658ac5655f82c3f587577a90a893b79d9c906a471198a7d694e66b7a8b75df2cb36d1c3ea5f62702f74d799971f65cbc362b04fbd356b9eb8beFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Signed-off-by: Danny Kopping <dannykopping@gmail.com>
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -258,7 +258,7 @@ func getStateFilePath(workdir string) string { | ||
| } | ||
| // revive:disable-next-line:flag-parameter | ||
| func (e *executor) plan(ctx, killCtx context.Context, env, vars []string, logr logSink,metadata *proto.Metadata) (*proto.PlanComplete, error) { | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This is the meat of the feature; everything else is just plumbing between system and user eyeball. | ||
| ctx, span := e.server.startTrace(ctx, tracing.FuncName()) | ||
| defer span.End() | ||
| @@ -274,6 +274,7 @@ func (e *executor) plan(ctx, killCtx context.Context, env, vars []string, logr l | ||
| "-refresh=true", | ||
| "-out=" + planfilePath, | ||
| } | ||
| destroy := metadata.GetWorkspaceTransition() == proto.WorkspaceTransition_DESTROY | ||
| if destroy { | ||
| args = append(args, "-destroy") | ||
| } | ||
| @@ -299,14 +300,28 @@ 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) | ||
| if err != nil { | ||
| graphTimings.ingest(createGraphTimingsEvent(timingGraphErrored)) | ||
| return nil, err | ||
| } | ||
| graphTimings.ingest(createGraphTimingsEvent(timingGraphComplete)) | ||
| // When a prebuild claim attempt is made, log a warning if a resource is due to be replaced, since this will obviate | ||
| // the point of prebuilding if the expensive resource is replaced once claimed! | ||
| // | ||
| // Future enhancement: send a notification (probably just to the inbox) to inform both the claimant and the template | ||
| // admin that a replacement took place. The provisioner does not connect to the database, so it can't enqueue notifications. | ||
| isPrebuildClaimAttempt := !destroy && metadata.PrebuildClaimForUserId != "" | ||
| if count := len(replacements); count > 0 && isPrebuildClaimAttempt { | ||
| e.server.logger.Warn(ctx, "plan introduces resource changes", slog.F("count", count)) | ||
| for n, p := range replacements { | ||
| // TODO: link to documentation for description and solution. | ||
| e.server.logger.Warn(ctx, "resource will be replaced, which may impact prebuilt workspace", slog.F("name", n), slog.F("replacement_paths", strings.Join(p, ","))) | ||
| } | ||
| } | ||
| return &proto.PlanComplete{ | ||
| Parameters: state.Parameters, | ||
| Resources: state.Resources, | ||
| @@ -335,18 +350,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) { | ||
| ctx, span := e.server.startTrace(ctx, tracing.FuncName()) | ||
| defer span.End() | ||
| plan, err := e.showPlan(ctx, killCtx, planfilePath) | ||
| if err != nil { | ||
| return nil, 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) | ||
| } | ||
| modules := []*tfjson.StateModule{} | ||
| if plan.PriorState != nil { | ||
| @@ -364,15 +379,15 @@ 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 state, planJSON,findResourceReplacements(plan),nil | ||
| } | ||
| // showPlan must only be called while the lock is held. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package terraform | ||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| tfjson "github.com/hashicorp/terraform-json" | ||
| ) | ||
| type resourceReplacements map[string][]string | ||
| // resourceReplacements finds all resources which would be replaced by the current plan, and the attribute paths which | ||
| // caused the replacement. | ||
| // | ||
| // NOTE: "replacement" in terraform terms means that a resource will have to be destroyed and replaced with a new resource | ||
| // since one of its immutable attributes was modified, which cannot be updated in-place. | ||
| func findResourceReplacements(plan *tfjson.Plan) resourceReplacements { | ||
| if plan == nil { | ||
| return nil | ||
| } | ||
| // No changes, no problem! | ||
| if len(plan.ResourceChanges) == 0 { | ||
| return nil | ||
| } | ||
| replacements := make(resourceReplacements, len(plan.ResourceChanges)) | ||
| for _, ch := range plan.ResourceChanges { | ||
| // No change, no problem! | ||
| if ch.Change == nil { | ||
| continue | ||
| } | ||
| // No-op change, no problem! | ||
| if ch.Change.Actions.NoOp() { | ||
| continue | ||
| } | ||
| // No replacements, no problem! | ||
| if len(ch.Change.ReplacePaths) == 0 { | ||
| continue | ||
| } | ||
| // Replacing our resources, no problem! | ||
| if strings.Index(ch.Type, "coder_") == 0 { | ||
| continue | ||
| } | ||
| // Replacements found, problem! | ||
| for _, p := range ch.Change.ReplacePaths { | ||
| var path string | ||
| switch p := p.(type) { | ||
| case []interface{}: | ||
| segs := p | ||
dannykopping marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| list := make([]string, 0, len(segs)) | ||
| for _, s := range segs { | ||
| list = append(list, fmt.Sprintf("%v", s)) | ||
| } | ||
| path = strings.Join(list, ".") | ||
| default: | ||
| path = fmt.Sprintf("%v", p) | ||
| } | ||
| replacements[ch.Address] = append(replacements[ch.Address], path) | ||
| } | ||
| } | ||
| if len(replacements) == 0 { | ||
| return nil | ||
| } | ||
| return replacements | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| package terraform | ||
| import ( | ||
| "testing" | ||
| tfjson "github.com/hashicorp/terraform-json" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
| func TestFindResourceReplacements(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| plan *tfjson.Plan | ||
| expected resourceReplacements | ||
| }{ | ||
| { | ||
| name: "nil plan", | ||
| }, | ||
| { | ||
| name: "no resource changes", | ||
| plan: &tfjson.Plan{}, | ||
| }, | ||
| { | ||
| name: "resource change with nil change", | ||
| plan: &tfjson.Plan{ | ||
| ResourceChanges: []*tfjson.ResourceChange{ | ||
| { | ||
| Address: "resource1", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "no-op action", | ||
| plan: &tfjson.Plan{ | ||
| ResourceChanges: []*tfjson.ResourceChange{ | ||
| { | ||
| Address: "resource1", | ||
| Change: &tfjson.Change{ | ||
| Actions: tfjson.Actions{tfjson.ActionNoop}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "empty replace paths", | ||
| plan: &tfjson.Plan{ | ||
| ResourceChanges: []*tfjson.ResourceChange{ | ||
| { | ||
| Address: "resource1", | ||
| Change: &tfjson.Change{ | ||
| Actions: tfjson.Actions{tfjson.ActionDelete, tfjson.ActionCreate}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "coder_* types are ignored", | ||
| plan: &tfjson.Plan{ | ||
| ResourceChanges: []*tfjson.ResourceChange{ | ||
| { | ||
| Address: "resource1", | ||
| Type: "coder_resource", | ||
| Change: &tfjson.Change{ | ||
| Actions: tfjson.Actions{tfjson.ActionDelete, tfjson.ActionCreate}, | ||
| ReplacePaths: []interface{}{"path1"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "valid replacements - single path", | ||
| plan: &tfjson.Plan{ | ||
| ResourceChanges: []*tfjson.ResourceChange{ | ||
| { | ||
| Address: "resource1", | ||
| Type: "example_resource", | ||
| Change: &tfjson.Change{ | ||
| Actions: tfjson.Actions{tfjson.ActionDelete, tfjson.ActionCreate}, | ||
| ReplacePaths: []interface{}{"path1"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expected: resourceReplacements{ | ||
| "resource1": {"path1"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "valid replacements - multiple paths", | ||
| plan: &tfjson.Plan{ | ||
| ResourceChanges: []*tfjson.ResourceChange{ | ||
| { | ||
| Address: "resource1", | ||
| Type: "example_resource", | ||
| Change: &tfjson.Change{ | ||
| Actions: tfjson.Actions{tfjson.ActionDelete, tfjson.ActionCreate}, | ||
| ReplacePaths: []interface{}{"path1", "path2"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expected: resourceReplacements{ | ||
| "resource1": {"path1", "path2"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "complex replace path", | ||
| plan: &tfjson.Plan{ | ||
| ResourceChanges: []*tfjson.ResourceChange{ | ||
| { | ||
| Address: "resource1", | ||
| Type: "example_resource", | ||
| Change: &tfjson.Change{ | ||
| Actions: tfjson.Actions{tfjson.ActionDelete, tfjson.ActionCreate}, | ||
| ReplacePaths: []interface{}{ | ||
| []interface{}{"path", "to", "key"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expected: resourceReplacements{ | ||
| "resource1": {"path.to.key"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "multiple changes", | ||
| plan: &tfjson.Plan{ | ||
| ResourceChanges: []*tfjson.ResourceChange{ | ||
| { | ||
| Address: "resource1", | ||
| Type: "example_resource", | ||
| Change: &tfjson.Change{ | ||
| Actions: tfjson.Actions{tfjson.ActionDelete, tfjson.ActionCreate}, | ||
| ReplacePaths: []interface{}{"path1"}, | ||
| }, | ||
| }, | ||
| { | ||
| Address: "resource2", | ||
| Type: "example_resource", | ||
| Change: &tfjson.Change{ | ||
| Actions: tfjson.Actions{tfjson.ActionDelete, tfjson.ActionCreate}, | ||
| ReplacePaths: []interface{}{"path2", "path3"}, | ||
| }, | ||
| }, | ||
| { | ||
| Address: "resource3", | ||
| Type: "coder_example", | ||
| Change: &tfjson.Change{ | ||
| Actions: tfjson.Actions{tfjson.ActionDelete, tfjson.ActionCreate}, | ||
| ReplacePaths: []interface{}{"ignored_path"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expected: resourceReplacements{ | ||
| "resource1": {"path1"}, | ||
| "resource2": {"path2", "path3"}, | ||
| }, | ||
| }, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| require.EqualValues(t, tc.expected, findResourceReplacements(tc.plan)) | ||
| }) | ||
| } | ||
| } |
Uh oh!
There was an error while loading.Please reload this page.