From 6bb78c4579a28c4f537522f24409deede814b0c9 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Fri, 1 Mar 2024 09:24:11 +0800 Subject: [PATCH] fix: upgrade minimum dockerapi version (#1510) * fix: upgrade minimum dockerapi version Signed-off-by: hantmac * fix codecov Signed-off-by: hantmac --------- Signed-off-by: hantmac --- pkg/daemon/criruntime/imageruntime/docker.go | 9 ++-- .../criruntime/imageruntime/docker_test.go | 47 +++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 pkg/daemon/criruntime/imageruntime/docker_test.go diff --git a/pkg/daemon/criruntime/imageruntime/docker.go b/pkg/daemon/criruntime/imageruntime/docker.go index 727734b94f..2b6e002006 100644 --- a/pkg/daemon/criruntime/imageruntime/docker.go +++ b/pkg/daemon/criruntime/imageruntime/docker.go @@ -23,12 +23,13 @@ import ( dockertypes "github.com/docker/docker/api/types" dockerapi "github.com/docker/docker/client" - appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1" - daemonutil "github.com/openkruise/kruise/pkg/daemon/util" - "github.com/openkruise/kruise/pkg/util/secret" v1 "k8s.io/api/core/v1" utilerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/klog/v2" + + appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1" + daemonutil "github.com/openkruise/kruise/pkg/daemon/util" + "github.com/openkruise/kruise/pkg/util/secret" ) // NewDockerImageService create a docker runtime @@ -54,7 +55,7 @@ func (d *dockerImageService) createRuntimeClientIfNecessary() error { if d.client != nil { return nil } - c, err := dockerapi.NewClient(d.runtimeURI, "1.23", nil, nil) + c, err := dockerapi.NewClientWithOpts(dockerapi.WithHost(d.runtimeURI), dockerapi.WithVersion("1.24")) if err != nil { return err } diff --git a/pkg/daemon/criruntime/imageruntime/docker_test.go b/pkg/daemon/criruntime/imageruntime/docker_test.go new file mode 100644 index 0000000000..d9934027ca --- /dev/null +++ b/pkg/daemon/criruntime/imageruntime/docker_test.go @@ -0,0 +1,47 @@ +package imageruntime + +import ( + "testing" + + dockerapi "github.com/docker/docker/client" +) + +func TestCreateRuntimeClientIfNecessary(t *testing.T) { + tests := []struct { + name string + clientExists bool + expectedError error + }{ + { + name: "ClientAlreadyExists", + clientExists: true, + expectedError: nil, + }, + { + name: "ClientDoesNotExist", + clientExists: false, + expectedError: nil, // Assuming that dockerapi.NewClientWithOpts does not return an error in this test environment + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + d := &dockerImageService{ + runtimeURI: "unix:///hostvarrun/docker.sock", + } + if tt.clientExists { + d.client = &dockerapi.Client{} + } + + err := d.createRuntimeClientIfNecessary() + + if err != tt.expectedError { + t.Errorf("createRuntimeClientIfNecessary() error = %v, expectedError %v", err, tt.expectedError) + } + + if !tt.clientExists && d.client == nil { + t.Errorf("createRuntimeClientIfNecessary() client was not created") + } + }) + } +}