Update to new cadvisor v0.48.1

Signed-off-by: Davanum Srinivas <davanum@gmail.com>
This commit is contained in:
Davanum Srinivas
2023-10-31 07:51:09 -04:00
parent 0294521985
commit 8b9fc325e2
36 changed files with 425 additions and 331 deletions

View File

@@ -17,9 +17,9 @@ package manager
import (
"flag"
"fmt"
"io/ioutil"
"math"
"math/rand"
"os"
"os/exec"
"path"
"regexp"
@@ -243,7 +243,7 @@ func (cd *containerData) ReadFile(filepath string, inHostNamespace bool) ([]byte
for _, pid := range pids {
filePath := path.Join(rootfs, "/proc", pid, "/root", filepath)
klog.V(3).Infof("Trying path %q", filePath)
data, err := ioutil.ReadFile(filePath)
data, err := os.ReadFile(filePath)
if err == nil {
return data, err
}
@@ -323,7 +323,7 @@ func (cd *containerData) parseProcessList(cadvisorContainer string, inHostNamesp
var fdCount int
dirPath := path.Join(rootfs, "/proc", strconv.Itoa(processInfo.Pid), "fd")
fds, err := ioutil.ReadDir(dirPath)
fds, err := os.ReadDir(dirPath)
if err != nil {
klog.V(4).Infof("error while listing directory %q to measure fd count: %v", dirPath, err)
continue
@@ -482,7 +482,7 @@ func (cd *containerData) nextHousekeepingInterval() time.Duration {
stats, err := cd.memoryCache.RecentStats(cd.info.Name, empty, empty, 2)
if err != nil {
if cd.allowErrorLogging() {
klog.Warningf("Failed to get RecentStats(%q) while determining the next housekeeping: %v", cd.info.Name, err)
klog.V(4).Infof("Failed to get RecentStats(%q) while determining the next housekeeping: %v", cd.info.Name, err)
}
} else if len(stats) == 2 {
// TODO(vishnuk): Use no processes as a signal.

View File

@@ -58,10 +58,13 @@ var eventStorageAgeLimit = flag.String("event_storage_age_limit", "default=24h",
var eventStorageEventLimit = flag.String("event_storage_event_limit", "default=100000", "Max number of events to store (per type). Value is a comma separated list of key values, where the keys are event types (e.g.: creation, oom) or \"default\" and the value is an integer. Default is applied to all non-specified event types")
var applicationMetricsCountLimit = flag.Int("application_metrics_count_limit", 100, "Max number of application metrics to store (per container)")
// The namespace under which Docker aliases are unique.
const DockerNamespace = "docker"
// The namespace under which aliases are unique.
const (
DockerNamespace = "docker"
PodmanNamespace = "podman"
)
var HousekeepingConfigFlags = HouskeepingConfig{
var HousekeepingConfigFlags = HousekeepingConfig{
flag.Duration("max_housekeeping_interval", 60*time.Second, "Largest interval to allow between container housekeepings"),
flag.Bool("allow_dynamic_housekeeping", true, "Whether to allow the housekeeping interval to be dynamic"),
}
@@ -136,16 +139,20 @@ type Manager interface {
// Returns debugging information. Map of lines per category.
DebugInfo() map[string][]string
AllPodmanContainers(c *info.ContainerInfoRequest) (map[string]info.ContainerInfo, error)
PodmanContainer(containerName string, query *info.ContainerInfoRequest) (info.ContainerInfo, error)
}
// Housekeeping configuration for the manager
type HouskeepingConfig = struct {
type HousekeepingConfig = struct {
Interval *time.Duration
AllowDynamic *bool
}
// New takes a memory storage and returns a new manager.
func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, houskeepingConfig HouskeepingConfig, includedMetricsSet container.MetricSet, collectorHTTPClient *http.Client, rawContainerCgroupPathPrefixWhiteList, containerEnvMetadataWhiteList []string, perfEventsFile string, resctrlInterval time.Duration) (Manager, error) {
func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, HousekeepingConfig HousekeepingConfig, includedMetricsSet container.MetricSet, collectorHTTPClient *http.Client, rawContainerCgroupPathPrefixWhiteList, containerEnvMetadataWhiteList []string, perfEventsFile string, resctrlInterval time.Duration) (Manager, error) {
if memoryCache == nil {
return nil, fmt.Errorf("manager requires memory storage")
}
@@ -192,8 +199,8 @@ func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, houskeepingConfig
cadvisorContainer: selfContainer,
inHostNamespace: inHostNamespace,
startupTime: time.Now(),
maxHousekeepingInterval: *houskeepingConfig.Interval,
allowDynamicHousekeeping: *houskeepingConfig.AllowDynamic,
maxHousekeepingInterval: *HousekeepingConfig.Interval,
allowDynamicHousekeeping: *HousekeepingConfig.AllowDynamic,
includedMetrics: includedMetricsSet,
containerWatchers: []watcher.ContainerWatcher{},
eventsChannel: eventsChannel,
@@ -265,6 +272,19 @@ type manager struct {
containerEnvMetadataWhiteList []string
}
func (m *manager) PodmanContainer(containerName string, query *info.ContainerInfoRequest) (info.ContainerInfo, error) {
container, err := m.namespacedContainer(containerName, PodmanNamespace)
if err != nil {
return info.ContainerInfo{}, err
}
inf, err := m.containerDataToContainerInfo(container, query)
if err != nil {
return info.ContainerInfo{}, err
}
return *inf, nil
}
// Start the container manager.
func (m *manager) Start() error {
m.containerWatchers = container.InitializePlugins(m, m.fsInfo, m.includedMetrics)
@@ -274,7 +294,7 @@ func (m *manager) Start() error {
klog.Errorf("Registration of the raw container factory failed: %v", err)
}
rawWatcher, err := raw.NewRawContainerWatcher()
rawWatcher, err := raw.NewRawContainerWatcher(m.includedMetrics)
if err != nil {
return err
}
@@ -581,14 +601,14 @@ func (m *manager) SubcontainersInfo(containerName string, query *info.ContainerI
return m.containerDataSliceToContainerInfoSlice(containers, query)
}
func (m *manager) getAllDockerContainers() map[string]*containerData {
func (m *manager) getAllNamespacedContainers(ns string) map[string]*containerData {
m.containersLock.RLock()
defer m.containersLock.RUnlock()
containers := make(map[string]*containerData, len(m.containers))
// Get containers in the Docker namespace.
// Get containers in a namespace.
for name, cont := range m.containers {
if name.Namespace == DockerNamespace {
if name.Namespace == ns {
containers[cont.info.Name] = cont
}
}
@@ -596,48 +616,34 @@ func (m *manager) getAllDockerContainers() map[string]*containerData {
}
func (m *manager) AllDockerContainers(query *info.ContainerInfoRequest) (map[string]info.ContainerInfo, error) {
containers := m.getAllDockerContainers()
output := make(map[string]info.ContainerInfo, len(containers))
for name, cont := range containers {
inf, err := m.containerDataToContainerInfo(cont, query)
if err != nil {
// Ignore the error because of race condition and return best-effort result.
if err == memory.ErrDataNotFound {
klog.Warningf("Error getting data for container %s because of race condition", name)
continue
}
return nil, err
}
output[name] = *inf
}
return output, nil
containers := m.getAllNamespacedContainers(DockerNamespace)
return m.containersInfo(containers, query)
}
func (m *manager) getDockerContainer(containerName string) (*containerData, error) {
func (m *manager) namespacedContainer(containerName string, ns string) (*containerData, error) {
m.containersLock.RLock()
defer m.containersLock.RUnlock()
// Check for the container in the Docker container namespace.
// Check for the container in the namespace.
cont, ok := m.containers[namespacedContainerName{
Namespace: DockerNamespace,
Namespace: ns,
Name: containerName,
}]
// Look for container by short prefix name if no exact match found.
if !ok {
for contName, c := range m.containers {
if contName.Namespace == DockerNamespace && strings.HasPrefix(contName.Name, containerName) {
if contName.Namespace == ns && strings.HasPrefix(contName.Name, containerName) {
if cont == nil {
cont = c
} else {
return nil, fmt.Errorf("unable to find container. Container %q is not unique", containerName)
return nil, fmt.Errorf("unable to find container in %q namespace. Container %q is not unique", ns, containerName)
}
}
}
if cont == nil {
return nil, fmt.Errorf("unable to find Docker container %q", containerName)
return nil, fmt.Errorf("unable to find container %q in %q namespace", containerName, ns)
}
}
@@ -645,7 +651,7 @@ func (m *manager) getDockerContainer(containerName string) (*containerData, erro
}
func (m *manager) DockerContainer(containerName string, query *info.ContainerInfoRequest) (info.ContainerInfo, error) {
container, err := m.getDockerContainer(containerName)
container, err := m.namespacedContainer(containerName, DockerNamespace)
if err != nil {
return info.ContainerInfo{}, err
}
@@ -691,7 +697,7 @@ func (m *manager) GetRequestedContainersInfo(containerName string, options v2.Re
info, err := m.containerDataToContainerInfo(data, &query)
if err != nil {
if err == memory.ErrDataNotFound {
klog.Warningf("Error getting data for container %s because of race condition", name)
klog.V(4).Infof("Error getting data for container %s because of race condition", name)
continue
}
errs.append(name, "containerDataToContainerInfo", err)
@@ -717,19 +723,23 @@ func (m *manager) getRequestedContainers(containerName string, options v2.Reques
return containersMap, fmt.Errorf("unknown container: %q", containerName)
}
}
case v2.TypeDocker:
case v2.TypeDocker, v2.TypePodman:
namespace := map[string]string{
v2.TypeDocker: DockerNamespace,
v2.TypePodman: PodmanNamespace,
}[options.IdType]
if !options.Recursive {
containerName = strings.TrimPrefix(containerName, "/")
cont, err := m.getDockerContainer(containerName)
cont, err := m.namespacedContainer(containerName, namespace)
if err != nil {
return containersMap, err
}
containersMap[cont.info.Name] = cont
} else {
if containerName != "/" {
return containersMap, fmt.Errorf("invalid request for docker container %q with subcontainers", containerName)
return containersMap, fmt.Errorf("invalid request for %s container %q with subcontainers", options.IdType, containerName)
}
containersMap = m.getAllDockerContainers()
containersMap = m.getAllNamespacedContainers(namespace)
}
default:
return containersMap, fmt.Errorf("invalid request type %q", options.IdType)
@@ -1357,6 +1367,28 @@ func (m *manager) getFsInfoByDeviceName(deviceName string) (v2.FsInfo, error) {
return v2.FsInfo{}, fmt.Errorf("cannot find filesystem info for device %q", deviceName)
}
func (m *manager) containersInfo(containers map[string]*containerData, query *info.ContainerInfoRequest) (map[string]info.ContainerInfo, error) {
output := make(map[string]info.ContainerInfo, len(containers))
for name, cont := range containers {
inf, err := m.containerDataToContainerInfo(cont, query)
if err != nil {
// Ignore the error because of race condition and return best-effort result.
if err == memory.ErrDataNotFound {
klog.V(4).Infof("Error getting data for container %s because of race condition", name)
continue
}
return nil, err
}
output[name] = *inf
}
return output, nil
}
func (m *manager) AllPodmanContainers(query *info.ContainerInfoRequest) (map[string]info.ContainerInfo, error) {
containers := m.getAllNamespacedContainers(PodmanNamespace)
return m.containersInfo(containers, query)
}
func getVersionInfo() (*info.VersionInfo, error) {
kernelVersion := machine.KernelVersion()