Skip to content

Commit c103059

Browse files
authored
feat: add remote_repo_build_mode envbuilder option (#20)
Adds support for setting the --remote-repo-build-mode Envbuilder option. Defaults to true if not set.
1 parent 1926b0b commit c103059

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

GNUmakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ test-registry: test-registry-container test-images-pull test-images-push
3636
test-registry-container: .registry-cache
3737
if ! curl -fsSL http://localhost:5000/v2/_catalog > /dev/null 2>&1; then \
3838
docker rm -f tfprov-envbuilder-registry && \
39-
docker run -d -p 5000:5000 --name envbuilder-registry --volume $(PWD)/.registry-cache:/var/lib/registry registry:2; \
39+
docker run -d -p 5000:5000 --name tfprov-envbuilder-registry --volume $(PWD)/.registry-cache:/var/lib/registry registry:2; \
4040
fi
4141

4242
# Pulls images referenced in integration tests and pushes them to the local cache.

docs/resources/cached_image.md

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ The cached image resource can be used to retrieve a cached image produced by env
4141
- `git_username` (String) (Envbuilder option) The username to use for Git authentication. This is optional.
4242
- `ignore_paths` (List of String) (Envbuilder option) The comma separated list of paths to ignore when building the workspace.
4343
- `insecure` (Boolean) (Envbuilder option) Bypass TLS verification when cloning and pulling from container registries.
44+
- `remote_repo_build_mode` (Boolean) (Envbuilder option) RemoteRepoBuildMode uses the remote repository as the source of truth when building the image. Enabling this option ignores user changes to local files and they will not be reflected in the image. This can be used to improve cache utilization when multiple users are working on the same repository. (NOTE: The Terraform provider will **always** use remote repo build mode for probing the cache repo.)
4445
- `ssl_cert_base64` (String) (Envbuilder option) The content of an SSL cert file. This is useful for self-signed certificates.
4546
- `verbose` (Boolean) (Envbuilder option) Enable verbose output.
4647
- `workspace_folder` (String) (Envbuilder option) path to the workspace folder that will be built. This is optional.

internal/provider/cached_image_resource.go

+22
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type CachedImageResourceModel struct {
7070
GitUsername types.String `tfsdk:"git_username"`
7171
IgnorePaths types.List `tfsdk:"ignore_paths"`
7272
Insecure types.Bool `tfsdk:"insecure"`
73+
RemoteRepoBuildMode types.Bool `tfsdk:"remote_repo_build_mode"`
7374
SSLCertBase64 types.String `tfsdk:"ssl_cert_base64"`
7475
Verbose types.Bool `tfsdk:"verbose"`
7576
WorkspaceFolder types.String `tfsdk:"workspace_folder"`
@@ -203,6 +204,13 @@ func (r *CachedImageResource) Schema(ctx context.Context, req resource.SchemaReq
203204
MarkdownDescription: "(Envbuilder option) Bypass TLS verification when cloning and pulling from container registries.",
204205
Optional: true,
205206
},
207+
"remote_repo_build_mode": schema.BoolAttribute{
208+
MarkdownDescription: "(Envbuilder option) RemoteRepoBuildMode uses the remote repository as the source of truth when building the image. Enabling this option ignores user changes to local files and they will not be reflected in the image. This can be used to improve cache utilization when multiple users are working on the same repository. (NOTE: The Terraform provider will **always** use remote repo build mode for probing the cache repo.)",
209+
Optional: true,
210+
PlanModifiers: []planmodifier.Bool{
211+
boolplanmodifier.RequiresReplace(),
212+
},
213+
},
206214
"ssl_cert_base64": schema.StringAttribute{
207215
MarkdownDescription: "(Envbuilder option) The content of an SSL cert file. This is useful for self-signed certificates.",
208216
Optional: true,
@@ -330,6 +338,12 @@ func (r *CachedImageResource) Read(ctx context.Context, req resource.ReadRequest
330338
if !data.GitPassword.IsNull() {
331339
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_GIT_PASSWORD", data.GitPassword)
332340
}
341+
// Default to remote build mode.
342+
if data.RemoteRepoBuildMode.IsNull() {
343+
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_REMOTE_REPO_BUILD_MODE", types.BoolValue(true))
344+
} else {
345+
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_REMOTE_REPO_BUILD_MODE", data.RemoteRepoBuildMode)
346+
}
333347

334348
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
335349
}
@@ -379,6 +393,12 @@ func (r *CachedImageResource) Create(ctx context.Context, req resource.CreateReq
379393
if !data.GitPassword.IsNull() {
380394
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_GIT_PASSWORD", data.GitPassword)
381395
}
396+
// Default to remote build mode.
397+
if data.RemoteRepoBuildMode.IsNull() {
398+
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_REMOTE_REPO_BUILD_MODE", types.BoolValue(true))
399+
} else {
400+
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_REMOTE_REPO_BUILD_MODE", data.RemoteRepoBuildMode)
401+
}
382402

383403
// Save data into Terraform state
384404
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
@@ -479,6 +499,8 @@ func (r *CachedImageResource) runCacheProbe(ctx context.Context, data CachedImag
479499
GitPassword: data.GitPassword.ValueString(),
480500
GitSSHPrivateKeyPath: data.GitSSHPrivateKeyPath.ValueString(),
481501
GitHTTPProxyURL: data.GitHTTPProxyURL.ValueString(),
502+
RemoteRepoBuildMode: data.RemoteRepoBuildMode.ValueBool(),
503+
RemoteRepoDir: filepath.Join(tmpDir, "repo"),
482504
SSLCertBase64: data.SSLCertBase64.ValueString(),
483505

484506
// Other options

internal/provider/cached_image_resource_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ func TestAccCachedImageDataSource(t *testing.T) {
9595
resource.TestCheckResourceAttr("envbuilder_cached_image.test", "env.0", "FOO=bar"),
9696
resource.TestCheckResourceAttr("envbuilder_cached_image.test", "env.1", fmt.Sprintf("ENVBUILDER_CACHE_REPO=%s", deps.CacheRepo)),
9797
resource.TestCheckResourceAttr("envbuilder_cached_image.test", "env.2", fmt.Sprintf("ENVBUILDER_GIT_URL=%s", deps.Repo.URL)),
98+
resource.TestCheckResourceAttr("envbuilder_cached_image.test", "env.3", "ENVBUILDER_REMOTE_REPO_BUILD_MODE=true"),
99+
resource.TestCheckNoResourceAttr("envbuilder_cached_image.test", "env.4"),
98100
),
99101
},
100102
// Should produce an empty plan after apply

internal/provider/provider_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ resource "envbuilder_cached_image" "test" {
5757
git_url = {{ quote .Repo.URL }}
5858
git_ssh_private_key_path = {{ quote .Repo.Key }}
5959
verbose = true
60-
workspace_folder = {{ quote .Repo.Dir }}
6160
}`
6261

6362
fm := template.FuncMap{"quote": quote}
@@ -131,10 +130,12 @@ func seedCache(ctx context.Context, t testing.TB, deps testDependencies) {
131130

132131
require.NoError(t, err, "failed to run envbuilder to seed cache")
133132
t.Cleanup(func() {
134-
_ = cli.ContainerRemove(ctx, ctr.ID, container.RemoveOptions{
133+
if err := cli.ContainerRemove(context.Background(), ctr.ID, container.RemoveOptions{
135134
RemoveVolumes: true,
136135
Force: true,
137-
})
136+
}); err != nil {
137+
t.Errorf("removing container: %s", err.Error())
138+
}
138139
})
139140
err = cli.ContainerStart(ctx, ctr.ID, container.StartOptions{})
140141
require.NoError(t, err)

0 commit comments

Comments
 (0)