Skip to content

Commit

Permalink
tbs: moved ensureBuildkitd() higher in its file
Browse files Browse the repository at this point in the history
  • Loading branch information
Omer Preminger committed Nov 2, 2023
1 parent 7376b59 commit 656bc85
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions internal/pkg/build/buildkit/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,31 @@ func Run(ctx context.Context, opts *Opts, dest, spec string) {
}
}

// ensureBuildkitd checks if a buildkitd daemon is already running, and if not,
// launches one. Once the server is ready, the value true will be sent over the
// provided readyChan. Make sure this is a buffered channel with sufficient room
// to avoid deadlocks.
func ensureBuildkitd(ctx context.Context) string {
if isBuildkitdRunning(ctx) {
sylog.Infof("Found buildkitd already running at %q; will use that daemon.", bkDefaultSocket)
return bkDefaultSocket
}

sylog.Infof("Did not find usable running buildkitd daemon; spawning our own.")
socketChan := make(chan string, 1)
go func() {
if err := bkdaemon.Run(ctx, socketChan); err != nil {
sylog.Fatalf("buildkitd returned error: %v", err)
}
}()
go func() {
time.Sleep(bkLaunchTimeout)
socketChan <- ""
}()

return <-socketChan
}

// isBuildkitdRunning tries to determine whether there's already an instance of buildkitd running.
func isBuildkitdRunning(ctx context.Context) bool {
c, err := client.New(ctx, bkDefaultSocket, client.WithFailFast())
Expand Down Expand Up @@ -222,28 +247,3 @@ func writeDockerTar(r io.Reader, outputFile *os.File) error {

return nil
}

// ensureBuildkitd checks if a buildkitd daemon is already running, and if not,
// launches one. Once the server is ready, the value true will be sent over the
// provided readyChan. Make sure this is a buffered channel with sufficient room
// to avoid deadlocks.
func ensureBuildkitd(ctx context.Context) string {
if isBuildkitdRunning(ctx) {
sylog.Infof("Found buildkitd already running at %q; will use that daemon.", bkDefaultSocket)
return bkDefaultSocket
}

sylog.Infof("Did not find usable running buildkitd daemon; spawning our own.")
socketChan := make(chan string, 1)
go func() {
if err := bkdaemon.Run(ctx, socketChan); err != nil {
sylog.Fatalf("buildkitd returned error: %v", err)
}
}()
go func() {
time.Sleep(bkLaunchTimeout)
socketChan <- ""
}()

return <-socketChan
}

0 comments on commit 656bc85

Please sign in to comment.