Skip to content

feat: use preview to compute workspace tags from terraform #18720

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 1 commit into from
Jul 3, 2025

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Jul 2, 2025

What this does

If using dynamic parameters, workspace tags are extracted using coder/preview. This occurs at template version import and workspace create.

Templates that are not opted into dynamic parameters will use tfparse.

Future Work

When preview is used as the source of truth for tags, we can remove tfparse.

Closes coder/internal#698

Copy link
Member Author

Emyrk commented Jul 2, 2025

@Emyrk Emyrk force-pushed the stevenmasley/preview_tags_rebase branch 2 times, most recently from 6c1fdf5 to 20d0254 Compare July 2, 2025 16:10
@Emyrk Emyrk force-pushed the stevenmasley/preview_errors branch from 15a6ca1 to f0dd768 Compare July 2, 2025 16:10
@Emyrk Emyrk force-pushed the stevenmasley/preview_tags_rebase branch 2 times, most recently from 6f3d388 to f4cd152 Compare July 2, 2025 16:46
@Emyrk Emyrk force-pushed the stevenmasley/preview_errors branch from f0dd768 to a87678d Compare July 2, 2025 16:48
@Emyrk Emyrk force-pushed the stevenmasley/preview_tags_rebase branch 2 times, most recently from 29a7cf6 to 4d06a6c Compare July 2, 2025 17:32
"github.com/coder/coder/v2/testutil"
)

func Test_DynamicWorkspaceTagDefaultsFromFile(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is copied from tfparse_test.go.

The tfparse test can be removed when this becomes the new standard.

@Emyrk Emyrk force-pushed the stevenmasley/preview_errors branch from 96c83c7 to 8d594a7 Compare July 2, 2025 21:51
@Emyrk Emyrk force-pushed the stevenmasley/preview_tags_rebase branch 5 times, most recently from fc1ad45 to 11e0407 Compare July 3, 2025 12:25
@Emyrk Emyrk changed the base branch from stevenmasley/preview_errors to graphite-base/18720 July 3, 2025 13:33
@Emyrk Emyrk force-pushed the graphite-base/18720 branch from 3cc9349 to 699dd8e Compare July 3, 2025 13:34
@Emyrk Emyrk force-pushed the stevenmasley/preview_tags_rebase branch from 11e0407 to d1ed169 Compare July 3, 2025 13:34
@graphite-app graphite-app bot changed the base branch from graphite-base/18720 to main July 3, 2025 13:35
@Emyrk Emyrk force-pushed the stevenmasley/preview_tags_rebase branch 4 times, most recently from 35d29b9 to ea564d1 Compare July 3, 2025 13:59
@Emyrk Emyrk requested a review from Copilot July 3, 2025 14:02
@@ -1464,8 +1470,9 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
return
}

var dynamicTemplate bool
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunate, but orphan'd template versions can never use dynamic parameters 😢

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By 'orphaned' you mean !(active version)?

I wonder how this squares with the UI option you get to create a workspace from this template version when you create a new template version.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I mean when you first create a new template, the flow is:

  • Create template version
  • Create template using template version

So in step 1, the template version is orphaned. And it can remain that way.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR replaces the tfparse-based workspace tag extraction with the preview SDK for dynamic-parameter templates, adds caching in the workspace builder, and expands end-to-end and unit tests for tag parsing.

  • Upgrade to github.com/coder/preview v1.0.3 for tag extraction
  • Implement dynamicTemplateVersionTags/classicTemplateVersionTags in the API and cache tags in the workspace builder
  • Add tags_internal_test.go and integration test TestDynamicWorkspaceTags, plus a zip‐FS helper

Reviewed Changes

Copilot reviewed 13 out of 14 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
go.mod Bump github.com/coder/preview to v1.0.3-…
enterprise/coderd/testdata/.../main.tf Add Terraform test case for workspace tags
enterprise/coderd/dynamicparameters_test.go Add TestDynamicWorkspaceTags integration test
coderd/wsbuilder/wsbuilder.go Cache and return dynamic workspace tags
coderd/templateversions.go Route tag parsing through preview.Preview for dynamic templates
coderd/dynamicparameters/tags.go Add CheckTags and diagnostics for invalid tag values
coderd/dynamicparameters/error.go Rename error constructors and update error messaging
coderd/dynamicparameters/render.go Refactor workspace owner resolution and add version support
coderd/dynamicparameters/resolver.go Rename error calls to lowercase versions
coderd/coderdtest/dynamicparameters.go Expose Version hook in test helper
archive/fs/zip.go Introduce FromZipReader helper for zip archives
Comments suppressed due to low confidence (4)

coderd/templateversions.go:1765

  • [nitpick] This new helper method lacks a GoDoc comment; consider adding a brief comment explaining its behavior and parameters.
func (api *API) dynamicTemplateVersionTags(ctx context.Context, rw http.ResponseWriter, orgID uuid.UUID, owner uuid.UUID, file database.File) (map[string]string, bool) {

coderd/templateversions.go:1795

  • The unsupported mimetype branch in dynamicTemplateVersionTags isn’t exercised by existing tests; consider adding a test to cover this error path.
	default:

archive/fs/zip.go:1

  • The new FromZipReader helper lacks unit tests; consider adding tests to validate correct in-memory FS creation from zip data.
package archivefs

coderd/dynamicparameters/error.go:21

  • [nitpick] The error message is now "Failed to parse provisioner tags", which may be confusing in a workspace-tag context; consider using "workspace tags" for consistency with other messages.
func tagValidationError(diags hcl.Diagnostics) *DiagnosticError {

@Emyrk Emyrk force-pushed the stevenmasley/preview_tags_rebase branch from ea564d1 to 76ca1fa Compare July 3, 2025 14:08
@Emyrk Emyrk marked this pull request as ready for review July 3, 2025 14:08
@Emyrk Emyrk requested a review from johnstcn July 3, 2025 14:30
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐐

},
expectTags: map[string]string{"cluster": "developers", "platform": "kubernetes", "region": "us"},
expectedFailedTags: map[string]string{
"az": "Tag value is not known, it likely refers to a variable that is not set or has no default.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: this also differs slightly from tfparse which returns an empty value, but the empty result is checked externally IIRC

Comment on lines +387 to +389
expectedFailedTags: map[string]string{
"hostname": unknownTag,
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: same result as tfparse, but the error is nicer here 👍

"az": "a",
},
expectedFailedTags: map[string]string{
"some_path": unknownTag,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: it might be better to have an explicit "function not allowed" error here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can actually use the function, but it's the ~ in the function call. The ~ cannot be expanded in preview, so that invocation of the function fails. I'll make a comment

Comment on lines +570 to +584
expectTags: map[string]string{
"stringvar": "a",
"numvar": "1",
"boolvar": "true",
"stringparam": "a",
"numparam": "1",
"boolparam": "true",
"listparam": `["a", "b"]`, // OK because params are cast to strings
},
expectedFailedTags: map[string]string{
"listvar": invalidValueType,
"mapvar": invalidValueType,
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: behavioural difference from tfparse, but this was discussed and agreed to be correct as the provider will complain about the type not being stringy enough

@Emyrk Emyrk force-pushed the stevenmasley/preview_tags_rebase branch from 76ca1fa to ff0cf69 Compare July 3, 2025 17:20
@Emyrk Emyrk merged commit a099a8a into main Jul 3, 2025
35 checks passed
@Emyrk Emyrk deleted the stevenmasley/preview_tags_rebase branch July 3, 2025 19:35
@github-actions github-actions bot locked and limited conversation to collaborators Jul 3, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

chore: use preview engine to evaluate coder_workspace_tags
2 participants