Reuse cached docker client

Fixes #925
This commit is contained in:
Jimmi Dyson 2015-10-20 09:55:35 +01:00
parent 5a5d0575f5
commit 357e6a0f23
5 changed files with 45 additions and 9 deletions

View File

@ -0,0 +1,37 @@
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Handler for /validate content.
// Validates cadvisor dependencies - kernel, os, docker setup.
package docker
import (
"sync"
dclient "github.com/fsouza/go-dockerclient"
)
var (
dockerClient *dclient.Client
dockerClientErr error
once sync.Once
)
func Client() (*dclient.Client, error) {
once.Do(func() {
dockerClient, dockerClientErr = dclient.NewClient(*ArgDockerEndpoint)
})
return dockerClient, dockerClientErr
}

View File

@ -101,7 +101,7 @@ func (self *dockerFactory) String() string {
} }
func (self *dockerFactory) NewContainerHandler(name string, inHostNamespace bool) (handler container.ContainerHandler, err error) { func (self *dockerFactory) NewContainerHandler(name string, inHostNamespace bool) (handler container.ContainerHandler, err error) {
client, err := docker.NewClient(*ArgDockerEndpoint) client, err := Client()
if err != nil { if err != nil {
return return
} }
@ -184,7 +184,7 @@ func parseDockerVersion(full_version_string string) ([]int, error) {
// Register root container before running this function! // Register root container before running this function!
func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo) error { func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo) error {
client, err := docker.NewClient(*ArgDockerEndpoint) client, err := Client()
if err != nil { if err != nil {
return fmt.Errorf("unable to communicate with docker daemon: %v", err) return fmt.Errorf("unable to communicate with docker daemon: %v", err)
} }

View File

@ -335,7 +335,7 @@ func (self *dockerContainerHandler) Exists() bool {
} }
func DockerInfo() (map[string]string, error) { func DockerInfo() (map[string]string, error) {
client, err := docker.NewClient(*ArgDockerEndpoint) client, err := Client()
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to communicate with docker daemon: %v", err) return nil, fmt.Errorf("unable to communicate with docker daemon: %v", err)
} }
@ -347,7 +347,7 @@ func DockerInfo() (map[string]string, error) {
} }
func DockerImages() ([]docker.APIImages, error) { func DockerImages() ([]docker.APIImages, error) {
client, err := docker.NewClient(*ArgDockerEndpoint) client, err := Client()
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to communicate with docker daemon: %v", err) return nil, fmt.Errorf("unable to communicate with docker daemon: %v", err)
} }

View File

@ -21,7 +21,6 @@ import (
"strings" "strings"
"syscall" "syscall"
dclient "github.com/fsouza/go-dockerclient"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/google/cadvisor/container/docker" "github.com/google/cadvisor/container/docker"
"github.com/google/cadvisor/fs" "github.com/google/cadvisor/fs"
@ -146,7 +145,7 @@ func getContainerOsVersion() string {
func getDockerVersion() string { func getDockerVersion() string {
docker_version := "Unknown" docker_version := "Unknown"
client, err := dclient.NewClient(*docker.ArgDockerEndpoint) client, err := docker.Client()
if err == nil { if err == nil {
version, err := client.Version() version, err := client.Version()
if err == nil { if err == nil {

View File

@ -19,15 +19,15 @@ package validate
import ( import (
"fmt" "fmt"
"github.com/google/cadvisor/manager"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"path" "path"
"strings" "strings"
"github.com/google/cadvisor/manager"
"github.com/docker/libcontainer/cgroups" "github.com/docker/libcontainer/cgroups"
dclient "github.com/fsouza/go-dockerclient"
"github.com/google/cadvisor/container/docker" "github.com/google/cadvisor/container/docker"
"github.com/google/cadvisor/utils" "github.com/google/cadvisor/utils"
) )
@ -185,7 +185,7 @@ func validateCgroups() (string, string) {
} }
func validateDockerInfo() (string, string) { func validateDockerInfo() (string, string) {
client, err := dclient.NewClient(*docker.ArgDockerEndpoint) client, err := docker.Client()
if err == nil { if err == nil {
info, err := client.Info() info, err := client.Info()
if err == nil { if err == nil {