handler.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. // Copyright 2014 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 Docker containers.
  15. package docker
  16. import (
  17. "fmt"
  18. "io/ioutil"
  19. "path"
  20. "strconv"
  21. "strings"
  22. "time"
  23. "github.com/google/cadvisor/container"
  24. "github.com/google/cadvisor/container/common"
  25. containerlibcontainer "github.com/google/cadvisor/container/libcontainer"
  26. "github.com/google/cadvisor/devicemapper"
  27. "github.com/google/cadvisor/fs"
  28. info "github.com/google/cadvisor/info/v1"
  29. dockerutil "github.com/google/cadvisor/utils/docker"
  30. "github.com/google/cadvisor/zfs"
  31. dockercontainer "github.com/docker/docker/api/types/container"
  32. docker "github.com/docker/docker/client"
  33. cgroupfs "github.com/opencontainers/runc/libcontainer/cgroups/fs"
  34. libcontainerconfigs "github.com/opencontainers/runc/libcontainer/configs"
  35. "golang.org/x/net/context"
  36. "k8s.io/klog"
  37. )
  38. const (
  39. // The read write layers exist here.
  40. aufsRWLayer = "diff"
  41. overlayRWLayer = "upper"
  42. overlay2RWLayer = "diff"
  43. // Path to the directory where docker stores log files if the json logging driver is enabled.
  44. pathToContainersDir = "containers"
  45. )
  46. type dockerContainerHandler struct {
  47. // machineInfoFactory provides info.MachineInfo
  48. machineInfoFactory info.MachineInfoFactory
  49. // Absolute path to the cgroup hierarchies of this container.
  50. // (e.g.: "cpu" -> "/sys/fs/cgroup/cpu/test")
  51. cgroupPaths map[string]string
  52. // the docker storage driver
  53. storageDriver storageDriver
  54. fsInfo fs.FsInfo
  55. rootfsStorageDir string
  56. // Time at which this container was created.
  57. creationTime time.Time
  58. // Metadata associated with the container.
  59. envs map[string]string
  60. labels map[string]string
  61. // Image name used for this container.
  62. image string
  63. // The network mode of the container
  64. networkMode dockercontainer.NetworkMode
  65. // Filesystem handler.
  66. fsHandler common.FsHandler
  67. // The IP address of the container
  68. ipAddress string
  69. includedMetrics container.MetricSet
  70. // the devicemapper poolname
  71. poolName string
  72. // zfsParent is the parent for docker zfs
  73. zfsParent string
  74. // Reference to the container
  75. reference info.ContainerReference
  76. libcontainerHandler *containerlibcontainer.Handler
  77. }
  78. var _ container.ContainerHandler = &dockerContainerHandler{}
  79. func getRwLayerID(containerID, storageDir string, sd storageDriver, dockerVersion []int) (string, error) {
  80. const (
  81. // Docker version >=1.10.0 have a randomized ID for the root fs of a container.
  82. randomizedRWLayerMinorVersion = 10
  83. rwLayerIDFile = "mount-id"
  84. )
  85. if (dockerVersion[0] <= 1) && (dockerVersion[1] < randomizedRWLayerMinorVersion) {
  86. return containerID, nil
  87. }
  88. bytes, err := ioutil.ReadFile(path.Join(storageDir, "image", string(sd), "layerdb", "mounts", containerID, rwLayerIDFile))
  89. if err != nil {
  90. return "", fmt.Errorf("failed to identify the read-write layer ID for container %q. - %v", containerID, err)
  91. }
  92. return string(bytes), err
  93. }
  94. // newDockerContainerHandler returns a new container.ContainerHandler
  95. func newDockerContainerHandler(
  96. client *docker.Client,
  97. name string,
  98. machineInfoFactory info.MachineInfoFactory,
  99. fsInfo fs.FsInfo,
  100. storageDriver storageDriver,
  101. storageDir string,
  102. cgroupSubsystems *containerlibcontainer.CgroupSubsystems,
  103. inHostNamespace bool,
  104. metadataEnvs []string,
  105. dockerVersion []int,
  106. includedMetrics container.MetricSet,
  107. thinPoolName string,
  108. thinPoolWatcher *devicemapper.ThinPoolWatcher,
  109. zfsWatcher *zfs.ZfsWatcher,
  110. ) (container.ContainerHandler, error) {
  111. // Create the cgroup paths.
  112. cgroupPaths := common.MakeCgroupPaths(cgroupSubsystems.MountPoints, name)
  113. // Generate the equivalent cgroup manager for this container.
  114. cgroupManager := &cgroupfs.Manager{
  115. Cgroups: &libcontainerconfigs.Cgroup{
  116. Name: name,
  117. },
  118. Paths: cgroupPaths,
  119. }
  120. rootFs := "/"
  121. if !inHostNamespace {
  122. rootFs = "/rootfs"
  123. storageDir = path.Join(rootFs, storageDir)
  124. }
  125. id := ContainerNameToDockerId(name)
  126. // Add the Containers dir where the log files are stored.
  127. // FIXME: Give `otherStorageDir` a more descriptive name.
  128. otherStorageDir := path.Join(storageDir, pathToContainersDir, id)
  129. rwLayerID, err := getRwLayerID(id, storageDir, storageDriver, dockerVersion)
  130. if err != nil {
  131. return nil, err
  132. }
  133. // Determine the rootfs storage dir OR the pool name to determine the device.
  134. // For devicemapper, we only need the thin pool name, and that is passed in to this call
  135. var (
  136. rootfsStorageDir string
  137. zfsFilesystem string
  138. zfsParent string
  139. )
  140. switch storageDriver {
  141. case aufsStorageDriver:
  142. rootfsStorageDir = path.Join(storageDir, string(aufsStorageDriver), aufsRWLayer, rwLayerID)
  143. case overlayStorageDriver:
  144. rootfsStorageDir = path.Join(storageDir, string(storageDriver), rwLayerID, overlayRWLayer)
  145. case overlay2StorageDriver:
  146. rootfsStorageDir = path.Join(storageDir, string(storageDriver), rwLayerID, overlay2RWLayer)
  147. case zfsStorageDriver:
  148. status, err := Status()
  149. if err != nil {
  150. return nil, fmt.Errorf("unable to determine docker status: %v", err)
  151. }
  152. zfsParent = status.DriverStatus[dockerutil.DriverStatusParentDataset]
  153. zfsFilesystem = path.Join(zfsParent, rwLayerID)
  154. }
  155. // We assume that if Inspect fails then the container is not known to docker.
  156. ctnr, err := client.ContainerInspect(context.Background(), id)
  157. if err != nil {
  158. return nil, fmt.Errorf("failed to inspect container %q: %v", id, err)
  159. }
  160. // TODO: extract object mother method
  161. handler := &dockerContainerHandler{
  162. machineInfoFactory: machineInfoFactory,
  163. cgroupPaths: cgroupPaths,
  164. fsInfo: fsInfo,
  165. storageDriver: storageDriver,
  166. poolName: thinPoolName,
  167. rootfsStorageDir: rootfsStorageDir,
  168. envs: make(map[string]string),
  169. labels: ctnr.Config.Labels,
  170. includedMetrics: includedMetrics,
  171. zfsParent: zfsParent,
  172. }
  173. // Timestamp returned by Docker is in time.RFC3339Nano format.
  174. handler.creationTime, err = time.Parse(time.RFC3339Nano, ctnr.Created)
  175. if err != nil {
  176. // This should not happen, report the error just in case
  177. return nil, fmt.Errorf("failed to parse the create timestamp %q for container %q: %v", ctnr.Created, id, err)
  178. }
  179. handler.libcontainerHandler = containerlibcontainer.NewHandler(cgroupManager, rootFs, ctnr.State.Pid, includedMetrics)
  180. // Add the name and bare ID as aliases of the container.
  181. handler.reference = info.ContainerReference{
  182. Id: id,
  183. Name: name,
  184. Aliases: []string{strings.TrimPrefix(ctnr.Name, "/"), id},
  185. Namespace: DockerNamespace,
  186. }
  187. handler.image = ctnr.Config.Image
  188. handler.networkMode = ctnr.HostConfig.NetworkMode
  189. // Only adds restartcount label if it's greater than 0
  190. if ctnr.RestartCount > 0 {
  191. handler.labels["restartcount"] = strconv.Itoa(ctnr.RestartCount)
  192. }
  193. // Obtain the IP address for the container.
  194. // If the NetworkMode starts with 'container:' then we need to use the IP address of the container specified.
  195. // This happens in cases such as kubernetes where the containers doesn't have an IP address itself and we need to use the pod's address
  196. ipAddress := ctnr.NetworkSettings.IPAddress
  197. networkMode := string(ctnr.HostConfig.NetworkMode)
  198. if ipAddress == "" && strings.HasPrefix(networkMode, "container:") {
  199. containerId := strings.TrimPrefix(networkMode, "container:")
  200. c, err := client.ContainerInspect(context.Background(), containerId)
  201. if err != nil {
  202. return nil, fmt.Errorf("failed to inspect container %q: %v", id, err)
  203. }
  204. ipAddress = c.NetworkSettings.IPAddress
  205. }
  206. handler.ipAddress = ipAddress
  207. if includedMetrics.Has(container.DiskUsageMetrics) {
  208. handler.fsHandler = &dockerFsHandler{
  209. fsHandler: common.NewFsHandler(common.DefaultPeriod, rootfsStorageDir, otherStorageDir, fsInfo),
  210. thinPoolWatcher: thinPoolWatcher,
  211. zfsWatcher: zfsWatcher,
  212. deviceID: ctnr.GraphDriver.Data["DeviceId"],
  213. zfsFilesystem: zfsFilesystem,
  214. }
  215. }
  216. // split env vars to get metadata map.
  217. for _, exposedEnv := range metadataEnvs {
  218. for _, envVar := range ctnr.Config.Env {
  219. if envVar != "" {
  220. splits := strings.SplitN(envVar, "=", 2)
  221. if len(splits) == 2 && splits[0] == exposedEnv {
  222. handler.envs[strings.ToLower(exposedEnv)] = splits[1]
  223. }
  224. }
  225. }
  226. }
  227. return handler, nil
  228. }
  229. // dockerFsHandler is a composite FsHandler implementation the incorporates
  230. // the common fs handler, a devicemapper ThinPoolWatcher, and a zfsWatcher
  231. type dockerFsHandler struct {
  232. fsHandler common.FsHandler
  233. // thinPoolWatcher is the devicemapper thin pool watcher
  234. thinPoolWatcher *devicemapper.ThinPoolWatcher
  235. // deviceID is the id of the container's fs device
  236. deviceID string
  237. // zfsWatcher is the zfs filesystem watcher
  238. zfsWatcher *zfs.ZfsWatcher
  239. // zfsFilesystem is the docker zfs filesystem
  240. zfsFilesystem string
  241. }
  242. var _ common.FsHandler = &dockerFsHandler{}
  243. func (h *dockerFsHandler) Start() {
  244. h.fsHandler.Start()
  245. }
  246. func (h *dockerFsHandler) Stop() {
  247. h.fsHandler.Stop()
  248. }
  249. func (h *dockerFsHandler) Usage() common.FsUsage {
  250. usage := h.fsHandler.Usage()
  251. // When devicemapper is the storage driver, the base usage of the container comes from the thin pool.
  252. // We still need the result of the fsHandler for any extra storage associated with the container.
  253. // To correctly factor in the thin pool usage, we should:
  254. // * Usage the thin pool usage as the base usage
  255. // * Calculate the overall usage by adding the overall usage from the fs handler to the thin pool usage
  256. if h.thinPoolWatcher != nil {
  257. thinPoolUsage, err := h.thinPoolWatcher.GetUsage(h.deviceID)
  258. if err != nil {
  259. // TODO: ideally we should keep track of how many times we failed to get the usage for this
  260. // device vs how many refreshes of the cache there have been, and display an error e.g. if we've
  261. // had at least 1 refresh and we still can't find the device.
  262. klog.V(5).Infof("unable to get fs usage from thin pool for device %s: %v", h.deviceID, err)
  263. } else {
  264. usage.BaseUsageBytes = thinPoolUsage
  265. usage.TotalUsageBytes += thinPoolUsage
  266. }
  267. }
  268. if h.zfsWatcher != nil {
  269. zfsUsage, err := h.zfsWatcher.GetUsage(h.zfsFilesystem)
  270. if err != nil {
  271. klog.V(5).Infof("unable to get fs usage from zfs for filesystem %s: %v", h.zfsFilesystem, err)
  272. } else {
  273. usage.BaseUsageBytes = zfsUsage
  274. usage.TotalUsageBytes += zfsUsage
  275. }
  276. }
  277. return usage
  278. }
  279. func (self *dockerContainerHandler) Start() {
  280. if self.fsHandler != nil {
  281. self.fsHandler.Start()
  282. }
  283. }
  284. func (self *dockerContainerHandler) Cleanup() {
  285. if self.fsHandler != nil {
  286. self.fsHandler.Stop()
  287. }
  288. }
  289. func (self *dockerContainerHandler) ContainerReference() (info.ContainerReference, error) {
  290. return self.reference, nil
  291. }
  292. func (self *dockerContainerHandler) needNet() bool {
  293. if self.includedMetrics.Has(container.NetworkUsageMetrics) {
  294. return !self.networkMode.IsContainer()
  295. }
  296. return false
  297. }
  298. func (self *dockerContainerHandler) GetSpec() (info.ContainerSpec, error) {
  299. hasFilesystem := self.includedMetrics.Has(container.DiskUsageMetrics)
  300. spec, err := common.GetSpec(self.cgroupPaths, self.machineInfoFactory, self.needNet(), hasFilesystem)
  301. spec.Labels = self.labels
  302. spec.Envs = self.envs
  303. spec.Image = self.image
  304. spec.CreationTime = self.creationTime
  305. return spec, err
  306. }
  307. func (self *dockerContainerHandler) getFsStats(stats *info.ContainerStats) error {
  308. mi, err := self.machineInfoFactory.GetMachineInfo()
  309. if err != nil {
  310. return err
  311. }
  312. if self.includedMetrics.Has(container.DiskIOMetrics) {
  313. common.AssignDeviceNamesToDiskStats((*common.MachineInfoNamer)(mi), &stats.DiskIo)
  314. }
  315. if !self.includedMetrics.Has(container.DiskUsageMetrics) {
  316. return nil
  317. }
  318. var device string
  319. switch self.storageDriver {
  320. case devicemapperStorageDriver:
  321. // Device has to be the pool name to correlate with the device name as
  322. // set in the machine info filesystems.
  323. device = self.poolName
  324. case aufsStorageDriver, overlayStorageDriver, overlay2StorageDriver:
  325. deviceInfo, err := self.fsInfo.GetDirFsDevice(self.rootfsStorageDir)
  326. if err != nil {
  327. return fmt.Errorf("unable to determine device info for dir: %v: %v", self.rootfsStorageDir, err)
  328. }
  329. device = deviceInfo.Device
  330. case zfsStorageDriver:
  331. device = self.zfsParent
  332. default:
  333. return nil
  334. }
  335. var (
  336. limit uint64
  337. fsType string
  338. )
  339. // Docker does not impose any filesystem limits for containers. So use capacity as limit.
  340. for _, fs := range mi.Filesystems {
  341. if fs.Device == device {
  342. limit = fs.Capacity
  343. fsType = fs.Type
  344. break
  345. }
  346. }
  347. fsStat := info.FsStats{Device: device, Type: fsType, Limit: limit}
  348. usage := self.fsHandler.Usage()
  349. fsStat.BaseUsage = usage.BaseUsageBytes
  350. fsStat.Usage = usage.TotalUsageBytes
  351. fsStat.Inodes = usage.InodeUsage
  352. stats.Filesystem = append(stats.Filesystem, fsStat)
  353. return nil
  354. }
  355. // TODO(vmarmol): Get from libcontainer API instead of cgroup manager when we don't have to support older Dockers.
  356. func (self *dockerContainerHandler) GetStats() (*info.ContainerStats, error) {
  357. stats, err := self.libcontainerHandler.GetStats()
  358. if err != nil {
  359. return stats, err
  360. }
  361. // Clean up stats for containers that don't have their own network - this
  362. // includes containers running in Kubernetes pods that use the network of the
  363. // infrastructure container. This stops metrics being reported multiple times
  364. // for each container in a pod.
  365. if !self.needNet() {
  366. stats.Network = info.NetworkStats{}
  367. }
  368. // Get filesystem stats.
  369. err = self.getFsStats(stats)
  370. if err != nil {
  371. return stats, err
  372. }
  373. return stats, nil
  374. }
  375. func (self *dockerContainerHandler) ListContainers(listType container.ListType) ([]info.ContainerReference, error) {
  376. // No-op for Docker driver.
  377. return []info.ContainerReference{}, nil
  378. }
  379. func (self *dockerContainerHandler) GetCgroupPath(resource string) (string, error) {
  380. path, ok := self.cgroupPaths[resource]
  381. if !ok {
  382. return "", fmt.Errorf("could not find path for resource %q for container %q\n", resource, self.reference.Name)
  383. }
  384. return path, nil
  385. }
  386. func (self *dockerContainerHandler) GetContainerLabels() map[string]string {
  387. return self.labels
  388. }
  389. func (self *dockerContainerHandler) GetContainerIPAddress() string {
  390. return self.ipAddress
  391. }
  392. func (self *dockerContainerHandler) ListProcesses(listType container.ListType) ([]int, error) {
  393. return self.libcontainerHandler.GetProcesses()
  394. }
  395. func (self *dockerContainerHandler) Exists() bool {
  396. return common.CgroupExists(self.cgroupPaths)
  397. }
  398. func (self *dockerContainerHandler) Type() container.ContainerType {
  399. return container.ContainerTypeDocker
  400. }