From fc0e5ebd501c2c10ab7ac3a159b5dc65342295a7 Mon Sep 17 00:00:00 2001 From: Bryan Kneis Date: Thu, 9 Jan 2025 12:25:55 +0000 Subject: [PATCH] PR feedback --- .env | 2 - examples/build-multi-stage/Dockerfile | 2 +- examples/compose/Dockerfile | 2 +- examples/compose/devcontainer.Dockerfile | 31 ++++++++++- pkg/devcontainer/compose.go | 65 ++++++++++++++---------- 5 files changed, 71 insertions(+), 31 deletions(-) delete mode 100644 .env diff --git a/.env b/.env deleted file mode 100644 index 82482f4c7..000000000 --- a/.env +++ /dev/null @@ -1,2 +0,0 @@ -TEST=test -COMPOSE_PROJECT_NAME=test diff --git a/examples/build-multi-stage/Dockerfile b/examples/build-multi-stage/Dockerfile index 7ffa5913a..d21d67690 100644 --- a/examples/build-multi-stage/Dockerfile +++ b/examples/build-multi-stage/Dockerfile @@ -28,4 +28,4 @@ FROM go AS final COPY app /app -RUN echo hello \ No newline at end of file +RUN echo hello diff --git a/examples/compose/Dockerfile b/examples/compose/Dockerfile index f24fbc75a..618365418 100644 --- a/examples/compose/Dockerfile +++ b/examples/compose/Dockerfile @@ -29,4 +29,4 @@ FROM go AS final COPY app /app -RUN echo hello \ No newline at end of file +RUN echo hello diff --git a/examples/compose/devcontainer.Dockerfile b/examples/compose/devcontainer.Dockerfile index 041723c61..d21d67690 100644 --- a/examples/compose/devcontainer.Dockerfile +++ b/examples/compose/devcontainer.Dockerfile @@ -1,2 +1,31 @@ -FROM mcr.microsoft.com/devcontainers/javascript-node +FROM mcr.microsoft.com/devcontainers/go:1.22-bullseye AS go + +ARG TARGETOS +ARG TARGETARCH + +# Install Node.js +RUN \ + curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \ + && apt-get update \ + && apt-get install -y --no-install-recommends nodejs \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +# Set environment variables for Rust +ENV RUSTUP_HOME=/usr/local/rustup \ + CARGO_HOME=/usr/local/cargo \ + PATH=/usr/local/cargo/bin:$PATH \ + RUST_VERSION=1.69.0 + +# Install Protobuf compiler +RUN \ + apt-get update \ + && apt-get install -y --no-install-recommends protobuf-compiler \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +FROM go AS final + +COPY app /app + RUN echo hello diff --git a/pkg/devcontainer/compose.go b/pkg/devcontainer/compose.go index b0bd35dc9..a8a572d3d 100644 --- a/pkg/devcontainer/compose.go +++ b/pkg/devcontainer/compose.go @@ -397,28 +397,12 @@ func (r *runner) startContainer( return containerDetails, nil } -// This extends the build information for docker compose containers -func (r *runner) buildAndExtendDockerCompose( - ctx context.Context, - parsedConfig *config.SubstitutedConfig, - substitutionContext *config.SubstitutionContext, - project *composetypes.Project, - composeHelper *compose.ComposeHelper, - composeService *composetypes.ServiceConfig, - globalArgs []string, -) (string, string, *config.ImageMetadataConfig, string, error) { - var dockerFilePath, dockerfileContents, dockerComposeFilePath string +// prepareComposeBuildInfo modifies a compose project's devcontainer Dockerfile to ensure it can be extended with features +// If an Image is specified instead of a Build, the metadata from the Image is used to populate the build info +func (r *runner) prepareComposeBuildInfo(ctx context.Context, subCtx *config.SubstitutionContext, composeService *composetypes.ServiceConfig, buildTarget string) (*config.ImageBuildInfo, string, string, error) { + var dockerFilePath, dockerfileContents string var imageBuildInfo *config.ImageBuildInfo var err error - - buildImageName := composeService.Image - // If Image is empty then we are building the dev container and use the default name docker-compose uses - if buildImageName == "" { - buildImageName = fmt.Sprintf("%s-%s", project.Name, composeService.Name) - } - buildTarget := "dev_container_auto_added_stage_label" - - // Determine base imageName for generated features build if composeService.Build != nil { // Read Dockerfile if path.IsAbs(composeService.Build.Dockerfile) { @@ -429,7 +413,7 @@ func (r *runner) buildAndExtendDockerCompose( originalDockerfile, err := os.ReadFile(dockerFilePath) if err != nil { - return "", "", nil, "", err + return nil, "", "", err } // Determine build target, if a multi stage build ensure it is valid and modify the Dockerfile if necessary @@ -439,7 +423,7 @@ func (r *runner) buildAndExtendDockerCompose( } else { lastStageName, modifiedDockerfile, err := dockerfile.EnsureDockerfileHasFinalStageName(string(originalDockerfile), config.DockerfileDefaultTarget) if err != nil { - return "", "", nil, "", err + return nil, "", "", err } buildTarget = lastStageName @@ -450,16 +434,45 @@ func (r *runner) buildAndExtendDockerCompose( dockerfileContents = string(originalDockerfile) } } - imageBuildInfo, err = r.getImageBuildInfoFromDockerfile(substitutionContext, string(originalDockerfile), mappingToMap(composeService.Build.Args), originalTarget) + imageBuildInfo, err = r.getImageBuildInfoFromDockerfile(subCtx, string(originalDockerfile), mappingToMap(composeService.Build.Args), originalTarget) if err != nil { - return "", "", nil, "", err + return nil, "", "", err } } else { - imageBuildInfo, err = r.getImageBuildInfoFromImage(ctx, substitutionContext, composeService.Image) + imageBuildInfo, err = r.getImageBuildInfoFromImage(ctx, subCtx, composeService.Image) if err != nil { - return "", "", nil, "", err + return nil, "", "", err } } + return imageBuildInfo, dockerfileContents, buildTarget, nil +} + +// This extends the build information for docker compose containers +func (r *runner) buildAndExtendDockerCompose( + ctx context.Context, + parsedConfig *config.SubstitutedConfig, + substitutionContext *config.SubstitutionContext, + project *composetypes.Project, + composeHelper *compose.ComposeHelper, + composeService *composetypes.ServiceConfig, + globalArgs []string, +) (string, string, *config.ImageMetadataConfig, string, error) { + var dockerFilePath, dockerfileContents, dockerComposeFilePath string + var imageBuildInfo *config.ImageBuildInfo + var err error + + buildImageName := composeService.Image + // If Image is empty then we are building the dev container and use the default name docker-compose uses + if buildImageName == "" { + buildImageName = fmt.Sprintf("%s-%s", project.Name, composeService.Name) + } + buildTarget := "dev_container_auto_added_stage_label" + + // Determine base imageName for generated features build + imageBuildInfo, dockerfileContents, buildTarget, err = r.prepareComposeBuildInfo(ctx, substitutionContext, composeService, buildTarget) + if err != nil { + return "", "", nil, "", err + } extendImageBuildInfo, err := feature.GetExtendedBuildInfo(substitutionContext, imageBuildInfo, buildTarget, parsedConfig, r.Log, false) if err != nil {