Example: Using `devcontainer.json` to influence a Coder (Terraform) template
- Ngôn ngữ chính
- Go
- Star
- 300
- Fork
- 64
- Merge trung bình
- 20 phút
- Pull request đã merge (30 ngày)
- 1
Mô tả
> [!NOTE]
> I'm not sure how realistic this is, or whether it should be out of scope envbuilder as it's likely a Terraform provider/Coder feature. However, this is a somewhat common use case. It may not be a good fit for envbuilder v1.0 but worth discussing
Since envbuilder is a container image, it cannot influence the underlying infrastructure it can be provisioned on. However some Dev Container features do have support for additional infrastructure layers. Can we provide an example of how a Coder template can read a devcontainer.json and then influence the template? We have some customers doing this today with a custom `coder.yaml`
## Example: Kubernetes Volume Mounts
```hcl
# The jank approach (pseudocode, untested)
data "http" "devcontainer_contents" {
url = "${var.git_repo}/${var.git_branch}/.devcontainer/devcontainer.json"
request_headers = {
Accept = "application/vnd.github.v3.raw"
}
}
locals {
# Attempt to parse mounts from the devcontainer JSON
parsed_mounts = try(jsondecode(data.http.devcontainer_contents.body).mounts, [])
# Use a default mount if no mounts are specified or if the mounts key doesn't exist
mounts = length(local.parsed_mounts) > 0 ? local.parsed_mounts : ["source=workspace,target=/workspaces,type=volume"]
# Extracting target paths for PVC creation and Pod mounts
target_paths = [for m in local.mounts : split(",", m)[1]]
target_path_names = { for m in local.target_paths : split("=", m)[1] => split("=", m)[1] }
}
resource "kubernetes_persistent_volume_claim" "pvc" {
for_each = local.target_path_names
metadata {
name = "pvc-${replace(each.value, "/", "-")}"
}
spec {
access_modes = ["ReadWriteOnce"]
resources {
requests = {
storage = "1Gi"
}
}
}
}
resource "kubernetes_pod" "example" {
metadata {
name = "example-pod"
}
spec {
container {
image = "nginx"
name = "example"
volume_mount {
for_each = local.target_path_names
name = "pvc-${replace(each.value, "/", "-")}"
mount_path = each.value
}
}
volume {
for_each = local.target_path_names
name = "pvc-${replace(each.value, "/", "-")}"
persistent_volume_claim {
claim_name = "pvc-${replace(each.value, "/", "-")}"
}
}
}
}
image = "nginx"
name = "example"
volume_mount {
for_each = local.target_path_names
name = "pvc-${replace(each.value, "/", "-")}"
mount_path = each.value
}
}
volume {
for_each = local.target_path_names
name = "pvc-${replace(each.value, "/", "-")}"
persistent_volume_claim {
claim_name = "pvc-${replace(each.value, "/", "-")}"
}
}
}
}
```
## Option 2: TF Provider
The Coder server reads the devcontainer.json and passes data via a Terraform data source. This is related to https://github.com/coder/coder/issues/8462.
## Option 3: envbuilder + docker on a VM
If envbuilder runs on a VM, it could realistically do significantly more such as create Docker volumes as well as run a container.
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Hướng nghiên cứu
Issue không nêu tên tệp hoặc test nào trong repository; hãy bắt đầu bằng việc xem xét Coder Terraform template được đề xuất và ba tùy chọn đã mô tả. Một đóng góp trước hết sẽ cần maintainer quyết định về phạm vi, sau đó là một ví dụ được lập tài liệu và xác thực, với vị trí rõ ràng và đường dẫn xác minh.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- docker, kubernetes, terraform
- Lĩnh vực
- documentation, infrastructure
- Loại issue
- Tài liệu
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 25/100