This repository was archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Fix CI #101
Merged
+450
−285
Merged
Fix CI #101
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,18 @@ package driver | |
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"fmt" | ||
"io" | ||
"math/rand" | ||
"math/big" | ||
"net" | ||
"strings" | ||
"time" | ||
|
||
"github.com/moby/buildkit/client" | ||
"github.com/moby/buildkit/session" | ||
specs "github.com/opencontainers/image-spec/specs-go/v1" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
"github.com/vmware-tanzu/buildkit-cli-for-kubectl/pkg/imagetools" | ||
"github.com/vmware-tanzu/buildkit-cli-for-kubectl/pkg/progress" | ||
"github.com/vmware-tanzu/buildkit-cli-for-kubectl/pkg/store" | ||
|
@@ -36,7 +38,16 @@ const ( | |
Stopped | ||
) | ||
|
||
const maxBootRetries = 3 | ||
// RandSleep sleeps a random amount of time between zero and maxMS milliseconds | ||
func RandSleep(maxMS int64) { | ||
sleepTime, err := rand.Int(rand.Reader, big.NewInt(maxMS)) | ||
if err == nil { | ||
time.Sleep(time.Duration(sleepTime.Int64()) * time.Millisecond) | ||
} else { | ||
logrus.Debugf("failed to get random number: %s", err) | ||
time.Sleep(time.Duration(maxMS) * time.Millisecond) | ||
} | ||
} | ||
|
||
func (s Status) String() string { | ||
switch s { | ||
|
@@ -66,7 +77,7 @@ type Driver interface { | |
Info(context.Context) (*Info, error) | ||
Stop(ctx context.Context, force bool) error | ||
Rm(ctx context.Context, force bool) error | ||
Client(ctx context.Context) (*client.Client, string, error) | ||
Clients(ctx context.Context) (*BuilderClients, error) | ||
Features() map[Feature]bool | ||
List(ctx context.Context) ([]Builder, error) | ||
RuntimeSockProxy(ctx context.Context, name string) (net.Conn, error) | ||
|
@@ -94,40 +105,51 @@ type Node struct { | |
Platforms []specs.Platform | ||
} | ||
|
||
func Boot(ctx context.Context, d Driver, pw progress.Writer) (*client.Client, string, error) { | ||
try := 0 | ||
rand.Seed(time.Now().UnixNano()) | ||
for { | ||
info, err := d.Info(ctx) | ||
type BuilderClients struct { | ||
// The builder on the chosen node/pod | ||
ChosenNode NodeClient | ||
|
||
// If multiple builders present, clients to the other builders (excluding the chosen pod) | ||
OtherNodes []NodeClient | ||
} | ||
type NodeClient struct { | ||
NodeName string | ||
ClusterAddr string | ||
BuildKitClient *client.Client | ||
} | ||
|
||
func Boot(ctx context.Context, d Driver, pw progress.Writer) (*BuilderClients, error) { | ||
err := fmt.Errorf("timeout before starting") | ||
var info *Info | ||
var results *BuilderClients | ||
for err != nil { | ||
select { | ||
case <-ctx.Done(): | ||
return nil, errors.Wrap(err, "timed out trying to bootstrap builder") | ||
default: | ||
} | ||
|
||
info, err = d.Info(ctx) | ||
if err != nil { | ||
return nil, "", err | ||
return nil, err | ||
} | ||
try++ | ||
|
||
if info.Status != Running { | ||
if try > maxBootRetries { | ||
return nil, "", errors.Errorf("failed to bootstrap builder in %d attempts (%s)", try, err) | ||
} | ||
if err = d.Bootstrap(ctx, func(s *client.SolveStatus) { | ||
if pw != nil { | ||
pw.Status() <- s | ||
} | ||
}); err != nil && (strings.Contains(err.Error(), "already exists") || strings.Contains(err.Error(), "not found")) { | ||
// This most likely means another build is running in parallel | ||
// Give it just enough time to finish creating resources then retry | ||
time.Sleep(25 * time.Millisecond * time.Duration(1+rand.Int63n(39))) // 25 - 1000 ms | ||
}); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes me a bit nervous. I get it might take a bit for the builders to simmer down, but just sleeping randomly for 1000 milliseconds feels a bit weird. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's fair. Let me drop this down to a much smaller pause to get the racing CLIs skewed but not as noticeable to a human. |
||
// Possibly another CLI running in parallel - random sleep then retry | ||
RandSleep(100) | ||
continue | ||
} else if err != nil { | ||
return nil, "", err | ||
} | ||
} | ||
|
||
c, chosenNodeName, err := d.Client(ctx) | ||
results, err = d.Clients(ctx) | ||
if err != nil { | ||
if errors.Cause(err) == ErrNotRunning && try <= maxBootRetries { | ||
continue | ||
} | ||
return nil, "", err | ||
// TODO - is there a fail-fast scenario we want to catch here? | ||
RandSleep(1000) | ||
} | ||
return c, chosenNodeName, nil | ||
} | ||
return results, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could run into a race here unless you use sync.Mutex and lock/unlock it around the clients[] hashmap.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch! You're right, this was unsafe usage.