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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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
Comment on lines 13 to 23
func ParameterValidationError(diags hcl.Diagnostics) *DiagnosticError {
func parameterValidationError(diags hcl.Diagnostics) *DiagnosticError {
return &DiagnosticError{
Message: "Unable to validate parameters",
Diagnostics: diags,
KeyedDiagnostics: make(map[string]hcl.Diagnostics),
}
}

func TagValidationError(diags hcl.Diagnostics) *DiagnosticError {
func tagValidationError(diags hcl.Diagnostics) *DiagnosticError {
return &DiagnosticError{
Message: "Failed to parse workspace tags",
Message: "Failed to parse provisioner tags",
Copy link
Member Author

Choose a reason for hiding this comment

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

Reducing the external api of this package a bit

@@ -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 😢

Comment on lines +1786 to +1794
case "application/zip":
files, err = archivefs.FromZipReader(bytes.NewReader(file.Data), int64(len(file.Data)))
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error checking workspace tags",
Detail: "extract zip archive: " + err.Error(),
})
return nil, false
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Do we have any zip based templates?

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
1 participant