Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: negotiate Docker API version #932

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading