Skip to content

Commit 01c2d1a

Browse files
authored
fix: negotiate Docker API version (#932)
1 parent 2e7370a commit 01c2d1a

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v0.44.0
4+
5+
- Added a call to `NegotiateAPIVersion` when creating a Docker client to
6+
ensure that the client is able to communicate with the Docker daemon.
7+
[#932](https://github.com/Kong/kubernetes-testing-framework/pull/932)
8+
39
## v0.43.0
410

511
- Added `WithReleaseChannel` to the GKE cluster builder to allow specifying

pkg/utils/docker/client.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package docker
2+
3+
import (
4+
"context"
5+
6+
"github.com/docker/docker/client"
7+
)
8+
9+
// NewNegotiatedClientWithOpts is a wrapper around docker.NewClientWithOpts that negotiates the API version
10+
// with the server.
11+
func NewNegotiatedClientWithOpts(ctx context.Context, opts ...client.Opt) (*client.Client, error) {
12+
c, err := client.NewClientWithOpts(opts...)
13+
if err != nil {
14+
return nil, err
15+
}
16+
// Make sure the client API version matches server's API version to avoid discrepancy issues.
17+
c.NegotiateAPIVersion(ctx)
18+
return c, nil
19+
}

pkg/utils/docker/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// given command and arguments on the given container (by ID) privileged.
1212
func RunPrivilegedCommand(ctx context.Context, containerID, command string, args ...string) error {
1313
// connect to the local docker env
14-
dockerc, err := client.NewClientWithOpts(client.FromEnv)
14+
dockerc, err := NewNegotiatedClientWithOpts(ctx, client.FromEnv)
1515
if err != nil {
1616
return err
1717
}

pkg/utils/docker/copy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// ReadFileFromContainer reads a specific file from a given container by ID.
1717
func ReadFileFromContainer(ctx context.Context, containerID string, path string) (*bytes.Buffer, error) {
1818
// connect to the local docker environment
19-
dockerc, err := client.NewClientWithOpts(client.FromEnv)
19+
dockerc, err := NewNegotiatedClientWithOpts(ctx, client.FromEnv)
2020
if err != nil {
2121
return nil, err
2222
}
@@ -76,7 +76,7 @@ func WriteFileToContainer(ctx context.Context, containerID string, path string,
7676
}
7777

7878
// connect to the local docker environment
79-
dockerc, err := client.NewClientWithOpts(client.FromEnv)
79+
dockerc, err := NewNegotiatedClientWithOpts(ctx, client.FromEnv)
8080
if err != nil {
8181
return fmt.Errorf("could not create a client with the local docker system: %w", err)
8282
}

pkg/utils/docker/docker.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ import (
1616
// InspectDockerContainer is a helper function that uses the local docker environment
1717
// provides the full container spec for a container present in that environment by name.
1818
func InspectDockerContainer(containerID string) (*types.ContainerJSON, error) {
19-
dockerc, err := client.NewClientWithOpts(client.FromEnv)
19+
ctx := context.Background()
20+
dockerc, err := NewNegotiatedClientWithOpts(ctx, client.FromEnv)
2021
if err != nil {
2122
return nil, err
2223
}
23-
containerJSON, err := dockerc.ContainerInspect(context.Background(), containerID)
24+
containerJSON, err := dockerc.ContainerInspect(ctx, containerID)
2425
return &containerJSON, err
2526
}
2627

0 commit comments

Comments
 (0)