Skip to content

Commit

Permalink
Remove command.DockerCli dependency
Browse files Browse the repository at this point in the history
Signed-off-by: Ulysses Souza <[email protected]>
  • Loading branch information
ulyssessouza committed Oct 31, 2021
1 parent 221aafd commit a28f618
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
27 changes: 16 additions & 11 deletions pkg/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import (
"github.com/docker/buildx/util/buildflags"
xprogress "github.com/docker/buildx/util/progress"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/flags"
"github.com/docker/docker/client"
bclient "github.com/moby/buildkit/client"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/auth/authprovider"
Expand Down Expand Up @@ -193,22 +191,29 @@ func (s *composeService) getLocalImagesDigests(ctx context.Context, project *typ
return images, nil
}

func (s *composeService) serverInfo(ctx context.Context) (command.ServerInfo, error) {
ping, err := s.apiClient.Ping(ctx)
if err != nil {
return command.ServerInfo{}, err
}
serverInfo := command.ServerInfo{
HasExperimental: ping.Experimental,
OSType: ping.OSType,
BuildkitVersion: ping.BuilderVersion,
}
return serverInfo, err
}

func (s *composeService) doBuild(ctx context.Context, project *types.Project, opts map[string]build.Options, mode string) (map[string]string, error) {
if len(opts) == 0 {
return nil, nil
}
dockerCli, err := command.NewDockerCli()
if err != nil {
return nil, err
}
err = dockerCli.Initialize(flags.NewClientOptions(), command.WithInitializeClient(func(cli *command.DockerCli) (client.APIClient, error) {
return s.apiClient, nil
}))
serverInfo, err := s.serverInfo(ctx)
if err != nil {
return nil, err
}
if buildkitEnabled, err := command.BuildKitEnabled(dockerCli.ServerInfo()); err != nil || !buildkitEnabled {
return s.doBuildClassic(ctx, dockerCli, opts)
if buildkitEnabled, err := command.BuildKitEnabled(serverInfo); err != nil || !buildkitEnabled {
return s.doBuildClassic(ctx, opts)
}
return s.doBuildBuildkit(ctx, project, opts, mode)
}
Expand Down
28 changes: 11 additions & 17 deletions pkg/compose/build_classic.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"strings"

buildx "github.com/docker/buildx/build"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/image/build"
dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/cli"
Expand All @@ -42,11 +41,11 @@ import (
"github.com/pkg/errors"
)

func (s *composeService) doBuildClassic(ctx context.Context, dockerCli *command.DockerCli, opts map[string]buildx.Options) (map[string]string, error) {
func (s *composeService) doBuildClassic(ctx context.Context, opts map[string]buildx.Options) (map[string]string, error) {
var nameDigests = make(map[string]string)
var errs error
for name, o := range opts {
digest, err := doBuildClassicSimpleImage(ctx, dockerCli, o)
digest, err := s.doBuildClassicSimpleImage(ctx, o)
if err != nil {
errs = multierror.Append(errs, err).ErrorOrNil()
}
Expand All @@ -57,7 +56,7 @@ func (s *composeService) doBuildClassic(ctx context.Context, dockerCli *command.
}

// nolint: gocyclo
func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli, options buildx.Options) (string, error) {
func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options buildx.Options) (string, error) {
var (
buildCtx io.ReadCloser
dockerfileCtx io.ReadCloser
Expand All @@ -70,8 +69,8 @@ func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli

dockerfileName := options.Inputs.DockerfilePath
specifiedContext := options.Inputs.ContextPath
progBuff := dockerCli.Out()
buildBuff := dockerCli.Out()
progBuff := os.Stdout
buildBuff := os.Stdout
if options.ImageIDFile != "" {
// Avoid leaving a stale file if we eventually fail
if err := os.Remove(options.ImageIDFile); err != nil && !os.IsNotExist(err) {
Expand Down Expand Up @@ -144,24 +143,19 @@ func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli
return "", err
}

// Setup an upload progress bar
progressOutput := streamformatter.NewProgressOutput(progBuff)
if !dockerCli.Out().IsTerminal() {
progressOutput = &lastProgressOutput{output: progressOutput}
}

// if up to this point nothing has set the context then we must have another
// way for sending it(streaming) and set the context to the Dockerfile
if dockerfileCtx != nil && buildCtx == nil {
buildCtx = dockerfileCtx
}

progressOutput := streamformatter.NewProgressOutput(progBuff)
var body io.Reader
if buildCtx != nil {
body = progress.NewProgressReader(buildCtx, progressOutput, 0, "", "Sending build context to Docker daemon")
}

configFile := dockerCli.ConfigFile()
configFile := s.configFile
creds, _ := configFile.GetAllCredentials()
authConfigs := make(map[string]dockertypes.AuthConfig, len(creds))
for k, auth := range creds {
Expand All @@ -174,7 +168,7 @@ func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli

ctx, cancel := context.WithCancel(ctx)
defer cancel()
response, err := dockerCli.Client().ImageBuild(ctx, body, buildOptions)
response, err := s.apiClient.ImageBuild(ctx, body, buildOptions)
if err != nil {
return "", err
}
Expand All @@ -184,13 +178,13 @@ func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli
aux := func(msg jsonmessage.JSONMessage) {
var result dockertypes.BuildResult
if err := json.Unmarshal(*msg.Aux, &result); err != nil {
fmt.Fprintf(dockerCli.Err(), "Failed to parse aux message: %s", err)
fmt.Fprintf(os.Stderr, "Failed to parse aux message: %s", err)
} else {
imageID = result.ID
}
}

err = jsonmessage.DisplayJSONMessagesStream(response.Body, buildBuff, dockerCli.Out().FD(), dockerCli.Out().IsTerminal(), aux)
err = jsonmessage.DisplayJSONMessagesStream(response.Body, buildBuff, progBuff.Fd(), true, aux)
if err != nil {
if jerr, ok := err.(*jsonmessage.JSONError); ok {
// If no error code is set, default to 1
Expand All @@ -206,7 +200,7 @@ func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli
// daemon isn't running Windows.
if response.OSType != "windows" && runtime.GOOS == "windows" {
// if response.OSType != "windows" && runtime.GOOS == "windows" && !options.quiet {
fmt.Fprintln(dockerCli.Out(), "SECURITY WARNING: You are building a Docker "+
fmt.Fprintln(os.Stdout, "SECURITY WARNING: You are building a Docker "+
"image from Windows against a non-Windows Docker host. All files and "+
"directories added to build context will have '-rwxr-xr-x' permissions. "+
"It is recommended to double check and reset permissions for sensitive "+
Expand Down

0 comments on commit a28f618

Please sign in to comment.