Skip to content

Commit

Permalink
FIX docker#2663 reduce calls to the driver in ls
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed Dec 23, 2015
1 parent 9f6764c commit 670c480
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 74 deletions.
29 changes: 24 additions & 5 deletions commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcndockerclient"
"github.com/docker/machine/libmachine/persist"
"github.com/docker/machine/libmachine/state"
"github.com/docker/machine/libmachine/swarm"
Expand Down Expand Up @@ -269,17 +270,35 @@ func matchesLabel(host *host.Host, labels []string) bool {
return false
}

// PERFORMANCE: The code of this function is complicated because we try
// to call the underlying drivers as less as possible to get the information
// we need.
func attemptGetHostState(h *host.Host, stateQueryChan chan<- HostListItem) {
url := ""
hostError := ""
currentState := state.None
dockerVersion := "Unknown"
hostError := ""

currentState, err := h.Driver.GetState()
url, err := h.URL()

// PERFORMANCE: if we have the url, it's ok to assume the host is running
// This reduces the number of calls to the drivers
if err == nil {
url, err = h.URL()
if url != "" {
currentState = state.Running
} else {
currentState, err = h.Driver.GetState()
}
} else {
currentState, _ = h.Driver.GetState()
}
if err == nil {
dockerVersion, err = h.DockerVersion()

if err == nil && url != "" {
// PERFORMANCE: Reuse the url instead of asking the host again.
// This reduces the number of calls to the drivers
dockerHost := &mcndockerclient.RemoteDocker{url, h.AuthOptions()}
dockerVersion, err = mcndockerclient.DockerVersion(dockerHost)

if err != nil {
dockerVersion = "Unknown"
} else {
Expand Down
3 changes: 2 additions & 1 deletion commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"

"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/mcndockerclient"
)

func cmdVersion(c CommandLine, api libmachine.API) error {
Expand All @@ -28,7 +29,7 @@ func printVersion(c CommandLine, api libmachine.API, out io.Writer) error {
return err
}

version, err := host.DockerVersion()
version, err := mcndockerclient.DockerVersion(host)
if err != nil {
return err
}
Expand Down
8 changes: 3 additions & 5 deletions libmachine/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcndockerclient"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/provision"
"github.com/docker/machine/libmachine/provision/pkgaction"
Expand Down Expand Up @@ -165,13 +164,12 @@ func (h *Host) URL() (string, error) {
}

func (h *Host) AuthOptions() *auth.Options {
if h.HostOptions == nil {
return nil
}
return h.HostOptions.AuthOptions
}

func (h *Host) DockerVersion() (string, error) {
return mcndockerclient.DockerVersion(h)
}

func (h *Host) ConfigureAuth() error {
provisioner, err := provision.DetectProvisioner(h.Driver)
if err != nil {
Expand Down
23 changes: 23 additions & 0 deletions libmachine/mcndockerclient/docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,26 @@ func DockerClient(host DockerHost) (*dockerclient.DockerClient, error) {

return dockerclient.NewDockerClient(url, tlsConfig)
}

//CreateContainer creates a docker container.
func CreateContainer(dockerHost RemoteDocker, config *dockerclient.ContainerConfig, name string) error {
docker, err := DockerClient(dockerHost)
if err != nil {
return err
}

if err = docker.PullImage(config.Image, nil); err != nil {
return fmt.Errorf("Unable to Pull Image: %s", err)
}

containerID, err := docker.CreateContainer(config, name)
if err != nil {
return fmt.Errorf("Error while creating container: %s", err)
}

if err = docker.StartContainer(containerID, &config.HostConfig); err != nil {
return fmt.Errorf("Error while starting container: %s", err)
}

return nil
}
27 changes: 26 additions & 1 deletion libmachine/mcndockerclient/docker_host.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
package mcndockerclient

import "github.com/docker/machine/libmachine/auth"
import (
"fmt"

"github.com/docker/machine/libmachine/auth"
)

type URLer interface {
// URL returns the Docker host URL
URL() (string, error)
}

type AuthOptionser interface {
// AuthOptions returns the authOptions
AuthOptions() *auth.Options
}

type DockerHost interface {
URLer
AuthOptionser
}

type RemoteDocker struct {
HostURL string
AuthOption *auth.Options
}

// URL returns the Docker host URL
func (rd RemoteDocker) URL() (string, error) {
if rd.HostURL == "" {
return "", fmt.Errorf("Docker Host URL not set")
}

return rd.HostURL, nil
}

// AuthOptions returns the authOptions
func (rd RemoteDocker) AuthOptions() *auth.Options {
return rd.AuthOption
}
13 changes: 4 additions & 9 deletions libmachine/provision/configure_swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcndockerclient"
"github.com/docker/machine/libmachine/swarm"
"github.com/samalba/dockerclient"
)
Expand Down Expand Up @@ -34,11 +35,10 @@ func configureSwarm(p Provisioner, swarmOptions swarm.Options, authOptions auth.
dockerPort := "2376"
dockerDir := p.GetDockerOptionsDir()
dockerHost := fmt.Sprintf("tcp://%s:%s", ip, dockerPort)
dockerClient := DockerClient{dockerHost, authOptions}
dockerClient := mcndockerclient.RemoteDocker{dockerHost, &authOptions}
advertiseInfo := fmt.Sprintf("%s:%s", ip, dockerPort)

if swarmOptions.Master {

cmd := fmt.Sprintf("manage --tlsverify --tlscacert=%s --tlscert=%s --tlskey=%s -H %s --strategy %s --advertise %s",
authOptions.CaCertRemotePath,
authOptions.ServerCertRemotePath,
Expand Down Expand Up @@ -78,11 +78,10 @@ func configureSwarm(p Provisioner, swarmOptions swarm.Options, authOptions auth.
HostConfig: masterHostConfig,
}

err = CreateContainer(dockerClient, swarmMasterConfig, "swarm-agent-master")
err = mcndockerclient.CreateContainer(dockerClient, swarmMasterConfig, "swarm-agent-master")
if err != nil {
return err
}

}

workerHostConfig := dockerclient.HostConfig{
Expand All @@ -105,9 +104,5 @@ func configureSwarm(p Provisioner, swarmOptions swarm.Options, authOptions auth.
HostConfig: workerHostConfig,
}

if err = CreateContainer(dockerClient, swarmWorkerConfig, "swarm-agent"); err != nil {
return err
}

return nil
return mcndockerclient.CreateContainer(dockerClient, swarmWorkerConfig, "swarm-agent")
}
53 changes: 0 additions & 53 deletions libmachine/provision/docker.go

This file was deleted.

0 comments on commit 670c480

Please sign in to comment.