Skip to content

Commit

Permalink
Merge pull request #2746 from buildkite/retry-k8s-socket
Browse files Browse the repository at this point in the history
Use roko to retry k8s socket dial
  • Loading branch information
DrJosh9000 authored Apr 24, 2024
2 parents 3de030e + afb530d commit 080375a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/job/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ func (e *Executor) startKubernetesClient(ctx context.Context, kubernetesClient *
return fmt.Errorf("failed to parse container id, %s", os.Getenv("BUILDKITE_CONTAINER_ID"))
}
kubernetesClient.ID = id
connect, err := kubernetesClient.Connect()
connect, err := kubernetesClient.Connect(ctx)
if err != nil {
return err
}
Expand Down
14 changes: 12 additions & 2 deletions kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/buildkite/agent/v3/logger"
"github.com/buildkite/agent/v3/process"
"github.com/buildkite/roko"
)

func init() {
Expand Down Expand Up @@ -287,11 +288,20 @@ type Client struct {

var errNotConnected = errors.New("client not connected")

func (c *Client) Connect() (RegisterResponse, error) {
func (c *Client) Connect(ctx context.Context) (RegisterResponse, error) {
if c.SocketPath == "" {
c.SocketPath = defaultSocketPath
}
client, err := rpc.DialHTTP("unix", c.SocketPath)

// Because k8s might run the containers "out of order", the server socket
// might not exist yet. Try to connect several times.
r := roko.NewRetrier(
roko.WithMaxAttempts(30),
roko.WithStrategy(roko.Constant(time.Second)),
)
client, err := roko.DoFunc(ctx, r, func(*roko.Retrier) (*rpc.Client, error) {
return rpc.DialHTTP("unix", c.SocketPath)
})
if err != nil {
return RegisterResponse{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,6 @@ func intptr(x int) *int {

// helper for ignoring the response from regular client.Connect
func connect(c *Client) error {
_, err := c.Connect()
_, err := c.Connect(context.Background())
return err
}

0 comments on commit 080375a

Please sign in to comment.