github.com/google/cadvisor@v0.49.1/container/docker/client.go (about)

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Handler for /validate content.
    16  // Validates cadvisor dependencies - kernel, os, docker setup.
    17  
    18  package docker
    19  
    20  import (
    21  	"net/http"
    22  	"sync"
    23  
    24  	dclient "github.com/docker/docker/client"
    25  	"github.com/docker/go-connections/tlsconfig"
    26  )
    27  
    28  var (
    29  	dockerClient     *dclient.Client
    30  	dockerClientErr  error
    31  	dockerClientOnce sync.Once
    32  )
    33  
    34  // Client creates a Docker API client based on the given Docker flags
    35  func Client() (*dclient.Client, error) {
    36  	dockerClientOnce.Do(func() {
    37  		var client *http.Client
    38  		if *ArgDockerTLS {
    39  			client = &http.Client{}
    40  			options := tlsconfig.Options{
    41  				CAFile:             *ArgDockerCA,
    42  				CertFile:           *ArgDockerCert,
    43  				KeyFile:            *ArgDockerKey,
    44  				InsecureSkipVerify: false,
    45  			}
    46  			tlsc, err := tlsconfig.Client(options)
    47  			if err != nil {
    48  				dockerClientErr = err
    49  				return
    50  			}
    51  			client.Transport = &http.Transport{
    52  				TLSClientConfig: tlsc,
    53  			}
    54  		}
    55  		dockerClient, dockerClientErr = dclient.NewClientWithOpts(
    56  			dclient.WithHost(*ArgDockerEndpoint),
    57  			dclient.WithHTTPClient(client),
    58  			dclient.WithAPIVersionNegotiation())
    59  	})
    60  	return dockerClient, dockerClientErr
    61  }