Skip to content

Commit 48b94fa

Browse files
authored
Merge branch 'main' into cj/385-push-image-fail
2 parents 84f066f + aba2f46 commit 48b94fa

File tree

7 files changed

+102
-102
lines changed

7 files changed

+102
-102
lines changed

devcontainer/devcontainer_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/stretchr/testify/require"
2323
)
2424

25-
const magicDir = "/.envbuilder"
25+
const workingDir = "/.envbuilder"
2626

2727
func stubLookupEnv(string) (string, bool) {
2828
return "", false
@@ -98,7 +98,7 @@ func TestCompileWithFeatures(t *testing.T) {
9898
featureTwoDir := fmt.Sprintf("/.envbuilder/features/two-%x", featureTwoMD5[:4])
9999

100100
t.Run("WithoutBuildContexts", func(t *testing.T) {
101-
params, err := dc.Compile(fs, "", magicDir, "", "", false, stubLookupEnv)
101+
params, err := dc.Compile(fs, "", workingDir, "", "", false, stubLookupEnv)
102102
require.NoError(t, err)
103103

104104
require.Equal(t, `FROM localhost:5000/envbuilder-test-codercom-code-server:latest
@@ -116,7 +116,7 @@ USER 1000`, params.DockerfileContent)
116116
})
117117

118118
t.Run("WithBuildContexts", func(t *testing.T) {
119-
params, err := dc.Compile(fs, "", magicDir, "", "", true, stubLookupEnv)
119+
params, err := dc.Compile(fs, "", workingDir, "", "", true, stubLookupEnv)
120120
require.NoError(t, err)
121121

122122
registryHost := strings.TrimPrefix(registry, "http://")
@@ -155,10 +155,10 @@ func TestCompileDevContainer(t *testing.T) {
155155
dc := &devcontainer.Spec{
156156
Image: "localhost:5000/envbuilder-test-ubuntu:latest",
157157
}
158-
params, err := dc.Compile(fs, "", magicDir, "", "", false, stubLookupEnv)
158+
params, err := dc.Compile(fs, "", workingDir, "", "", false, stubLookupEnv)
159159
require.NoError(t, err)
160-
require.Equal(t, filepath.Join(magicDir, "Dockerfile"), params.DockerfilePath)
161-
require.Equal(t, magicDir, params.BuildContext)
160+
require.Equal(t, filepath.Join(workingDir, "Dockerfile"), params.DockerfilePath)
161+
require.Equal(t, workingDir, params.BuildContext)
162162
})
163163
t.Run("WithBuild", func(t *testing.T) {
164164
t.Parallel()
@@ -181,7 +181,7 @@ func TestCompileDevContainer(t *testing.T) {
181181
_, err = io.WriteString(file, "FROM localhost:5000/envbuilder-test-ubuntu:latest")
182182
require.NoError(t, err)
183183
_ = file.Close()
184-
params, err := dc.Compile(fs, dcDir, magicDir, "", "/var/workspace", false, stubLookupEnv)
184+
params, err := dc.Compile(fs, dcDir, workingDir, "", "/var/workspace", false, stubLookupEnv)
185185
require.NoError(t, err)
186186
require.Equal(t, "ARG1=value1", params.BuildArgs[0])
187187
require.Equal(t, "ARG2=workspace", params.BuildArgs[1])

envbuilder.go

+34-34
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
"github.com/GoogleContainerTools/kaniko/pkg/util"
3636
"github.com/coder/envbuilder/devcontainer"
3737
"github.com/coder/envbuilder/internal/ebutil"
38-
"github.com/coder/envbuilder/internal/magicdir"
38+
"github.com/coder/envbuilder/internal/workingdir"
3939
"github.com/coder/envbuilder/log"
4040
"github.com/containerd/platforms"
4141
"github.com/distribution/distribution/v3/configuration"
@@ -120,7 +120,7 @@ func Run(ctx context.Context, opts options.Options, preExec ...func()) error {
120120
func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) error {
121121
defer options.UnsetEnv()
122122

123-
magicDir := magicdir.At(opts.MagicDirBase)
123+
workingDir := workingdir.At(opts.MagicDirBase)
124124

125125
stageNumber := 0
126126
startStage := func(format string, args ...any) func(format string, args ...any) {
@@ -154,7 +154,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
154154

155155
opts.Logger(log.LevelInfo, "%s %s - Build development environments from repositories in a container", newColor(color.Bold).Sprintf("envbuilder"), buildinfo.Version())
156156

157-
cleanupDockerConfigJSON, err := initDockerConfigJSON(opts.Logger, magicDir, opts.DockerConfigBase64)
157+
cleanupDockerConfigJSON, err := initDockerConfigJSON(opts.Logger, workingDir, opts.DockerConfigBase64)
158158
if err != nil {
159159
return err
160160
}
@@ -168,8 +168,8 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
168168
ContainerEnv: make(map[string]string),
169169
RemoteEnv: make(map[string]string),
170170
}
171-
if fileExists(opts.Filesystem, magicDir.Image()) {
172-
if err = parseMagicImageFile(opts.Filesystem, magicDir.Image(), &runtimeData); err != nil {
171+
if fileExists(opts.Filesystem, workingDir.Image()) {
172+
if err = parseMagicImageFile(opts.Filesystem, workingDir.Image(), &runtimeData); err != nil {
173173
return fmt.Errorf("parse magic image file: %w", err)
174174
}
175175
runtimeData.Image = true
@@ -186,7 +186,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
186186
opts.ExportEnvFile = ""
187187
}
188188
}
189-
runtimeData.Built = fileExists(opts.Filesystem, magicDir.Built())
189+
runtimeData.Built = fileExists(opts.Filesystem, workingDir.Built())
190190

191191
buildTimeWorkspaceFolder := opts.WorkspaceFolder
192192
var fallbackErr error
@@ -233,7 +233,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
233233
if err != nil {
234234
return fmt.Errorf("git clone options: %w", err)
235235
}
236-
cloneOpts.Path = magicDir.Join("repo")
236+
cloneOpts.Path = workingDir.Join("repo")
237237

238238
endStage := startStage("📦 Remote repo build mode enabled, cloning %s to %s for build context...",
239239
newColor(color.FgCyan).Sprintf(opts.GitURL),
@@ -259,7 +259,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
259259

260260
if !runtimeData.Image {
261261
defaultBuildParams := func() (*devcontainer.Compiled, error) {
262-
dockerfile := magicDir.Join("Dockerfile")
262+
dockerfile := workingDir.Join("Dockerfile")
263263
file, err := opts.Filesystem.OpenFile(dockerfile, os.O_CREATE|os.O_WRONLY, 0o644)
264264
if err != nil {
265265
return nil, err
@@ -281,7 +281,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
281281
return &devcontainer.Compiled{
282282
DockerfilePath: dockerfile,
283283
DockerfileContent: content,
284-
BuildContext: magicDir.Path(),
284+
BuildContext: workingDir.Path(),
285285
}, nil
286286
}
287287

@@ -318,7 +318,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
318318
opts.Logger(log.LevelInfo, "No Dockerfile or image specified; falling back to the default image...")
319319
fallbackDockerfile = defaultParams.DockerfilePath
320320
}
321-
buildParams, err = devContainer.Compile(opts.Filesystem, devcontainerDir, magicDir.Path(), fallbackDockerfile, opts.WorkspaceFolder, false, os.LookupEnv)
321+
buildParams, err = devContainer.Compile(opts.Filesystem, devcontainerDir, workingDir.Path(), fallbackDockerfile, opts.WorkspaceFolder, false, os.LookupEnv)
322322
if err != nil {
323323
return fmt.Errorf("compile devcontainer.json: %w", err)
324324
}
@@ -393,7 +393,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
393393
// So we add them to the default ignore list. See:
394394
// https://github.com/GoogleContainerTools/kaniko/blob/63be4990ca5a60bdf06ddc4d10aa4eca0c0bc714/cmd/executor/cmd/root.go#L136
395395
ignorePaths := append([]string{
396-
magicDir.Path(),
396+
workingDir.Path(),
397397
opts.WorkspaceFolder,
398398
// See: https://github.com/coder/envbuilder/issues/37
399399
"/etc/resolv.conf",
@@ -421,18 +421,18 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
421421
if err := util.AddAllowedPathToDefaultIgnoreList(opts.BinaryPath); err != nil {
422422
return fmt.Errorf("add envbuilder binary to ignore list: %w", err)
423423
}
424-
if err := util.AddAllowedPathToDefaultIgnoreList(magicDir.Image()); err != nil {
424+
if err := util.AddAllowedPathToDefaultIgnoreList(workingDir.Image()); err != nil {
425425
return fmt.Errorf("add magic image file to ignore list: %w", err)
426426
}
427-
if err := util.AddAllowedPathToDefaultIgnoreList(magicDir.Features()); err != nil {
427+
if err := util.AddAllowedPathToDefaultIgnoreList(workingDir.Features()); err != nil {
428428
return fmt.Errorf("add features to ignore list: %w", err)
429429
}
430-
magicTempDir := magicdir.At(buildParams.BuildContext, magicdir.TempDir)
430+
magicTempDir := workingdir.At(buildParams.BuildContext, workingdir.TempDir)
431431
if err := opts.Filesystem.MkdirAll(magicTempDir.Path(), 0o755); err != nil {
432432
return fmt.Errorf("create magic temp dir in build context: %w", err)
433433
}
434434
// Add the magic directives that embed the binary into the built image.
435-
buildParams.DockerfileContent += magicdir.Directives
435+
buildParams.DockerfileContent += workingdir.Directives
436436

437437
envbuilderBinDest := filepath.Join(magicTempDir.Path(), "envbuilder")
438438
magicImageDest := magicTempDir.Image()
@@ -467,7 +467,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
467467
}
468468

469469
// temp move of all ro mounts
470-
tempRemountDest := magicDir.Join("mnt")
470+
tempRemountDest := workingDir.Join("mnt")
471471
// ignorePrefixes is a superset of ignorePaths that we pass to kaniko's
472472
// IgnoreList.
473473
ignorePrefixes := append([]string{"/dev", "/proc", "/sys"}, ignorePaths...)
@@ -847,7 +847,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
847847
// Create the magic file to indicate that this build
848848
// has already been ran before!
849849
if !runtimeData.Built {
850-
file, err := opts.Filesystem.Create(magicDir.Built())
850+
file, err := opts.Filesystem.Create(workingDir.Built())
851851
if err != nil {
852852
return fmt.Errorf("create magic file: %w", err)
853853
}
@@ -866,7 +866,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
866866
opts.Logger(log.LevelInfo, "=== Running the setup command %q as the root user...", opts.SetupScript)
867867

868868
envKey := "ENVBUILDER_ENV"
869-
envFile := magicDir.Join("environ")
869+
envFile := workingDir.Join("environ")
870870
file, err := opts.Filesystem.Create(envFile)
871871
if err != nil {
872872
return fmt.Errorf("create environ file: %w", err)
@@ -964,7 +964,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
964964
return nil, fmt.Errorf("--cache-repo must be set when using --get-cached-image")
965965
}
966966

967-
magicDir := magicdir.At(opts.MagicDirBase)
967+
workingDir := workingdir.At(opts.MagicDirBase)
968968

969969
stageNumber := 0
970970
startStage := func(format string, args ...any) func(format string, args ...any) {
@@ -980,7 +980,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
980980

981981
opts.Logger(log.LevelInfo, "%s %s - Build development environments from repositories in a container", newColor(color.Bold).Sprintf("envbuilder"), buildinfo.Version())
982982

983-
cleanupDockerConfigJSON, err := initDockerConfigJSON(opts.Logger, magicDir, opts.DockerConfigBase64)
983+
cleanupDockerConfigJSON, err := initDockerConfigJSON(opts.Logger, workingDir, opts.DockerConfigBase64)
984984
if err != nil {
985985
return nil, err
986986
}
@@ -1033,7 +1033,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
10331033
if err != nil {
10341034
return nil, fmt.Errorf("git clone options: %w", err)
10351035
}
1036-
cloneOpts.Path = magicDir.Join("repo")
1036+
cloneOpts.Path = workingDir.Join("repo")
10371037

10381038
endStage := startStage("📦 Remote repo build mode enabled, cloning %s to %s for build context...",
10391039
newColor(color.FgCyan).Sprintf(opts.GitURL),
@@ -1058,7 +1058,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
10581058
}
10591059

10601060
defaultBuildParams := func() (*devcontainer.Compiled, error) {
1061-
dockerfile := magicDir.Join("Dockerfile")
1061+
dockerfile := workingDir.Join("Dockerfile")
10621062
file, err := opts.Filesystem.OpenFile(dockerfile, os.O_CREATE|os.O_WRONLY, 0o644)
10631063
if err != nil {
10641064
return nil, err
@@ -1080,7 +1080,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
10801080
return &devcontainer.Compiled{
10811081
DockerfilePath: dockerfile,
10821082
DockerfileContent: content,
1083-
BuildContext: magicDir.Path(),
1083+
BuildContext: workingDir.Path(),
10841084
}, nil
10851085
}
10861086

@@ -1120,7 +1120,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
11201120
opts.Logger(log.LevelInfo, "No Dockerfile or image specified; falling back to the default image...")
11211121
fallbackDockerfile = defaultParams.DockerfilePath
11221122
}
1123-
buildParams, err = devContainer.Compile(opts.Filesystem, devcontainerDir, magicDir.Path(), fallbackDockerfile, opts.WorkspaceFolder, false, os.LookupEnv)
1123+
buildParams, err = devContainer.Compile(opts.Filesystem, devcontainerDir, workingDir.Path(), fallbackDockerfile, opts.WorkspaceFolder, false, os.LookupEnv)
11241124
if err != nil {
11251125
return nil, fmt.Errorf("compile devcontainer.json: %w", err)
11261126
}
@@ -1186,7 +1186,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
11861186
// So we add them to the default ignore list. See:
11871187
// https://github.com/GoogleContainerTools/kaniko/blob/63be4990ca5a60bdf06ddc4d10aa4eca0c0bc714/cmd/executor/cmd/root.go#L136
11881188
ignorePaths := append([]string{
1189-
magicDir.Path(),
1189+
workingDir.Path(),
11901190
opts.WorkspaceFolder,
11911191
// See: https://github.com/coder/envbuilder/issues/37
11921192
"/etc/resolv.conf",
@@ -1209,10 +1209,10 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
12091209
// build via executor.RunCacheProbe we need to have the *exact* copy of the
12101210
// envbuilder binary available used to build the image and we also need to
12111211
// add the magic directives to the Dockerfile content.
1212-
// MAGICDIR
1213-
buildParams.DockerfileContent += magicdir.Directives
1212+
// WORKINGDIR
1213+
buildParams.DockerfileContent += workingdir.Directives
12141214

1215-
magicTempDir := filepath.Join(buildParams.BuildContext, magicdir.TempDir)
1215+
magicTempDir := filepath.Join(buildParams.BuildContext, workingdir.TempDir)
12161216
if err := opts.Filesystem.MkdirAll(magicTempDir, 0o755); err != nil {
12171217
return nil, fmt.Errorf("create magic temp dir in build context: %w", err)
12181218
}
@@ -1548,11 +1548,11 @@ func maybeDeleteFilesystem(logger log.Func, force bool) error {
15481548
// We always expect the magic directory to be set to the default, signifying that
15491549
// the user is running envbuilder in a container.
15501550
// If this is set to anything else we should bail out to prevent accidental data loss.
1551-
// defaultMagicDir := magicdir.MagicDir("")
1551+
// defaultWorkingDir := workingdir.WorkingDir("")
15521552
kanikoDir, ok := os.LookupEnv("KANIKO_DIR")
1553-
if !ok || strings.TrimSpace(kanikoDir) != magicdir.Default.Path() {
1553+
if !ok || strings.TrimSpace(kanikoDir) != workingdir.Default.Path() {
15541554
if !force {
1555-
logger(log.LevelError, "KANIKO_DIR is not set to %s. Bailing!\n", magicdir.Default.Path())
1555+
logger(log.LevelError, "KANIKO_DIR is not set to %s. Bailing!\n", workingdir.Default.Path())
15561556
logger(log.LevelError, "To bypass this check, set FORCE_SAFE=true.")
15571557
return errors.New("safety check failed")
15581558
}
@@ -1629,13 +1629,13 @@ func parseMagicImageFile(fs billy.Filesystem, path string, v any) error {
16291629
return nil
16301630
}
16311631

1632-
func initDockerConfigJSON(logf log.Func, magicDir magicdir.MagicDir, dockerConfigBase64 string) (func() error, error) {
1632+
func initDockerConfigJSON(logf log.Func, workingDir workingdir.WorkingDir, dockerConfigBase64 string) (func() error, error) {
16331633
var cleanupOnce sync.Once
16341634
noop := func() error { return nil }
16351635
if dockerConfigBase64 == "" {
16361636
return noop, nil
16371637
}
1638-
cfgPath := magicDir.Join("config.json")
1638+
cfgPath := workingDir.Join("config.json")
16391639
decoded, err := base64.StdEncoding.DecodeString(dockerConfigBase64)
16401640
if err != nil {
16411641
return noop, fmt.Errorf("decode docker config: %w", err)
@@ -1658,7 +1658,7 @@ func initDockerConfigJSON(logf log.Func, magicDir magicdir.MagicDir, dockerConfi
16581658
}
16591659
logf(log.LevelInfo, "Wrote Docker config JSON to %s", cfgPath)
16601660
oldDockerConfig := os.Getenv("DOCKER_CONFIG")
1661-
_ = os.Setenv("DOCKER_CONFIG", magicDir.Path())
1661+
_ = os.Setenv("DOCKER_CONFIG", workingDir.Path())
16621662
newDockerConfig := os.Getenv("DOCKER_CONFIG")
16631663
logf(log.LevelInfo, "Set DOCKER_CONFIG to %s", newDockerConfig)
16641664
cleanup := func() error {

integration/integration_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"github.com/coder/coder/v2/codersdk/agentsdk"
2828
"github.com/coder/envbuilder"
2929
"github.com/coder/envbuilder/devcontainer/features"
30-
"github.com/coder/envbuilder/internal/magicdir"
30+
"github.com/coder/envbuilder/internal/workingdir"
3131
"github.com/coder/envbuilder/options"
3232
"github.com/coder/envbuilder/testutil/gittest"
3333
"github.com/coder/envbuilder/testutil/mwtest"
@@ -506,7 +506,7 @@ func TestBuildFromDockerfile(t *testing.T) {
506506
require.Equal(t, "hello", strings.TrimSpace(output))
507507

508508
// Verify that the Docker configuration secret file is removed
509-
configJSONContainerPath := magicdir.Default.Join("config.json")
509+
configJSONContainerPath := workingdir.Default.Join("config.json")
510510
output = execContainer(t, ctr, "stat "+configJSONContainerPath)
511511
require.Contains(t, output, "No such file or directory")
512512
}

internal/magicdir/magicdir_internal_test.go

-38
This file was deleted.

0 commit comments

Comments
 (0)