Skip to content

Commit

Permalink
fix: upgrade minimum dockerapi version (openkruise#1510)
Browse files Browse the repository at this point in the history
* fix: upgrade minimum dockerapi version

Signed-off-by: hantmac <hantmac@outlook.com>

* fix codecov

Signed-off-by: hantmac <hantmac@outlook.com>

---------

Signed-off-by: hantmac <hantmac@outlook.com>
  • Loading branch information
hantmac authored Mar 1, 2024
1 parent 63bc96e commit 6bb78c4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pkg/daemon/criruntime/imageruntime/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
47 changes: 47 additions & 0 deletions pkg/daemon/criruntime/imageruntime/docker_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
})
}
}

0 comments on commit 6bb78c4

Please sign in to comment.