mirror of
https://github.com/coder/coder.git
synced 2025-07-10 23:53:15 +00:00
feat: reinitialize agents when a prebuilt workspace is claimed (#17475)
This pull request allows coder workspace agents to be reinitialized when a prebuilt workspace is claimed by a user. This facilitates the transfer of ownership between the anonymous prebuilds system user and the new owner of the workspace. Only a single agent per prebuilt workspace is supported for now, but plumbing has already been done to facilitate the seamless transition to multi-agent support. --------- Signed-off-by: Danny Kopping <dannykopping@gmail.com> Co-authored-by: Danny Kopping <dannykopping@gmail.com>
This commit is contained in:
@ -5,12 +5,19 @@ import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
||||
"github.com/coder/serpent"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/coder/v2/agent"
|
||||
"github.com/coder/coder/v2/cli/clitest"
|
||||
"github.com/coder/coder/v2/coderd/coderdtest"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
"github.com/coder/coder/v2/codersdk/agentsdk"
|
||||
@ -73,6 +80,168 @@ func TestBlockNonBrowser(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestReinitializeAgent(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tempAgentLog := testutil.CreateTemp(t, "", "testReinitializeAgent")
|
||||
|
||||
if !dbtestutil.WillUsePostgres() {
|
||||
t.Skip("dbmem cannot currently claim a workspace")
|
||||
}
|
||||
|
||||
db, ps := dbtestutil.NewDB(t)
|
||||
// GIVEN a live enterprise API with the prebuilds feature enabled
|
||||
client, user := coderdenttest.New(t, &coderdenttest.Options{
|
||||
Options: &coderdtest.Options{
|
||||
Database: db,
|
||||
Pubsub: ps,
|
||||
DeploymentValues: coderdtest.DeploymentValues(t, func(dv *codersdk.DeploymentValues) {
|
||||
dv.Prebuilds.ReconciliationInterval = serpent.Duration(time.Second)
|
||||
dv.Experiments.Append(string(codersdk.ExperimentWorkspacePrebuilds))
|
||||
}),
|
||||
IncludeProvisionerDaemon: true,
|
||||
},
|
||||
LicenseOptions: &coderdenttest.LicenseOptions{
|
||||
Features: license.Features{
|
||||
codersdk.FeatureWorkspacePrebuilds: 1,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// GIVEN a template, template version, preset and a prebuilt workspace that uses them all
|
||||
agentToken := uuid.UUID{3}
|
||||
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
|
||||
Parse: echo.ParseComplete,
|
||||
ProvisionPlan: []*proto.Response{
|
||||
{
|
||||
Type: &proto.Response_Plan{
|
||||
Plan: &proto.PlanComplete{
|
||||
Presets: []*proto.Preset{
|
||||
{
|
||||
Name: "test-preset",
|
||||
Prebuild: &proto.Prebuild{
|
||||
Instances: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
Resources: []*proto.Resource{
|
||||
{
|
||||
Agents: []*proto.Agent{
|
||||
{
|
||||
Name: "smith",
|
||||
OperatingSystem: "linux",
|
||||
Architecture: "i386",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
ProvisionApply: []*proto.Response{
|
||||
{
|
||||
Type: &proto.Response_Apply{
|
||||
Apply: &proto.ApplyComplete{
|
||||
Resources: []*proto.Resource{
|
||||
{
|
||||
Type: "compute",
|
||||
Name: "main",
|
||||
Agents: []*proto.Agent{
|
||||
{
|
||||
Name: "smith",
|
||||
OperatingSystem: "linux",
|
||||
Architecture: "i386",
|
||||
Scripts: []*proto.Script{
|
||||
{
|
||||
RunOnStart: true,
|
||||
Script: fmt.Sprintf("printenv >> %s; echo '---\n' >> %s", tempAgentLog.Name(), tempAgentLog.Name()), // Make reinitialization take long enough to assert that it happened
|
||||
},
|
||||
},
|
||||
Auth: &proto.Agent_Token{
|
||||
Token: agentToken.String(),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
|
||||
|
||||
coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
|
||||
|
||||
// Wait for prebuilds to create a prebuilt workspace
|
||||
ctx := context.Background()
|
||||
// ctx := testutil.Context(t, testutil.WaitLong)
|
||||
var (
|
||||
prebuildID uuid.UUID
|
||||
)
|
||||
require.Eventually(t, func() bool {
|
||||
agentAndBuild, err := db.GetWorkspaceAgentAndLatestBuildByAuthToken(ctx, agentToken)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
prebuildID = agentAndBuild.WorkspaceBuild.ID
|
||||
return true
|
||||
}, testutil.WaitLong, testutil.IntervalFast)
|
||||
|
||||
prebuild := coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, prebuildID)
|
||||
|
||||
preset, err := db.GetPresetByWorkspaceBuildID(ctx, prebuildID)
|
||||
require.NoError(t, err)
|
||||
|
||||
// GIVEN a running agent
|
||||
logDir := t.TempDir()
|
||||
inv, _ := clitest.New(t,
|
||||
"agent",
|
||||
"--auth", "token",
|
||||
"--agent-token", agentToken.String(),
|
||||
"--agent-url", client.URL.String(),
|
||||
"--log-dir", logDir,
|
||||
)
|
||||
clitest.Start(t, inv)
|
||||
|
||||
// GIVEN the agent is in a happy steady state
|
||||
waiter := coderdtest.NewWorkspaceAgentWaiter(t, client, prebuild.WorkspaceID)
|
||||
waiter.WaitFor(coderdtest.AgentsReady)
|
||||
|
||||
// WHEN a workspace is created that can benefit from prebuilds
|
||||
anotherClient, anotherUser := coderdtest.CreateAnotherUser(t, client, user.OrganizationID)
|
||||
workspace, err := anotherClient.CreateUserWorkspace(ctx, anotherUser.ID.String(), codersdk.CreateWorkspaceRequest{
|
||||
TemplateVersionID: version.ID,
|
||||
TemplateVersionPresetID: preset.ID,
|
||||
Name: "claimed-workspace",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
|
||||
|
||||
// THEN reinitialization completes
|
||||
waiter.WaitFor(coderdtest.AgentsReady)
|
||||
|
||||
var matches [][]byte
|
||||
require.Eventually(t, func() bool {
|
||||
// THEN the agent script ran again and reused the same agent token
|
||||
contents, err := os.ReadFile(tempAgentLog.Name())
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
// UUID regex pattern (matches UUID v4-like strings)
|
||||
uuidRegex := regexp.MustCompile(`\bCODER_AGENT_TOKEN=(.+)\b`)
|
||||
|
||||
matches = uuidRegex.FindAll(contents, -1)
|
||||
// When an agent reinitializes, we expect it to run startup scripts again.
|
||||
// As such, we expect to have written the agent environment to the temp file twice.
|
||||
// Once on initial startup and then once on reinitialization.
|
||||
return len(matches) == 2
|
||||
}, testutil.WaitLong, testutil.IntervalMedium)
|
||||
require.Equal(t, matches[0], matches[1])
|
||||
}
|
||||
|
||||
type setupResp struct {
|
||||
workspace codersdk.Workspace
|
||||
sdkAgent codersdk.WorkspaceAgent
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
@ -13,6 +14,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
@ -30,6 +32,8 @@ import (
|
||||
"github.com/coder/coder/v2/coderd/database/dbtime"
|
||||
"github.com/coder/coder/v2/coderd/httpmw"
|
||||
"github.com/coder/coder/v2/coderd/notifications"
|
||||
"github.com/coder/coder/v2/coderd/prebuilds"
|
||||
"github.com/coder/coder/v2/coderd/provisionerdserver"
|
||||
"github.com/coder/coder/v2/coderd/rbac"
|
||||
"github.com/coder/coder/v2/coderd/rbac/policy"
|
||||
agplschedule "github.com/coder/coder/v2/coderd/schedule"
|
||||
@ -43,6 +47,7 @@ import (
|
||||
"github.com/coder/coder/v2/enterprise/coderd/schedule"
|
||||
"github.com/coder/coder/v2/provisioner/echo"
|
||||
"github.com/coder/coder/v2/provisionersdk"
|
||||
"github.com/coder/coder/v2/provisionersdk/proto"
|
||||
"github.com/coder/coder/v2/testutil"
|
||||
"github.com/coder/quartz"
|
||||
)
|
||||
@ -459,6 +464,79 @@ func TestCreateUserWorkspace(t *testing.T) {
|
||||
_, err = client1.CreateUserWorkspace(ctx, user1.ID.String(), req)
|
||||
require.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("ClaimPrebuild", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if !dbtestutil.WillUsePostgres() {
|
||||
t.Skip("dbmem cannot currently claim a workspace")
|
||||
}
|
||||
|
||||
client, db, user := coderdenttest.NewWithDatabase(t, &coderdenttest.Options{
|
||||
Options: &coderdtest.Options{
|
||||
DeploymentValues: coderdtest.DeploymentValues(t, func(dv *codersdk.DeploymentValues) {
|
||||
err := dv.Experiments.Append(string(codersdk.ExperimentWorkspacePrebuilds))
|
||||
require.NoError(t, err)
|
||||
}),
|
||||
},
|
||||
LicenseOptions: &coderdenttest.LicenseOptions{
|
||||
Features: license.Features{
|
||||
codersdk.FeatureWorkspacePrebuilds: 1,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// GIVEN a template, template version, preset and a prebuilt workspace that uses them all
|
||||
presetID := uuid.New()
|
||||
tv := dbfake.TemplateVersion(t, db).Seed(database.TemplateVersion{
|
||||
OrganizationID: user.OrganizationID,
|
||||
CreatedBy: user.UserID,
|
||||
}).Preset(database.TemplateVersionPreset{
|
||||
ID: presetID,
|
||||
}).Do()
|
||||
|
||||
r := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
|
||||
OwnerID: prebuilds.SystemUserID,
|
||||
TemplateID: tv.Template.ID,
|
||||
}).Seed(database.WorkspaceBuild{
|
||||
TemplateVersionID: tv.TemplateVersion.ID,
|
||||
TemplateVersionPresetID: uuid.NullUUID{
|
||||
UUID: presetID,
|
||||
Valid: true,
|
||||
},
|
||||
}).WithAgent(func(a []*proto.Agent) []*proto.Agent {
|
||||
return a
|
||||
}).Do()
|
||||
|
||||
// nolint:gocritic // this is a test
|
||||
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitLong))
|
||||
agent, err := db.GetWorkspaceAgentAndLatestBuildByAuthToken(ctx, uuid.MustParse(r.AgentToken))
|
||||
require.NoError(t, err)
|
||||
|
||||
err = db.UpdateWorkspaceAgentLifecycleStateByID(ctx, database.UpdateWorkspaceAgentLifecycleStateByIDParams{
|
||||
ID: agent.WorkspaceAgent.ID,
|
||||
LifecycleState: database.WorkspaceAgentLifecycleStateReady,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// WHEN a workspace is created that matches the available prebuilt workspace
|
||||
_, err = client.CreateUserWorkspace(ctx, user.UserID.String(), codersdk.CreateWorkspaceRequest{
|
||||
TemplateVersionID: tv.TemplateVersion.ID,
|
||||
TemplateVersionPresetID: presetID,
|
||||
Name: "claimed-workspace",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// THEN a new build is scheduled with the build stage specified
|
||||
build, err := db.GetLatestWorkspaceBuildByWorkspaceID(ctx, r.Workspace.ID)
|
||||
require.NoError(t, err)
|
||||
require.NotEqual(t, build.ID, r.Build.ID)
|
||||
job, err := db.GetProvisionerJobByID(ctx, build.JobID)
|
||||
require.NoError(t, err)
|
||||
var metadata provisionerdserver.WorkspaceProvisionJob
|
||||
require.NoError(t, json.Unmarshal(job.Input, &metadata))
|
||||
require.Equal(t, metadata.PrebuiltWorkspaceBuildStage, proto.PrebuiltWorkspaceBuildStage_CLAIM)
|
||||
})
|
||||
}
|
||||
|
||||
func TestWorkspaceAutobuild(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user