func NewResourceUsageGatherer()

in clusterloader2/pkg/measurement/util/gatherers/container_resource_gatherer.go [84:161]


func NewResourceUsageGatherer(c clientset.Interface, host string, port int, provider provider.Provider, options ResourceGathererOptions, namespace string) (*ContainerResourceGatherer, error) {
	g := ContainerResourceGatherer{
		client:       c,
		isRunning:    true,
		stopCh:       make(chan struct{}),
		containerIDs: make([]string, 0),
		options:      options,
	}

	if options.InKubemark {
		g.workerWg.Add(1)
		g.workers = append(g.workers, resourceGatherWorker{
			inKubemark:                  true,
			stopCh:                      g.stopCh,
			wg:                          &g.workerWg,
			finished:                    false,
			resourceDataGatheringPeriod: options.ResourceDataGatheringPeriod,
			host:                        host,
			port:                        port,
			provider:                    provider,
		})
	} else {
		pods, err := c.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
		if err != nil {
			return nil, fmt.Errorf("listing pods error: %v", err)
		}

		nodeList, err := c.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
		if err != nil {
			return nil, fmt.Errorf("listing nodes error: %v", err)
		}

		masterNodes := sets.NewString()
		for _, node := range nodeList.Items {
			if pkgutil.LegacyIsMasterNode(&node) {
				masterNodes.Insert(node.Name)
			}
		}

		nodesToConsider := make(map[string]bool)
		for _, pod := range pods.Items {
			if (options.Nodes == MasterAndNonDaemons) && !masterNodes.Has(pod.Spec.NodeName) && isDaemonPod(&pod) {
				continue
			}
			for _, container := range pod.Status.InitContainerStatuses {
				g.containerIDs = append(g.containerIDs, container.Name)
			}
			for _, container := range pod.Status.ContainerStatuses {
				g.containerIDs = append(g.containerIDs, container.Name)
			}
			if options.Nodes == MasterAndNonDaemons {
				nodesToConsider[pod.Spec.NodeName] = true
			}
		}

		for _, node := range nodeList.Items {
			if options.Nodes == AllNodes || masterNodes.Has(node.Name) || nodesToConsider[node.Name] {
				g.workerWg.Add(1)
				resourceDataGatheringPeriod := options.ResourceDataGatheringPeriod
				if masterNodes.Has(node.Name) {
					resourceDataGatheringPeriod = options.MasterResourceDataGatheringPeriod
				}
				g.workers = append(g.workers, resourceGatherWorker{
					c:                           c,
					nodeName:                    node.Name,
					wg:                          &g.workerWg,
					containerIDs:                g.containerIDs,
					stopCh:                      g.stopCh,
					finished:                    false,
					inKubemark:                  false,
					resourceDataGatheringPeriod: resourceDataGatheringPeriod,
					port:                        port,
				})
			}
		}
	}
	return &g, nil
}