mirror of
https://github.com/coder/coder.git
synced 2026-09-23 05:41:11 +08:00
## Summary
GCP base templates (`gcp-linux`, `gcp-windows`) in the Template Builder
had a Terraform `variable "project_id"` with no default, but their
`base.json` manifests didn't declare it. The UI never prompted for it,
so the provisioner import always failed with:
```
required template variables need values: project_id
```
## Changes
- Add `project_id` as a required variable in both GCP `base.json`
manifests
- Convert templates from raw Terraform variable blocks to Go template
injection (`{{ .Variables.project_id }}`), matching the existing
kubernetes pattern
- Fix `DefaultBaseRenderContext` to supply a `"REQUIRED"` placeholder
for required variables without defaults (previously rendered as `<no
value>`)
- Replace duplicate test subtests with proper GCP coverage including a
missing-variable error case
<details>
<summary>Implementation plan</summary>
### Root cause
The GCP base templates contained `variable "project_id" {}` (no default
= required) in their `.tf.tmpl` files, but the `base.json` manifests had
an empty `variables` array. The Template Builder UI
(`BaseTemplateParametersStep`) is data-driven from `base.json`, so it
never showed a field for `project_id`. The composed Terraform output
still contained the required variable, causing the provisioner import to
fail.
### Fix approach
Follow the pattern established by the kubernetes base template:
1. Declare variables in `base.json` so the UI prompts for them
2. Use Go template syntax (`{{ .Variables.project_id }}`) to inject
values at compose time
3. Remove raw Terraform `variable` blocks from the template since the
value is now baked in
### Files changed
| File | Change |
|---|---|
| `bases/gcp-linux/base.json` | Added `project_id` as a required
variable |
| `bases/gcp-windows/base.json` | Added `project_id` as a required
variable |
| `bases/gcp-linux/main.tf.tmpl` | Removed Terraform variable block, use
Go template injection |
| `bases/gcp-windows/main.tf.tmpl` | Same |
| `bases.go` | `DefaultBaseRenderContext` supplies placeholder for
required vars without defaults |
| `compose_test.go` | Replaced duplicate subtests with proper GCP tests
|
| `templatebuilder_handler_test.go` | Updated `gcp-windows` spec to
expect `project_id` variable |
| Golden files | Regenerated |
</details>
> 🤖 Generated by Coder Agents on behalf of @jeremyruppel
305 lines
8.1 KiB
Plaintext
305 lines
8.1 KiB
Plaintext
terraform {
|
|
required_providers {
|
|
coder = {
|
|
source = "coder/coder"
|
|
}
|
|
kubernetes = {
|
|
source = "hashicorp/kubernetes"
|
|
}
|
|
}
|
|
}
|
|
|
|
provider "coder" {
|
|
}
|
|
|
|
provider "kubernetes" {
|
|
# Authenticate via ~/.kube/config or a Coder-specific ServiceAccount, depending on admin preferences
|
|
config_path = false == true ? "~/.kube/config" : null
|
|
}
|
|
|
|
data "coder_parameter" "cpu" {
|
|
name = "cpu"
|
|
display_name = "CPU"
|
|
description = "The number of CPU cores"
|
|
default = "2"
|
|
icon = "/icon/memory.svg"
|
|
mutable = true
|
|
option {
|
|
name = "2 Cores"
|
|
value = "2"
|
|
}
|
|
option {
|
|
name = "4 Cores"
|
|
value = "4"
|
|
}
|
|
option {
|
|
name = "6 Cores"
|
|
value = "6"
|
|
}
|
|
option {
|
|
name = "8 Cores"
|
|
value = "8"
|
|
}
|
|
}
|
|
|
|
data "coder_parameter" "memory" {
|
|
name = "memory"
|
|
display_name = "Memory"
|
|
description = "The amount of memory in GB"
|
|
default = "2"
|
|
icon = "/icon/memory.svg"
|
|
mutable = true
|
|
option {
|
|
name = "2 GB"
|
|
value = "2"
|
|
}
|
|
option {
|
|
name = "4 GB"
|
|
value = "4"
|
|
}
|
|
option {
|
|
name = "6 GB"
|
|
value = "6"
|
|
}
|
|
option {
|
|
name = "8 GB"
|
|
value = "8"
|
|
}
|
|
}
|
|
|
|
data "coder_parameter" "home_disk_size" {
|
|
name = "home_disk_size"
|
|
display_name = "Home disk size"
|
|
description = "The size of the home disk in GB"
|
|
default = "10"
|
|
type = "number"
|
|
icon = "/emojis/1f4be.png"
|
|
mutable = false
|
|
validation {
|
|
min = 1
|
|
max = 99999
|
|
}
|
|
}
|
|
|
|
data "coder_workspace" "me" {}
|
|
data "coder_workspace_owner" "me" {}
|
|
|
|
resource "coder_agent" "main" {
|
|
os = "linux"
|
|
arch = "amd64"
|
|
startup_script = <<-EOT
|
|
set -e
|
|
|
|
# Add any commands that should be executed at workspace startup (e.g install requirements, start a program, etc) here
|
|
EOT
|
|
|
|
# The following metadata blocks are optional. They are used to display
|
|
# information about your workspace in the dashboard. You can remove them
|
|
# if you don't want to display any information.
|
|
# For basic resources, you can use the `coder stat` command.
|
|
# If you need more control, you can write your own script.
|
|
metadata {
|
|
display_name = "CPU Usage"
|
|
key = "0_cpu_usage"
|
|
script = "coder stat cpu"
|
|
interval = 10
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "RAM Usage"
|
|
key = "1_ram_usage"
|
|
script = "coder stat mem"
|
|
interval = 10
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "Home Disk"
|
|
key = "3_home_disk"
|
|
script = "coder stat disk --path $${HOME}"
|
|
interval = 60
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "CPU Usage (Host)"
|
|
key = "4_cpu_usage_host"
|
|
script = "coder stat cpu --host"
|
|
interval = 10
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "Memory Usage (Host)"
|
|
key = "5_mem_usage_host"
|
|
script = "coder stat mem --host"
|
|
interval = 10
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "Load Average (Host)"
|
|
key = "6_load_host"
|
|
# get load avg scaled by number of cores
|
|
script = <<EOT
|
|
echo "`cat /proc/loadavg | awk '{ print $1 }'` `nproc`" | awk '{ printf "%0.2f", $1/$2 }'
|
|
EOT
|
|
interval = 60
|
|
timeout = 1
|
|
}
|
|
}
|
|
|
|
resource "kubernetes_persistent_volume_claim_v1" "home" {
|
|
metadata {
|
|
name = "coder-${data.coder_workspace.me.id}-home"
|
|
namespace = "test-namespace"
|
|
labels = {
|
|
"app.kubernetes.io/name" = "coder-pvc"
|
|
"app.kubernetes.io/instance" = "coder-pvc-${data.coder_workspace.me.id}"
|
|
"app.kubernetes.io/part-of" = "coder"
|
|
//Coder-specific labels.
|
|
"com.coder.resource" = "true"
|
|
"com.coder.workspace.id" = data.coder_workspace.me.id
|
|
"com.coder.workspace.name" = data.coder_workspace.me.name
|
|
"com.coder.user.id" = data.coder_workspace_owner.me.id
|
|
"com.coder.user.username" = data.coder_workspace_owner.me.name
|
|
}
|
|
annotations = {
|
|
"com.coder.user.email" = data.coder_workspace_owner.me.email
|
|
}
|
|
}
|
|
wait_until_bound = false
|
|
spec {
|
|
access_modes = ["ReadWriteOnce"]
|
|
resources {
|
|
requests = {
|
|
storage = "${data.coder_parameter.home_disk_size.value}Gi"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "kubernetes_deployment_v1" "main" {
|
|
count = data.coder_workspace.me.start_count
|
|
depends_on = [
|
|
kubernetes_persistent_volume_claim_v1.home
|
|
]
|
|
wait_for_rollout = false
|
|
metadata {
|
|
name = "coder-${data.coder_workspace.me.id}"
|
|
namespace = "test-namespace"
|
|
labels = {
|
|
"app.kubernetes.io/name" = "coder-workspace"
|
|
"app.kubernetes.io/instance" = "coder-workspace-${data.coder_workspace.me.id}"
|
|
"app.kubernetes.io/part-of" = "coder"
|
|
"com.coder.resource" = "true"
|
|
"com.coder.workspace.id" = data.coder_workspace.me.id
|
|
"com.coder.workspace.name" = data.coder_workspace.me.name
|
|
"com.coder.user.id" = data.coder_workspace_owner.me.id
|
|
"com.coder.user.username" = data.coder_workspace_owner.me.name
|
|
}
|
|
annotations = {
|
|
"com.coder.user.email" = data.coder_workspace_owner.me.email
|
|
}
|
|
}
|
|
|
|
spec {
|
|
replicas = 1
|
|
selector {
|
|
match_labels = {
|
|
"app.kubernetes.io/name" = "coder-workspace"
|
|
"app.kubernetes.io/instance" = "coder-workspace-${data.coder_workspace.me.id}"
|
|
"app.kubernetes.io/part-of" = "coder"
|
|
"com.coder.resource" = "true"
|
|
"com.coder.workspace.id" = data.coder_workspace.me.id
|
|
"com.coder.workspace.name" = data.coder_workspace.me.name
|
|
"com.coder.user.id" = data.coder_workspace_owner.me.id
|
|
"com.coder.user.username" = data.coder_workspace_owner.me.name
|
|
}
|
|
}
|
|
strategy {
|
|
type = "Recreate"
|
|
}
|
|
|
|
template {
|
|
metadata {
|
|
labels = {
|
|
"app.kubernetes.io/name" = "coder-workspace"
|
|
"app.kubernetes.io/instance" = "coder-workspace-${data.coder_workspace.me.id}"
|
|
"app.kubernetes.io/part-of" = "coder"
|
|
"com.coder.resource" = "true"
|
|
"com.coder.workspace.id" = data.coder_workspace.me.id
|
|
"com.coder.workspace.name" = data.coder_workspace.me.name
|
|
"com.coder.user.id" = data.coder_workspace_owner.me.id
|
|
"com.coder.user.username" = data.coder_workspace_owner.me.name
|
|
}
|
|
}
|
|
spec {
|
|
security_context {
|
|
run_as_user = 1000
|
|
fs_group = 1000
|
|
run_as_non_root = true
|
|
}
|
|
|
|
container {
|
|
name = "dev"
|
|
image = "codercom/example-base:ubuntu"
|
|
image_pull_policy = "Always"
|
|
command = ["sh", "-c", coder_agent.main.init_script]
|
|
security_context {
|
|
run_as_user = "1000"
|
|
}
|
|
env {
|
|
name = "CODER_AGENT_TOKEN"
|
|
value = coder_agent.main.token
|
|
}
|
|
resources {
|
|
requests = {
|
|
"cpu" = "250m"
|
|
"memory" = "512Mi"
|
|
}
|
|
limits = {
|
|
"cpu" = "${data.coder_parameter.cpu.value}"
|
|
"memory" = "${data.coder_parameter.memory.value}Gi"
|
|
}
|
|
}
|
|
volume_mount {
|
|
mount_path = "/home/coder"
|
|
name = "home"
|
|
read_only = false
|
|
}
|
|
}
|
|
|
|
volume {
|
|
name = "home"
|
|
persistent_volume_claim {
|
|
claim_name = kubernetes_persistent_volume_claim_v1.home.metadata.0.name
|
|
read_only = false
|
|
}
|
|
}
|
|
|
|
affinity {
|
|
// This affinity attempts to spread out all workspace pods evenly across
|
|
// nodes.
|
|
pod_anti_affinity {
|
|
preferred_during_scheduling_ignored_during_execution {
|
|
weight = 1
|
|
pod_affinity_term {
|
|
topology_key = "kubernetes.io/hostname"
|
|
label_selector {
|
|
match_expressions {
|
|
key = "app.kubernetes.io/name"
|
|
operator = "In"
|
|
values = ["coder-workspace"]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|