Skip to content

Commit 138b6fa

Browse files
authored
2.5 replacement github.com/fsouza/go-dockerclient to native github.com/moby/moby/client (#5355)
Signed-off-by: Fedor Partanskiy <[email protected]>
1 parent f44beda commit 138b6fa

File tree

722 files changed

+65394
-30140
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

722 files changed

+65394
-30140
lines changed

core/chaincode/chaincode_support_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"testing"
2222
"time"
2323

24-
docker "github.com/fsouza/go-dockerclient"
2524
"github.com/golang/protobuf/proto"
2625
"github.com/hyperledger/fabric-chaincode-go/shim"
2726
"github.com/hyperledger/fabric-protos-go/common"
@@ -59,6 +58,7 @@ import (
5958
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
6059
msptesttools "github.com/hyperledger/fabric/msp/mgmt/testtools"
6160
"github.com/hyperledger/fabric/protoutil"
61+
dcli "github.com/moby/moby/client"
6262
"github.com/spf13/viper"
6363
"github.com/stretchr/testify/require"
6464
)
@@ -192,7 +192,7 @@ func initMockPeer(channelIDs ...string) (*peer.Peer, *ChaincodeSupport, func(),
192192

193193
buildRegistry := &container.BuildRegistry{}
194194

195-
client, err := docker.NewClientFromEnv()
195+
client, err := dcli.New(dcli.FromEnv)
196196
if err != nil {
197197
panic(err)
198198
}
@@ -523,8 +523,8 @@ func initializeCC(t *testing.T, chainID, ccname string, ccSide *mock.MockCCComm,
523523
// be triggered by the chaincode stream. We just expect an error from fabric. Hence pass nil for done
524524
execCC(t, txParams, ccSide, "badccname", false, true, nil, cis, respSet, chaincodeSupport)
525525

526-
//---------try a successful init at last-------
527-
//everything lined up
526+
// ---------try a successful init at last-------
527+
// everything lined up
528528
// correct registered chaincode version
529529
// matching txid
530530
// txsim context

core/chaincode/platforms/builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ package platforms
99
import (
1010
"io"
1111

12-
docker "github.com/fsouza/go-dockerclient"
12+
dcli "github.com/moby/moby/client"
1313
)
1414

1515
type Builder struct {
1616
Registry *Registry
17-
Client *docker.Client
17+
Client dcli.APIClient
1818
}
1919

2020
func (b *Builder) GenerateDockerBuild(ccType, path string, codePackage io.Reader) (io.Reader, error) {

core/chaincode/platforms/platforms.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import (
1414
"io"
1515
"strings"
1616

17-
docker "github.com/fsouza/go-dockerclient"
1817
"github.com/hyperledger/fabric/common/flogging"
1918
"github.com/hyperledger/fabric/common/metadata"
2019
"github.com/hyperledger/fabric/core/chaincode/platforms/golang"
2120
"github.com/hyperledger/fabric/core/chaincode/platforms/java"
2221
"github.com/hyperledger/fabric/core/chaincode/platforms/node"
2322
"github.com/hyperledger/fabric/core/chaincode/platforms/util"
23+
dcli "github.com/moby/moby/client"
2424
"github.com/pkg/errors"
2525
)
2626

@@ -49,7 +49,7 @@ func (pw PackageWriterWrapper) Write(name string, payload []byte, tw *tar.Writer
4949
return pw(name, payload, tw)
5050
}
5151

52-
type BuildFunc func(util.DockerBuildOptions, *docker.Client) error
52+
type BuildFunc func(util.DockerBuildOptions, dcli.APIClient) error
5353

5454
type Registry struct {
5555
Platforms map[string]Platform
@@ -107,7 +107,7 @@ func (r *Registry) GenerateDockerfile(ccType string) (string, error) {
107107
return contents, nil
108108
}
109109

110-
func (r *Registry) StreamDockerBuild(ccType, path string, codePackage io.Reader, inputFiles map[string][]byte, tw *tar.Writer, client *docker.Client) error {
110+
func (r *Registry) StreamDockerBuild(ccType, path string, codePackage io.Reader, inputFiles map[string][]byte, tw *tar.Writer, client dcli.APIClient) error {
111111
var err error
112112

113113
// ----------------------------------------------------------------------------------------------------
@@ -163,7 +163,7 @@ func writeBytesToPackage(name string, payload []byte, tw *tar.Writer) error {
163163
return nil
164164
}
165165

166-
func (r *Registry) GenerateDockerBuild(ccType, path string, codePackage io.Reader, client *docker.Client) (io.Reader, error) {
166+
func (r *Registry) GenerateDockerBuild(ccType, path string, codePackage io.Reader, client dcli.APIClient) (io.Reader, error) {
167167
inputFiles := make(map[string][]byte)
168168

169169
// ----------------------------------------------------------------------------------------------------

core/chaincode/platforms/platforms_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import (
1313
"fmt"
1414
"io/ioutil"
1515

16-
docker "github.com/fsouza/go-dockerclient"
1716
"github.com/hyperledger/fabric/common/metadata"
1817
"github.com/hyperledger/fabric/core/chaincode/platforms"
1918
"github.com/hyperledger/fabric/core/chaincode/platforms/mock"
2019
"github.com/hyperledger/fabric/core/chaincode/platforms/util"
20+
dcli "github.com/moby/moby/client"
2121
. "github.com/onsi/ginkgo/v2"
2222
. "github.com/onsi/gomega"
2323
)
@@ -71,16 +71,16 @@ ENV CORE_CHAINCODE_BUILDLEVEL=%s`, metadata.Version, metadata.Version)
7171
buf *bytes.Buffer
7272
tw *tar.Writer
7373
pw *mock.PackageWriter
74-
client *docker.Client
74+
client dcli.APIClient
7575
)
7676

7777
BeforeEach(func() {
7878
buf = &bytes.Buffer{}
7979
tw = tar.NewWriter(buf)
8080
pw = &mock.PackageWriter{}
8181
registry.PackageWriter = pw
82-
registry.DockerBuild = func(util.DockerBuildOptions, *docker.Client) error { return nil }
83-
dockerClient, err := docker.NewClientFromEnv()
82+
registry.DockerBuild = func(util.DockerBuildOptions, dcli.APIClient) error { return nil }
83+
dockerClient, err := dcli.New(dcli.FromEnv)
8484
Expect(err).NotTo(HaveOccurred())
8585
client = dockerClient
8686
})
@@ -134,7 +134,7 @@ ENV CORE_CHAINCODE_BUILDLEVEL=%s`, metadata.Version, metadata.Version)
134134

135135
Context("when the docker build fails", func() {
136136
It("returns an error", func() {
137-
registry.DockerBuild = func(util.DockerBuildOptions, *docker.Client) error { return errors.New("kaboom") }
137+
registry.DockerBuild = func(util.DockerBuildOptions, dcli.APIClient) error { return errors.New("kaboom") }
138138
err := registry.StreamDockerBuild("fakeType", "", nil, nil, tw, client)
139139
Expect(err).To(MatchError("docker build failed: kaboom"))
140140
})

core/chaincode/platforms/util/utils.go

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ SPDX-License-Identifier: Apache-2.0
77
package util
88

99
import (
10-
"bytes"
10+
"context"
1111
"fmt"
1212
"io"
1313
"runtime"
1414
"strings"
1515

16-
docker "github.com/fsouza/go-dockerclient"
1716
"github.com/hyperledger/fabric/common/flogging"
1817
"github.com/hyperledger/fabric/common/metadata"
18+
dcontainer "github.com/moby/moby/api/types/container"
19+
dcli "github.com/moby/moby/client"
1920
"github.com/spf13/viper"
2021
)
2122

@@ -57,7 +58,7 @@ func (dbo DockerBuildOptions) String() string {
5758
// after successful execution of Cmd.
5859
//
5960
// -------------------------------------------------------------------------------------------
60-
func DockerBuild(opts DockerBuildOptions, client *docker.Client) error {
61+
func DockerBuild(opts DockerBuildOptions, client dcli.APIClient) error {
6162
if opts.Image == "" {
6263
opts.Image = GetDockerImageFromConfig("chaincode.builder")
6364
if opts.Image == "" {
@@ -67,105 +68,103 @@ func DockerBuild(opts DockerBuildOptions, client *docker.Client) error {
6768

6869
logger.Debugf("Attempting build with options: %s", opts)
6970

70-
//-----------------------------------------------------------------------------------
71+
// -----------------------------------------------------------------------------------
7172
// Ensure the image exists locally, or pull it from a registry if it doesn't
72-
//-----------------------------------------------------------------------------------
73-
_, err := client.InspectImage(opts.Image)
73+
// -----------------------------------------------------------------------------------
74+
_, err := client.ImageInspect(context.Background(), opts.Image)
7475
if err != nil {
7576
logger.Debugf("Image %s does not exist locally, attempt pull", opts.Image)
7677

77-
err = client.PullImage(docker.PullImageOptions{Repository: opts.Image}, docker.AuthConfiguration{})
78+
_, err = client.ImagePull(context.Background(), opts.Image, dcli.ImagePullOptions{})
7879
if err != nil {
7980
return fmt.Errorf("Failed to pull %s: %s", opts.Image, err)
8081
}
8182
}
8283

83-
//-----------------------------------------------------------------------------------
84+
// -----------------------------------------------------------------------------------
8485
// Create an ephemeral container, armed with our Image/Cmd
85-
//-----------------------------------------------------------------------------------
86-
container, err := client.CreateContainer(docker.CreateContainerOptions{
87-
Config: &docker.Config{
88-
Image: opts.Image,
89-
Cmd: []string{"/bin/sh", "-c", opts.Cmd},
90-
Env: opts.Env,
86+
// -----------------------------------------------------------------------------------
87+
container, err := client.ContainerCreate(context.Background(), dcli.ContainerCreateOptions{
88+
Config: &dcontainer.Config{
9189
AttachStdout: true,
9290
AttachStderr: true,
91+
Env: opts.Env,
92+
Cmd: []string{"/bin/sh", "-c", opts.Cmd},
93+
Image: opts.Image,
9394
},
9495
})
9596
if err != nil {
9697
return fmt.Errorf("Error creating container: %s", err)
9798
}
98-
defer client.RemoveContainer(docker.RemoveContainerOptions{ID: container.ID})
99+
defer client.ContainerRemove(context.Background(), container.ID, dcli.ContainerRemoveOptions{})
99100

100-
//-----------------------------------------------------------------------------------
101+
// -----------------------------------------------------------------------------------
101102
// Upload our input stream
102-
//-----------------------------------------------------------------------------------
103-
err = client.UploadToContainer(container.ID, docker.UploadToContainerOptions{
104-
Path: "/chaincode/input",
105-
InputStream: opts.InputStream,
103+
// -----------------------------------------------------------------------------------
104+
_, err = client.CopyToContainer(context.Background(), container.ID, dcli.CopyToContainerOptions{
105+
DestinationPath: "/chaincode/input",
106+
Content: opts.InputStream,
107+
AllowOverwriteDirWithFile: true,
106108
})
107109
if err != nil {
108110
return fmt.Errorf("Error uploading input to container: %s", err)
109111
}
110112

111-
//-----------------------------------------------------------------------------------
113+
// -----------------------------------------------------------------------------------
112114
// Attach stdout buffer to capture possible compilation errors
113-
//-----------------------------------------------------------------------------------
114-
stdout := bytes.NewBuffer(nil)
115-
cw, err := client.AttachToContainerNonBlocking(docker.AttachToContainerOptions{
116-
Container: container.ID,
117-
OutputStream: stdout,
118-
ErrorStream: stdout,
119-
Logs: true,
120-
Stdout: true,
121-
Stderr: true,
122-
Stream: true,
115+
// -----------------------------------------------------------------------------------
116+
cw, err := client.ContainerAttach(context.Background(), container.ID, dcli.ContainerAttachOptions{
117+
Stream: true,
118+
Stdout: true,
119+
Stderr: true,
120+
Logs: true,
123121
})
124122
if err != nil {
125123
return fmt.Errorf("Error attaching to container: %s", err)
126124
}
127125

128-
//-----------------------------------------------------------------------------------
126+
// -----------------------------------------------------------------------------------
129127
// Launch the actual build, realizing the Env/Cmd specified at container creation
130-
//-----------------------------------------------------------------------------------
131-
err = client.StartContainer(container.ID, nil)
128+
// -----------------------------------------------------------------------------------
129+
_, err = client.ContainerStart(context.Background(), container.ID, dcli.ContainerStartOptions{})
132130
if err != nil {
131+
buff, _ := io.ReadAll(cw.Reader)
133132
cw.Close()
134-
return fmt.Errorf("Error executing build: %s \"%s\"", err, stdout.String())
133+
return fmt.Errorf("Error executing build: %s \"%s\"", err, string(buff))
135134
}
136135

137-
//-----------------------------------------------------------------------------------
136+
// -----------------------------------------------------------------------------------
138137
// Wait for the build to complete and gather the return value
139-
//-----------------------------------------------------------------------------------
140-
retval, err := client.WaitContainer(container.ID)
141-
if err != nil {
138+
// -----------------------------------------------------------------------------------
139+
resWait := client.ContainerWait(context.Background(), container.ID, dcli.ContainerWaitOptions{})
140+
var res dcontainer.WaitResponse
141+
select {
142+
case res = <-resWait.Result:
143+
case err = <-resWait.Error:
144+
buff, _ := io.ReadAll(cw.Reader)
142145
cw.Close()
143-
return fmt.Errorf("Error waiting for container to complete: %s", err)
146+
return fmt.Errorf("Error waiting for container to complete: %s \"%s\"", err, string(buff))
144147
}
145148

146149
// Wait for stream copying to complete before accessing stdout.
147-
cw.Close()
148-
if err := cw.Wait(); err != nil {
149-
logger.Errorf("attach wait failed: %s", err)
150-
}
151-
152-
if retval > 0 {
150+
defer cw.Close()
151+
buff, _ := io.ReadAll(cw.Reader)
152+
if res.StatusCode > 0 {
153153
logger.Errorf("Docker build failed using options: %s", opts)
154-
return fmt.Errorf("Error returned from build: %d \"%s\"", retval, stdout.String())
154+
return fmt.Errorf("Error returned from build: %d \"%s\"", res.StatusCode, string(buff))
155155
}
156156

157-
logger.Debugf("Build output is %s", stdout.String())
157+
logger.Debugf("Build output is %s", string(buff))
158158

159-
//-----------------------------------------------------------------------------------
159+
// -----------------------------------------------------------------------------------
160160
// Finally, download the result
161-
//-----------------------------------------------------------------------------------
162-
err = client.DownloadFromContainer(container.ID, docker.DownloadFromContainerOptions{
163-
Path: "/chaincode/output/.",
164-
OutputStream: opts.OutputStream,
165-
})
161+
// -----------------------------------------------------------------------------------
162+
resCont, err := client.CopyFromContainer(context.Background(), container.ID, dcli.CopyFromContainerOptions{SourcePath: "/chaincode/output/."})
166163
if err != nil {
167164
return fmt.Errorf("Error downloading output: %s", err)
168165
}
166+
defer resCont.Content.Close()
167+
io.Copy(opts.OutputStream, resCont.Content)
169168

170169
return nil
171170
}

core/chaincode/platforms/util/utils_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,26 @@ package util
99
import (
1010
"archive/tar"
1111
"bytes"
12+
"context"
1213
"encoding/base32"
1314
"fmt"
15+
"io"
1416
"io/ioutil"
1517
"os"
1618
"runtime"
1719
"strings"
1820
"testing"
1921

20-
docker "github.com/fsouza/go-dockerclient"
2122
"github.com/hyperledger/fabric/common/metadata"
2223
"github.com/hyperledger/fabric/common/util"
2324
"github.com/hyperledger/fabric/core/config/configtest"
25+
dcli "github.com/moby/moby/client"
2426
"github.com/spf13/viper"
2527
"github.com/stretchr/testify/require"
2628
)
2729

2830
func TestDockerBuild(t *testing.T) {
29-
client, err := docker.NewClientFromEnv()
31+
client, err := dcli.New(dcli.FromEnv)
3032
require.NoError(t, err, "failed to get docker client")
3133

3234
is := bytes.NewBuffer(nil)
@@ -57,13 +59,16 @@ func TestDockerBuild(t *testing.T) {
5759
tw.Close()
5860

5961
imageName := uniqueName()
60-
err = client.BuildImage(docker.BuildImageOptions{
61-
Name: imageName,
62-
InputStream: is,
63-
OutputStream: ioutil.Discard,
62+
res, err := client.ImageBuild(context.Background(), is, dcli.ImageBuildOptions{
63+
Tags: []string{imageName},
6464
})
6565
require.NoError(t, err, "failed to build base image")
66-
defer client.RemoveImageExtended(imageName, docker.RemoveImageOptions{Force: true})
66+
defer res.Body.Close()
67+
defer client.ImageRemove(context.Background(), imageName, dcli.ImageRemoveOptions{
68+
Force: true,
69+
})
70+
71+
io.Copy(io.Discard, res.Body)
6772

6873
codepackage := bytes.NewBuffer(nil)
6974
tw = tar.NewWriter(codepackage)

0 commit comments

Comments
 (0)