Skip to content

Commit

Permalink
support for docker copy commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dleviminzi committed Dec 10, 2024
1 parent 654af01 commit 0c62909
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 75 deletions.
8 changes: 6 additions & 2 deletions pkg/abstractions/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type BuildOpts struct {
ExistingImageUri string
ExistingImageCreds map[string]string
Dockerfile string
BuildCtxObject string
PythonVersion string
PythonPackages []string
Commands []string
Expand All @@ -73,12 +74,14 @@ func (o *BuildOpts) String() string {
fmt.Fprintf(&b, " \"BaseImageName\": %q,", o.BaseImageName)
fmt.Fprintf(&b, " \"BaseImageTag\": %q,", o.BaseImageTag)
fmt.Fprintf(&b, " \"BaseImageCreds\": %q,", o.BaseImageCreds)
fmt.Fprintf(&b, " \"ExistingImageUri\": %q,", o.ExistingImageUri)
fmt.Fprintf(&b, " \"ExistingImageCreds\": %#v,", o.ExistingImageCreds)
fmt.Fprintf(&b, " \"Dockerfile\": %q,", o.Dockerfile)
fmt.Fprintf(&b, " \"BuildCtxObject\": %q,", o.BuildCtxObject)
fmt.Fprintf(&b, " \"PythonVersion\": %q,", o.PythonVersion)
fmt.Fprintf(&b, " \"PythonPackages\": %#v,", o.PythonPackages)
fmt.Fprintf(&b, " \"Commands\": %#v,", o.Commands)
fmt.Fprintf(&b, " \"BuildSteps\": %#v,", o.BuildSteps)
fmt.Fprintf(&b, " \"ExistingImageUri\": %q,", o.ExistingImageUri)
fmt.Fprintf(&b, " \"ExistingImageCreds\": %#v,", o.ExistingImageCreds)
fmt.Fprintf(&b, " \"ForceRebuild\": %v", o.ForceRebuild)
fmt.Fprintf(&b, "}")
return b.String()
Expand Down Expand Up @@ -257,6 +260,7 @@ func (b *Builder) Build(ctx context.Context, opts *BuildOpts, outputChan chan co
SourceImage: &sourceImage,
SourceImageCreds: opts.BaseImageCreds,
Dockerfile: dockerfile,
BuildCtxObject: &opts.BuildCtxObject,
WorkspaceId: authInfo.Workspace.ExternalId,
Workspace: *authInfo.Workspace,
EntryPoint: []string{"tail", "-f", "/dev/null"},
Expand Down
1 change: 1 addition & 0 deletions pkg/abstractions/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (is *RuncImageService) BuildImage(in *pb.BuildImageRequest, stream pb.Image
ExistingImageCreds: in.ExistingImageCreds,
EnvVars: in.EnvVars,
Dockerfile: in.Dockerfile,
BuildCtxObject: in.BuildCtxObject,
}

ctx := stream.Context()
Expand Down
2 changes: 2 additions & 0 deletions pkg/abstractions/image/image.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ message VerifyImageBuildRequest {
repeated BuildStep build_steps = 6;
repeated string env_vars = 7;
string dockerfile = 8;
string build_ctx_object = 9;
}

message VerifyImageBuildResponse {
Expand All @@ -46,6 +47,7 @@ message BuildImageRequest {
repeated BuildStep build_steps = 6;
repeated string env_vars = 7;
string dockerfile = 8;
string build_ctx_object = 9;
}

message BuildImageResponse {
Expand Down
1 change: 1 addition & 0 deletions pkg/types/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type ContainerRequest struct {
GpuCount uint32 `json:"gpu_count"`
SourceImage *string `json:"source_image"`
Dockerfile *string `json:"dockerfile"`
BuildCtxObject *string `json:"build_context"`
SourceImageCreds string `json:"source_image_creds"`
ImageId string `json:"image_id"`
StubId string `json:"stub_id"`
Expand Down
19 changes: 11 additions & 8 deletions pkg/worker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (c *ImageClient) InspectAndVerifyImage(ctx context.Context, sourceImage str
return nil
}

func (c *ImageClient) BuildAndArchiveImage(ctx context.Context, dockerfile string, imageId string) error {
func (c *ImageClient) BuildAndArchiveImage(ctx context.Context, dockerfile string, imageId string, buildCtxPath string) error {
buildPath, err := os.MkdirTemp("", "")
if err != nil {
return errors.Wrap(err, "create temp dir")
Expand All @@ -276,7 +276,7 @@ func (c *ImageClient) BuildAndArchiveImage(ctx context.Context, dockerfile strin
os.MkdirAll(imagePath, 0755)
os.MkdirAll(ociPath, 0755)

cmd := exec.Command("buildah", "--root", imagePath, "bud", "-f", tempDockerFile, "-t", imageId+":latest", ".")
cmd := exec.Command("buildah", "--root", imagePath, "bud", "-f", tempDockerFile, "-t", imageId+":latest", buildCtxPath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
Expand Down Expand Up @@ -312,14 +312,17 @@ func (c *ImageClient) BuildAndArchiveImage(ctx context.Context, dockerfile strin
fullPath := filepath.Join(tempBundlePath, "rootfs", dir)
err := os.MkdirAll(fullPath, 0755)
if err != nil {
errors.Wrap(err, fmt.Sprintf("creating /%s directory", dir))
return err
return errors.Wrap(err, fmt.Sprintf("creating /%s directory", dir))
}
}

// TODO: Not sure if we need this or not?
defer os.RemoveAll(tempBundlePath)
return c.Archive(ctx, tempBundlePath, imageId, nil)
err = c.Archive(ctx, tempBundlePath, imageId, nil)
if err != nil {
return errors.Wrap(err, "archive image")
}

return nil
}

func (c *ImageClient) PullAndArchiveImage(ctx context.Context, sourceImage string, imageId string, creds string) error {
Expand Down Expand Up @@ -492,7 +495,7 @@ func (c *ImageClient) Archive(ctx context.Context, bundlePath string, imageId st

if err != nil {
log.Printf("Unable to create archive: %v\n", err)
return err
return errors.Wrap(err, "create archive")
}
log.Printf("Container <%v> archive took %v\n", imageId, time.Since(startTime))

Expand All @@ -501,7 +504,7 @@ func (c *ImageClient) Archive(ctx context.Context, bundlePath string, imageId st
err = c.registry.Push(ctx, archivePath, imageId)
if err != nil {
log.Printf("Failed to push image <%v>: %v\n", imageId, err)
return err
return errors.Wrap(err, "push image")
}

log.Printf("Image <%v> push took %v\n", imageId, time.Since(startTime))
Expand Down
30 changes: 23 additions & 7 deletions pkg/worker/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,22 @@ func (s *Worker) RunContainer(request *types.ContainerRequest) error {
switch request.ImageSourceType() {
case types.ImageSourceTypeBuild:
log.Printf("<%s> - lazy-pull failed, building image: %s\n", containerId, *request.Dockerfile)
err = s.imageClient.BuildAndArchiveImage(context.TODO(), *request.Dockerfile, request.ImageId)
buildCtxPath, err := getBuildContext(request)
if err != nil {
return err
}
if err := s.imageClient.BuildAndArchiveImage(context.TODO(), *request.Dockerfile, request.ImageId, buildCtxPath); err != nil {
return err
}
case types.ImageSourceTypePull:
log.Printf("<%s> - lazy-pull failed, pulling source image: %s\n", containerId, *request.SourceImage)
err = s.imageClient.PullAndArchiveImage(context.TODO(), *request.SourceImage, request.ImageId, request.SourceImageCreds)
}
if err != nil {
return err
if err := s.imageClient.PullAndArchiveImage(context.TODO(), *request.SourceImage, request.ImageId, request.SourceImageCreds); err != nil {
return err
}
}

// Try pull again after building or pulling the source image
err = s.imageClient.PullLazy(request)
if err != nil {
if err = s.imageClient.PullLazy(request); err != nil {
return err
}
}
Expand Down Expand Up @@ -220,6 +224,18 @@ func (s *Worker) RunContainer(request *types.ContainerRequest) error {
return nil
}

func getBuildContext(request *types.ContainerRequest) (string, error) {
buildCtxPath := "."
if request.BuildCtxObject != nil {
err := common.ExtractObjectFile(context.TODO(), *request.BuildCtxObject, request.Workspace.Name)
if err != nil {
return "", err
}
buildCtxPath = filepath.Join(types.DefaultExtractedObjectPath, request.Workspace.Name, *request.BuildCtxObject)
}
return buildCtxPath, nil
}

func (s *Worker) readBundleConfig(imageId string) (*specs.Spec, error) {
imageConfigPath := filepath.Join(s.imageMountPath, imageId, initialSpecBaseName)

Expand Down
Loading

0 comments on commit 0c62909

Please sign in to comment.