Skip to content

Commit

Permalink
Separate TTY and tar output in nerdctl build with tty
Browse files Browse the repository at this point in the history
    - Write buildctl output to terminal and tarball to file

    Signed-off-by: Christine Murimi <[email protected]>
  • Loading branch information
TinaMor committed Oct 28, 2024
1 parent b17673d commit 09ad9c7
Showing 1 changed file with 90 additions and 32 deletions.
122 changes: 90 additions & 32 deletions pkg/cmd/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,22 @@ func (p platformParser) DefaultSpec() platforms.Platform {
}

func Build(ctx context.Context, client *containerd.Client, options types.BuilderBuildOptions) error {
buildctlBinary, buildctlArgs, needsLoading, metaFile, tags, cleanup, err := generateBuildctlArgs(ctx, client, options)
buildCtlArgs, err := generateBuildctlArgs(ctx, client, options)
if err != nil {
return err
}
if cleanup != nil {
defer cleanup()
if buildCtlArgs.Cleanup != nil {
defer buildCtlArgs.Cleanup()
}

buildctlBinary := buildCtlArgs.BuildctlBinary
buildctlArgs := buildCtlArgs.BuildctlArgs

log.L.Debugf("running %s %v", buildctlBinary, buildctlArgs)
buildctlCmd := exec.Command(buildctlBinary, buildctlArgs...)
buildctlCmd.Env = os.Environ()

needsLoading := buildCtlArgs.NeedsLoading
var buildctlStdout io.Reader
if needsLoading {
buildctlStdout, err = buildctlCmd.StdoutPipe()
Expand All @@ -95,7 +99,21 @@ func Build(ctx context.Context, client *containerd.Client, options types.Builder
if err != nil {
return err
}
if err = loadImage(ctx, buildctlStdout, options.GOptions.Namespace, options.GOptions.Address, options.GOptions.Snapshotter, options.Stdout, platMC, options.Quiet); err != nil {

// Write buildctl output to stdout
if _, err := io.Copy(os.Stdout, buildctlStdout); err != nil {
return err
}

// Open the tar file
reader, err := os.Open(buildCtlArgs.DestFile)
if err != nil {
return fmt.Errorf("Failed to open file destination file: %v", err)

Check failure on line 111 in pkg/cmd/builder/build.go

View workflow job for this annotation

GitHub Actions / go | windows |

ST1005: error strings should not be capitalized (stylecheck)
}
defer reader.Close()

// Load the image into the containerd image store
if err = loadImage(ctx, reader, options.GOptions.Namespace, options.GOptions.Address, options.GOptions.Snapshotter, options.Stdout, platMC, options.Quiet); err != nil {
return err
}
}
Expand All @@ -105,7 +123,7 @@ func Build(ctx context.Context, client *containerd.Client, options types.Builder
}

if options.IidFile != "" {
id, err := getDigestFromMetaFile(metaFile)
id, err := getDigestFromMetaFile(buildCtlArgs.MetaFile)
if err != nil {
return err
}
Expand All @@ -114,6 +132,7 @@ func Build(ctx context.Context, client *containerd.Client, options types.Builder
}
}

tags := buildCtlArgs.Tags
if len(tags) > 1 {
log.L.Debug("Found more than 1 tag")
imageService := client.ImageService()
Expand Down Expand Up @@ -160,10 +179,15 @@ func loadImage(ctx context.Context, in io.Reader, namespace, address, snapshotte
client.Close()
}()
r := &readCounter{Reader: in}
imgs, err := client.Import(ctx, r, containerd.WithDigestRef(archive.DigestTranslator(snapshotter)), containerd.WithSkipDigestRef(func(name string) bool { return name != "" }), containerd.WithImportPlatform(platMC))
imgs, err := client.Import(ctx, r,
containerd.WithDigestRef(archive.DigestTranslator(snapshotter)),
containerd.WithSkipDigestRef(func(name string) bool { return name != "" }),
containerd.WithImportPlatform(platMC),
)
if err != nil {
if r.N == 0 {
// Avoid confusing "unrecognized image format"
log.L.Debugf("no image was built. error: %v", err)
return errors.New("no image was built")
}
if errors.Is(err, images.ErrEmptyWalk) {
Expand Down Expand Up @@ -192,23 +216,39 @@ func loadImage(ctx context.Context, in io.Reader, namespace, address, snapshotte
return nil
}

func generateBuildctlArgs(ctx context.Context, client *containerd.Client, options types.BuilderBuildOptions) (buildCtlBinary string,
buildctlArgs []string, needsLoading bool, metaFile string, tags []string, cleanup func(), err error) {
type BuildctlArgsResult struct {
BuildctlBinary string
BuildctlArgs []string
NeedsLoading bool
MetaFile string
DestFile string
Tags []string
Cleanup func()
}

func generateBuildctlArgs(ctx context.Context, client *containerd.Client, options types.BuilderBuildOptions) (result BuildctlArgsResult, err error) {
buildctlBinary, err := buildkitutil.BuildctlBinary()
if err != nil {
return "", nil, false, "", nil, nil, err
return result, err
}
result.BuildctlBinary = buildctlBinary

// Set the default destination file
var defaultDestFile = "output.tar"
// defaultDestFile, err := filepath.Abs("output.tar")
// if err != nil {
// return result, fmt.Errorf("failed to set the default destination file path: %v", err)
// }

output := options.Output
if output == "" {
info, err := client.Server(ctx)
if err != nil {
return "", nil, false, "", nil, nil, err
return result, err
}
sharable, err := isImageSharable(options.BuildKitHost, options.GOptions.Namespace, info.UUID, options.GOptions.Snapshotter, options.Platform)
if err != nil {
return "", nil, false, "", nil, nil, err
return result, err
}
if sharable {
output = "type=image,unpack=true" // ensure the target stage is unlazied (needed for any snapshotters)
Expand All @@ -219,42 +259,58 @@ func generateBuildctlArgs(ctx context.Context, client *containerd.Client, option
// TODO: consider using type=oci for single-options.Platform build too
output = "type=oci"
}
needsLoading = true

// output of the `buildctl` command needs to be loaded into the containerd image store.
result.NeedsLoading = true
}
} else {
if !strings.Contains(output, "type=") {
// should accept --output <DIR> as an alias of --output
// type=local,dest=<DIR>
output = fmt.Sprintf("type=local,dest=%s", output)
}
if strings.Contains(output, "type=docker") || strings.Contains(output, "type=oci") {
if !strings.Contains(output, "dest=") {
needsLoading = true
}
}
}

var tags []string
if tags = strutil.DedupeStrSlice(options.Tag); len(tags) > 0 {
ref := tags[0]
parsedReference, err := referenceutil.Parse(ref)
if err != nil {
return "", nil, false, "", nil, nil, err
return result, err
}
output += ",name=" + parsedReference.String()

// pick the first tag and add it to output
for idx, tag := range tags {
parsedReference, err = referenceutil.Parse(tag)
if err != nil {
return "", nil, false, "", nil, nil, err
return result, err
}
tags[idx] = parsedReference.String()
}
} else if len(tags) == 0 {
output = output + ",dangling-name-prefix=<none>"
}
result.Tags = tags

if strings.Contains(output, "type=docker") || strings.Contains(output, "type=oci") {
if !strings.Contains(output, "dest=") {
// Set the destination for tarball output
output = fmt.Sprintf("%s,dest=%s", output, defaultDestFile)

// output of the `buildctl` command needs to be loaded into the containerd image store.
result.NeedsLoading = true
}
}

buildctlArgs = buildkitutil.BuildctlBaseArgs(options.BuildKitHost)
// Extract destination file from output
if strings.Contains(output, "dest=") {
_, destFilePath, _ := strings.Cut(output, "dest=")
result.DestFile = destFilePath
}
log.L.Debugf("\n------------output: %s\n--------------", output)

buildctlArgs := buildkitutil.BuildctlBaseArgs(options.BuildKitHost)
buildctlArgs = append(buildctlArgs, []string{
"build",
"--progress=" + options.Progress,
Expand All @@ -271,9 +327,9 @@ func generateBuildctlArgs(ctx context.Context, client *containerd.Client, option
var err error
dir, err = buildkitutil.WriteTempDockerfile(options.Stdin)
if err != nil {
return "", nil, false, "", nil, nil, err
return result, err
}
cleanup = func() {
result.Cleanup = func() {
os.RemoveAll(dir)
}
} else {
Expand All @@ -286,12 +342,12 @@ func generateBuildctlArgs(ctx context.Context, client *containerd.Client, option
}
dir, file, err = buildkitutil.BuildKitFile(dir, file)
if err != nil {
return "", nil, false, "", nil, nil, err
return result, err
}

buildCtx, err := parseContextNames(options.ExtendedBuildContext)
if err != nil {
return "", nil, false, "", nil, nil, err
return result, err
}

for k, v := range buildCtx {
Expand All @@ -306,7 +362,7 @@ func generateBuildctlArgs(ctx context.Context, client *containerd.Client, option
if isOCILayout := strings.HasPrefix(v, "oci-layout://"); isOCILayout {
args, err := parseBuildContextFromOCILayout(k, v)
if err != nil {
return "", nil, false, "", nil, nil, err
return result, err
}

buildctlArgs = append(buildctlArgs, args...)
Expand All @@ -315,7 +371,7 @@ func generateBuildctlArgs(ctx context.Context, client *containerd.Client, option

path, err := filepath.Abs(v)
if err != nil {
return "", nil, false, "", nil, nil, err
return result, err
}
buildctlArgs = append(buildctlArgs, fmt.Sprintf("--local=%s=%s", k, path))
buildctlArgs = append(buildctlArgs, fmt.Sprintf("--opt=context:%s=local:%s", k, k))
Expand Down Expand Up @@ -362,7 +418,7 @@ func generateBuildctlArgs(ctx context.Context, client *containerd.Client, option
}
}
} else {
return "", nil, false, "", nil, nil, fmt.Errorf("invalid build arg %q", ba)
return result, fmt.Errorf("invalid build arg %q", ba)
}
}

Expand Down Expand Up @@ -405,7 +461,7 @@ func generateBuildctlArgs(ctx context.Context, client *containerd.Client, option
optAttestType := strings.TrimPrefix(optAttestType, "type=")
buildctlArgs = append(buildctlArgs, fmt.Sprintf("--opt=attest:%s=%s", optAttestType, optAttestAttrs))
} else {
return "", nil, false, "", nil, nil, fmt.Errorf("attestation type not specified")
return result, fmt.Errorf("attestation type not specified")
}
}

Expand Down Expand Up @@ -434,11 +490,11 @@ func generateBuildctlArgs(ctx context.Context, client *containerd.Client, option
if options.IidFile != "" {
file, err := os.CreateTemp("", "buildkit-meta-*")
if err != nil {
return "", nil, false, "", nil, cleanup, err
return result, err
}
defer file.Close()
metaFile = file.Name()
buildctlArgs = append(buildctlArgs, "--metadata-file="+metaFile)
result.MetaFile = file.Name()
buildctlArgs = append(buildctlArgs, "--metadata-file="+result.MetaFile)
}

if options.NetworkMode != "" {
Expand All @@ -453,7 +509,9 @@ func generateBuildctlArgs(ctx context.Context, client *containerd.Client, option
}
}

return buildctlBinary, buildctlArgs, needsLoading, metaFile, tags, cleanup, nil
result.BuildctlArgs = buildctlArgs

return result, nil
}

func getDigestFromMetaFile(path string) (string, error) {
Expand Down

0 comments on commit 09ad9c7

Please sign in to comment.