|
| 1 | +// Copyright 2019 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 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "net/http" |
| 21 | + "os" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/google/cadvisor/accelerators" |
| 25 | + "github.com/google/cadvisor/cache/memory" |
| 26 | + "github.com/google/cadvisor/container" |
| 27 | + _ "github.com/google/cadvisor/container/containerd" |
| 28 | + "github.com/google/cadvisor/container/crio" |
| 29 | + "github.com/google/cadvisor/container/docker" |
| 30 | + _ "github.com/google/cadvisor/container/mesos" |
| 31 | + _ "github.com/google/cadvisor/container/raw" |
| 32 | + "github.com/google/cadvisor/container/rkt" |
| 33 | + _ "github.com/google/cadvisor/container/systemd" |
| 34 | + "github.com/google/cadvisor/fs" |
| 35 | + info "github.com/google/cadvisor/info/v1" |
| 36 | + "github.com/google/cadvisor/machine" |
| 37 | + "github.com/google/cadvisor/manager" |
| 38 | + "github.com/google/cadvisor/utils/sysfs" |
| 39 | + "github.com/google/cadvisor/watcher" |
| 40 | + |
| 41 | + "github.com/opencontainers/runc/libcontainer/cgroups" |
| 42 | + "k8s.io/klog" |
| 43 | +) |
| 44 | + |
| 45 | +const dockerClientTimeout = 10 * time.Second |
| 46 | + |
| 47 | +// New takes a memory storage and returns a new manager. |
| 48 | +func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingInterval time.Duration, allowDynamicHousekeeping bool, includedMetricsSet container.MetricSet, collectorHttpClient *http.Client, rawContainerCgroupPathPrefixWhiteList []string) (manager.Manager, error) { |
| 49 | + if memoryCache == nil { |
| 50 | + return nil, fmt.Errorf("manager requires memory storage") |
| 51 | + } |
| 52 | + |
| 53 | + // Detect the container we are running on. |
| 54 | + selfContainer, err := cgroups.GetOwnCgroupPath("cpu") |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + klog.V(2).Infof("cAdvisor running in container: %q", selfContainer) |
| 59 | + |
| 60 | + var ( |
| 61 | + dockerStatus info.DockerStatus |
| 62 | + rktPath string |
| 63 | + ) |
| 64 | + docker.SetTimeout(dockerClientTimeout) |
| 65 | + // Try to connect to docker indefinitely on startup. |
| 66 | + dockerStatus = retryDockerStatus() |
| 67 | + |
| 68 | + if tmpRktPath, err := rkt.RktPath(); err != nil { |
| 69 | + klog.V(5).Infof("Rkt not connected: %v", err) |
| 70 | + } else { |
| 71 | + rktPath = tmpRktPath |
| 72 | + } |
| 73 | + |
| 74 | + crioClient, err := crio.Client() |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + crioInfo, err := crioClient.Info() |
| 79 | + if err != nil { |
| 80 | + klog.V(5).Infof("CRI-O not connected: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + context := fs.Context{ |
| 84 | + Docker: fs.DockerContext{ |
| 85 | + Root: docker.RootDir(), |
| 86 | + Driver: dockerStatus.Driver, |
| 87 | + DriverStatus: dockerStatus.DriverStatus, |
| 88 | + }, |
| 89 | + RktPath: rktPath, |
| 90 | + Crio: fs.CrioContext{ |
| 91 | + Root: crioInfo.StorageRoot, |
| 92 | + }, |
| 93 | + } |
| 94 | + fsInfo, err := fs.NewFsInfo(context) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + // If cAdvisor was started with host's rootfs mounted, assume that its running |
| 100 | + // in its own namespaces. |
| 101 | + inHostNamespace := false |
| 102 | + if _, err := os.Stat("/rootfs/proc"); os.IsNotExist(err) { |
| 103 | + inHostNamespace = true |
| 104 | + } |
| 105 | + |
| 106 | + // Register for new subcontainers. |
| 107 | + eventsChannel := make(chan watcher.ContainerEvent, 16) |
| 108 | + |
| 109 | + machineInfo, err := machine.Info(sysfs, fsInfo, inHostNamespace) |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + klog.V(1).Infof("Machine: %+v", *machineInfo) |
| 114 | + |
| 115 | + newManager := manager.New( |
| 116 | + memoryCache, |
| 117 | + fsInfo, |
| 118 | + sysfs, |
| 119 | + *machineInfo, |
| 120 | + make([]chan error, 0, 2), |
| 121 | + selfContainer, |
| 122 | + inHostNamespace, |
| 123 | + time.Now(), |
| 124 | + maxHousekeepingInterval, |
| 125 | + allowDynamicHousekeeping, |
| 126 | + includedMetricsSet, |
| 127 | + []watcher.ContainerWatcher{}, |
| 128 | + eventsChannel, |
| 129 | + collectorHttpClient, |
| 130 | + &accelerators.NvidiaManager{}, |
| 131 | + rawContainerCgroupPathPrefixWhiteList) |
| 132 | + |
| 133 | + versionInfo, err := newManager.GetVersionInfo() |
| 134 | + if err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + klog.V(1).Infof("Version: %+v", *versionInfo) |
| 138 | + return newManager, nil |
| 139 | +} |
| 140 | + |
| 141 | +func retryDockerStatus() info.DockerStatus { |
| 142 | + startupTimeout := dockerClientTimeout |
| 143 | + maxTimeout := 4 * startupTimeout |
| 144 | + for { |
| 145 | + ctx, e := context.WithTimeout(context.Background(), startupTimeout) |
| 146 | + if e != nil { |
| 147 | + klog.V(5).Infof("error during timeout: %v", e) |
| 148 | + } |
| 149 | + dockerStatus, err := docker.StatusWithContext(ctx) |
| 150 | + if err == nil { |
| 151 | + return dockerStatus |
| 152 | + } |
| 153 | + |
| 154 | + switch err { |
| 155 | + case context.DeadlineExceeded: |
| 156 | + klog.Warningf("Timeout trying to communicate with docker during initialization, will retry") |
| 157 | + default: |
| 158 | + klog.V(5).Infof("Docker not connected: %v", err) |
| 159 | + return info.DockerStatus{} |
| 160 | + } |
| 161 | + |
| 162 | + startupTimeout = 2 * startupTimeout |
| 163 | + if startupTimeout > maxTimeout { |
| 164 | + startupTimeout = maxTimeout |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments