client.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. // Handler for /validate content.
  15. // Validates cadvisor dependencies - kernel, os, docker setup.
  16. package docker
  17. import (
  18. "net/http"
  19. "sync"
  20. dclient "github.com/docker/docker/client"
  21. "github.com/docker/go-connections/tlsconfig"
  22. )
  23. var (
  24. dockerClient *dclient.Client
  25. dockerClientErr error
  26. dockerClientOnce sync.Once
  27. )
  28. // Client creates a Docker API client based on the given Docker flags
  29. func Client() (*dclient.Client, error) {
  30. dockerClientOnce.Do(func() {
  31. var client *http.Client
  32. if *ArgDockerTLS {
  33. client = &http.Client{}
  34. options := tlsconfig.Options{
  35. CAFile: *ArgDockerCA,
  36. CertFile: *ArgDockerCert,
  37. KeyFile: *ArgDockerKey,
  38. InsecureSkipVerify: false,
  39. }
  40. tlsc, err := tlsconfig.Client(options)
  41. if err != nil {
  42. dockerClientErr = err
  43. return
  44. }
  45. client.Transport = &http.Transport{
  46. TLSClientConfig: tlsc,
  47. }
  48. }
  49. dockerClient, dockerClientErr = dclient.NewClient(*ArgDockerEndpoint,
  50. "",
  51. client,
  52. nil)
  53. })
  54. return dockerClient, dockerClientErr
  55. }