From b45e75fb9fca42db2c59431d5debc188757903e0 Mon Sep 17 00:00:00 2001 From: Daniel Levi-Minzi Date: Wed, 11 Dec 2024 18:30:02 -0500 Subject: [PATCH] pipe output with container id so users can see it --- pkg/worker/image.go | 36 +++++++++++++++++++++++------------- pkg/worker/lifecycle.go | 2 +- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/pkg/worker/image.go b/pkg/worker/image.go index 4fd792ee9..a6371cc35 100644 --- a/pkg/worker/image.go +++ b/pkg/worker/image.go @@ -256,16 +256,26 @@ func (c *ImageClient) InspectAndVerifyImage(ctx context.Context, sourceImage str return nil } -func (c *ImageClient) BuildAndArchiveImage(ctx context.Context, dockerfile string, imageId string, buildCtxPath string) error { +// Will be replaced when structured logging is merged +type ExecWriter struct { + containerId string +} + +func (c *ExecWriter) Write(p []byte) (n int, err error) { + log.Printf("<%s> - %s", c.containerId, string(p)) + return len(p), nil +} + +func (c *ImageClient) BuildAndArchiveImage(ctx context.Context, containerId string, dockerfile string, imageId string, buildCtxPath string) error { buildPath, err := os.MkdirTemp("", "") if err != nil { - return errors.Wrap(err, "create temp dir") + return err } defer os.RemoveAll(buildPath) tempDockerFile := filepath.Join(buildPath, "Dockerfile") f, err := os.Create(tempDockerFile) if err != nil { - return errors.Wrap(err, "create temp dockerfile") + return err } fmt.Fprintf(f, dockerfile) f.Close() @@ -278,24 +288,24 @@ func (c *ImageClient) BuildAndArchiveImage(ctx context.Context, dockerfile strin os.MkdirAll(ociPath, 0755) cmd := exec.Command("buildah", "--root", imagePath, "bud", "-f", tempDockerFile, "-t", imageId+":latest", buildCtxPath) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + cmd.Stdout = &ExecWriter{containerId: containerId} + cmd.Stderr = &ExecWriter{containerId: containerId} err = cmd.Run() if err != nil { - return errors.Wrap(err, "buildah bud") + return err } cmd = exec.Command("buildah", "--root", imagePath, "push", imageId+":latest", "oci:"+ociPath+":latest") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + cmd.Stdout = &ExecWriter{containerId: containerId} + cmd.Stderr = &ExecWriter{containerId: containerId} err = cmd.Run() if err != nil { - return errors.Wrap(err, "buildah push") + return err } engine, err := dir.Open(ociPath) if err != nil { - return errors.Wrap(err, "open CAS "+ociPath) + return err } defer engine.Close() @@ -306,20 +316,20 @@ func (c *ImageClient) BuildAndArchiveImage(ctx context.Context, dockerfile strin err = umoci.Unpack(engineExt, "latest", tempBundlePath, unpackOptions) if err != nil { - return errors.Wrap(err, "unpack image") + return err } for _, dir := range requiredContainerDirectories { fullPath := filepath.Join(tempBundlePath, "rootfs", dir) err := os.MkdirAll(fullPath, 0755) if err != nil { - return errors.Wrap(err, fmt.Sprintf("creating /%s directory", dir)) + return err } } err = c.Archive(ctx, tempBundlePath, imageId, nil) if err != nil { - return errors.Wrap(err, "archive image") + return err } return nil diff --git a/pkg/worker/lifecycle.go b/pkg/worker/lifecycle.go index 88c28fcf5..1fa3e9c05 100644 --- a/pkg/worker/lifecycle.go +++ b/pkg/worker/lifecycle.go @@ -217,7 +217,7 @@ func (s *Worker) buildOrPullImage(request *types.ContainerRequest, containerId s if err != nil { return err } - if err := s.imageClient.BuildAndArchiveImage(context.TODO(), *request.Dockerfile, request.ImageId, buildCtxPath); err != nil { + if err := s.imageClient.BuildAndArchiveImage(context.TODO(), containerId, *request.Dockerfile, request.ImageId, buildCtxPath); err != nil { return err } case request.SourceImage != nil: