mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add is_prebuild_claim to distinguish post-claim provisioning (#17757)
Used in combination with https://github.com/coder/terraform-provider-coder/pull/396 This is required by both https://github.com/coder/coder/pull/17475 and https://github.com/coder/coder/pull/17571 Operators may need to conditionalize their templates to perform certain operations once a prebuilt workspace has been claimed. This value will **only** be set once a claim takes place and a subsequent `terraform apply` occurs. Any `terraform apply` runs thereafter will be indistinguishable from a normal run on a workspace. --------- Signed-off-by: Danny Kopping <dannykopping@gmail.com>
This commit is contained in:
@@ -270,9 +270,12 @@ func provisionEnv(
|
||||
"CODER_WORKSPACE_TEMPLATE_VERSION="+metadata.GetTemplateVersion(),
|
||||
"CODER_WORKSPACE_BUILD_ID="+metadata.GetWorkspaceBuildId(),
|
||||
)
|
||||
if metadata.GetIsPrebuild() {
|
||||
if metadata.GetPrebuiltWorkspaceBuildStage().IsPrebuild() {
|
||||
env = append(env, provider.IsPrebuildEnvironmentVariable()+"=true")
|
||||
}
|
||||
if metadata.GetPrebuiltWorkspaceBuildStage().IsPrebuiltWorkspaceClaim() {
|
||||
env = append(env, provider.IsPrebuildClaimEnvironmentVariable()+"=true")
|
||||
}
|
||||
|
||||
for key, value := range provisionersdk.AgentScriptEnv() {
|
||||
env = append(env, key+"="+value)
|
||||
|
||||
@@ -25,6 +25,7 @@ import (
|
||||
|
||||
"cdr.dev/slog"
|
||||
"cdr.dev/slog/sloggers/slogtest"
|
||||
|
||||
"github.com/coder/coder/v2/codersdk/drpc"
|
||||
"github.com/coder/coder/v2/provisioner/terraform"
|
||||
"github.com/coder/coder/v2/provisionersdk"
|
||||
@@ -977,7 +978,7 @@ func TestProvision(t *testing.T) {
|
||||
required_providers {
|
||||
coder = {
|
||||
source = "coder/coder"
|
||||
version = "2.3.0-pre2"
|
||||
version = ">= 2.4.1"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -994,7 +995,7 @@ func TestProvision(t *testing.T) {
|
||||
},
|
||||
Request: &proto.PlanRequest{
|
||||
Metadata: &proto.Metadata{
|
||||
IsPrebuild: true,
|
||||
PrebuiltWorkspaceBuildStage: proto.PrebuiltWorkspaceBuildStage_CREATE,
|
||||
},
|
||||
},
|
||||
Response: &proto.PlanComplete{
|
||||
@@ -1008,6 +1009,44 @@ func TestProvision(t *testing.T) {
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "is-prebuild-claim",
|
||||
Files: map[string]string{
|
||||
"main.tf": `terraform {
|
||||
required_providers {
|
||||
coder = {
|
||||
source = "coder/coder"
|
||||
version = ">= 2.4.1"
|
||||
}
|
||||
}
|
||||
}
|
||||
data "coder_workspace" "me" {}
|
||||
resource "null_resource" "example" {}
|
||||
resource "coder_metadata" "example" {
|
||||
resource_id = null_resource.example.id
|
||||
item {
|
||||
key = "is_prebuild_claim"
|
||||
value = data.coder_workspace.me.is_prebuild_claim
|
||||
}
|
||||
}
|
||||
`,
|
||||
},
|
||||
Request: &proto.PlanRequest{
|
||||
Metadata: &proto.Metadata{
|
||||
PrebuiltWorkspaceBuildStage: proto.PrebuiltWorkspaceBuildStage_CLAIM,
|
||||
},
|
||||
},
|
||||
Response: &proto.PlanComplete{
|
||||
Resources: []*proto.Resource{{
|
||||
Name: "example",
|
||||
Type: "null_resource",
|
||||
Metadata: []*proto.Resource_Metadata{{
|
||||
Key: "is_prebuild_claim",
|
||||
Value: "true",
|
||||
}},
|
||||
}},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Remove unused cache dirs before running tests.
|
||||
|
||||
Reference in New Issue
Block a user