Skip to content

Commit c9e7cb8

Browse files
authored
implement first pass at cached image data source (#3)
implements envbuilder_cached_image data source
1 parent 1a49822 commit c9e7cb8

File tree

10 files changed

+1697
-146
lines changed

10 files changed

+1697
-146
lines changed

.github/workflows/test.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ jobs:
6565
matrix:
6666
# list whatever Terraform versions here you would like to support
6767
terraform:
68-
- '1.0.*'
69-
- '1.1.*'
70-
- '1.2.*'
71-
- '1.3.*'
72-
- '1.4.*'
68+
# TODO: test with more terraform versions. For now, testing with latest.
69+
# - "1.5.*"
70+
# - "1.6.*"
71+
# - "1.7.*"
72+
# - "1.8.*"
73+
- "1.9.*"
7374
steps:
7475
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
7576
- uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1
@@ -81,6 +82,7 @@ jobs:
8182
terraform_version: ${{ matrix.terraform }}
8283
terraform_wrapper: false
8384
- run: go mod download
85+
- run: make test-registry
8486
- env:
8587
TF_ACC: "1"
8688
run: go test -v -cover ./internal/provider/

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ website/vendor
3434
# Keep windows files with windows line endings
3535
*.winfile eol=crlf
3636

37-
terraform-provider-envbuilder
37+
terraform-provider-envbuilder
38+
.registry-cache/

GNUmakefile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,34 @@ build: terraform-provider-envbuilder
1717

1818
terraform-provider-envbuilder: internal/provider/*.go main.go
1919
CGO_ENABLED=0 go build .
20+
21+
.PHONY: update-envbuilder-version
22+
update-envbuilder-version:
23+
go get github.com/coder/envbuilder@main
24+
go mod tidy
25+
26+
# Starts a local Docker registry on port 5000 with a local disk cache.
27+
.PHONY: test-registry
28+
test-registry: test-registry-container test-images-pull test-images-push
29+
30+
.PHONY: test-registry-container
31+
test-registry-container: .registry-cache
32+
if ! curl -fsSL http://localhost:5000/v2/_catalog > /dev/null 2>&1; then \
33+
docker rm -f tfprov-envbuilder-registry && \
34+
docker run -d -p 5000:5000 --name envbuilder-registry --volume $(PWD)/.registry-cache:/var/lib/registry registry:2; \
35+
fi
36+
37+
# Pulls images referenced in integration tests and pushes them to the local cache.
38+
.PHONY: test-images-push
39+
test-images-push: .registry-cache/docker/registry/v2/repositories/test-ubuntu
40+
41+
.PHONY: test-images-pull
42+
test-images-pull:
43+
docker pull ubuntu:latest
44+
docker tag ubuntu:latest localhost:5000/test-ubuntu:latest
45+
46+
.registry-cache:
47+
mkdir -p .registry-cache && chmod -R ag+w .registry-cache
48+
49+
.registry-cache/docker/registry/v2/repositories/test-ubuntu:
50+
docker push localhost:5000/test-ubuntu:latest

docs/data-sources/cached_image.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
page_title: "envbuilder_cached_image Data Source - envbuilder"
44
subcategory: ""
55
description: |-
6-
The cached image data source can be used to retrieve a cached image produced by envbuilder.
6+
The cached image data source can be used to retrieve a cached image produced by envbuilder. Reading from this data source will clone the specified Git repository, read a Devcontainer specification or Dockerfile, and check for its presence in the provided cache repo.
77
---
88

99
# envbuilder_cached_image (Data Source)
1010

11-
The cached image data source can be used to retrieve a cached image produced by envbuilder.
11+
The cached image data source can be used to retrieve a cached image produced by envbuilder. Reading from this data source will clone the specified Git repository, read a Devcontainer specification or Dockerfile, and check for its presence in the provided cache repo.
1212

1313
## Example Usage
1414

@@ -33,20 +33,36 @@ resource "docker_container" "container" {
3333

3434
### Required
3535

36-
- `builder_image` (String) The builder image URL to use if the cache does not exist.
37-
- `cache_repo` (String) The name of the container registry to fetch the cache image from.
38-
- `git_url` (String) The URL of a Git repository containing a Devcontainer or Docker image to clone.
36+
- `builder_image` (String) The envbuilder image to use if the cached version is not found.
37+
- `cache_repo` (String) (Envbuilder option) The name of the container registry to fetch the cache image from.
38+
- `git_url` (String) (Envbuilder option) The URL of a Git repository containing a Devcontainer or Docker image to clone.
3939

4040
### Optional
4141

42-
- `cache_ttl_days` (Number) The number of days to use cached layers before expiring them. Defaults to 7 days.
43-
- `extra_env` (Map of String) Extra environment variables to set for the container. This may include evbuilder options.
44-
- `git_password` (String, Sensitive) The password to use for Git authentication. This is optional.
45-
- `git_username` (String) The username to use for Git authentication. This is optional.
42+
- `base_image_cache_dir` (String) (Envbuilder option) The path to a directory where the base image can be found. This should be a read-only directory solely mounted for the purpose of caching the base image.
43+
- `build_context_path` (String) (Envbuilder option) Can be specified when a DockerfilePath is specified outside the base WorkspaceFolder. This path MUST be relative to the WorkspaceFolder path into which the repo is cloned.
44+
- `cache_ttl_days` (Number) (Envbuilder option) The number of days to use cached layers before expiring them. Defaults to 7 days.
45+
- `devcontainer_dir` (String) (Envbuilder option) The path to the folder containing the devcontainer.json file that will be used to build the workspace and can either be an absolute path or a path relative to the workspace folder. If not provided, defaults to `.devcontainer`.
46+
- `devcontainer_json_path` (String) (Envbuilder option) The path to a devcontainer.json file that is either an absolute path or a path relative to DevcontainerDir. This can be used in cases where one wants to substitute an edited devcontainer.json file for the one that exists in the repo.
47+
- `docker_config_base64` (String) (Envbuilder option) The base64 encoded Docker config file that will be used to pull images from private container registries.
48+
- `dockerfile_path` (String) (Envbuilder option) The relative path to the Dockerfile that will be used to build the workspace. This is an alternative to using a devcontainer that some might find simpler.
49+
- `exit_on_build_failure` (Boolean) (Envbuilder option) Terminates upon a build failure. This is handy when preferring the FALLBACK_IMAGE in cases where no devcontainer.json or image is provided. However, it ensures that the container stops if the build process encounters an error.
50+
- `extra_env` (Map of String) Extra environment variables to set for the container. This may include envbuilder options.
51+
- `fallback_image` (String) (Envbuilder option) Specifies an alternative image to use when neither an image is declared in the devcontainer.json file nor a Dockerfile is present. If there's a build failure (from a faulty Dockerfile) or a misconfiguration, this image will be the substitute. Set ExitOnBuildFailure to true to halt the container if the build faces an issue.
52+
- `git_clone_depth` (Number) (Envbuilder option) The depth to use when cloning the Git repository.
53+
- `git_clone_single_branch` (Boolean) (Envbuilder option) Clone only a single branch of the Git repository.
54+
- `git_http_proxy_url` (String) (Envbuilder option) The URL for the HTTP proxy. This is optional.
55+
- `git_password` (String, Sensitive) (Envbuilder option) The password to use for Git authentication. This is optional.
56+
- `git_ssh_private_key_path` (String) (Envbuilder option) Path to an SSH private key to be used for Git authentication.
57+
- `git_username` (String) (Envbuilder option) The username to use for Git authentication. This is optional.
58+
- `ignore_paths` (List of String) (Envbuilder option) The comma separated list of paths to ignore when building the workspace.
59+
- `insecure` (Boolean) (Envbuilder option) Bypass TLS verification when cloning and pulling from container registries.
60+
- `ssl_cert_base64` (String) (Envbuilder option) The content of an SSL cert file. This is useful for self-signed certificates.
61+
- `verbose` (Boolean) (Envbuilder option) Enable verbose output.
4662

4763
### Read-Only
4864

4965
- `env` (List of String) Computed envbuilder configuration to be set for the container.
5066
- `exists` (Boolean) Whether the cached image was exists or not for the given config.
51-
- `id` (String) Cached image identifier
52-
- `image` (String) Outputs the cached image URL if it exists, otherwise the builder image URL is output instead.
67+
- `id` (String) Cached image identifier. This will generally be the image's SHA256 digest.
68+
- `image` (String) Outputs the cached image repo@digest if it exists, and builder image otherwise.

0 commit comments

Comments
 (0)