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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 0 additions & 28 deletions .coderabbit.yaml

This file was deleted.

1 change: 1 addition & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
If you have used AI to produce some or all of this PR, please ensure you have read our [AI Contribution guidelines](https://coder.com/docs/about/contributing/AI_CONTRIBUTING) before submitting.
111 changes: 52 additions & 59 deletions cli/provisionerjobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"
"time"

"github.com/aws/smithy-go/ptr"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -20,6 +19,7 @@ import (
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/provisionersdk"
"github.com/coder/coder/v2/testutil"
)

Expand All @@ -36,67 +36,43 @@ func TestProvisionerJobs(t *testing.T) {
templateAdminClient, templateAdmin := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.ScopedRoleOrgTemplateAdmin(owner.OrganizationID))
memberClient, member := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)

// Create initial resources with a running provisioner.
firstProvisioner := coderdtest.NewTaggedProvisionerDaemon(t, coderdAPI, "default-provisioner", map[string]string{"owner": "", "scope": "organization"})
t.Cleanup(func() { _ = firstProvisioner.Close() })
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, completeWithAgent())
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID, func(req *codersdk.CreateTemplateRequest) {
req.AllowUserCancelWorkspaceJobs = ptr.Bool(true)
// These CLI tests are related to provisioner job CRUD operations and as such
// do not require the overhead of starting a provisioner. Other provisioner job
// functionalities (acquisition etc.) are tested elsewhere.
template := dbgen.Template(t, db, database.Template{
OrganizationID: owner.OrganizationID,
CreatedBy: owner.UserID,
AllowUserCancelWorkspaceJobs: true,
})
version := dbgen.TemplateVersion(t, db, database.TemplateVersion{
OrganizationID: owner.OrganizationID,
CreatedBy: owner.UserID,
TemplateID: uuid.NullUUID{UUID: template.ID, Valid: true},
})

// Stop the provisioner so it doesn't grab any more jobs.
firstProvisioner.Close()

t.Run("Cancel", func(t *testing.T) {
t.Parallel()

// Set up test helpers.
type jobInput struct {
WorkspaceBuildID string `json:"workspace_build_id,omitempty"`
TemplateVersionID string `json:"template_version_id,omitempty"`
DryRun bool `json:"dry_run,omitempty"`
}
prepareJob := func(t *testing.T, input jobInput) database.ProvisionerJob {
// Test helper to create a provisioner job of a given type with a given input.
prepareJob := func(t *testing.T, jobType database.ProvisionerJobType, input json.RawMessage) database.ProvisionerJob {
t.Helper()

inputBytes, err := json.Marshal(input)
require.NoError(t, err)

var typ database.ProvisionerJobType
switch {
case input.WorkspaceBuildID != "":
typ = database.ProvisionerJobTypeWorkspaceBuild
case input.TemplateVersionID != "":
if input.DryRun {
typ = database.ProvisionerJobTypeTemplateVersionDryRun
} else {
typ = database.ProvisionerJobTypeTemplateVersionImport
}
default:
t.Fatal("invalid input")
}

var (
tags = database.StringMap{"owner": "", "scope": "organization", "foo": uuid.New().String()}
_ = dbgen.ProvisionerDaemon(t, db, database.ProvisionerDaemon{Tags: tags})
job = dbgen.ProvisionerJob(t, db, coderdAPI.Pubsub, database.ProvisionerJob{
InitiatorID: member.ID,
Input: json.RawMessage(inputBytes),
Type: typ,
Tags: tags,
StartedAt: sql.NullTime{Time: coderdAPI.Clock.Now().Add(-time.Minute), Valid: true},
})
)
return job
return dbgen.ProvisionerJob(t, db, coderdAPI.Pubsub, database.ProvisionerJob{
InitiatorID: member.ID,
Input: input,
Type: jobType,
StartedAt: sql.NullTime{Time: coderdAPI.Clock.Now().Add(-time.Minute), Valid: true},
Tags: database.StringMap{provisionersdk.TagOwner: "", provisionersdk.TagScope: provisionersdk.ScopeOrganization, "foo": uuid.NewString()},
})
}

// Test helper to create a workspace build job with a predefined input.
prepareWorkspaceBuildJob := func(t *testing.T) database.ProvisionerJob {
t.Helper()
var (
wbID = uuid.New()
job = prepareJob(t, jobInput{WorkspaceBuildID: wbID.String()})
w = dbgen.Workspace(t, db, database.WorkspaceTable{
wbID = uuid.New()
input, _ = json.Marshal(map[string]string{"workspace_build_id": wbID.String()})
job = prepareJob(t, database.ProvisionerJobTypeWorkspaceBuild, input)
w = dbgen.Workspace(t, db, database.WorkspaceTable{
OrganizationID: owner.OrganizationID,
OwnerID: member.ID,
TemplateID: template.ID,
Expand All @@ -112,12 +88,14 @@ func TestProvisionerJobs(t *testing.T) {
return job
}

prepareTemplateVersionImportJobBuilder := func(t *testing.T, dryRun bool) database.ProvisionerJob {
// Test helper to create a template version import job with a predefined input.
prepareTemplateVersionImportJob := func(t *testing.T) database.ProvisionerJob {
t.Helper()
var (
tvID = uuid.New()
job = prepareJob(t, jobInput{TemplateVersionID: tvID.String(), DryRun: dryRun})
_ = dbgen.TemplateVersion(t, db, database.TemplateVersion{
tvID = uuid.New()
input, _ = json.Marshal(map[string]string{"template_version_id": tvID.String()})
job = prepareJob(t, database.ProvisionerJobTypeTemplateVersionImport, input)
_ = dbgen.TemplateVersion(t, db, database.TemplateVersion{
OrganizationID: owner.OrganizationID,
CreatedBy: templateAdmin.ID,
ID: tvID,
Expand All @@ -127,11 +105,26 @@ func TestProvisionerJobs(t *testing.T) {
)
return job
}
prepareTemplateVersionImportJob := func(t *testing.T) database.ProvisionerJob {
return prepareTemplateVersionImportJobBuilder(t, false)
}

// Test helper to create a template version import dry run job with a predefined input.
prepareTemplateVersionImportJobDryRun := func(t *testing.T) database.ProvisionerJob {
return prepareTemplateVersionImportJobBuilder(t, true)
t.Helper()
var (
tvID = uuid.New()
input, _ = json.Marshal(map[string]interface{}{
"template_version_id": tvID.String(),
"dry_run": true,
})
job = prepareJob(t, database.ProvisionerJobTypeTemplateVersionDryRun, input)
_ = dbgen.TemplateVersion(t, db, database.TemplateVersion{
OrganizationID: owner.OrganizationID,
CreatedBy: templateAdmin.ID,
ID: tvID,
TemplateID: uuid.NullUUID{UUID: template.ID, Valid: true},
JobID: job.ID,
})
)
return job
}

// Run the cancellation test suite.
Expand Down
46 changes: 18 additions & 28 deletions docs/admin/templates/extending-templates/prebuilt-workspaces.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
# Prebuilt workspaces

> [!WARNING]
> Prebuilds Compatibility Limitations:
> Prebuilt workspaces currently do not work reliably with [DevContainers feature](../managing-templates/devcontainers/index.md).
> If your project relies on DevContainer configuration, we recommend disabling prebuilds or carefully testing behavior before enabling them.
>
> We’re actively working to improve compatibility, but for now, please avoid using prebuilds with this feature to ensure stability and expected behavior.
Prebuilt workspaces (prebuilds) reduce workspace creation time with an automatically-maintained pool of
ready-to-use workspaces for specific parameter presets.

Prebuilt workspaces allow template administrators to improve the developer experience by reducing workspace
creation time with an automatically maintained pool of ready-to-use workspaces for specific parameter presets.

The template administrator configures a template to provision prebuilt workspaces in the background, and then when a developer creates
a new workspace that matches the preset, Coder assigns them an existing prebuilt instance.
Prebuilt workspaces significantly reduce wait times, especially for templates with complex provisioning or lengthy startup procedures.
The template administrator defines the prebuilt workspace's parameters and number of instances to keep provisioned.
The desired number of workspaces are then provisioned transparently.
When a developer creates a new workspace that matches the definition, Coder assigns them an existing prebuilt workspace.
This significantly reduces wait times, especially for templates with complex provisioning or lengthy startup procedures.

Prebuilt workspaces are:

Expand All @@ -21,6 +15,9 @@ Prebuilt workspaces are:
- Monitored and replaced automatically to maintain your desired pool size.
- Automatically scaled based on time-based schedules to optimize resource usage.

Prebuilt workspaces are a special type of workspace that don't follow the
[regular workspace scheduling features](../../../user-guides/workspace-scheduling.md) like autostart and autostop. Instead, they have their own reconciliation loop that handles prebuild-specific scheduling features such as TTL and prebuild scheduling.

## Relationship to workspace presets

Prebuilt workspaces are tightly integrated with [workspace presets](./parameters.md#workspace-presets):
Expand Down Expand Up @@ -53,7 +50,7 @@ instances your Coder deployment should maintain, and optionally configure a `exp
prebuilds {
instances = 3 # Number of prebuilt workspaces to maintain
expiration_policy {
ttl = 86400 # Time (in seconds) after which unclaimed prebuilds are expired (1 day)
ttl = 86400 # Time (in seconds) after which unclaimed prebuilds are expired (86400 = 1 day)
}
}
}
Expand Down Expand Up @@ -159,17 +156,17 @@ data "coder_workspace_preset" "goland" {

**Scheduling configuration:**

- **`timezone`**: The timezone for all cron expressions (required). Only a single timezone is supported per scheduling configuration.
- **`schedule`**: One or more schedule blocks defining when to scale to specific instance counts.
- **`cron`**: Cron expression interpreted as continuous time ranges (required).
- **`instances`**: Number of prebuilt workspaces to maintain during this schedule (required).
- `timezone`: (Required) The timezone for all cron expressions. Only a single timezone is supported per scheduling configuration.
- `schedule`: One or more schedule blocks defining when to scale to specific instance counts.
- `cron`: (Required) Cron expression interpreted as continuous time ranges.
- `instances`: (Required) Number of prebuilt workspaces to maintain during this schedule.

**How scheduling works:**

1. The reconciliation loop evaluates all active schedules every reconciliation interval (`CODER_WORKSPACE_PREBUILDS_RECONCILIATION_INTERVAL`).
2. The schedule that matches the current time becomes active. Overlapping schedules are disallowed by validation rules.
3. If no schedules match the current time, the base `instances` count is used.
4. The reconciliation loop automatically creates or destroys prebuilt workspaces to match the target count.
1. The schedule that matches the current time becomes active. Overlapping schedules are disallowed by validation rules.
1. If no schedules match the current time, the base `instances` count is used.
1. The reconciliation loop automatically creates or destroys prebuilt workspaces to match the target count.

**Cron expression format:**

Expand Down Expand Up @@ -227,7 +224,7 @@ When a template's active version is updated:
1. Prebuilt workspaces for old versions are automatically deleted.
1. New prebuilt workspaces are created for the active template version.
1. If dependencies change (e.g., an [AMI](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html) update) without a template version change:
- You may delete the existing prebuilt workspaces manually.
- You can delete the existing prebuilt workspaces manually.
- Coder will automatically create new prebuilt workspaces with the updated dependencies.

The system always maintains the desired number of prebuilt workspaces for the active template version.
Expand Down Expand Up @@ -285,13 +282,6 @@ For example, the [`ami`](https://registry.terraform.io/providers/hashicorp/aws/l
has [`ForceNew`](https://github.com/hashicorp/terraform-provider-aws/blob/main/internal/service/ec2/ec2_instance.go#L75-L81) set,
since the AMI cannot be changed in-place._

#### Updating claimed prebuilt workspace templates

Once a prebuilt workspace has been claimed, and if its template uses `ignore_changes`, users may run into an issue where the agent
does not reconnect after a template update. This shortcoming is described in [this issue](https://github.com/coder/coder/issues/17840)
and will be addressed before the next release (v2.23). In the interim, a simple workaround is to restart the workspace
when it is in this problematic state.

### Monitoring and observability

#### Available metrics
Expand Down
7 changes: 7 additions & 0 deletions dogfood/coder/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,13 @@ module "dotfiles" {
agent_id = coder_agent.dev.id
}

module "git-config" {
count = data.coder_workspace.me.start_count
source = "dev.registry.coder.com/coder/git-config/coder"
version = "1.0.31"
agent_id = coder_agent.dev.id
}

module "git-clone" {
count = data.coder_workspace.me.start_count
source = "dev.registry.coder.com/coder/git-clone/coder"
Expand Down
Loading