Skip to content

Commit

Permalink
fix: negotiate Docker API version (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
czeslavo authored Jan 22, 2024
1 parent 2e7370a commit 01c2d1a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v0.44.0

- Added a call to `NegotiateAPIVersion` when creating a Docker client to
ensure that the client is able to communicate with the Docker daemon.
[#932](https://github.com/Kong/kubernetes-testing-framework/pull/932)

## v0.43.0

- Added `WithReleaseChannel` to the GKE cluster builder to allow specifying
Expand Down
19 changes: 19 additions & 0 deletions pkg/utils/docker/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package docker

import (
"context"

"github.com/docker/docker/client"
)

// NewNegotiatedClientWithOpts is a wrapper around docker.NewClientWithOpts that negotiates the API version
// with the server.
func NewNegotiatedClientWithOpts(ctx context.Context, opts ...client.Opt) (*client.Client, error) {
c, err := client.NewClientWithOpts(opts...)
if err != nil {
return nil, err
}
// Make sure the client API version matches server's API version to avoid discrepancy issues.
c.NegotiateAPIVersion(ctx)
return c, nil
}
2 changes: 1 addition & 1 deletion pkg/utils/docker/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// given command and arguments on the given container (by ID) privileged.
func RunPrivilegedCommand(ctx context.Context, containerID, command string, args ...string) error {
// connect to the local docker env
dockerc, err := client.NewClientWithOpts(client.FromEnv)
dockerc, err := NewNegotiatedClientWithOpts(ctx, client.FromEnv)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/docker/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// ReadFileFromContainer reads a specific file from a given container by ID.
func ReadFileFromContainer(ctx context.Context, containerID string, path string) (*bytes.Buffer, error) {
// connect to the local docker environment
dockerc, err := client.NewClientWithOpts(client.FromEnv)
dockerc, err := NewNegotiatedClientWithOpts(ctx, client.FromEnv)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -76,7 +76,7 @@ func WriteFileToContainer(ctx context.Context, containerID string, path string,
}

// connect to the local docker environment
dockerc, err := client.NewClientWithOpts(client.FromEnv)
dockerc, err := NewNegotiatedClientWithOpts(ctx, client.FromEnv)
if err != nil {
return fmt.Errorf("could not create a client with the local docker system: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/utils/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import (
// InspectDockerContainer is a helper function that uses the local docker environment
// provides the full container spec for a container present in that environment by name.
func InspectDockerContainer(containerID string) (*types.ContainerJSON, error) {
dockerc, err := client.NewClientWithOpts(client.FromEnv)
ctx := context.Background()
dockerc, err := NewNegotiatedClientWithOpts(ctx, client.FromEnv)
if err != nil {
return nil, err
}
containerJSON, err := dockerc.ContainerInspect(context.Background(), containerID)
containerJSON, err := dockerc.ContainerInspect(ctx, containerID)
return &containerJSON, err
}

Expand Down

0 comments on commit 01c2d1a

Please sign in to comment.