Skip to content

Commit

Permalink
move tar writer close
Browse files Browse the repository at this point in the history
  • Loading branch information
zulkhair committed Aug 8, 2024
1 parent 2306458 commit 8628e29
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
37 changes: 29 additions & 8 deletions common/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"

"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -291,9 +292,9 @@ func pullImageIfNotExists(ctx context.Context, cli *client.Client, imageName str

func buildImage(ctx context.Context, cli *client.Client, dockerfile Dockerfile, imageName string) error {
fmt.Println("Building image ", imageName)

buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
defer tw.Close()

// Add the Dockerfile to the tar archive
header := &tar.Header{
Expand All @@ -312,18 +313,32 @@ func buildImage(ctx context.Context, cli *client.Client, dockerfile Dockerfile,
return err
}

if err := tw.Close(); err != nil {
return eris.Wrap(err, "Failed to close tar writer")

Check warning on line 317 in common/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

common/docker/docker.go#L317

Added line #L317 was not covered by tests
}

// Read the tar archive
tarReader := bytes.NewReader(buf.Bytes())
buildContext := io.NopCloser(bytes.NewReader(buf.Bytes()))
defer buildContext.Close()

tarFile, err := os.Create("context.tar")
if err != nil {
return err

Check warning on line 326 in common/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

common/docker/docker.go#L326

Added line #L326 was not covered by tests
}
defer tarFile.Close()
_, err = buf.WriteTo(tarFile)
if err != nil {
return err

Check warning on line 331 in common/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

common/docker/docker.go#L331

Added line #L331 was not covered by tests
}

buildOptions := types.ImageBuildOptions{
Dockerfile: "Dockerfile",
Tags: []string{imageName},
Target: dockerfile.Target,
Version: types.BuilderBuildKit,
}

// Build the image
buildResponse, err := cli.ImageBuild(ctx, tarReader, buildOptions)
buildResponse, err := cli.ImageBuild(ctx, buildContext, buildOptions)
if err != nil {
return err
}
Expand Down Expand Up @@ -451,17 +466,23 @@ func addFileToTarWriter(baseDir string, tw *tar.Writer) error {
return err
}

// Create a tar header for the file or directory
header, err := tar.FileInfoHeader(info, "")
// Check if the file is world.toml or inside the cardinal directory
relPath, err := filepath.Rel(baseDir, path)
if err != nil {
return err
}
if !(info.Name() == "world.toml" || strings.HasPrefix(filepath.ToSlash(relPath), "cardinal/")) {
logger.Debugf("Skipping %s addition to tar", info.Name())
return nil
}

// Adjust the header name to be relative to the baseDir
relPath, err := filepath.Rel(baseDir, path)
// Create a tar header for the file or directory
header, err := tar.FileInfoHeader(info, "")
if err != nil {
return err
}

// Adjust the header name to be relative to the baseDir
header.Name = filepath.ToSlash(relPath)

// Write the header to the tar writer
Expand Down
4 changes: 2 additions & 2 deletions common/docker/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
# Copy the rest of the source code and build the binary
COPY /cardinal ./
RUN --mount=type=cache,target="/root/.cache/go-build" go build -v -o /go/bin/app
RUN go build -v -o /go/bin/app
################################
# Runtime Image - Normal
Expand Down Expand Up @@ -57,7 +57,7 @@ const (
# Copy the rest of the source code and build the binary with debugging symbols
COPY /cardinal ./
RUN --mount=type=cache,target="/root/.cache/go-build" go build -gcflags="all=-N -l" -v -o /usr/bin/app
RUN go build -gcflags="all=-N -l" -v -o /usr/bin/app
# Copy world.toml to the image
COPY world.toml world.toml
Expand Down

0 comments on commit 8628e29

Please sign in to comment.