Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Stuck build containers #836

Merged
merged 18 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/ecr v1.24.4
github.com/aws/aws-sdk-go-v2/service/s3 v1.51.4
github.com/beam-cloud/blobcache-v2 v0.0.0-20241206144620-d0ca8ede5095
github.com/beam-cloud/clip v0.0.0-20240826223025-899feb184e88
github.com/beam-cloud/clip v0.0.0-20250109221532-5d9d7744594d
github.com/beam-cloud/go-runc v0.0.0-20231222221338-b89899f33170
github.com/bsm/redislock v0.9.4
github.com/cedana/cedana v0.9.234
Expand Down Expand Up @@ -152,7 +152,6 @@ require (
github.com/godbus/dbus/v5 v5.1.1-0.20230522191255-76236955d466 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect
Expand Down
24 changes: 2 additions & 22 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ github.com/aws/smithy-go v1.20.3/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I=
github.com/beam-cloud/blobcache-v2 v0.0.0-20241206144620-d0ca8ede5095 h1:SKsyMkCblfDew79rNGL7nCQwXRSuXMSR+ceTOixPOiA=
github.com/beam-cloud/blobcache-v2 v0.0.0-20241206144620-d0ca8ede5095/go.mod h1:eK80pNykYLVTKmfjfTVqvrJqX4HKXqeCl1OQt6/F8ZM=
github.com/beam-cloud/clip v0.0.0-20240826223025-899feb184e88 h1:SAzRxbcUKx0fFfqBRfrm39fyh0ixtuPM8/1HNJpnh9U=
github.com/beam-cloud/clip v0.0.0-20240826223025-899feb184e88/go.mod h1:FO7taXHUAqgx33PjeB6LbSLpKob3Ceyo9Po64nq5TR0=
github.com/beam-cloud/clip v0.0.0-20250109221532-5d9d7744594d h1:EEWoW32RCi3i9/PWkvrPMDIo4j2attsKkMXxwZQRKt4=
github.com/beam-cloud/clip v0.0.0-20250109221532-5d9d7744594d/go.mod h1:FO7taXHUAqgx33PjeB6LbSLpKob3Ceyo9Po64nq5TR0=
github.com/beam-cloud/go-runc v0.0.0-20231222221338-b89899f33170 h1:KYVz18kobBGU8URM9Srn++2tcL9e0PcwYyH0Z4GYicM=
github.com/beam-cloud/go-runc v0.0.0-20231222221338-b89899f33170/go.mod h1:aw0zhDi28Hemve0raHcfU9suxZwkCpyNANOEwKZSSXo=
github.com/beam-cloud/ristretto v0.0.0-20241013204426-d1403e359aa2 h1:RvgYbYPfBiq2IvimR4Vll2ImJsElX3oPLU9OjVvjE0c=
Expand Down Expand Up @@ -233,8 +233,6 @@ github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -366,8 +364,6 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/labstack/echo-contrib v0.17.1 h1:7I/he7ylVKsDUieaGRZ9XxxTYOjfQwVzHzUYrNykfCU=
github.com/labstack/echo-contrib v0.17.1/go.mod h1:SnsCZtwHBAZm5uBSAtQtXQHI3wqEA73hvTn0bYMKnZA=
github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0=
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
github.com/labstack/echo/v4 v4.13.3 h1:pwhpCPrTl5qry5HRdM5FwdXnhXSLSY+WE+YQSeCaafY=
github.com/labstack/echo/v4 v4.13.3/go.mod h1:o90YNEeQWjDozo584l7AwhJMHN0bOC4tAfg+Xox9q5g=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
Expand Down Expand Up @@ -559,8 +555,6 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tailscale/certstore v0.1.1-0.20231202035212-d3fa0460f47e h1:PtWT87weP5LWHEY//SWsYkSO3RWRZo4OSWagh3YD2vQ=
Expand Down Expand Up @@ -677,8 +671,6 @@ golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand All @@ -704,8 +696,6 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand All @@ -718,8 +708,6 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -751,23 +739,15 @@ golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.25.0 h1:WtHI/ltw4NvSUig5KARz9h521QvRC8RmF/cuYqifU24=
golang.org/x/term v0.25.0/go.mod h1:RPyXicDX+6vLxogjjRxjgD2TKtmAO6NZBsBRfrOLu7M=
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ=
golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=
golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
69 changes: 44 additions & 25 deletions pkg/abstractions/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ func (b *Builder) Build(ctx context.Context, opts *BuildOpts, outputChan chan co
sourceImage := fmt.Sprintf("%s/%s:%s", opts.BaseImageRegistry, opts.BaseImageName, opts.BaseImageTag)
containerId := b.genContainerId()

go func() {
<-ctx.Done() // If user cancels the build, send a stop-build event to the scheduler
jsun-m marked this conversation as resolved.
Show resolved Hide resolved
err := b.scheduler.StopBuild(containerId)
if err != nil {
log.Error().Str("container_id", containerId).Err(err).Msg("failed to stop build")
}
}()

// Allow config to override default build container settings
cpu := defaultBuildContainerCpu
memory := defaultBuildContainerMemory
Expand Down Expand Up @@ -306,36 +314,47 @@ func (b *Builder) Build(ctx context.Context, opts *BuildOpts, outputChan chan co
outputChan <- common.OutputMsg{Done: false, Success: false, Msg: "Waiting for build container to start...\n"}
start := time.Now()
buildContainerRunning := false
for {
r, err := client.Status(containerId)
if err != nil {
outputChan <- common.OutputMsg{Done: true, Success: false, Msg: "Error occured while checking container status: " + err.Error()}
return err
}

if r.Running {
buildContainerRunning = true
break
}
for !buildContainerRunning {
select {
case <-ctx.Done():
log.Info().Str("container_id", containerId).Msg("build was aborted")
outputChan <- common.OutputMsg{Done: true, Success: false, Msg: "Build was aborted.\n"}
return ctx.Err()

exitCode, err := b.containerRepo.GetContainerExitCode(containerId)
if err == nil && exitCode != 0 {
msg, ok := types.WorkerContainerExitCodes[exitCode]
if !ok {
msg = types.WorkerContainerExitCodes[types.WorkerContainerExitCodeUnknownError]
case <-time.After(100 * time.Millisecond):
r, err := client.Status(containerId)
if err != nil {
outputChan <- common.OutputMsg{Done: true, Success: false, Msg: "Error occurred while checking container status: " + err.Error()}
jsun-m marked this conversation as resolved.
Show resolved Hide resolved
return err
}
// Wait for any final logs to get sent before returning
time.Sleep(200 * time.Millisecond)
outputChan <- common.OutputMsg{Done: true, Success: false, Msg: fmt.Sprintf("Container exited with error: %s\n", msg)}
return errors.New(fmt.Sprintf("container exited with error: %s\n", msg))
}

if time.Since(start) > containerSpinupTimeout {
outputChan <- common.OutputMsg{Done: true, Success: false, Msg: fmt.Sprintf("Timeout: container not running after %d seconds.\n", containerSpinupTimeout)}
return errors.New(fmt.Sprintf("timeout: container not running after %d seconds", containerSpinupTimeout))
}
if r.Running {
buildContainerRunning = true
continue
}

time.Sleep(100 * time.Millisecond)
exitCode, err := b.containerRepo.GetContainerExitCode(containerId)
if err == nil && exitCode != 0 {
msg, ok := types.WorkerContainerExitCodes[exitCode]
if !ok {
msg = types.WorkerContainerExitCodes[types.WorkerContainerExitCodeUnknownError]
}
// Wait for any final logs to get sent before returning
time.Sleep(200 * time.Millisecond)
outputChan <- common.OutputMsg{Done: true, Success: false, Msg: fmt.Sprintf("Container exited with error: %s\n", msg)}
return errors.New(fmt.Sprintf("container exited with error: %s\n", msg))
}

if time.Since(start) > containerSpinupTimeout {
err := b.scheduler.StopBuild(containerId)
if err != nil {
log.Error().Str("container_id", containerId).Err(err).Msg("failed to stop build")
}
outputChan <- common.OutputMsg{Done: true, Success: false, Msg: fmt.Sprintf("Timeout: container not running after %s seconds.\n", containerSpinupTimeout)}
return errors.New(fmt.Sprintf("timeout: container not running after %s seconds", containerSpinupTimeout))
}
}
}

imageId, err := b.GetImageId(opts)
Expand Down
4 changes: 4 additions & 0 deletions pkg/common/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ type LocalObjectStore struct {
}

func (s *LocalObjectStore) Put(ctx context.Context, localPath string, key string) error {
if ctx.Err() != nil {
return ctx.Err()
}

srcFile, err := os.Open(localPath)
if err != nil {
return err
Expand Down
15 changes: 15 additions & 0 deletions pkg/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@ func (s *Scheduler) Stop(stopArgs *types.StopContainerArgs) error {
return nil
}

func (s *Scheduler) StopBuild(containerId string) error {
luke-lombardi marked this conversation as resolved.
Show resolved Hide resolved
_, err := s.eventBus.Send(&common.Event{
Type: common.EventType("stop-build" + "-" + containerId),
Args: map[string]any{"container_id": containerId},
LockAndDelete: false,
})
if err != nil {
log.Error().Err(err).Msg("failed to send stop build event")
return err
}

log.Info().Str("container_id", containerId).Msg("sent stop build event")
return nil
}

func (s *Scheduler) getControllers(request *types.ContainerRequest) ([]WorkerPoolController, error) {
controllers := []WorkerPoolController{}

Expand Down
18 changes: 13 additions & 5 deletions pkg/worker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,15 @@ func (c *ImageClient) BuildAndArchiveImage(ctx context.Context, outputLogger *sl
os.MkdirAll(imagePath, 0755)
os.MkdirAll(ociPath, 0755)

cmd := exec.Command("buildah", "--root", imagePath, "bud", "-f", tempDockerFile, "-t", imageId+":latest", buildCtxPath)
cmd := exec.CommandContext(ctx, "buildah", "--root", imagePath, "bud", "-f", tempDockerFile, "-t", imageId+":latest", buildCtxPath)
cmd.Stdout = &ExecWriter{outputLogger: outputLogger}
cmd.Stderr = &ExecWriter{outputLogger: outputLogger}
err = cmd.Run()
if err != nil {
return err
}

cmd = exec.Command("buildah", "--root", imagePath, "push", imageId+":latest", "oci:"+ociPath+":latest")
cmd = exec.CommandContext(ctx, "buildah", "--root", imagePath, "push", imageId+":latest", "oci:"+ociPath+":latest")
cmd.Stdout = &ExecWriter{outputLogger: outputLogger}
cmd.Stderr = &ExecWriter{outputLogger: outputLogger}
err = cmd.Run()
Expand Down Expand Up @@ -378,7 +378,7 @@ func (c *ImageClient) PullAndArchiveImage(ctx context.Context, outputLogger *slo

outputLogger.Info("Unpacking image...\n")
tmpBundlePath := filepath.Join(baseTmpBundlePath, imageId)
err = c.unpack(baseImage.Repo, baseImage.Tag, tmpBundlePath)
err = c.unpack(ctx, baseImage.Repo, baseImage.Tag, tmpBundlePath)
if err != nil {
return fmt.Errorf("unable to unpack image: %v", err)
}
Expand Down Expand Up @@ -445,7 +445,11 @@ func (c *ImageClient) inspectArgs(creds string) (out []string) {
return out
}

func (c *ImageClient) unpack(baseImageName string, baseImageTag string, bundlePath string) error {
func (c *ImageClient) unpack(ctx context.Context, baseImageName string, baseImageTag string, bundlePath string) error {
if ctx.Err() != nil {
return ctx.Err()
}

unpackOptions := umociUnpackOptions()

// Get a reference to the CAS.
Expand Down Expand Up @@ -479,6 +483,10 @@ func (c *ImageClient) unpack(baseImageName string, baseImageTag string, bundlePa

// Generate and upload archived version of the image for distribution
func (c *ImageClient) Archive(ctx context.Context, bundlePath string, imageId string, progressChan chan int) error {
if ctx.Err() != nil {
return ctx.Err()
}

startTime := time.Now()

archiveName := fmt.Sprintf("%s.%s.tmp", imageId, c.registry.ImageFileExtension)
Expand All @@ -491,7 +499,7 @@ func (c *ImageClient) Archive(ctx context.Context, bundlePath string, imageId st
var err error = nil
switch c.config.ImageService.RegistryStore {
case common.S3ImageRegistryStore:
err = clip.CreateAndUploadArchive(clip.CreateOptions{
err = clip.CreateAndUploadArchive(ctx, clip.CreateOptions{
InputPath: bundlePath,
OutputPath: archivePath,
Credentials: storage.ClipStorageCredentials{
Expand Down
32 changes: 18 additions & 14 deletions pkg/worker/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,10 @@ func (s *Worker) deleteContainer(containerId string, err error) {
}

// Spawn a single container and stream output to stdout/stderr
func (s *Worker) RunContainer(request *types.ContainerRequest) error {
func (s *Worker) RunContainer(ctx context.Context, request *types.ContainerRequest) error {
containerId := request.ContainerId

bundlePath := filepath.Join(s.imageMountPath, request.ImageId)
s.containerInstances.Set(containerId, &ContainerInstance{
Id: containerId,
StubId: request.StubId,
LogBuffer: common.NewLogBuffer(),
Request: request,
})

// Set worker hostname
hostname := fmt.Sprintf("%s:%d", s.podAddr, s.runcServer.port)
Expand All @@ -181,8 +175,13 @@ func (s *Worker) RunContainer(request *types.ContainerRequest) error {
return err
}

if err := s.buildOrPullImage(request, containerId, outputLogger); err != nil {
return err
select {
case <-ctx.Done():
return nil
default:
if err := s.buildOrPullImage(ctx, request, containerId, outputLogger); err != nil {
return err
}
}
}

Expand Down Expand Up @@ -224,14 +223,19 @@ func (s *Worker) RunContainer(request *types.ContainerRequest) error {

go s.containerWg.Add(1)

// Start the container
go s.spawn(request, spec, outputLogger, opts)
select {
case <-ctx.Done():
return ctx.Err()
default:
// Start the container
go s.spawn(request, spec, outputLogger, opts)
}

log.Info().Str("container_id", containerId).Msg("spawned successfully")
return nil
}

func (s *Worker) buildOrPullImage(request *types.ContainerRequest, containerId string, outputLogger *slog.Logger) error {
func (s *Worker) buildOrPullImage(ctx context.Context, request *types.ContainerRequest, containerId string, outputLogger *slog.Logger) error {
switch {
case request.BuildOptions.Dockerfile != nil:
log.Info().Str("container_id", containerId).Msg("lazy-pull failed, building image from Dockerfile")
Expand All @@ -241,13 +245,13 @@ func (s *Worker) buildOrPullImage(request *types.ContainerRequest, containerId s
return err
}

if err := s.imageClient.BuildAndArchiveImage(context.TODO(), outputLogger, *request.BuildOptions.Dockerfile, request.ImageId, buildCtxPath); err != nil {
if err := s.imageClient.BuildAndArchiveImage(ctx, outputLogger, *request.BuildOptions.Dockerfile, request.ImageId, buildCtxPath); err != nil {
return err
}
case request.BuildOptions.SourceImage != nil:
log.Info().Str("container_id", containerId).Msgf("lazy-pull failed, pulling source image: %s", *request.BuildOptions.SourceImage)

if err := s.imageClient.PullAndArchiveImage(context.TODO(), outputLogger, *request.BuildOptions.SourceImage, request.ImageId, request.BuildOptions.SourceImageCreds); err != nil {
if err := s.imageClient.PullAndArchiveImage(ctx, outputLogger, *request.BuildOptions.SourceImage, request.ImageId, request.BuildOptions.SourceImageCreds); err != nil {
return err
}
}
Expand Down
Loading
Loading