diff --git a/engine/docker/docker.go b/engine/docker/docker.go index 7fe5647e6..6cb1066f6 100644 --- a/engine/docker/docker.go +++ b/engine/docker/docker.go @@ -94,3 +94,8 @@ func (e *Engine) Ping(ctx context.Context) error { _, err := e.client.Ping(ctx) return err } + +// CloseConn close connection +func (e *Engine) CloseConn() error { + return e.client.Close() +} diff --git a/engine/engine.go b/engine/engine.go index b2a117378..2d55c4de3 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -13,6 +13,7 @@ import ( type API interface { Info(ctx context.Context) (*enginetypes.Info, error) Ping(ctx context.Context) error + CloseConn() error Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (result string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) ExecResize(ctx context.Context, ID, result string, height, width uint) (err error) diff --git a/engine/factory/factory.go b/engine/factory/factory.go index d4e92fe50..60dcf2ac6 100644 --- a/engine/factory/factory.go +++ b/engine/factory/factory.go @@ -139,6 +139,11 @@ func validateEngine(ctx context.Context, engine engine.API, timeout time.Duratio utils.WithTimeout(ctx, timeout, func(ctx context.Context) { err = engine.Ping(ctx) }) + if err != nil { + if closeErr := engine.CloseConn(); closeErr != nil { + log.Errorf(ctx, "[validateEngine] close conn error: %v", closeErr) + } + } return err } diff --git a/engine/fake/fake.go b/engine/fake/fake.go index 2ec7d3f7e..b26e453e9 100644 --- a/engine/fake/fake.go +++ b/engine/fake/fake.go @@ -24,6 +24,11 @@ func (f *Engine) Ping(ctx context.Context) error { return f.DefaultErr } +// CloseConn . +func (f *Engine) CloseConn() error { + return nil +} + // Execute . func (f *Engine) Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (result string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) { return "", nil, nil, nil, f.DefaultErr diff --git a/engine/mocks/API.go b/engine/mocks/API.go index de04eb33f..9ab5f2d6f 100644 --- a/engine/mocks/API.go +++ b/engine/mocks/API.go @@ -67,6 +67,20 @@ func (_m *API) BuildRefs(ctx context.Context, opts *types.BuildRefOptions) []str return r0 } +// CloseConn provides a mock function with given fields: +func (_m *API) CloseConn() error { + ret := _m.Called() + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 +} + // ExecExitCode provides a mock function with given fields: ctx, ID, result func (_m *API) ExecExitCode(ctx context.Context, ID string, result string) (int, error) { ret := _m.Called(ctx, ID, result) diff --git a/engine/virt/virt.go b/engine/virt/virt.go index 645360749..fa0344b0e 100644 --- a/engine/virt/virt.go +++ b/engine/virt/virt.go @@ -81,6 +81,11 @@ func (v *Virt) Ping(ctx context.Context) error { return err } +// CloseConn closes the connection. +func (v *Virt) CloseConn() error { + return v.client.Close() +} + // Execute executes a command in vm func (v *Virt) Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (pid string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) { if config.Tty { diff --git a/go.mod b/go.mod index 10d850684..1de442856 100644 --- a/go.mod +++ b/go.mod @@ -32,7 +32,7 @@ require ( github.com/opencontainers/runc v1.0.0-rc95 // indirect github.com/patrickmn/go-cache v2.1.0+incompatible github.com/pkg/errors v0.9.1 - github.com/projecteru2/libyavirt v0.0.0-20211217082140-493b61aa9b0d + github.com/projecteru2/libyavirt v0.0.0-20220328133352-2a6302e90fc9 github.com/prometheus/client_golang v1.11.0 github.com/sanity-io/litter v1.5.1 github.com/sirupsen/logrus v1.7.0 diff --git a/go.sum b/go.sum index 76e40a808..3aa266499 100644 --- a/go.sum +++ b/go.sum @@ -432,6 +432,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/projecteru2/libyavirt v0.0.0-20211217082140-493b61aa9b0d h1:BMFqsvIB3nmK5l53nz8r2ndK8//T0njciu5nr/kj9A4= github.com/projecteru2/libyavirt v0.0.0-20211217082140-493b61aa9b0d/go.mod h1:FOc+hWBMLsMrmx5p3/moizKeSomedZPNwB6LhS+kEnE= +github.com/projecteru2/libyavirt v0.0.0-20220328133352-2a6302e90fc9 h1:7atvx2788Nf+HvY5uZFLlogjplvvF7iw5nWI/8UkjoY= +github.com/projecteru2/libyavirt v0.0.0-20220328133352-2a6302e90fc9/go.mod h1:FOc+hWBMLsMrmx5p3/moizKeSomedZPNwB6LhS+kEnE= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=