func (r *ResourceCollector) Start() { // Get the cgroup containers for kubelet and docker kubeletContainer, err := getContainerNameForProcess(kubeletProcessName, "") dockerContainer, err := getContainerNameForProcess(dockerProcessName, dockerPidFile) if err == nil { systemContainers = map[string]string{ stats.SystemContainerKubelet: kubeletContainer, stats.SystemContainerRuntime: dockerContainer, } } else { framework.Failf("Failed to get docker container name in test-e2e-node resource collector.") } wait.Poll(1*time.Second, 1*time.Minute, func() (bool, error) { var err error r.client, err = cadvisorclient.NewClient(fmt.Sprintf("http://localhost:%d/", cadvisorPort)) if err == nil { return true, nil } return false, err }) Expect(r.client).NotTo(BeNil(), "cadvisor client not ready") r.request = &cadvisorapiv2.RequestOptions{IdType: "name", Count: 1, Recursive: false} r.stopCh = make(chan struct{}) oldStatsMap := make(map[string]*cadvisorapiv2.ContainerStats) go wait.Until(func() { r.collectStats(oldStatsMap) }, r.pollingInterval, r.stopCh) }
// Gets a v2 client to the cAdvisor being tested. func (self *realFramework) ClientV2() *v2.Client { if self.cadvisorClientV2 == nil { cadvisorClientV2, err := v2.NewClient(self.Hostname().FullHostname()) if err != nil { self.t.Fatalf("Failed to instantiate the cAdvisor client: %v", err) } self.cadvisorClientV2 = cadvisorClientV2 } return self.cadvisorClientV2 }
// Update node information by CAdvisor func (core *Core) updateNodesByCAdvisor() { for _, node := range core.GetAllNodes() { if node.Host != "" { client, err := client.NewClient("http://" + node.Host + ":" + core.GetAgentLisenPort()) if err != nil { log.Errorf("Cannot connect to cadvisor agent: %v", err) node.DockerDaemonHealth = registry.DockerDaemonDown continue } node.DockerDaemonHealth = registry.DockerDaemonUp // Fetch software versions and hardware information // One node fetches just one time if !node.MachineInfoFetched { attributes, err := client.Attributes() if err != nil { log.Errorf("Fetch machine info failed: %v", err) continue } node.NumCores = attributes.NumCores node.KernelVersion = attributes.KernelVersion node.CPUFrequency = attributes.CpuFrequency node.ContainerOsVersion = attributes.ContainerOsVersion node.DockerVersion = attributes.DockerVersion node.MemoryCapacity = attributes.MemoryCapacity node.MachineInfoFetched = true } request := info.RequestOptions{ IdType: "docker", Count: 5, Recursive: true, } containerInfo, err := client.Stats("", &request) if err != nil { log.Errorf("Fetch container info failed: %v", err) } node.Containers = make([]string, 0, len(containerInfo)) for container, info := range containerInfo { node.Containers = append(node.Containers, container) if len(info.Stats) > 1 { lastStats := info.Stats[len(info.Stats)-2] currentStats := info.Stats[len(info.Stats)-1] // ms -> ns. timeInterval := float64((currentStats.Timestamp.Unix() - lastStats.Timestamp.Unix()) * 1000000) node.CPUUsage = float64(currentStats.Cpu.Usage.Total-lastStats.Cpu.Usage.Total) / timeInterval node.MemoryUsage = currentStats.Memory.Usage node.LastUpdateTime = time.Now().Unix() } } } } }
func (core *Core) updateTasksByCAdvisor() { for _, task := range core.GetAllTasks() { if task.State == "TASK_RUNNING" && task.DockerID != "" && task.SlaveID != "" { node, _ := core.GetNode(task.SlaveID) client, err := client.NewClient("http://" + node.Host + ":" + core.GetAgentLisenPort()) if err != nil { log.Errorf("Cannot connect to cadvisor agent: %v", err) continue } request := info.RequestOptions{ IdType: "docker", Count: 15, } containerInfo, err := client.Stats(task.DockerID, &request) if err != nil { log.Errorf("Fetch container info failed: %v", err) } var cpuStats []*registry.Usage var memoryStats []*registry.Usage for _, containerInfo := range containerInfo { for _, containerStats := range containerInfo.Stats { cpuStats = append(cpuStats, ®istry.Usage{ Total: containerStats.Cpu.Usage.Total, Timestamp: containerStats.Timestamp, }) memoryStats = append(memoryStats, ®istry.Usage{ Total: containerStats.Memory.Usage, Timestamp: containerStats.Timestamp, }) } } task.CPUUsage = cpuStats task.MemoryUsage = memoryStats } task.LastUpdateTime = time.Now().Unix() } }